Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion frontend/src/components/ui/evidence-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

export type EvidenceArtifact = { id?: string; name?: string; screenshot?: string; html?: string };

export function visualEvidenceArtifacts(files: SavedDataFile[]): EvidenceArtifact[] {

Check warning on line 13 in frontend/src/components/ui/evidence-viewer.tsx

View workflow job for this annotation

GitHub Actions / frontend

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components
return files.reduce<EvidenceArtifact[]>((artifacts, file, index) => {
const path = file.relative_path ?? file.path;
if (!path) return artifacts;
if (artifacts.some((artifact) => artifact.id === path)) return artifacts;
const name = file.label || file.filename || `#${index + 1}`;
const contentType = file.content_type?.toLowerCase();
const extension = path.toLowerCase().split(".").at(-1);
Expand Down Expand Up @@ -53,11 +54,26 @@
return <CodeMirror aria-label="HTML evidence source" value={result.source ?? ""} editable={false} height="min(60vh, 640px)" theme={oneDark} extensions={[html(), EditorView.lineWrapping]} basicSetup={{ lineNumbers: true, highlightActiveLine: true, bracketMatching: true, foldGutter: true }} />;
}

export function VisualEvidenceModal({ open, onClose, title, runId, iteration, artifacts }: { open: boolean; onClose: () => void; title: string; runId: string; iteration: number; artifacts: EvidenceArtifact[] }) {
const imageArtifacts = artifacts.filter((artifact) => artifact.screenshot);
const htmlArtifacts = artifacts.filter((artifact) => artifact.html);
const tabs = [
...imageArtifacts.map((artifact, index) => ({ id: `image-${index}`, label: artifact.screenshot!.split("/").at(-1) ?? `#${index + 1}`, artifact })),
...(htmlArtifacts.length ? [{ id: "html", label: "HTML" }] : []),
];
const [activeTab, setActiveTab] = useState(tabs[0]?.id ?? "");
const selectedImage = imageArtifacts[Number(activeTab.replace("image-", ""))];
return <Modal open={open} title={title} onClose={onClose} className="modal--evidence">
{tabs.length > 1 && <div className="run-tabs" role="tablist">{tabs.map((tab) => <button key={tab.id} type="button" role="tab" aria-selected={activeTab === tab.id} className={activeTab === tab.id ? "active" : ""} onClick={() => setActiveTab(tab.id)}>{tab.label}</button>)}</div>}
{activeTab === "html" ? htmlArtifacts.map((artifact, index) => <figure className="persona-evidence" key={`${artifact.id ?? index}-html`}><figcaption>{artifact.html!.split("/").at(-1) ?? artifact.name ?? `#${index + 1}`}</figcaption><HtmlEvidenceViewer url={artifactUrl(runId, iteration, artifact.html!)} /></figure>) : selectedImage?.screenshot && <figure className="persona-evidence"><figcaption>{selectedImage.screenshot.split("/").at(-1) ?? selectedImage.name}</figcaption><img src={artifactUrl(runId, iteration, selectedImage.screenshot)} alt={selectedImage.name ?? selectedImage.id ?? ""} /></figure>}
</Modal>;
}

export function EvidenceViewer({ runId, iteration, artifacts, className, imageLabel, htmlLabel, imageTitle, htmlTitle }: { runId: string; iteration: number; artifacts: EvidenceArtifact[]; className?: string; imageLabel: string; htmlLabel: string; imageTitle: string; htmlTitle: string }) {
const [evidenceModal, setEvidenceModal] = useState<{ kind: "image" | "html"; artifacts: EvidenceArtifact[] } | null>(null);
const imageArtifacts = artifacts.filter((artifact) => artifact.screenshot);
const htmlArtifacts = artifacts.filter((artifact) => artifact.html);
const modalArtifacts = evidenceModal?.artifacts.filter((artifact) => evidenceModal.kind === "image" ? Boolean(artifact.screenshot) : Boolean(artifact.html)) ?? [];
if (!imageArtifacts.length && !htmlArtifacts.length) return null;
return <><span className={className}>{imageArtifacts.length > 0 && <button className="ghost icon-button" type="button" aria-label={imageLabel} title={imageLabel} onClick={() => setEvidenceModal({ kind: "image", artifacts: imageArtifacts })}><Image size={16} /></button>}{htmlArtifacts.length > 0 && <button className="ghost icon-button" type="button" aria-label={htmlLabel} title={htmlLabel} onClick={() => setEvidenceModal({ kind: "html", artifacts: htmlArtifacts })}><CodeXml size={16} /></button>}</span>{evidenceModal && createPortal(<Modal open title={evidenceModal.kind === "image" ? imageTitle : htmlTitle} onClose={() => setEvidenceModal(null)} className="modal--evidence">{modalArtifacts.map((artifact, index) => <figure className="persona-evidence" key={`${artifact.id ?? index}-${evidenceModal.kind}`}><figcaption>{artifact.name ?? artifact.id ?? `#${index + 1}`}</figcaption>{evidenceModal.kind === "image" ? <img src={artifactUrl(runId, iteration, artifact.screenshot!)} alt={artifact.name ?? artifact.id ?? ""} /> : <HtmlEvidenceViewer url={artifactUrl(runId, iteration, artifact.html!)} />}</figure>)}</Modal>, document.body)}</>;
return <><span className={className}>{imageArtifacts.length > 0 && <button className="ghost icon-button" type="button" aria-label={imageLabel} title={imageLabel} onClick={() => setEvidenceModal({ kind: "image", artifacts: imageArtifacts })}><Image size={16} /></button>}{htmlArtifacts.length > 0 && <button className="ghost icon-button" type="button" aria-label={htmlLabel} title={htmlLabel} onClick={() => setEvidenceModal({ kind: "html", artifacts: htmlArtifacts })}><CodeXml size={16} /></button>}</span>{evidenceModal && createPortal(<VisualEvidenceModal open onClose={() => setEvidenceModal(null)} title={evidenceModal.kind === "image" ? imageTitle : htmlTitle} runId={runId} iteration={iteration} artifacts={modalArtifacts} />, document.body)}</>;
}
28 changes: 20 additions & 8 deletions frontend/src/features/improvements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { DataTable, type Column } from "../../components/ui/data-table";
import { CircleStop, RotateCcw } from "lucide-react";
import { EvaluationsPage } from "../evaluations/page";
import { IssueManagementSection } from "../issues/page";
import { VisualEvidenceModal, visualEvidenceArtifacts } from "../../components/ui/evidence-viewer";

type ImprovementCopy = {
improvement: string;
Expand Down Expand Up @@ -223,17 +224,18 @@ type PersonaJourneyEvent = {
};

function PersonaJourneyTimeline({
buildId, runs, locale, t, hours, onSelect,
buildId, runs, locale, t, hours,
}: {
buildId: string;
runs: Run[];
locale: Locale;
t: (typeof copy)["en"];
hours: number;
onSelect: (run: Run) => void;
}) {
const [rangeStart] = useState(() => hours ? Date.now() - hours * 60 * 60 * 1000 : 0);
const drag = useRef<{ pointerId: number; startX: number; startScroll: number; moved: boolean } | null>(null);
const [isDragging, setIsDragging] = useState(false);
const [selectedEvent, setSelectedEvent] = useState<PersonaJourneyEvent | null>(null);
const lanes = useMemo(() => {
const groups = new Map<string, PersonaJourneyEvent[]>();
for (const run of runs) {
Expand Down Expand Up @@ -297,32 +299,41 @@ function PersonaJourneyTimeline({
}, [buildId, rangeStart, runs]);
const startDrag = (event: ReactPointerEvent<HTMLDivElement>) => {
if (event.button !== 0) return;
event.currentTarget.setPointerCapture(event.pointerId);
drag.current = { pointerId: event.pointerId, startX: event.clientX, startScroll: event.currentTarget.scrollLeft, moved: false };
};
const moveDrag = (event: ReactPointerEvent<HTMLDivElement>) => {
const active = drag.current;
if (!active || active.pointerId !== event.pointerId) return;
const distance = event.clientX - active.startX;
if (Math.abs(distance) > 4) active.moved = true;
if (!active.moved && Math.abs(distance) <= 6) return;
if (!active.moved) {
active.moved = true;
event.currentTarget.setPointerCapture(event.pointerId);
setIsDragging(true);
}
event.currentTarget.scrollLeft = active.startScroll - distance;
};
const endDrag = (event: ReactPointerEvent<HTMLDivElement>) => {
if (drag.current?.pointerId === event.pointerId) event.currentTarget.releasePointerCapture(event.pointerId);
if (drag.current?.pointerId !== event.pointerId) return;
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
if (!drag.current.moved) drag.current = null;
setIsDragging(false);
};
const selectedArtifacts = selectedEvent ? visualEvidenceArtifacts((selectedEvent.run.step_results ?? []).filter((step) => step.loop_index === selectedEvent.iteration).flatMap((step) => step.data_files ?? [])) : [];
return <section className="panel persona-journeys">
<h3><SectionInfo title={t.personaJourneys} description={t.personaJourneysHint} /></h3>
<p className="hint">{t.personaJourneysHint}</p>
{lanes.length ? <div className="persona-journeys__lanes">
{lanes.map((lane) => <section className="persona-journeys__lane" key={lane.persona}>
<header><strong>{lane.persona}</strong><small>{lane.events.length}</small></header>
<div className="persona-journeys__events" onPointerDown={startDrag} onPointerMove={moveDrag} onPointerUp={endDrag} onPointerCancel={endDrag} onClickCapture={(event) => {
<div className={`persona-journeys__events${isDragging ? " is-dragging" : ""}`} onPointerDown={startDrag} onPointerMove={moveDrag} onPointerUp={endDrag} onPointerCancel={endDrag} onClickCapture={(event) => {
if (!drag.current?.moved) return;
event.preventDefault();
event.stopPropagation();
drag.current = null;
setIsDragging(false);
}}>
{lane.events.map((event) => <button className="persona-journeys__event" key={event.id} onClick={() => onSelect(event.run)}>
{lane.events.map((event) => <button className="persona-journeys__event" key={event.id} onClick={() => setSelectedEvent(event)}>
<time>{compactTimestamp(locale, event.recordedAt)}</time>
<small>{t.run} {event.run.id} · {t.iteration} #{event.iteration}</small>
<dl>
Expand All @@ -335,6 +346,7 @@ function PersonaJourneyTimeline({
</div>
</section>)}
</div> : <p className="catalog-empty">{t.noPersonaJourneys}</p>}
{selectedEvent && <VisualEvidenceModal open onClose={() => setSelectedEvent(null)} title={`${selectedEvent.persona} · ${t.iteration} #${selectedEvent.iteration}`} runId={selectedEvent.run.id} iteration={selectedEvent.iteration} artifacts={selectedArtifacts} />}
</section>;
}
function Card({
Expand Down Expand Up @@ -771,7 +783,7 @@ function ImprovementBuildContent({
onRetryRequest={setRetryingRun}
onSelect={setSelectedRun}
/>
<PersonaJourneyTimeline key={`${buildId}:${hours}`} buildId={buildId} runs={runs} locale={locale} t={t} hours={hours} onSelect={setSelectedRun} />
<PersonaJourneyTimeline key={`${buildId}:${hours}`} buildId={buildId} runs={runs} locale={locale} t={t} hours={hours} />
<IssueManagementSection key={buildId} buildId={buildId} locale={locale} onNotice={onNotice} />
<StoredState buildId={buildId} locale={locale} t={t} />
{selectedRun && <EvaluationsPage
Expand Down
Loading
Loading