{"version":3,"file":"useHomeReads.mjs","names":[],"sources":["../../../src/web/stores/useHomeReads.ts"],"sourcesContent":["import { HOME_PAGE_LIMIT, parseHomeSnapshot, type HomeSnapshot } from '@agimon-ai/po-core/sessionProtocol';\nimport { hostRevisionTopic, subscribeHubRevision } from '@agimon-ai/po-core/web';\nimport { useEffect, useState } from 'react';\n\nimport { onHostConnected, readHostHome } from '../lib/transport';\n\nexport interface HomeRead {\n  snapshot?: HomeSnapshot;\n  loading: boolean;\n  error?: string;\n}\n\nconst NOT_CONNECTED = 'The hub protocol is not connected.';\n\n/**\n * Each host's Home summary by host id ('' for the page's own host): read on mount, when the host\n * raises its `home` revision, and after a reconnect. A host nothing on the page can read stays empty.\n */\nexport function useHomeReads(hostIds: readonly (string | undefined)[]): Readonly<Record<string, HomeRead>> {\n  const key = JSON.stringify(hostIds.map((id) => id ?? ''));\n  const [reads, setReads] = useState<Readonly<Record<string, HomeRead>>>({});\n  useEffect(() => {\n    const stops: (() => void)[] = [];\n    for (const slot of JSON.parse(key) as string[]) {\n      const hostId = slot === '' ? undefined : slot;\n      let controller: AbortController | undefined;\n      const set = (read: (previous: HomeRead | undefined) => HomeRead) =>\n        setReads((previous) => ({ ...previous, [slot]: read(previous[slot]) }));\n      const read = (): void => {\n        controller?.abort();\n        const current = new AbortController();\n        controller = current;\n        set((previous) => ({ ...previous, loading: true }));\n        readHostHome(hostId, { limit: HOME_PAGE_LIMIT }, current.signal).then(\n          (value) => {\n            if (current.signal.aborted) return;\n            const snapshot = parseHomeSnapshot(value);\n            set(() =>\n              snapshot ? { snapshot, loading: false } : { loading: false, error: 'its summary was unreadable' },\n            );\n          },\n          (error: unknown) => {\n            if (current.signal.aborted) return;\n            const message = error instanceof Error ? error.message : String(error);\n            // Nothing is bound yet: the next connection reads again.\n            set(() => (message === NOT_CONNECTED ? { loading: false } : { loading: false, error: message }));\n          },\n        );\n      };\n      read();\n      stops.push(subscribeHubRevision(hostRevisionTopic('home', hostId), read), onHostConnected(hostId, read), () =>\n        controller?.abort(),\n      );\n    }\n    return () => {\n      for (const stop of stops) stop();\n    };\n  }, [key]);\n  return reads;\n}\n"],"mappings":";;;;;AAYA,MAAM,gBAAgB;;;;;AAMtB,SAAgB,aAAa,SAA8E;CACzG,MAAM,MAAM,KAAK,UAAU,QAAQ,KAAK,OAAO,MAAM,EAAE,CAAC;CACxD,MAAM,CAAC,OAAO,YAAY,SAA6C,CAAC,CAAC;CACzE,gBAAgB;EACd,MAAM,QAAwB,CAAC;EAC/B,KAAK,MAAM,QAAQ,KAAK,MAAM,GAAG,GAAe;GAC9C,MAAM,SAAS,SAAS,KAAK,KAAA,IAAY;GACzC,IAAI;GACJ,MAAM,OAAO,SACX,UAAU,cAAc;IAAE,GAAG;KAAW,OAAO,KAAK,SAAS,KAAK;GAAE,EAAE;GACxE,MAAM,aAAmB;IACvB,YAAY,MAAM;IAClB,MAAM,UAAU,IAAI,gBAAgB;IACpC,aAAa;IACb,KAAK,cAAc;KAAE,GAAG;KAAU,SAAS;IAAK,EAAE;IAClD,aAAa,QAAQ,EAAE,OAAO,gBAAgB,GAAG,QAAQ,MAAM,CAAC,CAAC,MAC9D,UAAU;KACT,IAAI,QAAQ,OAAO,SAAS;KAC5B,MAAM,WAAW,kBAAkB,KAAK;KACxC,UACE,WAAW;MAAE;MAAU,SAAS;KAAM,IAAI;MAAE,SAAS;MAAO,OAAO;KAA6B,CAClG;IACF,IACC,UAAmB;KAClB,IAAI,QAAQ,OAAO,SAAS;KAC5B,MAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;KAErE,UAAW,YAAY,gBAAgB,EAAE,SAAS,MAAM,IAAI;MAAE,SAAS;MAAO,OAAO;KAAQ,CAAE;IACjG,CACF;GACF;GACA,KAAK;GACL,MAAM,KAAK,qBAAqB,kBAAkB,QAAQ,MAAM,GAAG,IAAI,GAAG,gBAAgB,QAAQ,IAAI,SACpG,YAAY,MAAM,CACpB;EACF;EACA,aAAa;GACX,KAAK,MAAM,QAAQ,OAAO,KAAK;EACjC;CACF,GAAG,CAAC,GAAG,CAAC;CACR,OAAO;AACT"}