From 09c4550e8c71810d21ef28f56218c9b109d91dc0 Mon Sep 17 00:00:00 2001 From: Arlo Kharod <42082766+firegiant9000@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:43:42 -0600 Subject: [PATCH 1/5] Link a form to its application in the popup --- shell/ui/FormLink.tsx | 171 ++++++++++++++++++++++++++++++++ shell/ui/Popup.tsx | 99 ++++++++++++++++++ shell/ui/theme/popup-layout.css | 17 ++++ 3 files changed, 287 insertions(+) create mode 100644 shell/ui/FormLink.tsx diff --git a/shell/ui/FormLink.tsx b/shell/ui/FormLink.tsx new file mode 100644 index 0000000..38be968 --- /dev/null +++ b/shell/ui/FormLink.tsx @@ -0,0 +1,171 @@ +import type { VNode } from 'preact'; +import { useState } from 'preact/hooks'; +import { applicationRows } from '../../core/src/application-list'; +import type { Id, Snapshot } from '../../core/src/types'; +import { Button } from './components/Button'; +import { Field } from './components/Field'; +import { Text } from './components/Text'; + +const MAX_ROWS = 20; + +type Mode = 'idle' | 'choose' | 'new'; + +function nameOf(value: string | undefined, fallback: string): string { + return value !== undefined && value.trim() !== '' ? value : fallback; +} + +/** + * Candidate application ids for the picker: active applications, newest + * created first. + */ +export function pickerCandidates(data: Snapshot): Id[] { + return applicationRows(data) + .filter((r) => !r.archived) + .sort((a, b) => + a.createdAt < b.createdAt ? 1 : a.createdAt > b.createdAt ? -1 : 0, + ) + .map((r) => r.id); +} + +export function FormLink(props: { + data: Snapshot; + linkedId?: Id; + candidates: Id[]; + initialCompany?: string; + initialTitle?: string; + /** Resolves true when the write succeeded. */ + onLink: (id: Id) => Promise; + onNew: (input: { companyName: string; title: string }) => Promise; + busy: boolean; + error?: string; +}): VNode { + const [mode, setMode] = useState('idle'); + const [query, setQuery] = useState(''); + const [company, setCompany] = useState(undefined); + const [title, setTitle] = useState(undefined); + + const rows = applicationRows(props.data); + const byId = new Map(rows.map((r) => [r.id, r] as const)); + const label = (id: Id): string => { + const row = byId.get(id); + return `${nameOf(row?.companyName, 'Unknown company')} · ${nameOf(row?.title, 'Untitled')}`; + }; + + const done = (ok: boolean): void => { + if (ok) setMode('idle'); + }; + + let panel: VNode | null = null; + if (mode === 'choose') { + const needle = query.trim().toLowerCase(); + const matching = props.candidates.filter((id) => { + if (needle === '') return true; + const row = byId.get(id); + return ( + (row?.companyName ?? '').toLowerCase().includes(needle) || + (row?.title ?? '').toLowerCase().includes(needle) + ); + }); + panel = ( + + ); + } else if (mode === 'new') { + const companyValue = company ?? props.initialCompany ?? ''; + const titleValue = title ?? props.initialTitle ?? ''; + panel = ( + + ); + } + + const linked = props.linkedId !== undefined; + return ( + + ); +} diff --git a/shell/ui/Popup.tsx b/shell/ui/Popup.tsx index e237910..1f65d30 100644 --- a/shell/ui/Popup.tsx +++ b/shell/ui/Popup.tsx @@ -8,7 +8,15 @@ import { saveJobOps, } from '../../core/src/job-ops'; import type { JobInput } from '../../core/src/job-ops'; +import { newApplicationFromFormOps } from '../../core/src/form-application'; +import { + findApplicationsForForm, + formUrlKey, + linkFormOps, +} from '../../core/src/form-link'; import { profileValues } from '../../core/src/profile'; +import type { WriteOp } from '../../core/src/repository'; +import type { Id, Snapshot } from '../../core/src/types'; import { canonicalizeUrl } from '../../core/src/url'; import { capturePage } from '../capture/inject'; import type { CaptureOutcome } from '../capture/inject'; @@ -18,6 +26,7 @@ import { openDashboard } from '../open-dashboard'; import { useAppData } from './app-data'; import { AnyoneHere } from './AnyoneHere'; import { FillSection } from './FillSection'; +import { FormLink, pickerCandidates } from './FormLink'; import { Button } from './components/Button'; import { Fact } from './components/Fact'; import { Field } from './components/Field'; @@ -115,6 +124,18 @@ export function Popup(): VNode { } const controller = controllerRef.current; + const [fillStatus, setFillStatus] = useState(() => controller.state().status); + useEffect(() => { + const unsubscribe = controller.subscribe((s) => setFillStatus(s.status)); + setFillStatus(controller.state().status); + return unsubscribe; + }, [controller]); + + // Set by Choose… and Create and link; see linkedId below. + const [chosenId, setChosenId] = useState(undefined); + const [linking, setLinking] = useState(false); + const [linkError, setLinkError] = useState(undefined); + useEffect(() => { controller.refresh(); }, [data]); @@ -208,6 +229,71 @@ export function Popup(): VNode { const outcomeUrl = outcome.ok ? outcome.capture.url : outcome.url; const linkValue = pasteLink ?? (isHttp(outcomeUrl) ? outcomeUrl.trim() : ''); + const formKey = + outcomeUrl === undefined + ? undefined + : formUrlKey(outcomeUrl, captured?.relCanonical); + // A chosen application that still exists wins; otherwise the first match. + // Task 8 records a fill to linkedId; Task 9 shows a panel for it. + const linkedId: Id | undefined = + chosenId !== undefined && data.applications.some((a) => a.id === chosenId) + ? chosenId + : formKey === undefined + ? undefined + : findApplicationsForForm(data, formKey)[0]; + + // One apply per write; Task 8 adds the held fill result to these builders. + const linkOps = (d: Snapshot, id: Id): WriteOp[] => + linkFormOps(d, id, formKey ?? ''); + const runLink = ( + build: (d: Snapshot) => { ops: WriteOp[]; id: Id }, + ): Promise => { + setLinking(true); + setLinkError(undefined); + let id: Id | undefined; + return app + .apply((d) => { + const built = build(d); + id = built.id; + return built.ops; + }) + .then( + () => { + setChosenId(id); + setLinking(false); + return true; + }, + (err: unknown) => { + setLinkError(messageOf(err)); + setLinking(false); + return false; + }, + ); + }; + const linkExisting = (id: Id): Promise => + runLink((d) => ({ ops: linkOps(d, id), id })); + const linkNew = (names: { + companyName: string; + title: string; + }): Promise => + runLink((d) => { + const input: JobInput = { + source: captured + ? captured.source === 'jsonld' + ? 'jsonld' + : 'selection' + : 'paste', + url: isHttp(outcomeUrl) ? outcomeUrl.trim() : undefined, + relCanonical: captured?.relCanonical, + title: names.title, + companyName: names.companyName, + location: captured?.location, + text: captured?.text ?? '', + }; + const built = newApplicationFromFormOps(d, input, deps); + return { ops: built.ops, id: built.applicationId }; + }); + let body: VNode; if (captured) { const input: JobInput = { @@ -367,6 +453,19 @@ export function Popup(): VNode { ) : null} + {fillStatus === 'ready' && formKey !== undefined ? ( + + ) : null} @@ -125,7 +165,7 @@ export function FormLink(props: { > Create and link - @@ -141,7 +181,7 @@ export function FormLink(props: {