ext: popup, options, and the panel bridge (build order step 6)
Popup and Options are separate documents from the background page and can't reach its live VeloxTransport directly, so background/bridge.ts relays it over one browser.runtime.connect port per document (call/subscribe/getStatus/reconnect/pair/unpair/setOverride in; result/event/status/pairError out). shared/panel-client.ts is the client side both surfaces use. Popup (src/popup/): status dot + text, active-downloads list driven by event.task.progress/added/state/removed (repaints ride the event's own <=4 Hz cap rather than adding a second timer), pause/resume buttons, "Start it" wired to a reconnect request. State lives in a DOM-free store.ts for unit testing; rendering uses createElement, not innerHTML (web-ext lint flags the latter). Options (src/options/): transport override select, pairing (code entry + pair/unpair, backed by two new WebSocketTransport methods, pairWithCode/unpair), and the daemon's capture policy mirrored via capture.getRules. The capture-policy form is editable only when the active transport is native messaging (uds) -- settings.set and rules.upsert are privileged, uds-only methods per shared/protocol METHODS, so WebSocket can't write them no matter what the page shows; CLAUDE.md section 2 rules out working around that locally. The bridge's status payload adds a kind field (which transport is live) for this to key off. Default category is the one piece of state that's genuinely the extension's own, not the daemon's, and lives in browser.storage.local via options/prefs.ts. Pure decisions (statusLine, pairingAvailable, captureRulesEditable) are split into view.ts for unit testing without a DOM. manifest.json registers the popup action and options_ui page (and, in the same edit, the content_scripts entry the next commit's media detection needs -- split by file, not by manifest line). build.mjs gains popup/options as further esbuild entry points, plus copying their static HTML/CSS into dist/. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Ed8KEmAW48v4YHdxLtqsMB
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
// Pure state for the popup's task list — no DOM, so it is unit-testable without a
|
||||
// browser. Fed by download.list (initial) and event.task.{added,progress,state,removed}
|
||||
// relayed over the panel bridge (docs/05 §3: "live progress via event.task.progress").
|
||||
|
||||
import type {
|
||||
TaskAddedEvent,
|
||||
TaskProgressEvent,
|
||||
TaskRemovedEvent,
|
||||
TaskState,
|
||||
TaskStateEvent,
|
||||
TaskSummary,
|
||||
} from '../shared/protocol/index.js';
|
||||
|
||||
export interface TaskRow {
|
||||
taskId: string;
|
||||
filename: string;
|
||||
state: TaskState;
|
||||
downloadedBytes: number;
|
||||
sizeBytes: number | null;
|
||||
speedBps: number;
|
||||
etaSeconds: number | null;
|
||||
}
|
||||
|
||||
const ACTIVE_STATES: readonly TaskState[] = ['connecting', 'downloading', 'assembling', 'verifying', 'probing', 'queued'];
|
||||
|
||||
function fromSummary(s: TaskSummary): TaskRow {
|
||||
return {
|
||||
taskId: s.taskId,
|
||||
filename: s.filename,
|
||||
state: s.state,
|
||||
downloadedBytes: s.downloadedBytes,
|
||||
sizeBytes: s.sizeBytes ?? null,
|
||||
speedBps: s.speedBps,
|
||||
etaSeconds: s.etaSeconds ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export class PopupStore {
|
||||
private rowsById = new Map<string, TaskRow>();
|
||||
|
||||
setInitial(items: TaskSummary[]): void {
|
||||
this.rowsById.clear();
|
||||
for (const s of items) this.rowsById.set(s.taskId, fromSummary(s));
|
||||
}
|
||||
|
||||
onAdded(evt: TaskAddedEvent): void {
|
||||
this.rowsById.set(evt.taskId, fromSummary(evt.summary));
|
||||
}
|
||||
|
||||
/** event.task.progress is a patch, never a rebuild (docs/05 §3 / EVENTS contract). */
|
||||
onProgress(evt: TaskProgressEvent): void {
|
||||
for (const t of evt.tasks) {
|
||||
const row = this.rowsById.get(t.taskId);
|
||||
if (!row) continue; // a progress tick for a task we haven't seen added yet — ignore
|
||||
row.downloadedBytes = t.downloadedBytes;
|
||||
row.speedBps = t.speedBps;
|
||||
row.etaSeconds = t.etaSeconds ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
onState(evt: TaskStateEvent): void {
|
||||
const row = this.rowsById.get(evt.taskId);
|
||||
if (evt.summary) {
|
||||
this.rowsById.set(evt.taskId, fromSummary(evt.summary));
|
||||
} else if (row) {
|
||||
row.state = evt.state;
|
||||
}
|
||||
}
|
||||
|
||||
onRemoved(evt: TaskRemovedEvent): void {
|
||||
this.rowsById.delete(evt.taskId);
|
||||
}
|
||||
|
||||
/** Active tasks first (what the user opened the popup to watch), then by filename. */
|
||||
rows(): TaskRow[] {
|
||||
const isActive = (r: TaskRow): boolean => ACTIVE_STATES.includes(r.state) || r.state === 'paused';
|
||||
return [...this.rowsById.values()].sort((a, b) => {
|
||||
const activeDiff = Number(isActive(b)) - Number(isActive(a));
|
||||
if (activeDiff !== 0) return activeDiff;
|
||||
return a.filename.localeCompare(b.filename);
|
||||
});
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this.rowsById.size;
|
||||
}
|
||||
}
|
||||
|
||||
export function formatBytes(n: number): string {
|
||||
if (n < 1024) return `${n} B`;
|
||||
const units = ['KB', 'MB', 'GB', 'TB'];
|
||||
let v = n / 1024;
|
||||
let i = 0;
|
||||
while (v >= 1024 && i < units.length - 1) {
|
||||
v /= 1024;
|
||||
i += 1;
|
||||
}
|
||||
return `${v.toFixed(v < 10 ? 1 : 0)} ${units[i]}`;
|
||||
}
|
||||
|
||||
export function formatSpeed(bps: number): string {
|
||||
return bps > 0 ? `${formatBytes(bps)}/s` : '';
|
||||
}
|
||||
|
||||
export function formatEta(seconds: number | null): string {
|
||||
if (seconds === null || seconds < 0 || !Number.isFinite(seconds)) return '';
|
||||
const m = Math.floor(seconds / 60);
|
||||
const s = Math.floor(seconds % 60);
|
||||
return m > 0 ? `${m}m ${s}s` : `${s}s`;
|
||||
}
|
||||
|
||||
export function progressPercent(row: TaskRow): number | null {
|
||||
if (!row.sizeBytes || row.sizeBytes <= 0) return null;
|
||||
return Math.min(100, Math.round((row.downloadedBytes / row.sizeBytes) * 100));
|
||||
}
|
||||
Reference in New Issue
Block a user