Files
archie/ui/src/chat/task-ref.ts
T
sam 7ffbf0435c
ci/woodpecker/pr/quality Pipeline was canceled
Quality / check (pull_request) Successful in 16m18s
feat(chat): link #N in replies to tasks that exist (#1882)
2026-11-12 22:46:22 +11:00

51 lines
1.9 KiB
TypeScript

import { api, ApiError } from "@/lib/api";
const FOUND_TTL_MS = 10 * 60_000;
const MISSING_TTL_MS = 30_000;
const lookups = new Map<number, { at: number; found: boolean }>();
const inflight = new Map<number, Promise<boolean | null>>();
/**
* Whether task `id` exists, asked of the tasks API. Answers are kept briefly
* so a reply that re-renders every frame asks once. A task that was missing a
* moment ago may have been created since, so that answer expires quickly. A
* failed lookup is null and is not kept: the reference stays plain text.
*/
export function taskExists(id: number): Promise<boolean | null> {
const known = lookups.get(id);
if (known && Date.now() - known.at < (known.found ? FOUND_TTL_MS : MISSING_TTL_MS))
return Promise.resolve(known.found);
let pending = inflight.get(id);
if (!pending) {
pending = api
.task(String(id))
.then(() => true)
.catch((err) => (err instanceof ApiError && err.status === 404 ? false : null))
.then((found) => {
if (found !== null) lookups.set(id, { at: Date.now(), found });
inflight.delete(id);
return found;
});
inflight.set(id, pending);
}
return pending;
}
const HIGHLIGHT = ["bg-primary-soft", "motion-safe:transition-colors", "motion-safe:duration-700"];
/**
* Scrolls to task `id`'s row when the page lists it and marks it once.
* Returns false when no such row is on screen.
*/
export function revealTaskRow(id: number): boolean {
const row = document.querySelector<HTMLElement>(`tr[data-row-key="${id}"]`);
if (!row) return false;
const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
row.scrollIntoView({ block: "center", behavior: still ? "auto" : "smooth" });
row.classList.add(...HIGHLIGHT);
window.setTimeout(() => row.classList.remove("bg-primary-soft"), 1600);
window.setTimeout(() => row.classList.remove(...HIGHLIGHT), 2400);
return true;
}