export type IssueStatus = "open" | "in_progress" | "resolved" | "closed"; export const ISSUE_STATUS_OPTIONS: IssueStatus[] = ["open", "in_progress", "resolved", "closed"]; export const STATUS_LABELS: Record = { open: "open", in_progress: "in progress", resolved: "resolved", closed: "closed", }; export type User = { id: number; username: string; avatarUrl: string | null; }; export type ProjectInfo = { name: string; description: string | null; repoUrl: string | null; }; // A spool — one community/project on this instance, owned like a GitHub repo. export type Spool = { id: number; slug: string; name: string; description: string | null; repoUrl: string | null; ownerId: number; threadsLocked: boolean; owner: string; createdAt: string; // Only set on the single-spool detail fetch — whether the signed-in viewer // (if any) may start a new thread here right now. canStartThread?: boolean; }; export type SpoolSummary = Spool & { openIssues: number; totalIssues: number; }; // One message in a spool's threads view. A message with parentId === null // starts a thread; replies point at their thread's root. export type ThreadMessage = { id: number; parentId: number | null; body: string; author: string; authorAvatar: string | null; cutIssueNumber: number | null; createdAt: string; }; // A Tangled repo the logged-in user owns that hasn't yet been accepted or // declined for syncing into Spool. export type PendingTangledRepo = { uri: string; name: string; description: string | null; repoUrl: string; }; export type SearchResult = | { kind: "issue"; spoolSlug: string; issueNumber: number; title: string; status: IssueStatus; snippet: string } | { kind: "comment"; spoolSlug: string; issueNumber: number; issueTitle: string; snippet: string } | { kind: "message"; spoolSlug: string; messageRootId: number; author: string; snippet: string }; export type IssueSummary = { id: number; number: number; // per-spool issue number — what URLs and "#N" labels use title: string; status: IssueStatus; author: string; commentCount: number; createdAt: string; }; export type IssueComment = { id: number; body: string; author: string; authorAvatar: string | null; createdAt: string; }; export type IssueDetail = { id: number; // global id — the handle for mutations (status, title, comments) number: number; title: string; body: string; status: IssueStatus; author: string; authorAvatar: string | null; source: { messageId: number; author: string } | null; createdAt: string; comments: IssueComment[]; }; async function request(path: string, init?: RequestInit): Promise { const res = await fetch(path, { headers: { "Content-Type": "application/json" }, ...init, }); if (!res.ok) { const data = await res.json().catch(() => null); throw new Error(data?.error ?? `Request failed (${res.status})`); } return res.json(); } export const api = { me: () => request<{ user: User | null }>("/api/me"), logout: () => request<{ ok: boolean }>("/auth/logout", { method: "POST" }), project: () => request("/api/project"), spools: () => request("/api/spools"), createSpool: (data: { name: string; description?: string; repoUrl?: string }) => request("/api/spools", { method: "POST", body: JSON.stringify(data), }), spool: (slug: string) => request(`/api/spools/${slug}`), updateSpool: ( slug: string, patch: { description?: string; repoUrl?: string; threadsLocked?: boolean } ) => request(`/api/spools/${slug}`, { method: "PATCH", body: JSON.stringify(patch), }), threads: (slug: string) => request(`/api/spools/${slug}/threads`), postMessage: (slug: string, body: string, parentId?: number) => request<{ id: number }>(`/api/spools/${slug}/threads`, { method: "POST", body: JSON.stringify({ body, parentId }), }), cutMessage: (id: number) => request<{ id: number; number: number }>(`/api/messages/${id}/cut`, { method: "POST" }), search: (q: string) => request(`/api/search?q=${encodeURIComponent(q)}`), pendingTangledRepos: () => request("/api/tangled-repos/pending"), decideTangledRepos: (accepted: string[], declined: string[]) => request<{ ok: boolean }>("/api/tangled-repos/decide", { method: "POST", body: JSON.stringify({ accepted, declined }), }), issues: (slug: string) => request(`/api/spools/${slug}/issues`), createIssue: (slug: string, data: { title: string; body: string }) => request<{ id: number }>(`/api/spools/${slug}/issues`, { method: "POST", body: JSON.stringify(data), }), issue: (slug: string, number: string) => request(`/api/spools/${slug}/issues/${number}`), updateIssue: (id: number, patch: { status?: IssueStatus; title?: string }) => request(`/api/issues/${id}`, { method: "PATCH", body: JSON.stringify(patch), }), addComment: (id: number, body: string) => request(`/api/issues/${id}/comments`, { method: "POST", body: JSON.stringify({ body }), }), }; export function timeAgo(iso: string): string { const seconds = Math.floor((Date.now() - new Date(iso).getTime()) / 1000); if (seconds < 60) return "just now"; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `${minutes}m ago`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours}h ago`; const days = Math.floor(hours / 24); if (days < 30) return `${days}d ago`; return new Date(iso).toLocaleDateString(); }