patchwise-agent-studio / web /src /pages /run /ScenarioSplit.tsx
EnergyVenom's picture
Tidy the web app: pages by area, CSS in load order, web/README.md (b2a039d)
fb43301 verified
Raw History Blame Contribute Delete
7.06 kB
import { TextInput } from '@mantine/core';
import { IconListCheck, IconSearch } from '@/components/icons';
import { useMemo, useState, type ComponentType } from 'react';
import type { Scenario } from '../../api/types';
import { useSidePanelOptional } from '../../components/shell/SidePanel';
import './split.css';
type Kind = 'fail' | 'flaky' | 'pass' | 'todo';
const kindOf = (s: Scenario): Kind => (s.outcome === 'failed' ? 'fail' : s.outcome === 'flaky' ? 'flaky' : s.outcome === 'passed' ? 'pass' : 'todo');
const WORDS: Record<Kind, string> = { fail: 'Failed', flaky: 'Passed on retry', pass: 'Passed', todo: 'Not tested' };
const FILTERS: { value: 'all' | Kind; label: string }[] = [
{ value: 'all', label: 'All' }, { value: 'fail', label: 'Failed' }, { value: 'flaky', label: 'On retry' }, { value: 'pass', label: 'Passed' }, { value: 'todo', label: 'Not tested' },
];
/** The scenarios of a run as a split workspace: a searchable list grouped by process step on the left, the chosen
* scenario on the right (its steps, roles and test data, drawn by `Body`). Failed scenarios come first in each step. */
export function ScenarioSplit({ list, screens, Body }: { list: Scenario[]; screens: boolean; Body: ComponentType<{ s: Scenario }> }) {
const panel = useSidePanelOptional();
const [q, setQ] = useState('');
const [filter, setFilter] = useState<'all' | Kind>('all');
const [pick, setPick] = useState<string | null>(null);
const count = (k: Kind) => list.filter((s) => kindOf(s) === k).length;
const shown = useMemo(() => {
const t = q.trim().toLowerCase();
const order: Record<Kind, number> = { fail: 0, flaky: 1, todo: 2, pass: 3 };
return list.filter((s) => (filter === 'all' || kindOf(s) === filter) && (!t || s.id.toLowerCase().includes(t) || s.title.toLowerCase().includes(t)))
.sort((a, b) => order[kindOf(a)] - order[kindOf(b)]);
}, [list, q, filter]);
const groups = useMemo(() => {
const m = new Map<string, Scenario[]>();
for (const s of shown) { const k = s.step ?? s.process ?? 'Other'; m.set(k, [...(m.get(k) ?? []), s]); }
return [...m.entries()];
}, [shown]);
const cur = list.find((s) => s.id === pick) ?? shown[0] ?? null;
const [view, setView] = useState<'list' | 'board'>(() => { try { return localStorage.getItem('pw.scenarioView') === 'board' ? 'board' : 'list'; } catch { return 'list'; } });
const choose = (v: 'list' | 'board') => { setView(v); try { localStorage.setItem('pw.scenarioView', v); } catch { /* private window */ } };
const toggle = (
<div className="sp-view" role="group" aria-label="Show the scenarios as">
<button type="button" aria-pressed={view === 'list'} onClick={() => choose('list')}>List</button>
<button type="button" aria-pressed={view === 'board'} onClick={() => choose('board')}>Board</button>
</div>
);
if (view === 'board') {
return (
<div className="sp-boardwrap">
<div className="sp-boardbar">{toggle}<span className="sp-sub">Pick a card to open the scenario.</span></div>
<div className="sp-board">
{(['fail', 'flaky', 'todo', 'pass'] as Kind[]).filter((k) => count(k) > 0).map((k) => (
<section key={k} className="sp-col" data-k={k} aria-label={`${WORDS[k]}: ${count(k)}`}>
<h3 className="sp-colh"><span className="sp-dot" data-k={k} aria-hidden />{WORDS[k]}<span className="fpt-num">{count(k)}</span></h3>
{list.filter((s) => kindOf(s) === k).slice(0, 40).map((s) => (
<button key={s.id} type="button" className="sp-card" onClick={() => { setPick(s.id); choose('list'); }}>
<span className="sp-kicker">{s.id}{s.priority_words ? ` 路 ${s.priority_words}` : ''}</span>
<b>{s.title}</b>
{(s.step ?? s.process) && <span className="sp-sub">{s.step ?? s.process}</span>}
</button>
))}
{count(k) > 40 && <span className="sp-sub sp-more">and {count(k) - 40} more in the list</span>}
</section>
))}
</div>
</div>
);
}
return (
<div className="sp-split">
<nav className="sp-list" aria-label="Scenarios">
{toggle}
<TextInput size="sm" placeholder="Title or ID" aria-label="Find a scenario" leftSection={<IconSearch size={14} />} value={q} onChange={(e) => setQ(e.currentTarget.value)} />
<div className="sp-chips" role="group" aria-label="Show scenarios">
{FILTERS.filter((f) => f.value === 'all' || count(f.value) > 0).map((f) => (
<button key={f.value} type="button" className="sp-chip" data-k={f.value} aria-pressed={filter === f.value} onClick={() => setFilter(f.value)}>
{f.label} <span className="fpt-num">{f.value === 'all' ? list.length : count(f.value)}</span>
</button>
))}
</div>
<div className="sp-items">
{groups.length === 0 && <p className="sp-none">No scenario matches.</p>}
{groups.map(([step, items]) => (
<div key={step} className="sp-group">
<h3 className="sp-gh">{step}</h3>
{items.map((s) => {
const k = kindOf(s);
return (
<button key={s.id} type="button" className="sp-item" data-k={k} aria-current={cur?.id === s.id || undefined} onClick={() => setPick(s.id)}>
<span className="sp-dot" data-k={k} aria-hidden />
<span className="sp-it"><b>{s.title}</b><span className="sp-sub">{s.id}{s.priority_words ? ` 路 ${s.priority_words}` : ''} 路 {WORDS[k]}</span></span>
</button>
);
})}
</div>
))}
</div>
</nav>
<section className="sp-detail" aria-label={cur ? `Scenario ${cur.id}` : 'Scenario'}>
{!cur ? <p className="sp-none">Pick a scenario on the left.</p> : (
<>
<header className="sp-dh">
<div className="sp-minw0">
<span className="sp-kicker">{[cur.id, cur.priority_words, cur.step ?? cur.process].filter(Boolean).join(' 路 ')}</span>
<h2 className="sp-title">{cur.title}</h2>
</div>
<span className="sp-pill" data-k={kindOf(cur)}>{WORDS[kindOf(cur)]}</span>
</header>
{panel && (
<div className="sp-actions">
<button type="button" className="fpt-textlink" onClick={() => panel.show('steps', { scenario: cur.id })}><IconListCheck size={14} aria-hidden /> Steps in the side panel</button>
{screens && kindOf(cur) !== 'todo' && <button type="button" className="fpt-textlink" onClick={() => panel.show('screens', { scenario: cur.id })}>See the screens</button>}
</div>
)}
<div className="sp-body"><Body s={cur} /></div>
</>
)}
</section>
</div>
);
}