{"version":3,"file":"useThreadHighlighting.mjs","names":[],"sources":["../../../../../src/components/Threads/ThreadList/useThreadHighlighting.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\nimport type { ThreadManager } from 'stream-chat';\n\n/**\n * Threads to flash as newly arrived, as a map of thread ID to a callback that ends the flash.\n *\n * Arrival comes from the manager's `unseenThreadIds` (appended to only when a thread the list does\n * not hold receives a message), not from diffing the list, which would flash the whole first page\n * on load and every page while scrolling. IDs are remembered when reported because `reload()`\n * clears `unseenThreadIds` once it has put those threads into the list, and the two stores update\n * in either order.\n */\nexport const useThreadHighlighting = (threadManager: ThreadManager) => {\n  const [threadsToHighlight, setThreadsToHighlight] = useState<\n    Record<string, () => void>\n  >({});\n  const awaitingArrival = useRef(new Set<string>());\n\n  useEffect(() => {\n    const unsubscribeUnseen = threadManager.state.subscribeWithSelector(\n      (state) => ({ unseenThreadIds: state.unseenThreadIds }),\n      ({ unseenThreadIds }) => {\n        unseenThreadIds.forEach((id) => awaitingArrival.current.add(id));\n      },\n    );\n\n    const unsubscribeThreads = threadManager.paginator.state.subscribeWithSelector(\n      (state) => ({ threads: state.items ?? [] }),\n      ({ threads }) => {\n        if (!awaitingArrival.current.size) return;\n\n        const arrived = threads.filter(({ id }) => awaitingArrival.current.has(id));\n        if (!arrived.length) return;\n\n        arrived.forEach(({ id }) => awaitingArrival.current.delete(id));\n\n        // Merged, not replaced, so a second arrival does not cut the first flash short.\n        setThreadsToHighlight((current) => {\n          const next = { ...current };\n          for (const { id } of arrived) {\n            next[id] = () =>\n              setThreadsToHighlight((pv) => {\n                const copy = { ...pv };\n                delete copy[id];\n                return copy;\n              });\n          }\n          return next;\n        });\n      },\n    );\n\n    return () => {\n      unsubscribeUnseen();\n      unsubscribeThreads();\n    };\n  }, [threadManager]);\n\n  return threadsToHighlight;\n};\n"],"mappings":";;;;;;;;;;;AAYA,IAAa,yBAAyB,kBAAiC;CACrE,MAAM,CAAC,oBAAoB,yBAAyB,SAElD,CAAC,CAAC;CACJ,MAAM,kBAAkB,uBAAO,IAAI,IAAY,CAAC;CAEhD,gBAAgB;EACd,MAAM,oBAAoB,cAAc,MAAM,uBAC3C,WAAW,EAAE,iBAAiB,MAAM,gBAAgB,KACpD,EAAE,sBAAsB;GACvB,gBAAgB,SAAS,OAAO,gBAAgB,QAAQ,IAAI,EAAE,CAAC;EACjE,CACF;EAEA,MAAM,qBAAqB,cAAc,UAAU,MAAM,uBACtD,WAAW,EAAE,SAAS,MAAM,SAAS,CAAC,EAAE,KACxC,EAAE,cAAc;GACf,IAAI,CAAC,gBAAgB,QAAQ,MAAM;GAEnC,MAAM,UAAU,QAAQ,QAAQ,EAAE,SAAS,gBAAgB,QAAQ,IAAI,EAAE,CAAC;GAC1E,IAAI,CAAC,QAAQ,QAAQ;GAErB,QAAQ,SAAS,EAAE,SAAS,gBAAgB,QAAQ,OAAO,EAAE,CAAC;GAG9D,uBAAuB,YAAY;IACjC,MAAM,OAAO,EAAE,GAAG,QAAQ;IAC1B,KAAK,MAAM,EAAE,QAAQ,SACnB,KAAK,YACH,uBAAuB,OAAO;KAC5B,MAAM,OAAO,EAAE,GAAG,GAAG;KACrB,OAAO,KAAK;KACZ,OAAO;IACT,CAAC;IAEL,OAAO;GACT,CAAC;EACH,CACF;EAEA,aAAa;GACX,kBAAkB;GAClB,mBAAmB;EACrB;CACF,GAAG,CAAC,aAAa,CAAC;CAElB,OAAO;AACT"}