Files
loki/widget/attachments.ts
CatoandClaude Fable 5.1 fca81031b0 feat(widget): the widget reports on itself, to Loki (#1142)
* feat(widget): the widget reports on itself, to Loki

Watch mode reads the host page and is careful never to blame the site for
Loki's own work: the widget's own requests, errors and layout were left out of
what it says — which meant dropped. Nobody heard about a panel that did not fit
a phone unless the operator was the one holding it.

- A fourth "About" chip, "Loki itself": a note about the panel goes to Loki's
  own project (FEEDBACK_WIDGET_TOKEN's), never to the site's builder, with the
  same screenshot attach. About bar extracted to widget/about-bar.ts.
- Self-watch (owner, while watching): the widget's own errors, its calls to
  Loki failing or over 20s, and a panel wider than the screen are filed to
  the same place automatically — once per kind per tab, said in one line in
  the conversation, and never looping through the report call itself. A 4xx
  is Loki answering on purpose, not a defect. Watch now hands own-requests
  over instead of discarding them.
- New public route /api/widget/self-report: host fcw_ token + origin
  allowlist, owner pass required for anything the widget noticed unasked,
  one row per kind per site via noticeKey.
- The owner's top bar steps aside while the site has a modal open: it sat on
  top of "Choose a model" on the operator's phone.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E1qoQ9syUYXCRNbyrpqPGa

* feat(widget): Loki's own owner's self-reports start the fix on the spot

A self-report filed by Loki's OWN owner — using Loki's widget on one of their
sites — is the owner's word on their own product, and the owner's word is the
decision (api/feedback): a note, a crash or a panel that did not fit starts a
build into Loki there and then, and the receipt reads "On it … built into
Loki itself" with Follow the build →. A slow or failed call to Loki only
files; its duplicate count is the signal. Anyone else's waits in the inbox.

The receipt wording moves to widget/receipt.ts (pure, tested) — the thread
renderer was over the lint's line cap.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E1qoQ9syUYXCRNbyrpqPGa

* test(widget): the About row has four chips now

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E1qoQ9syUYXCRNbyrpqPGa

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-11 14:42:51 -07:00

98 lines
3.0 KiB
TypeScript

/**
* Screenshot attachments: the file picker + paste path, client-side downscale,
* and the thumbnail strip. Owns the shot list; the panel reads it at submit.
*/
import { CAMERA_SVG, downscaleImage, h } from "./dom";
export type Attachments = {
shots(): string[];
render(): void;
attach(file: Blob | null | undefined): Promise<void>;
attachMany(files: FileList | null): Promise<void>;
/** Pasting an image anywhere inside `el` attaches it (text pastes as usual). */
listenPaste(el: HTMLElement): void;
reset(): void;
};
export function createAttachments(opts: {
attachBtn: HTMLButtonElement;
container: HTMLElement;
max: number;
onError(message: string): void;
}): Attachments {
const { attachBtn, container, max } = opts;
let shots: string[] = [];
function render() {
container.textContent = "";
for (let i = 0; i < shots.length; i++) {
const shotWrap = h("span", "shot");
const shotImg = h("img");
shotImg.alt = `Screenshot ${i + 1}`;
shotImg.src = shots[i];
const shotRm = h("button", "rm", "✕");
shotRm.setAttribute("aria-label", `Remove screenshot ${i + 1}`);
const idx = i;
shotRm.addEventListener("click", () => {
shots.splice(idx, 1);
render();
});
shotWrap.append(shotImg, shotRm);
container.appendChild(shotWrap);
}
attachBtn.style.display = shots.length >= max ? "none" : "";
// A compact composer shows the label only once it says something new.
attachBtn.classList.toggle("has", shots.length > 0);
// Icon + label every time: the old branch replaced the whole button
// with bare text once a shot was attached, so the camera vanished.
attachBtn.textContent = "";
const icon = h("span");
icon.innerHTML = CAMERA_SVG;
attachBtn.append(
icon,
h("span", undefined, shots.length > 0 ? `Add more (${shots.length}/${max})` : "Screenshots"),
);
}
async function attach(file: Blob | null | undefined) {
if (!file || !file.type.startsWith("image/")) return;
if (shots.length >= max) {
opts.onError(`Maximum ${max} screenshots`);
return;
}
const dataUrl = await downscaleImage(file);
if (dataUrl) {
shots.push(dataUrl);
render();
} else {
opts.onError("Could not attach that image — try a smaller one");
}
}
async function attachMany(files: FileList | null) {
if (!files) return;
for (let i = 0; i < files.length && shots.length < max; i++) {
await attach(files[i]);
}
}
return {
shots: () => shots,
render,
attach,
attachMany,
listenPaste: (el) =>
el.addEventListener("paste", (e: ClipboardEvent) => {
const images = Array.from(e.clipboardData?.items ?? []).filter((i) =>
i.type.startsWith("image/"),
);
if (!images.length) return;
e.preventDefault();
for (const item of images) void attach(item.getAsFile());
}),
reset: () => {
shots = [];
},
};
}