import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react"; import { commentsApi } from "../../api/comments"; import { useSession } from "../../store/SessionContext"; import { useTeam } from "../../store/TeamContext"; import { useToast } from "../../store/ToastContext"; import type { Comment, Member } from "../../types"; import { formatDateTime, initials } from "../../utils/dates"; import { MentionTextarea } from "./MentionTextarea"; /** Highlights "@Full Name" for people on the team. */ function withMentions(body: string, members: Member[]): ReactNode[] { const names = members.map((m) => m.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); if (!names.length) return [body]; return body.split(new RegExp(`(@(?:${names.join("|")}))`, "g")).map((part, i) => i % 2 ? ( {part} ) : ( part ), ); } const draftKey = (taskId: number) => `team-tasks.comment-draft.${taskId}`; function readDraft(taskId: number): string { try { return localStorage.getItem(draftKey(taskId)) ?? ""; } catch { return ""; } } function writeDraft(taskId: number, text: string): void { try { if (text) localStorage.setItem(draftKey(taskId), text); else localStorage.removeItem(draftKey(taskId)); } catch { // not kept, still usable } } /** * A task's comments, oldest first, in their own scroll area that starts at the newest. An unsent * comment is kept per task in this browser, so it survives switching tasks and reloading. */ export function CommentThread({ taskId }: { taskId: number }) { const { members } = useTeam(); const { canEdit } = useSession(); const toast = useToast(); const [comments, setComments] = useState(null); const [error, setError] = useState(null); const [draft, setDraftState] = useState(() => readDraft(taskId)); const [posting, setPosting] = useState(false); const list = useRef(null); const setDraft = (text: string) => { setDraftState(text); writeDraft(taskId, text); }; // Another task in the same place: show its draft. useEffect(() => setDraftState(readDraft(taskId)), [taskId]); useEffect(() => { let live = true; commentsApi.list(taskId).then( (cs) => live && setComments(cs), (e) => live && setError((e as Error).message), ); return () => { live = false; }; }, [taskId]); // Show the latest comment: start scrolled to the bottom, and go there again after posting. useLayoutEffect(() => { if (list.current) list.current.scrollTop = list.current.scrollHeight; }, [comments]); const post = async () => { if (!draft.trim() || posting) return; setPosting(true); try { const c = await commentsApi.post(taskId, draft.trim()); setComments((cs) => [...(cs ?? []), c]); setDraft(""); } catch (e) { toast((e as Error).message, "error"); } finally { setPosting(false); } }; const author = (id: string) => members.find((m) => m.id === id)?.name ?? "Former member"; return ( Comments {comments && {comments.length}} {error && Couldn't load comments: {error}} {!comments && !error && Loading…} {comments && ( {comments.map((c) => ( {initials(author(c.authorId))} {author(c.authorId)} {formatDateTime(c.createdAt)} {withMentions(c.body, members)} ))} {comments.length === 0 && No comments yet.} )} {canEdit && ( { e.preventDefault(); void post(); }} > Add a comment void post()} /> {posting ? "Posting…" : "Post comment"} )} ); }
Couldn't load comments: {error}
Loading…
{withMentions(c.body, members)}