Files
loki/widget/attachments.ts
T
CatoandClaude Opus 5.5 17e63d1396 feat(widget): one designed piece — the launcher is the status, Loki stops crying wolf, the panel fits (#1106)
Found by using the live widget on loki.orangecat.ch as owner, not by tests:

- The composer overflowed in every real browser: with a mic present, Record +
  Screenshots + Send as feedback pushed Ask Loki out of the 372px panel (the
  test browser has no mic, so nothing caught it). Tools are icons at rest, the
  row wraps; widget-embed-browser now fakes a mic and fails at 1200/390/320 if
  the panel scrolls sideways or any button sits outside it (proven against the
  #1099 CSS: 6 failures).
- The top bar covered the site's own navigation. For the owner the LAUNCHER is
  now the status: "Loki · watching", an unread count of what Loki said while
  the panel was closed, the newest remark in a bubble beside it. The bar is the
  fallback, shown only when neither launcher nor panel can be seen.
- Loki cried wolf on Loki's own page: "15 links have no name" (innerText is ""
  under content-visibility:auto, and ?? never fell back) and "25 tap targets
  too small" (WCAG 2.5.8's spacing exception ignored). Now 0 and 1 (a really
  crowded 154x16 link); examples name a link's address instead of "a". Pinned
  by fixtures that fail with the old code.
- A remark arriving mid-tap moved the button away (it happened live): the
  thread follows new messages only if you were at the bottom or acted
  yourself; opening the panel lands on the newest message.
- "On it" was a dead end: owner builds now return followUrl (Loki's Watch the
  fix page for that item) and the receipt says "Follow the build →".

widget/README.md now opens with the design model and the rules that resolve
its trade-offs.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:17:56 -07:00

87 lines
2.5 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>;
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,
reset: () => {
shots = [];
},
};
}