{
  "version": 3,
  "sources": ["../../src/canvas/hooks.ts"],
  "sourcesContent": ["import {\n\tcomputed,\n\tComputed,\n\tEditor,\n\tisUninitialized,\n\tTLComment,\n\tTLCommentThread,\n\tTLCommentThreadId,\n\tuseValue,\n\tWeakCache,\n} from 'tldraw'\nimport { getLiveComments, getLiveCommentThreads } from './comment-store'\n\n/**\n * The comment threads that should render (pins, sidebar), reactively \u2014 the live set described by\n * {@link getLiveCommentThreads}. Use `getCommentThreads` for the unfiltered set, including\n * soft-deleted threads awaiting the server's prune.\n *\n * @public\n */\nexport function useCommentThreads(editor: Editor): TLCommentThread[] {\n\treturn useValue('comment threads', () => getLiveCommentThreads(editor), [editor])\n}\n\n/**\n * Every live comment grouped by thread, oldest first within each thread. One computed shared by\n * every mounted thread: each `useThreadComments` would otherwise scan and sort the whole comment\n * set, so N open threads did N full passes on every comment mutation. Cached per editor so the\n * grouping survives re-renders and is rebuilt only when the comment set actually changes.\n */\nconst commentsByThread = new WeakCache<Editor, Computed<Map<TLCommentThreadId, TLComment[]>>>()\n\n/** Exported for tests; use {@link useThreadComments} to consume. @internal */\nexport function getCommentsByThread(editor: Editor) {\n\treturn commentsByThread.get(editor, () =>\n\t\tcomputed('comments by thread', (prev) => {\n\t\t\tconst byThread = new Map<TLCommentThreadId, TLComment[]>()\n\t\t\tfor (const comment of getLiveComments(editor)) {\n\t\t\t\tconst existing = byThread.get(comment.threadId)\n\t\t\t\tif (existing) existing.push(comment)\n\t\t\t\telse byThread.set(comment.threadId, [comment])\n\t\t\t}\n\t\t\tfor (const [threadId, comments] of byThread) {\n\t\t\t\tcomments.sort((a, b) => a.createdAt - b.createdAt)\n\t\t\t\t// Reuse the previous per-thread array when that thread's comment list is unchanged\n\t\t\t\t// (record identities are stable while unchanged), so `useThreadComments` subscribers\n\t\t\t\t// on other threads don't re-render when one thread gains a reply.\n\t\t\t\tif (isUninitialized(prev)) continue\n\t\t\t\tconst prevComments = prev.get(threadId)\n\t\t\t\tif (\n\t\t\t\t\tprevComments &&\n\t\t\t\t\tprevComments.length === comments.length &&\n\t\t\t\t\tcomments.every((comment, i) => prevComments[i] === comment)\n\t\t\t\t) {\n\t\t\t\t\tbyThread.set(threadId, prevComments)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn byThread\n\t\t})\n\t)\n}\n\n/** A thread's live comments, oldest first, reactively. @public */\nexport function useThreadComments(editor: Editor, threadId: TLCommentThreadId): TLComment[] {\n\treturn useValue(\n\t\t'thread comments',\n\t\t() => getCommentsByThread(editor).get().get(threadId) ?? EMPTY_COMMENTS,\n\t\t[editor, threadId]\n\t)\n}\n\n/** Shared empty result so a thread with no comments doesn't churn referential equality. */\nconst EMPTY_COMMENTS: TLComment[] = []\n\n/** Every live comment in the store ({@link getLiveComments}), oldest first, reactively. Group by\n *  `threadId` for per-thread lists. @public */\nexport function useComments(editor: Editor): TLComment[] {\n\treturn useValue(\n\t\t'all comments',\n\t\t() => getLiveComments(editor).sort((a, b) => a.createdAt - b.createdAt),\n\t\t[editor]\n\t)\n}\n"],
  "mappings": "AAAA;AAAA,EACC;AAAA,EAGA;AAAA,EAIA;AAAA,EACA;AAAA,OACM;AACP,SAAS,iBAAiB,6BAA6B;AAShD,SAAS,kBAAkB,QAAmC;AACpE,SAAO,SAAS,mBAAmB,MAAM,sBAAsB,MAAM,GAAG,CAAC,MAAM,CAAC;AACjF;AAQA,MAAM,mBAAmB,IAAI,UAAiE;AAGvF,SAAS,oBAAoB,QAAgB;AACnD,SAAO,iBAAiB;AAAA,IAAI;AAAA,IAAQ,MACnC,SAAS,sBAAsB,CAAC,SAAS;AACxC,YAAM,WAAW,oBAAI,IAAoC;AACzD,iBAAW,WAAW,gBAAgB,MAAM,GAAG;AAC9C,cAAM,WAAW,SAAS,IAAI,QAAQ,QAAQ;AAC9C,YAAI,SAAU,UAAS,KAAK,OAAO;AAAA,YAC9B,UAAS,IAAI,QAAQ,UAAU,CAAC,OAAO,CAAC;AAAA,MAC9C;AACA,iBAAW,CAAC,UA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