From e55c2539239d2addc7f24f867147ea1793732966 Mon Sep 17 00:00:00 2001 From: Arlo Kharod <42082766+firegiant9000@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:25:50 -0600 Subject: [PATCH 1/3] Add the wording of the fill review --- shell/ui/fill-view.test.ts | 317 +++++++++++++++++++++++++++++++++++++ shell/ui/fill-view.ts | 165 +++++++++++++++++++ 2 files changed, 482 insertions(+) create mode 100644 shell/ui/fill-view.test.ts create mode 100644 shell/ui/fill-view.ts diff --git a/shell/ui/fill-view.test.ts b/shell/ui/fill-view.test.ts new file mode 100644 index 0000000..0f1b8b6 --- /dev/null +++ b/shell/ui/fill-view.test.ts @@ -0,0 +1,317 @@ +import { describe, expect, it } from 'vitest'; +import type { + FillPlan, + FillWrite, + FrameAdvice, + NotFilledReason, + PlanRow, +} from '../../core/src/fill/types'; +import type { FillState } from '../fill/controller'; +import { fillView } from './fill-view'; + +type Ready = Extract; + +function row(over: Partial = {}): PlanRow { + return { + ref: 1, + label: 'Full name', + control: 'text', + required: false, + status: 'will_fill', + formValue: '', + ownValue: 'Sam Example', + ...over, + }; +} + +function valueWrite(ref: number, value = 'Sam Example'): FillWrite { + return { ref, kind: 'value', value }; +} + +function ready( + over: { + rows?: PlanRow[]; + writes?: FillWrite[]; + unrecognisedOptional?: number; + frames?: FrameAdvice[]; + filled?: number; + failed?: PlanRow[]; + applyFailed?: boolean; + } = {}, +): Ready { + const plan: FillPlan = { + rows: over.rows ?? [], + writes: over.writes ?? [], + unrecognisedOptional: over.unrecognisedOptional ?? 0, + frames: over.frames ?? [], + }; + return { + status: 'ready', + plan, + busy: false, + filled: over.filled, + failed: over.failed ?? [], + applyFailed: over.applyFailed ?? false, + }; +} + +function notFilled( + reason: NotFilledReason, + over: Partial = {}, +): PlanRow { + return row({ + status: 'not_filled', + reason, + ownValue: '', + target: { kind: 'answer', key: 'work_authorisation' }, + ...over, + }); +} + +const GREENHOUSE: FrameAdvice = { + site: 'greenhouse', + src: 'https://job-boards.greenhouse.io/embed/job_app?for=examplecorp&token=123', +}; +const ASHBY: FrameAdvice = { + site: 'ashby', + src: 'https://jobs.ashbyhq.com/examplecorp/abc', +}; + +describe('fillView', () => { + it('counts and plurals', () => { + const one = fillView(ready({ writes: [valueWrite(1)] })); + expect(one.button).toBe('Fill 1 empty field'); + expect(one.nothing).toBeUndefined(); + + const many = fillView( + ready({ writes: [1, 2, 3, 4, 5, 6, 7].map((n) => valueWrite(n)) }), + ); + expect(many.button).toBe('Fill 7 empty fields'); + expect(many.nothing).toBeUndefined(); + + const none = fillView(ready()); + expect(none.button).toBeUndefined(); + expect(none.nothing).toBe('Nothing to fill.'); + + expect(fillView(ready({ filled: 1 })).filledLine).toBe('Filled 1 field.'); + expect(fillView(ready({ filled: 3 })).filledLine).toBe('Filled 3 fields.'); + expect(fillView(ready()).filledLine).toBeUndefined(); + + expect(fillView(ready({ unrecognisedOptional: 1 })).optionalLine).toBe( + '1 optional field not recognised', + ); + expect(fillView(ready({ unrecognisedOptional: 4 })).optionalLine).toBe( + '4 optional fields not recognised', + ); + expect(fillView(ready()).optionalLine).toBeUndefined(); + }); + + it('has the footer', () => { + expect(fillView(ready()).footer).toBe( + 'Countersign never submits. Check the form, then submit it yourself.', + ); + }); + + it('writes the value for a value write and the own value for an option write', () => { + const view = fillView( + ready({ + rows: [ + row({ + ref: 1, + write: valueWrite(1, 'sam@example.com'), + ownValue: 'ignored', + }), + row({ + ref: 2, + label: 'Country', + write: { ref: 2, kind: 'option', index: 3 }, + ownValue: 'Canada', + }), + ], + }), + ); + expect(view.willFill).toEqual([ + { key: '1', label: 'Full name', lines: ['sam@example.com'] }, + { key: '2', label: 'Country', lines: ['Canada'] }, + ]); + }); + + it('matches rows show the form value', () => { + const view = fillView( + ready({ + rows: [row({ status: 'matches', formValue: 'Sam Example' })], + }), + ); + expect(view.matches).toEqual([ + { key: '1', label: 'Full name', lines: ['Sam Example'] }, + ]); + }); + + it.each<[NotFilledReason, string]>([ + ['custom_control', 'custom dropdown, choose it yourself'], + ['unsupported_control', 'this kind of field is not filled'], + ['never_auto', 'never auto-filled'], + ['ambiguous', 'more than one match, not filled'], + ['negated', 'worded as a negative, answer it yourself'], + ['other_country', 'names another place, answer it yourself'], + ['file', 'attach it yourself'], + ['not_recognised', 'required, not recognised'], + ['changed', 'changed while filling'], + ])('each reason has its line: %s', (reason, line) => { + const view = fillView(ready({ rows: [notFilled(reason)] })); + expect(view.notFilled).toHaveLength(1); + expect(view.notFilled[0]?.lines).toEqual([line]); + }); + + it('no_value names the profile field for a profile target', () => { + const view = fillView( + ready({ + rows: [ + notFilled('no_value', { + target: { kind: 'profile', key: 'first_name' }, + }), + ], + }), + ); + expect(view.notFilled[0]?.lines).toEqual(['add First name in My details']); + }); + + it('no_value points to the Answers page for an answer target', () => { + const view = fillView(ready({ rows: [notFilled('no_value')] })); + expect(view.notFilled[0]?.lines).toEqual(['add it on the Answers page']); + }); + + it('differs shows both values and a copy', () => { + const view = fillView( + ready({ + rows: [ + row({ + status: 'differs', + formValue: 'S. Example', + ownValue: 'Sam Example', + }), + ], + }), + ); + expect(view.differs).toEqual([ + { + key: '1', + label: 'Full name', + lines: ['form: S. Example', 'yours: Sam Example'], + copy: 'Sam Example', + }, + ]); + }); + + it('a differs row with an empty own value has no copy', () => { + const view = fillView( + ready({ + rows: [row({ status: 'differs', formValue: 'x', ownValue: '' })], + }), + ); + expect(view.differs[0]?.copy).toBeUndefined(); + }); + + it('a salary row copies the formatted range', () => { + const view = fillView( + ready({ + rows: [ + row({ + label: 'Salary expectations', + status: 'differs', + formValue: '90000', + ownValue: '$100,000 to $120,000', + target: { kind: 'answer', key: 'salary' }, + }), + ], + }), + ); + expect(view.differs[0]?.copy).toBe('$100,000 to $120,000'); + }); + + it('a negated row has no copy, because the plan blanks its own value', () => { + const view = fillView(ready({ rows: [notFilled('negated')] })); + expect(view.notFilled[0]?.copy).toBeUndefined(); + }); + + it('a not_filled row with a copyable reason copies its own value', () => { + const view = fillView( + ready({ rows: [notFilled('custom_control', { ownValue: 'Canada' })] }), + ); + expect(view.notFilled[0]?.copy).toBe('Canada'); + }); + + it('a no_value row has no copy', () => { + const view = fillView( + ready({ rows: [notFilled('no_value', { ownValue: 'stray' })] }), + ); + expect(view.notFilled[0]?.copy).toBeUndefined(); + }); + + it.each(['file', 'not_recognised', 'changed'])( + 'a %s row has no copy', + (reason) => { + const view = fillView( + ready({ rows: [notFilled(reason, { ownValue: 'stray' })] }), + ); + expect(view.notFilled[0]?.copy).toBeUndefined(); + }, + ); + + it('frame messages', () => { + expect(fillView(ready({ frames: [GREENHOUSE] })).frames).toEqual([ + 'This form is inside a Greenhouse frame, which Countersign cannot reach from here. Right-click the form, choose This Frame, then Open Frame in New Tab, and open Countersign there.', + ]); + expect(fillView(ready({ frames: [ASHBY] })).frames).toEqual([ + 'This form is inside an Ashby frame, which Countersign cannot reach from here. Right-click the form, choose This Frame, then Open Frame in New Tab, and open Countersign there.', + ]); + expect(fillView(ready()).frames).toEqual([]); + }); + + it('gives one sentence per site, in the order first seen', () => { + const view = fillView( + ready({ frames: [GREENHOUSE, ASHBY, { ...GREENHOUSE }] }), + ); + expect(view.frames).toHaveLength(2); + expect(view.frames[0]).toContain('a Greenhouse frame'); + expect(view.frames[1]).toContain('an Ashby frame'); + }); + + it('failed rows come first under not filled, as changed while filling', () => { + const failed = notFilled('changed', { ref: 9, label: 'Phone' }); + const planned = notFilled('never_auto', { ref: 4, label: 'Password' }); + const view = fillView(ready({ rows: [planned], failed: [failed] })); + expect(view.notFilled.map((r) => [r.key, r.lines])).toEqual([ + ['failed-9', ['changed while filling']], + ['4', ['never auto-filled']], + ]); + }); + + it('an empty label reads (no label)', () => { + const view = fillView(ready({ rows: [row({ label: '' })] })); + expect(view.willFill[0]?.label).toBe('(no label)'); + }); + + it('apply failed sets the error', () => { + expect(fillView(ready({ applyFailed: true })).error).toBe( + 'Countersign could not confirm the fill. Check the form before you submit it.', + ); + expect(fillView(ready()).error).toBeUndefined(); + }); + + it('groups rows by status in plan order', () => { + const view = fillView( + ready({ + rows: [ + row({ ref: 1, status: 'will_fill', write: valueWrite(1) }), + row({ ref: 2, status: 'matches' }), + row({ ref: 3, status: 'differs' }), + row({ ref: 4, status: 'will_fill', write: valueWrite(4) }), + ], + }), + ); + expect(view.willFill.map((r) => r.key)).toEqual(['1', '4']); + expect(view.matches.map((r) => r.key)).toEqual(['2']); + expect(view.differs.map((r) => r.key)).toEqual(['3']); + }); +}); diff --git a/shell/ui/fill-view.ts b/shell/ui/fill-view.ts new file mode 100644 index 0000000..36d41c0 --- /dev/null +++ b/shell/ui/fill-view.ts @@ -0,0 +1,165 @@ +import { PROFILE_FIELDS } from '../../core/src/profile'; +import type { NotFilledReason, PlanRow } from '../../core/src/fill/types'; +import type { FillState } from '../fill/controller'; + +export interface FillViewRow { + key: string; + label: string; + lines: string[]; + copy?: string; +} + +export interface FillView { + button?: string; + nothing?: string; + filledLine?: string; + error?: string; + frames: string[]; + willFill: FillViewRow[]; + matches: FillViewRow[]; + differs: FillViewRow[]; + notFilled: FillViewRow[]; + optionalLine?: string; + footer: string; +} + +const PROFILE_LABELS = new Map( + PROFILE_FIELDS.map((f) => [f.key, f.label]), +); + +const NO_COPY_REASONS: ReadonlySet = new Set([ + 'no_value', + 'file', + 'not_recognised', + 'changed', +]); + +function plural(n: number, one: string, many: string): string { + return n === 1 ? one : many; +} + +function reasonLine(row: PlanRow): string { + switch (row.reason) { + case 'no_value': + if (row.target?.kind === 'profile') { + const label = PROFILE_LABELS.get(row.target.key); + return label === undefined + ? 'add it in My details' + : `add ${label} in My details`; + } + return 'add it on the Answers page'; + case 'custom_control': + return 'custom dropdown, choose it yourself'; + case 'unsupported_control': + return 'this kind of field is not filled'; + case 'never_auto': + return 'never auto-filled'; + case 'ambiguous': + return 'more than one match, not filled'; + case 'negated': + return 'worded as a negative, answer it yourself'; + case 'other_country': + return 'names another place, answer it yourself'; + case 'file': + return 'attach it yourself'; + case 'not_recognised': + return 'required, not recognised'; + case 'changed': + return 'changed while filling'; + default: + return 'not filled'; + } +} + +function frameSentence(site: 'greenhouse' | 'ashby'): string { + const name = site === 'greenhouse' ? 'a Greenhouse' : 'an Ashby'; + return `This form is inside ${name} frame, which Countersign cannot reach from here. Right-click the form, choose This Frame, then Open Frame in New Tab, and open Countersign there.`; +} + +function viewRow(row: PlanRow, key: string, lines: string[]): FillViewRow { + const out: FillViewRow = { + key, + label: row.label === '' ? '(no label)' : row.label, + lines, + }; + const copyable = + row.status === 'differs' || + (row.status === 'not_filled' && + row.reason !== undefined && + !NO_COPY_REASONS.has(row.reason)); + if (copyable && row.ownValue !== '') out.copy = row.ownValue; + return out; +} + +function willFillLine(row: PlanRow): string { + return row.write?.kind === 'value' ? row.write.value : row.ownValue; +} + +export function fillView( + state: Extract, +): FillView { + const { plan } = state; + const view: FillView = { + frames: [], + willFill: [], + matches: [], + differs: [], + notFilled: [], + footer: + 'Countersign never submits. Check the form, then submit it yourself.', + }; + + const count = plan.writes.length; + if (count > 0) { + view.button = `Fill ${count} empty ${plural(count, 'field', 'fields')}`; + } else { + view.nothing = 'Nothing to fill.'; + } + + if (state.filled !== undefined) { + view.filledLine = `Filled ${state.filled} ${plural(state.filled, 'field', 'fields')}.`; + } + if (state.applyFailed) { + view.error = + 'Countersign could not confirm the fill. Check the form before you submit it.'; + } + + const seen = new Set(); + for (const frame of plan.frames) { + if (seen.has(frame.site)) continue; + seen.add(frame.site); + view.frames.push(frameSentence(frame.site)); + } + + for (const row of state.failed) { + view.notFilled.push(viewRow(row, `failed-${row.ref}`, [reasonLine(row)])); + } + for (const row of plan.rows) { + const key = String(row.ref); + switch (row.status) { + case 'will_fill': + view.willFill.push(viewRow(row, key, [willFillLine(row)])); + break; + case 'matches': + view.matches.push(viewRow(row, key, [row.formValue])); + break; + case 'differs': + view.differs.push( + viewRow(row, key, [ + `form: ${row.formValue}`, + `yours: ${row.ownValue}`, + ]), + ); + break; + case 'not_filled': + view.notFilled.push(viewRow(row, key, [reasonLine(row)])); + break; + } + } + + const optional = plan.unrecognisedOptional; + if (optional > 0) { + view.optionalLine = `${optional} optional ${plural(optional, 'field', 'fields')} not recognised`; + } + return view; +} From 7bd1ae325cff85a862c19ca352112502e38959d0 Mon Sep 17 00:00:00 2001 From: Arlo Kharod <42082766+firegiant9000@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:27:46 -0600 Subject: [PATCH 2/3] Show the fill review in the popup --- shell/ui/FillSection.tsx | 128 +++++++++++++++++++++++++++ shell/ui/Popup.tsx | 37 ++++++++ shell/ui/fill-section-source.test.ts | 34 +++++++ shell/ui/theme/popup-layout.css | 21 +++++ 4 files changed, 220 insertions(+) create mode 100644 shell/ui/FillSection.tsx create mode 100644 shell/ui/fill-section-source.test.ts diff --git a/shell/ui/FillSection.tsx b/shell/ui/FillSection.tsx new file mode 100644 index 0000000..1ab5475 --- /dev/null +++ b/shell/ui/FillSection.tsx @@ -0,0 +1,128 @@ +import type { VNode } from 'preact'; +import { useEffect, useState } from 'preact/hooks'; +import type { FillResult } from '../../core/src/fill/result'; +import type { FillController, FillState } from '../fill/controller'; +import { copyText } from '../clipboard'; +import { Button } from './components/Button'; +import { CardSection } from './components/IndexCard'; +import { Disclosure } from './components/Disclosure'; +import { Fact } from './components/Fact'; +import { Text } from './components/Text'; +import { TypedLine } from './components/TypedLine'; +import { fillView } from './fill-view'; +import type { FillViewRow } from './fill-view'; + +type CopyState = { key: string; ok: boolean; message: string }; + +export function FillSection(props: { + controller: FillController; + onFilled?: (result: FillResult) => void; +}): VNode | null { + const [state, setState] = useState(() => props.controller.state()); + const [copy, setCopy] = useState(undefined); + + useEffect(() => { + const unsubscribe = props.controller.subscribe(setState); + // A change between render and subscribe must not be missed. + setState(props.controller.state()); + return unsubscribe; + }, [props.controller]); + + if (state.status !== 'ready') return null; + const view = fillView(state); + + // Called synchronously from onClick: no await before copyText. + const doCopy = (key: string, text: string): void => { + copyText(text).then( + () => setCopy({ key, ok: true, message: 'Copied' }), + () => setCopy({ key, ok: false, message: 'Could not copy.' }), + ); + }; + + const status = (key: string): VNode | null => { + if (copy?.key !== key) return null; + return copy.ok ? ( + {copy.message} + ) : ( + + {' '} + {copy.message} + + ); + }; + + const row = (r: FillViewRow): VNode => ( + + ); + + const group = ( + title: string, + rows: FillViewRow[], + open: boolean, + ): VNode | null => + rows.length === 0 ? null : ( + + {rows.map(row)} + + ); + + return ( + + + + ); +} diff --git a/shell/ui/Popup.tsx b/shell/ui/Popup.tsx index afb5b70..e237910 100644 --- a/shell/ui/Popup.tsx +++ b/shell/ui/Popup.tsx @@ -8,12 +8,16 @@ import { saveJobOps, } from '../../core/src/job-ops'; import type { JobInput } from '../../core/src/job-ops'; +import { profileValues } from '../../core/src/profile'; import { canonicalizeUrl } from '../../core/src/url'; import { capturePage } from '../capture/inject'; import type { CaptureOutcome } from '../capture/inject'; +import { createFillController } from '../fill/controller'; +import type { FillController } from '../fill/controller'; import { openDashboard } from '../open-dashboard'; import { useAppData } from './app-data'; import { AnyoneHere } from './AnyoneHere'; +import { FillSection } from './FillSection'; import { Button } from './components/Button'; import { Fact } from './components/Fact'; import { Field } from './components/Field'; @@ -84,9 +88,41 @@ export function Popup(): VNode { const [createError, setCreateError] = useState(undefined); const [openError, setOpenError] = useState(undefined); + const dataRef = useRef(data); + dataRef.current = data; + const depsRef = useRef(deps); + depsRef.current = deps; + const captureRef = useRef(capture); + captureRef.current = capture; + const controllerRef = useRef(undefined); + if (controllerRef.current === undefined) { + controllerRef.current = createFillController({ + input: () => ({ + profile: profileValues(dataRef.current), + answers: Array.isArray(dataRef.current.canonicalAnswers) + ? dataRef.current.canonicalAnswers + : [], + }), + now: () => depsRef.current.now(), + relCanonical: () => { + const c = captureRef.current; + if (c.status !== 'done' || !c.outcome.ok) return undefined; + return c.outcome.capture.source !== 'none' + ? c.outcome.capture.relCanonical + : undefined; + }, + }); + } + const controller = controllerRef.current; + + useEffect(() => { + controller.refresh(); + }, [data]); + useEffect(() => { if (started.current) return; started.current = true; + void controller.start(); capturePage().then( (outcome) => setCapture({ status: 'done', outcome }), (err: unknown) => @@ -331,6 +367,7 @@ export function Popup(): VNode { ) : null} +