@hipnation-truth/sdk 0.17.0 → 0.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +11 -0
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.ts +9 -0
- package/dist/react.js +1 -4
- package/dist/react.js.map +1 -1
- package/package.json +1 -1
package/dist/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/react.ts","../src/react/calls.ts","../src/react/conversation-by-id.ts","../src/react/conversations.ts","../src/react/hooks.ts","../src/react/notifications.ts","../src/web-push.ts","../src/react/patient-family.ts","../src/react/patient-search.ts","../src/react/patients-bulk.ts","../src/react/provider.ts","../src/react/reminders.ts","../src/react/tasks.ts","../src/react/tracking.ts","../src/tracking/tracker.ts","../src/react/user-settings.ts","../src/react/voicemail.ts"],"sourcesContent":["/**\n * @hipnation-truth/sdk/react\n *\n * React hooks and provider for Truth SDK — real-time Convex-backed data.\n *\n * @example\n * ```tsx\n * import { TruthProvider, usePatients, useAppointments } from '@hipnation-truth/sdk/react';\n *\n * function App() {\n * return (\n * <TruthProvider environment=\"sandbox\">\n * <PatientList />\n * </TruthProvider>\n * );\n * }\n *\n * function PatientList() {\n * const patients = usePatients({ limit: 20 });\n * if (!patients) return <div>Loading...</div>;\n * return patients.map(p => <div key={p._id}>{p.firstName}</div>);\n * }\n * ```\n */\n\n// Dialpad call event hooks (Convex-backed)\nexport type {\n DialpadCallLogRow,\n DialpadCallRow,\n UseActiveCallsOptions,\n} from \"./react/calls\";\nexport {\n ACTIVE_CALL_STATES,\n CONNECTED_CALL_STATES,\n DialpadCallState,\n RINGING_CALL_STATES,\n TERMINAL_CALL_STATES,\n useActiveCalls,\n useDialpadCallByCallId,\n useDialpadCallLog,\n useDialpadCallsForConversation,\n} from \"./react/calls\";\n// Conversation-by-id lookup\nexport { useConversationById } from \"./react/conversation-by-id\";\n// Reactive conversation / message hooks (Convex-backed, PR #110 schema)\nexport type {\n ConversationListItem,\n ConversationMessageRow,\n ConversationNoteRow,\n ConversationRow,\n ConversationTaskForUserRow,\n ConversationTaskRow,\n UseConversationsFilters,\n UseMessagesOptions,\n UseQueryResult,\n} from \"./react/conversations\";\nexport {\n useConversationByPhonePair,\n useConversationNotes,\n useConversationNotesByPhonePair,\n useConversations,\n useConversationTasks,\n useConversationTasksByPhonePair,\n useConversationTasksForUser,\n useMessages,\n useUnreadAggregate,\n useUnreadCount,\n} from \"./react/conversations\";\n// Re-export types consumers commonly need\nexport type {\n ConversationMessage,\n Physician,\n UseAppointmentListOptions,\n UseConversationMessagesOptions,\n UsePatientBasicOptions,\n UsePatientBasicResult,\n UsePatientListOptions,\n UsePatientMedicalOptions,\n UsePatientPhotoOptions,\n} from \"./react/hooks\";\n// Patient / Appointment / Physician / Medical / Messages hooks\nexport {\n useAppointment,\n useAppointmentByElationId,\n useAppointments,\n useConversationMessages,\n usePatient,\n usePatientBasic,\n usePatientByElationId,\n usePatientByHintId,\n usePatientMedical,\n usePatientPhoto,\n usePatients,\n usePharmacyByNcpdpId,\n usePhysicianByElationId,\n usePhysiciansByElationIds,\n} from \"./react/hooks\";\n// Notifications\nexport type {\n PermissionStatus,\n UseNotificationsOptions,\n UseNotificationsResult,\n} from \"./react/notifications\";\nexport { useNotifications } from \"./react/notifications\";\n// Patient family members\nexport type {\n FamilyMemberRow,\n UsePatientFamilyMembersInput,\n} from \"./react/patient-family\";\nexport { usePatientFamilyMembers } from \"./react/patient-family\";\n// Patient search (multi-mode, office-scoped)\nexport type {\n PatientSearchResult,\n UsePatientSearchOptions,\n} from \"./react/patient-search\";\nexport { usePatientSearch } from \"./react/patient-search\";\n// Bulk patient lookup\nexport {\n usePatientsByIds,\n usePatientsByPhones,\n} from \"./react/patients-bulk\";\nexport type { TruthProviderProps } from \"./react/provider\";\n// Provider\nexport { TruthProvider } from \"./react/provider\";\n// Reminders\nexport { useRemindersForConversations } from \"./react/reminders\";\n// Conversation-task seen tracking\nexport { useConversationTaskMarkSeen } from \"./react/tasks\";\nexport type {\n TruthTrackingContextValue,\n TruthTrackingProviderProps,\n} from \"./react/tracking\";\n// Tracking\nexport { TruthTrackingProvider, useTruth } from \"./react/tracking\";\n// User settings\nexport { useUserSettings } from \"./react/user-settings\";\n// Voicemail URL resolver\nexport { useVoicemailUrl } from \"./react/voicemail\";\n// Re-export event types for track() calls\nexport type {\n EventPayloadMap,\n EventType,\n TrackOptions,\n} from \"./tracking/events\";\nexport type { Appointment, AppointmentListOptions } from \"./types/appointment\";\nexport type { Patient, PatientListOptions } from \"./types/patient\";\n","/**\n * React hooks for Dialpad call events — replaces CommHub's Hasura\n * `useDialpad_EventsSubscription`. Powered by the new\n * `dialpadCallEvents` + `dialpadCallEventLog` Convex tables that the\n * Truth webhook handler writes on every call state transition.\n *\n * Same `{ data, loading, error }` contract as the other hooks in this\n * SDK; same `skip` semantics on missing arguments.\n *\n * @example\n * ```tsx\n * import { useActiveCalls, useDialpadCallByCallId } from '@hipnation-truth/sdk/react';\n *\n * function IncomingBanner() {\n * const { data: active } = useActiveCalls();\n * if (!active?.length) return null;\n * return <Banner calls={active} />;\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { UseQueryResult } from \"./conversations\";\n\n// ---------------------------------------------------------------------------\n// Call state — single source of truth, mirrors the Dialpad webhook\n// `state` field and the values written to the Convex `dialpadCallEvents`\n// table by the Truth webhook handler. Consumers can switch on the\n// const-as-enum or compare the row's raw `state` string directly.\n// ---------------------------------------------------------------------------\n\nexport const DialpadCallState = {\n Calling: \"calling\",\n Ringing: \"ringing\",\n Connected: \"connected\",\n Hold: \"hold\",\n Hangup: \"hangup\",\n Missed: \"missed\",\n VoicemailUploaded: \"voicemail_uploaded\",\n} as const;\nexport type DialpadCallState =\n (typeof DialpadCallState)[keyof typeof DialpadCallState];\n\n/** Ringing states — caller hasn't been picked up yet. */\nexport const RINGING_CALL_STATES: ReadonlySet<DialpadCallState> = new Set([\n DialpadCallState.Calling,\n DialpadCallState.Ringing,\n]);\n\n/** Connected states — call is live (or on hold). */\nexport const CONNECTED_CALL_STATES: ReadonlySet<DialpadCallState> = new Set([\n DialpadCallState.Connected,\n]);\n\n/**\n * Active states — anything not terminal. Default filter for\n * `useActiveCalls`; the union of ringing + connected. `hold` is NOT\n * included by default because the IncomingCalls UI doesn't surface\n * held calls in the inbox banner.\n */\nexport const ACTIVE_CALL_STATES: ReadonlySet<DialpadCallState> = new Set([\n ...RINGING_CALL_STATES,\n ...CONNECTED_CALL_STATES,\n]);\n\n/** Terminal states — call has ended. Used to exclude rows from active lists. */\nexport const TERMINAL_CALL_STATES: ReadonlySet<DialpadCallState> = new Set([\n DialpadCallState.Hangup,\n DialpadCallState.Missed,\n DialpadCallState.VoicemailUploaded,\n]);\n\n// ---------------------------------------------------------------------------\n// Row shapes — mirror the Convex `dialpadCallEvents` table on the Truth\n// schema. Once the consuming app regenerates `convex/_generated/api.d.ts`\n// these can be inferred.\n// ---------------------------------------------------------------------------\n\nexport interface DialpadCallRow {\n _id: string;\n _creationTime: number;\n callId: string;\n state: string;\n direction: string | null;\n fromNumber: string | null;\n toNumber: string | null;\n phonePair: string | null;\n conversationId: string | null;\n patientId: string | null;\n voicemailLink: string | null;\n voicemailDurationSec: number | null;\n transcript: unknown | null;\n duration: number | null;\n occurredAt: string;\n updatedAt: string;\n}\n\nexport interface DialpadCallLogRow {\n _id: string;\n _creationTime: number;\n callId: string;\n state: string;\n direction: string | null;\n payload: unknown | null;\n occurredAt: string;\n}\n\n// ---------------------------------------------------------------------------\n// Convex refs\n// ---------------------------------------------------------------------------\n\nconst listActiveRef = makeFunctionReference<\n \"query\",\n { limit?: number; terminalStates?: string[] },\n DialpadCallRow[]\n>(\"dialpadCallEvents:listActive\");\n\nconst listForConversationRef = makeFunctionReference<\n \"query\",\n { conversationId: string; limit?: number },\n DialpadCallRow[]\n>(\"dialpadCallEvents:listForConversation\");\n\nconst getByCallIdRef = makeFunctionReference<\n \"query\",\n { callId: string },\n DialpadCallRow | null\n>(\"dialpadCallEvents:getByCallId\");\n\nconst listLogForCallIdRef = makeFunctionReference<\n \"query\",\n { callId: string; limit?: number },\n DialpadCallLogRow[]\n>(\"dialpadCallEvents:listLogForCallId\");\n\n// ---------------------------------------------------------------------------\n// Helpers — duplicated from conversations.ts to keep this file independent\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\nfunction toResult<T>(\n value: T | undefined,\n skipped: boolean,\n): UseQueryResult<T> {\n if (skipped) {\n return { data: undefined, loading: false, error: undefined };\n }\n return {\n data: value,\n loading: value === undefined,\n error: undefined,\n };\n}\n\n// ---------------------------------------------------------------------------\n// Hooks\n// ---------------------------------------------------------------------------\n\nexport interface UseActiveCallsOptions {\n /** Page cap. Default 50. */\n limit?: number;\n /**\n * Terminal states excluded from the active list. Override only if\n * Dialpad introduces a new state.\n */\n terminalStates?: string[];\n}\n\n/**\n * Live list of every Dialpad call that hasn't reached a terminal state\n * — used by CommHub's IncomingCalls banner. Updates as the webhook\n * handler patches the per-call row.\n */\nexport function useActiveCalls(\n options?: UseActiveCallsOptions,\n): UseQueryResult<DialpadCallRow[]> {\n const result = useQuery(\n listActiveRef as FunctionReference<\"query\">,\n options ?? {},\n ) as DialpadCallRow[] | undefined;\n return toResult(result, false);\n}\n\n/**\n * All calls (most-recent-first) on a single conversation. Pass the\n * Convex conversation `_id` (e.g. from `useConversationByPhonePair`).\n */\nexport function useDialpadCallsForConversation(\n conversationId: string | null | undefined,\n options?: { limit?: number },\n): UseQueryResult<DialpadCallRow[]> {\n const skipped = !conversationId;\n const result = useQuery(\n listForConversationRef as FunctionReference<\"query\">,\n skipped\n ? SKIP\n : {\n conversationId: conversationId as string,\n limit: options?.limit,\n },\n ) as DialpadCallRow[] | undefined;\n return toResult(result, skipped);\n}\n\n/** Single call lookup by Dialpad `call_id`. */\nexport function useDialpadCallByCallId(\n callId: string | null | undefined,\n): UseQueryResult<DialpadCallRow | null> {\n const skipped = !callId;\n const result = useQuery(\n getByCallIdRef as FunctionReference<\"query\">,\n skipped ? SKIP : { callId: callId as string },\n ) as DialpadCallRow | null | undefined;\n return toResult(result, skipped);\n}\n\n/**\n * Full audit history of state transitions for a call — backs the\n * hold / recording / transcription timeline in the right panel.\n */\nexport function useDialpadCallLog(\n callId: string | null | undefined,\n options?: { limit?: number },\n): UseQueryResult<DialpadCallLogRow[]> {\n const skipped = !callId;\n const result = useQuery(\n listLogForCallIdRef as FunctionReference<\"query\">,\n skipped ? SKIP : { callId: callId as string, limit: options?.limit },\n ) as DialpadCallLogRow[] | undefined;\n return toResult(result, skipped);\n}\n","/**\n * React hook for a single conversation lookup by Convex id — Truth SDK.\n *\n * Replaces CommHub's `useGet_Conversation_EventQuery` (backed by the\n * Hasura `Get_Conversation_Event` GraphQL query). Queries the Convex\n * `conversations:getById` function which returns the raw conversation\n * row, or `null` when the id doesn't exist.\n *\n * The returned `ConversationRow` carries the same phone-pair fields as\n * `useConversationByPhonePair` — `patientPhone`, `providerPhone`,\n * `phonePair`, `patientId` — but is keyed on the Convex document `_id`\n * rather than the pair. Use this when you have a Convex id from a push\n * notification or a deep-link route param.\n *\n * Must be used within a `<TruthProvider />`.\n *\n * @example\n * ```tsx\n * import { useConversationById } from '@hipnation-truth/sdk/react/conversation-by-id';\n *\n * function ConversationScreen({ id }: { id: string }) {\n * const { data: conversation, loading } = useConversationById(id);\n * if (loading) return <Spinner />;\n * if (!conversation) return <NotFound />;\n * return <View>{conversation.patientPhone}</View>;\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { ConversationRow, UseQueryResult } from \"./conversations\";\n\n// ---------------------------------------------------------------------------\n// Convex function reference\n// ---------------------------------------------------------------------------\n\nconst conversationsGetByIdRef = makeFunctionReference<\n \"query\",\n { id: string },\n ConversationRow | null\n>(\"conversations:getById\");\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Subscribe to a single conversation by its Convex document id. Returns\n * `null` when no conversation exists for that id.\n *\n * `data` is `undefined` while loading or when `id` is falsy (skip). Pass\n * `null` or `undefined` to skip the query — useful when the route param\n * hasn't resolved yet.\n */\nfunction useConversationById(\n id: string | null | undefined,\n): UseQueryResult<ConversationRow | null> {\n const skipped = !id;\n const result = useQuery(\n conversationsGetByIdRef as FunctionReference<\"query\">,\n skipped ? \"skip\" : { id: id as string },\n ) as ConversationRow | null | undefined;\n\n if (skipped) {\n return { data: undefined, loading: false, error: undefined };\n }\n return {\n data: result,\n loading: result === undefined,\n error: undefined,\n };\n}\n\nexport { useConversationById };\n","/**\n * React hooks for Truth SDK — reactive conversation + message data.\n *\n * Wraps Convex queries owned by the Truth backend so consumers (CommHub\n * Expo frontend) get live-updating conversation lists, message threads,\n * and unread totals without managing subscriptions themselves.\n *\n * **Hook contract:** every hook in this file returns\n * `{ data, loading, error }`. `data` is `undefined` while loading and\n * also when the underlying query is `\"skip\"`'d (caller didn't pass the\n * required arg yet). `loading` is `true` only while a real subscription\n * is in-flight; once `data` resolves (even to `null`) `loading` flips\n * to `false`. `error` is reserved for SDK-side validation — Convex\n * itself throws on subscribe rather than returning an error value, so\n * unhandled query errors propagate as React errors and should be caught\n * with an error boundary.\n *\n * **Backed by PR #110 schema (commit `41dbb59` on\n * `feat/migrate-dialpad-webhook-and-messages-to-truth`):**\n * - `conversations:listForUser` → `useConversations`\n * - `conversations:getUnreadTotalForUser` → `useUnreadCount`\n * - `conversations:getByPhonePair` → `useConversationByPhonePair`\n * - `conversationMessages:getByConversationId` → `useMessages`\n *\n * **Deliberately NOT shipped here (no backend query yet — flagged in\n * PR #111 body for follow-up):**\n * - Single-conversation-by-id lookup. Convex `conversations` has no\n * `get(id)` query — only `getByPhonePair`. CommHub uses phonePair as\n * the primary handle, so the byPair flavor is what consumers need;\n * if id-based lookup is needed later we can add a `conversations:get`\n * in Truth.\n * - Participants / \"seen by\" hook. The migrated schema has no\n * `conversationParticipants` table — read state is per-user via\n * `conversationReads` and there's no public list query for it yet.\n * - `markRead` mutation. PR #110 declares `markRead` as\n * `internalMutation`, so the frontend can't invoke it directly — it\n * has to go through a Truth HTTP endpoint, or webhook-migrator needs\n * to flip it to a public `mutation`. Flagged for a follow-up.\n *\n * Must be used within `<TruthProvider />` (see `./provider`).\n *\n * @example\n * ```tsx\n * import {\n * useConversations,\n * useConversationByPhonePair,\n * useMessages,\n * useUnreadCount,\n * } from '@hipnation-truth/sdk/react';\n *\n * function Inbox({ userId }: { userId: string }) {\n * const { data: convos, loading } = useConversations({ userId });\n * const { data: unread } = useUnreadCount(userId);\n * if (loading) return <Spinner />;\n * return (\n * <div>\n * <Badge count={unread ?? 0} />\n * {convos?.map((c) => <ConvoRow key={c.id} convo={c} />)}\n * </div>\n * );\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport { useMemo } from \"react\";\n\n// ---------------------------------------------------------------------------\n// Shared types\n// ---------------------------------------------------------------------------\n//\n// Mirror the shapes returned by the Convex queries on PR #110. Once\n// the consuming app regenerates `convex/_generated/api.d.ts` against\n// the merged schema these can be replaced with inferred types — for\n// today they give consumers useful autocomplete.\n\n/**\n * Row shape returned by `conversations:listForUser` — joins the base\n * `conversations` row with the caller's per-user `conversationReads`\n * entry so unread state lives on each item.\n */\nexport interface ConversationListItem {\n /** Convex id of the conversation row. */\n id: string;\n patientPhone: string;\n providerPhone: string;\n /** Sorted-digit-pair key (commutative — same regardless of direction). */\n phonePair: string;\n /** Truth-side patient id, when the kinesis pipeline could resolve it. */\n patientId: string | null;\n /** ISO timestamp of the most recent message in the conversation. */\n lastMessageAt: string;\n /** Caller's unread count for this conversation. */\n unreadCount: number;\n /** ISO timestamp of when the caller last marked the conversation read. */\n lastReadAt: string | null;\n}\n\n/**\n * Raw `conversations` table row — what `getByPhonePair` returns. No\n * unread / read joining; for that, prefer `useConversations`.\n */\nexport interface ConversationRow {\n _id: string;\n _creationTime: number;\n patientPhone: string;\n providerPhone: string;\n phonePair: string;\n patientId: string | null;\n lastEventId: string | null;\n lastMessageAt: string;\n createdAt: string;\n}\n\n/**\n * Message row returned by `conversationMessages:getByConversationId` —\n * calls + SMS merged chronologically. Mirrors `ConversationMessage`\n * already exported from the SDK; re-exported here under a clearer name\n * for the new hook surface.\n */\n/**\n * Conversation note row — single entry shown in the right-panel notes\n * tab in CommHub. Replaces the urql Hasura `internal_activities` row\n * with `type='note'`.\n */\nexport interface ConversationNoteRow {\n /** Convex id of the conversationNotes row. */\n id: string;\n conversationId: string;\n /** Dialpad event the note is attached to, when present. */\n eventId: string | null;\n author: string;\n content: string;\n /** Free-form status flag — matches CommHub's existing schema. */\n status: string | null;\n /** Free-form `type` discriminator — matches CommHub's existing schema. */\n type: string | null;\n createdAt: string;\n updatedAt: string;\n}\n\n/**\n * Conversation task row — single entry shown in the right-panel tasks\n * tab. Replaces the urql Hasura `internal_activities` row with\n * `type='task'`.\n */\nexport interface ConversationTaskRow {\n id: string;\n conversationId: string;\n eventId: string | null;\n author: string;\n description: string;\n status: \"pending\" | \"completed\";\n priority: \"high\" | \"medium\" | \"low\" | null;\n assignee: string | null;\n resolvedBy: string | null;\n type: string | null;\n createdAt: string;\n updatedAt: string;\n}\n\n/**\n * Cross-conversation task row used by the \"My Tasks\" tab. Adds the\n * conversation's normalized phone pair so the UI can render the\n * patient handle without a second reactive query.\n */\nexport interface ConversationTaskForUserRow extends ConversationTaskRow {\n /** Normalized `(patient_phone | provider_phone)` for the task's conversation. */\n phonePair: string | null;\n /**\n * Set of user ids who have seen this task (opened the detail panel).\n * Replaces the Hasura `event_activities.seen` boolean — use\n * `seenBy.includes(userId)` to derive the per-user boolean.\n */\n seenBy: string[];\n}\n\nexport interface ConversationMessageRow {\n kind: \"call\" | \"sms\";\n id: string;\n providerId: string;\n state: string | null;\n direction: string | null;\n fromNumber: string | null;\n toNumber: string | null;\n voicemailLink: string | null;\n duration: number | null;\n text: string | null;\n mms: boolean;\n mmsUrl: string | null;\n messageStatus: string | null;\n occurredAt: string;\n conversationId: string | null;\n patientId: string | null;\n}\n\nexport interface UseQueryResult<T> {\n /**\n * Query data. `undefined` while loading or while the query is\n * intentionally skipped (e.g. caller hasn't supplied `userId` yet).\n */\n data: T | undefined;\n /** True only while a real subscription is in-flight. */\n loading: boolean;\n /**\n * Reserved for client-side validation errors surfaced by the SDK\n * itself. Convex query errors propagate as React errors and should\n * be caught with an error boundary.\n */\n error: Error | undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Convex function references — string-keyed, decoupled from the\n// consuming app's generated `convex/_generated/api`. Names below match\n// the queries on PR #110 (commit 41dbb59) verbatim.\n// ---------------------------------------------------------------------------\n\nconst conversationsListForUserRef = makeFunctionReference<\n \"query\",\n { userId: string; limit?: number },\n ConversationListItem[]\n>(\"conversations:listForUser\");\n\nconst conversationsSearchForUserRef = makeFunctionReference<\n \"query\",\n { userId: string; search: string; limit?: number },\n ConversationListItem[]\n>(\"conversations:searchForUser\");\n\nconst conversationsGetUnreadTotalForUserRef = makeFunctionReference<\n \"query\",\n { userId: string },\n number\n>(\"conversations:getUnreadTotalForUser\");\n\nconst conversationsGetUnreadAggregateForUserRef = makeFunctionReference<\n \"query\",\n { userId: string; providerPhones?: string[] },\n { totalMessages: number; conversationCount: number }\n>(\"conversations:getUnreadAggregateForUser\");\n\nconst conversationsGetByPhonePairRef = makeFunctionReference<\n \"query\",\n { phonePair: string },\n ConversationRow | null\n>(\"conversations:getByPhonePair\");\n\n// Lives on the existing `conversationMessages` module (pre-#110). PR\n// #110 added `conversationId` indexing on `messageSms` / `messageCalls`,\n// so this query is now reliable for the new conversations table.\nconst conversationMessagesGetByConversationIdRef = makeFunctionReference<\n \"query\",\n { conversationId: string; limit?: number },\n ConversationMessageRow[]\n>(\"conversationMessages:getByConversationId\");\n\n// Notes + tasks for a conversation thread (Truth #730). Both lists are\n// keyed on the Convex conversation `_id` — same id that comes back\n// from `useConversations`.\nconst conversationNotesListForConversationRef = makeFunctionReference<\n \"query\",\n { conversationId: string },\n ConversationNoteRow[]\n>(\"conversationNotes:listForConversation\");\n\nconst conversationTasksListForConversationRef = makeFunctionReference<\n \"query\",\n { conversationId: string },\n ConversationTaskRow[]\n>(\"conversationTasks:listForConversation\");\n\n// Phone-pair-keyed variants (Truth SDK 0.10.0). CommHub holds the\n// conversation by `(patient_phone, provider_phone)` and shouldn't have\n// to chain `useConversationByPhonePair → useConversationNotes`. These\n// resolve the conversation server-side and return the same row shape.\nconst conversationNotesListByPhonePairRef = makeFunctionReference<\n \"query\",\n { phonePair: string },\n ConversationNoteRow[]\n>(\"conversationNotes:listByPhonePair\");\n\nconst conversationTasksListByPhonePairRef = makeFunctionReference<\n \"query\",\n { phonePair: string },\n ConversationTaskRow[]\n>(\"conversationTasks:listByPhonePair\");\n\nconst conversationTasksListForUserRef = makeFunctionReference<\n \"query\",\n { userId: string; limit?: number },\n ConversationTaskForUserRow[]\n>(\"conversationTasks:listForUser\");\n\n// ---------------------------------------------------------------------------\n// Skipped-query sentinel + result shaping\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\nfunction toResult<T>(\n value: T | undefined,\n skipped: boolean,\n): UseQueryResult<T> {\n if (skipped) {\n return { data: undefined, loading: false, error: undefined };\n }\n return {\n data: value,\n loading: value === undefined,\n error: undefined,\n };\n}\n\n// ---------------------------------------------------------------------------\n// Filters + options\n// ---------------------------------------------------------------------------\n\nexport interface UseConversationsFilters {\n /**\n * Truth user id (the Better Auth subject). Pass `undefined` to skip\n * the query — useful when the auth session is still loading.\n */\n userId: string | null | undefined;\n /**\n * Optional search term — when non-empty, switches the hook from\n * `conversations:listForUser` to `conversations:searchForUser`, which\n * fans out two parallel Convex full-text patient searches\n * (firstName + lastName) and returns the matching conversations the\n * user can see, sorted by recency. Whitespace-only values are treated\n * as empty.\n */\n search?: string;\n /** Page size. Server caps at 100 by default. */\n limit?: number;\n}\n\nexport interface UseMessagesOptions {\n /** Page size. Server caps at 200 by default. */\n limit?: number;\n}\n\n// ---------------------------------------------------------------------------\n// Hooks\n// ---------------------------------------------------------------------------\n\n/**\n * Subscribe to a user's conversations — joined with their per-conversation\n * unread count, sorted by most recent message. Updates live as new\n * SMS / calls land in Convex and the webhook bumps `unreadCount`.\n *\n * Returns `{ data: undefined, loading: false }` while the auth session\n * resolves (`userId === undefined`); not an error, just a skip.\n */\nfunction useConversations(\n filters: UseConversationsFilters,\n): UseQueryResult<ConversationListItem[]> {\n const trimmedSearch = filters.search?.trim() ?? \"\";\n const isSearchMode = trimmedSearch.length > 0;\n const skipped = !filters.userId;\n\n const listResult = useQuery(\n conversationsListForUserRef as FunctionReference<\"query\">,\n skipped || isSearchMode\n ? SKIP\n : {\n userId: filters.userId as string,\n limit: filters.limit,\n },\n ) as ConversationListItem[] | undefined;\n\n const searchResult = useQuery(\n conversationsSearchForUserRef as FunctionReference<\"query\">,\n skipped || !isSearchMode\n ? SKIP\n : {\n userId: filters.userId as string,\n search: trimmedSearch,\n limit: filters.limit,\n },\n ) as ConversationListItem[] | undefined;\n\n return toResult(isSearchMode ? searchResult : listResult, skipped);\n}\n\n/**\n * Look up a single conversation by its normalized phonePair (sorted\n * digit-pair, e.g. `\"5125550123|6505551234\"`). Returns `null` if no\n * conversation exists yet for that pair.\n *\n * Use the `phonePairKey(patientPhone, providerPhone)` helper exposed by\n * the Truth Convex module to derive the key from raw phone strings.\n */\nfunction useConversationByPhonePair(\n phonePair: string | null | undefined,\n): UseQueryResult<ConversationRow | null> {\n const skipped = !phonePair;\n const result = useQuery(\n conversationsGetByPhonePairRef as FunctionReference<\"query\">,\n skipped ? SKIP : { phonePair: phonePair as string },\n ) as ConversationRow | null | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Subscribe to a paginated message stream for a single conversation.\n * Calls + SMS merged chronologically, newest-first. Live as the\n * kinesis consumer writes to `messageCalls` / `messageSms`.\n *\n * Pass the Convex `_id` of the conversation row (from\n * `useConversations` / `useConversationByPhonePair`) as\n * `conversationId`.\n */\nfunction useMessages(\n conversationId: string | null | undefined,\n options?: UseMessagesOptions,\n): UseQueryResult<ConversationMessageRow[]> {\n const skipped = !conversationId;\n const result = useQuery(\n conversationMessagesGetByConversationIdRef as FunctionReference<\"query\">,\n skipped\n ? SKIP\n : {\n conversationId: conversationId as string,\n limit: options?.limit,\n },\n ) as ConversationMessageRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Subscribe to the user's total unread message count across all\n * conversations. Backs the inbox tab badge in CommHub.\n *\n * Returns `0` when the underlying query resolves with no unread\n * messages, `undefined` while loading or when `userId` is missing.\n */\nfunction useUnreadCount(\n userId: string | null | undefined,\n): UseQueryResult<number> {\n const skipped = !userId;\n const result = useQuery(\n conversationsGetUnreadTotalForUserRef as FunctionReference<\"query\">,\n skipped ? SKIP : { userId: userId as string },\n ) as number | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Server-side aggregate of the user's unread total + count of\n * conversations with at least one unread. Optionally filtered by\n * `providerPhones` (e.g. selected offices) — the aggregate is\n * computed in Convex, no client-side fetch-and-filter.\n */\nfunction useUnreadAggregate(\n userId: string | null | undefined,\n options?: { providerPhones?: string[] },\n): UseQueryResult<{ totalMessages: number; conversationCount: number }> {\n const skipped = !userId;\n const phones = options?.providerPhones;\n const stablePhones = useMemoizedPhones(phones);\n const result = useQuery(\n conversationsGetUnreadAggregateForUserRef as FunctionReference<\"query\">,\n skipped\n ? SKIP\n : {\n userId: userId as string,\n providerPhones: stablePhones,\n },\n ) as { totalMessages: number; conversationCount: number } | undefined;\n return toResult(result, skipped);\n}\n\n// Memoize the phones array so a new reference each render doesn't\n// retrigger the Convex subscription. Sort to canonicalize identity.\nfunction useMemoizedPhones(phones: string[] | undefined): string[] | undefined {\n const key = phones ? [...phones].sort().join(\"|\") : \"\";\n return useMemo(\n () => (phones && phones.length ? [...phones].sort() : undefined),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [key],\n );\n}\n\n/**\n * Subscribe to all notes attached to a conversation, newest first.\n * Replaces the urql Hasura `useConversation_NotesSubscription` in\n * CommHub (Truth #730). Pass `null`/`undefined` to skip the query.\n */\nfunction useConversationNotes(\n conversationId: string | null | undefined,\n): UseQueryResult<ConversationNoteRow[]> {\n const skipped = !conversationId;\n const result = useQuery(\n conversationNotesListForConversationRef as FunctionReference<\"query\">,\n skipped ? SKIP : { conversationId: conversationId as string },\n ) as ConversationNoteRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Subscribe to all tasks attached to a conversation, newest first.\n * Replaces the urql Hasura `useConversation_TasksSubscription` in\n * CommHub (Truth #730).\n */\nfunction useConversationTasks(\n conversationId: string | null | undefined,\n): UseQueryResult<ConversationTaskRow[]> {\n const skipped = !conversationId;\n const result = useQuery(\n conversationTasksListForConversationRef as FunctionReference<\"query\">,\n skipped ? SKIP : { conversationId: conversationId as string },\n ) as ConversationTaskRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Same as `useConversationNotes` but keyed on the normalized\n * `(patient_phone | provider_phone)` pair — saves the caller from\n * chaining a separate `useConversationByPhonePair` call. Returns\n * `[]` if no conversation exists for the pair yet.\n */\nfunction useConversationNotesByPhonePair(\n phonePair: string | null | undefined,\n): UseQueryResult<ConversationNoteRow[]> {\n const skipped = !phonePair;\n const result = useQuery(\n conversationNotesListByPhonePairRef as FunctionReference<\"query\">,\n skipped ? SKIP : { phonePair: phonePair as string },\n ) as ConversationNoteRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Same as `useConversationTasks` but keyed on phonePair.\n */\n/**\n * Subscribe to every conversation task where the caller is the\n * assignee or the author. Backs CommHub's \"My Tasks\" tab.\n */\nfunction useConversationTasksForUser(\n userId: string | null | undefined,\n options?: { limit?: number },\n): UseQueryResult<ConversationTaskForUserRow[]> {\n const skipped = !userId;\n const result = useQuery(\n conversationTasksListForUserRef as FunctionReference<\"query\">,\n skipped ? SKIP : { userId: userId as string, limit: options?.limit },\n ) as ConversationTaskForUserRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\nfunction useConversationTasksByPhonePair(\n phonePair: string | null | undefined,\n): UseQueryResult<ConversationTaskRow[]> {\n const skipped = !phonePair;\n const result = useQuery(\n conversationTasksListByPhonePairRef as FunctionReference<\"query\">,\n skipped ? SKIP : { phonePair: phonePair as string },\n ) as ConversationTaskRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\nexport {\n useConversationByPhonePair,\n useConversationNotes,\n useConversationNotesByPhonePair,\n useConversations,\n useConversationTasks,\n useConversationTasksByPhonePair,\n useConversationTasksForUser,\n useMessages,\n useUnreadAggregate,\n useUnreadCount,\n};\n","/**\n * React hooks for Truth SDK — real-time Convex-backed data access.\n *\n * These hooks use Convex React subscriptions for live-updating data.\n * Must be used within a ConvexProvider (see TruthProvider).\n *\n * @example\n * ```tsx\n * import { usePatients, useAppointments } from '@hipnation-truth/sdk/react';\n *\n * function PatientList() {\n * const patients = usePatients({ limit: 20 });\n * return patients?.map(p => <div key={p._id}>{p.firstName} {p.lastName}</div>);\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport { useEffect } from \"react\";\n\n// ---------------------------------------------------------------------------\n// Convex function references\n// ---------------------------------------------------------------------------\n// We use string-based references to avoid importing the generated API\n// (which lives in the consuming app, not the SDK). Convex supports this\n// via the `api` module pattern, but for a published SDK we use\n// makeFunctionReference to stay decoupled from the app's codegen.\n\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\n\n// Patient query references\nconst patientsListRef = makeFunctionReference<\n \"query\",\n {\n search?: string;\n lastName?: string;\n cursor?: string;\n limit?: number;\n },\n unknown[]\n>(\"patients:list\");\n\nconst patientsGetRef = makeFunctionReference<\n \"query\",\n { id: string },\n unknown | null\n>(\"patients:get\");\n\nconst patientsByElationIdRef = makeFunctionReference<\n \"query\",\n { elationId: string },\n unknown | null\n>(\"patients:getByElationId\");\n\nconst patientsByHintIdRef = makeFunctionReference<\n \"query\",\n { hintId: string },\n unknown | null\n>(\"patients:getByHintId\");\n\n// Appointment query references\nconst appointmentsListRef = makeFunctionReference<\n \"query\",\n {\n patientId?: string;\n status?: string;\n startDate?: string;\n endDate?: string;\n limit?: number;\n },\n unknown[]\n>(\"appointments:list\");\n\nconst appointmentsGetRef = makeFunctionReference<\n \"query\",\n { id: string },\n unknown | null\n>(\"appointments:get\");\n\nconst appointmentsByElationIdRef = makeFunctionReference<\n \"query\",\n { elationId: string },\n unknown | null\n>(\"appointments:getByElationId\");\n\n// ---------------------------------------------------------------------------\n// Patient hooks\n// ---------------------------------------------------------------------------\n\ninterface UsePatientListOptions {\n search?: string;\n lastName?: string;\n limit?: number;\n}\n\n/**\n * Subscribe to a list of patients with optional search and filtering.\n * Returns undefined while loading, then an array of patients.\n */\nfunction usePatients(options?: UsePatientListOptions) {\n return useQuery(patientsListRef as FunctionReference<\"query\">, options ?? {});\n}\n\n/**\n * Subscribe to a single patient by Convex document ID.\n */\nfunction usePatient(id: string) {\n return useQuery(patientsGetRef as FunctionReference<\"query\">, { id });\n}\n\n/**\n * Subscribe to a patient by their Elation ID.\n */\nfunction usePatientByElationId(elationId: string) {\n return useQuery(patientsByElationIdRef as FunctionReference<\"query\">, {\n elationId,\n });\n}\n\n/**\n * Subscribe to a patient by their Hint ID.\n */\nfunction usePatientByHintId(hintId: string) {\n return useQuery(patientsByHintIdRef as FunctionReference<\"query\">, {\n hintId,\n });\n}\n\n// ---------------------------------------------------------------------------\n// Appointment hooks\n// ---------------------------------------------------------------------------\n\ninterface UseAppointmentListOptions {\n patientId?: string;\n status?: string;\n startDate?: string;\n endDate?: string;\n limit?: number;\n}\n\n/**\n * Subscribe to a list of appointments with optional filtering.\n */\nfunction useAppointments(options?: UseAppointmentListOptions) {\n return useQuery(\n appointmentsListRef as FunctionReference<\"query\">,\n options ?? {},\n );\n}\n\n/**\n * Subscribe to a single appointment by Convex document ID.\n */\nfunction useAppointment(id: string) {\n return useQuery(appointmentsGetRef as FunctionReference<\"query\">, { id });\n}\n\n/**\n * Subscribe to an appointment by its Elation ID.\n */\nfunction useAppointmentByElationId(elationId: string) {\n return useQuery(appointmentsByElationIdRef as FunctionReference<\"query\">, {\n elationId,\n });\n}\n\n// ---------------------------------------------------------------------------\n// Physician hooks\n// ---------------------------------------------------------------------------\n\ninterface Physician {\n _id: string;\n elationId: number;\n firstName?: string;\n lastName?: string;\n npi?: string;\n credentials?: string;\n specialties?: string[];\n practice?: number;\n email?: string;\n phone?: string;\n lastSyncedAt: string;\n}\n\nconst physiciansGetByElationIdsRef = makeFunctionReference<\n \"query\",\n { ids: number[] },\n Physician[]\n>(\"physicians:getByElationIds\");\n\nconst physiciansGetByElationIdRef = makeFunctionReference<\n \"query\",\n { id: number },\n Physician | null\n>(\"physicians:getByElationId\");\n\n/**\n * Resolve a batch of physicians by their Elation IDs. Returns an array\n * of physicians that exist in Convex; missing ids are silently dropped.\n *\n * Use this to resolve medication `prescribing_physician` / appointment\n * physician ids to display names, avoiding the per-physician HTTP\n * round-trip through Truth's Elation proxy.\n *\n * Pass `undefined` (or an empty array) to skip the query — the hook\n * returns `undefined` until you pass a populated list.\n */\nfunction usePhysiciansByElationIds(ids: number[] | undefined) {\n return useQuery(\n physiciansGetByElationIdsRef as FunctionReference<\"query\">,\n ids && ids.length > 0 ? { ids } : \"skip\",\n );\n}\n\n/**\n * Subscribe to a single physician by their Elation ID.\n */\nfunction usePhysicianByElationId(id: number | undefined) {\n return useQuery(\n physiciansGetByElationIdRef as FunctionReference<\"query\">,\n id !== undefined ? { id } : \"skip\",\n );\n}\n\n// ---------------------------------------------------------------------------\n// Patient medical hooks (medications, problems, allergies, appointments)\n// ---------------------------------------------------------------------------\n\nconst medicationsByPatientRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown[]\n>(\"medicalRecords:getMedicationsByElationPatient\");\n\nconst problemsByPatientRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown[]\n>(\"medicalRecords:getProblemsByElationPatient\");\n\nconst allergiesByPatientRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown[]\n>(\"medicalRecords:getAllergiesByElationPatient\");\n\nconst appointmentsByPatientRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown[]\n>(\"medicalRecords:getAppointmentsByElationPatient\");\n\ninterface UsePatientMedicalOptions {\n /**\n * Base URL of the Truth API. Required to trigger the background refresh\n * (e.g. `https://app.truth.communication-hub.com`). If omitted, the hook\n * only reads from Convex and does not refresh.\n */\n apiBaseUrl?: string;\n /** API key used for the refresh call. */\n apiKey?: string;\n /**\n * If true, suppress the background refresh. Useful when you know the\n * data was just refreshed by another component on the page.\n */\n skipRefresh?: boolean;\n}\n\n/**\n * Composite hook that returns a patient's medical records — medications,\n * problems, allergies, appointments — from the Convex cache.\n *\n * On mount (and when `elationId` changes) fires a background refresh\n * against Truth's `/api/patients/medical/refresh` endpoint so stale data\n * is pulled in without blocking render. Returns cached data immediately;\n * Convex subscription updates the UI when refresh completes.\n */\nfunction usePatientMedical(\n elationId: number | undefined,\n options?: UsePatientMedicalOptions,\n) {\n const medications = useQuery(\n medicationsByPatientRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n ) as unknown[] | undefined;\n\n const problems = useQuery(\n problemsByPatientRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n ) as unknown[] | undefined;\n\n const allergies = useQuery(\n allergiesByPatientRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n ) as unknown[] | undefined;\n\n const appointments = useQuery(\n appointmentsByPatientRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n ) as unknown[] | undefined;\n\n useEffect(() => {\n if (elationId === undefined || options?.skipRefresh) {\n return;\n }\n const apiBaseUrl = options?.apiBaseUrl;\n const apiKey = options?.apiKey;\n if (!apiBaseUrl || !apiKey) {\n return;\n }\n\n const controller = new AbortController();\n void fetch(`${apiBaseUrl}/api/patients/medical/refresh`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": apiKey,\n },\n body: JSON.stringify({ elationId }),\n signal: controller.signal,\n }).catch(() => {\n /* background refresh — failures are non-fatal */\n });\n\n return () => controller.abort();\n }, [elationId, options?.apiBaseUrl, options?.apiKey, options?.skipRefresh]);\n\n return { medications, problems, allergies, appointments };\n}\n\n// ---------------------------------------------------------------------------\n// Patient basic hooks (Elation demographics + Hint memberships/account)\n// ---------------------------------------------------------------------------\n//\n// Replaces the CommHub `getPatientBasicDetails` Hasura Action. Reads\n// from Convex-cached rows (populated by the patients-backfill cron +\n// `/api/patients/basic/refresh` on-demand), falling back to an empty\n// state until the background refresh completes.\n\nconst elationPatientByIdRef = makeFunctionReference<\n \"query\",\n { elationId: number },\n unknown | null\n>(\"elationPatients:getByElationId\");\n\nconst hintPatientByIdRef = makeFunctionReference<\n \"query\",\n { hintId: string },\n unknown | null\n>(\"hintPatients:getByHintId\");\n\nconst pharmacyByNcpdpRef = makeFunctionReference<\n \"query\",\n { ncpdpId: string },\n unknown | null\n>(\"elationPharmacies:getByNcpdpId\");\n\nconst patientPhotoByIdRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown | null\n>(\"elationPatientPhotos:getByElationPatientId\");\n\ninterface UsePatientBasicOptions {\n /** Truth API base URL used for the background refresh. */\n apiBaseUrl?: string;\n /** API key for the refresh call. */\n apiKey?: string;\n /** Suppress the background refresh. */\n skipRefresh?: boolean;\n}\n\ninterface UsePatientBasicResult {\n /**\n * Raw Elation patient payload (matches the shape Elation's\n * `/patients/{id}` returns — `first_name`, `last_name`, `phones`, etc).\n * Returns `undefined` while the cache miss is loading, `null` if the\n * patient isn't in Convex yet (first-open-after-backfill).\n */\n elationPatient: Record<string, unknown> | null | undefined;\n /**\n * Raw Hint patient payload (matches the shape Hint's\n * `/provider/patients/{id}` returns — `memberships`, `account`,\n * `phones`, etc).\n */\n hintPatient: Record<string, unknown> | null | undefined;\n /**\n * The full Convex row for Elation (includes `elationId`, `lastSyncedAt`,\n * `photoS3Key`, `preferredPharmacyNcpdpId`, etc). Use this when you\n * need the structured/typed fields rather than the raw Elation payload.\n */\n elationRow: Record<string, unknown> | null | undefined;\n /**\n * The full Convex row for Hint. Similar relationship to `hintPatient`.\n */\n hintRow: Record<string, unknown> | null | undefined;\n /** True while either cache miss is still pending. */\n loading: boolean;\n}\n\n/**\n * Composite hook returning a patient's basic details — Hint demographics\n * + memberships + account, Elation demographics + clinical metadata —\n * from the Convex cache.\n *\n * On mount (and when inputs change) fires a background refresh against\n * `/api/patients/basic/refresh` so stale rows get pulled fresh without\n * blocking render. Returns cached data immediately; Convex subscription\n * updates the UI when refresh completes.\n */\nfunction usePatientBasic(\n input: { hintId?: string; elationId?: number },\n options?: UsePatientBasicOptions,\n): UsePatientBasicResult {\n const elationRow = useQuery(\n elationPatientByIdRef as FunctionReference<\"query\">,\n input.elationId !== undefined ? { elationId: input.elationId } : \"skip\",\n ) as Record<string, unknown> | null | undefined;\n\n const hintRow = useQuery(\n hintPatientByIdRef as FunctionReference<\"query\">,\n input.hintId !== undefined ? { hintId: input.hintId } : \"skip\",\n ) as Record<string, unknown> | null | undefined;\n\n useEffect(() => {\n if (options?.skipRefresh) {\n return;\n }\n if (!input.hintId && input.elationId === undefined) {\n return;\n }\n const apiBaseUrl = options?.apiBaseUrl;\n const apiKey = options?.apiKey;\n if (!apiBaseUrl || !apiKey) {\n return;\n }\n\n const controller = new AbortController();\n void fetch(`${apiBaseUrl}/api/patients/basic/refresh`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": apiKey,\n },\n body: JSON.stringify({\n hintId: input.hintId,\n elationId: input.elationId,\n }),\n signal: controller.signal,\n }).catch(() => {\n /* background refresh — failures are non-fatal */\n });\n\n return () => controller.abort();\n }, [\n input.hintId,\n input.elationId,\n options?.apiBaseUrl,\n options?.apiKey,\n options?.skipRefresh,\n ]);\n\n // Surface the raw payload under elationPatient/hintPatient so consumers\n // can keep using the exact Elation/Hint field names (first_name,\n // memberships, account.past_due_in_cents) with zero mapping.\n const elationPatient =\n elationRow === undefined\n ? undefined\n : elationRow === null\n ? null\n : ((elationRow.raw as Record<string, unknown>) ?? null);\n const hintPatient =\n hintRow === undefined\n ? undefined\n : hintRow === null\n ? null\n : ((hintRow.raw as Record<string, unknown>) ?? null);\n\n const elationLoading =\n input.elationId !== undefined && elationRow === undefined;\n const hintLoading = input.hintId !== undefined && hintRow === undefined;\n\n return {\n elationPatient,\n hintPatient,\n elationRow,\n hintRow,\n loading: elationLoading || hintLoading,\n };\n}\n\n/**\n * Read a shared pharmacy row by NCPDP id. Backs the pharmacy card in\n * the patient panel (from Elation's preferred_pharmacy reference).\n */\nfunction usePharmacyByNcpdpId(ncpdpId: string | undefined) {\n return useQuery(\n pharmacyByNcpdpRef as FunctionReference<\"query\">,\n ncpdpId ? { ncpdpId } : \"skip\",\n );\n}\n\ninterface UsePatientPhotoOptions {\n apiBaseUrl?: string;\n apiKey?: string;\n skipRefresh?: boolean;\n}\n\n/**\n * Subscribe to a patient's profile photo (s3Key + metadata). The\n * consumer constructs a download URL via the Truth attachments resource\n * (signed S3 URL). Fires a background refresh to pull the latest photo\n * binary from Elation + upload to S3.\n */\nfunction usePatientPhoto(\n elationId: number | undefined,\n options?: UsePatientPhotoOptions,\n) {\n const photo = useQuery(\n patientPhotoByIdRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n );\n\n useEffect(() => {\n if (options?.skipRefresh) {\n return;\n }\n if (elationId === undefined) {\n return;\n }\n const apiBaseUrl = options?.apiBaseUrl;\n const apiKey = options?.apiKey;\n if (!apiBaseUrl || !apiKey) {\n return;\n }\n\n const controller = new AbortController();\n void fetch(`${apiBaseUrl}/api/patients/photo/refresh`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": apiKey,\n },\n body: JSON.stringify({ elationId }),\n signal: controller.signal,\n }).catch(() => {\n /* non-fatal */\n });\n\n return () => controller.abort();\n }, [elationId, options?.apiBaseUrl, options?.apiKey, options?.skipRefresh]);\n\n return photo;\n}\n\n// ---------------------------------------------------------------------------\n// Conversation messages (Dialpad calls + SMS merged)\n// ---------------------------------------------------------------------------\n\ninterface ConversationMessage {\n kind: \"call\" | \"sms\";\n id: string;\n providerId: string;\n state: string | null;\n direction: string | null;\n fromNumber: string | null;\n toNumber: string | null;\n voicemailLink: string | null;\n duration: number | null;\n text: string | null;\n mms: boolean;\n mmsUrl: string | null;\n messageStatus: string | null;\n occurredAt: string;\n conversationId: string | null;\n patientId: string | null;\n}\n\nconst messagesByPhonesRef = makeFunctionReference<\n \"query\",\n { phoneA: string; phoneB: string; limit?: number },\n ConversationMessage[]\n>(\"conversationMessages:getByPhones\");\n\nconst messagesByConversationIdRef = makeFunctionReference<\n \"query\",\n { conversationId: string; limit?: number },\n ConversationMessage[]\n>(\"conversationMessages:getByConversationId\");\n\ninterface UseConversationMessagesOptions {\n /** Max items to return (default 200). */\n limit?: number;\n}\n\n/**\n * Subscribe to a conversation's calls + SMS merged chronologically.\n * Pass the patient phone + the provider phone — Truth computes a\n * normalized pair key server-side so formatting differences don't\n * matter.\n *\n * Returns `undefined` while loading, then `ConversationMessage[]`\n * sorted newest-first. Updates live as new webhook events land in\n * Convex via the kinesis consumer.\n */\nfunction useConversationMessages(\n input: { phoneA?: string; phoneB?: string; conversationId?: string },\n options?: UseConversationMessagesOptions,\n): ConversationMessage[] | undefined {\n const hasPair = !!input.phoneA && !!input.phoneB;\n const byPair = useQuery(\n messagesByPhonesRef as FunctionReference<\"query\">,\n hasPair\n ? {\n phoneA: input.phoneA as string,\n phoneB: input.phoneB as string,\n limit: options?.limit,\n }\n : \"skip\",\n ) as ConversationMessage[] | undefined;\n\n const byConvo = useQuery(\n messagesByConversationIdRef as FunctionReference<\"query\">,\n !hasPair && input.conversationId\n ? { conversationId: input.conversationId, limit: options?.limit }\n : \"skip\",\n ) as ConversationMessage[] | undefined;\n\n return hasPair ? byPair : byConvo;\n}\n\nexport {\n usePatients,\n usePatient,\n usePatientByElationId,\n usePatientByHintId,\n useAppointments,\n useAppointment,\n useAppointmentByElationId,\n usePhysiciansByElationIds,\n usePhysicianByElationId,\n usePatientMedical,\n usePatientBasic,\n usePharmacyByNcpdpId,\n usePatientPhoto,\n useConversationMessages,\n};\nexport type { ConversationMessage, UseConversationMessagesOptions };\nexport type {\n UsePatientListOptions,\n UseAppointmentListOptions,\n UsePatientMedicalOptions,\n UsePatientBasicOptions,\n UsePatientBasicResult,\n UsePatientPhotoOptions,\n Physician,\n};\n","/**\n * useNotifications — Truth SDK React hook for push notifications.\n *\n * Shape-compatible with `expo-notifications` where practical so\n * consumers port with minimal change. The hook dynamically imports\n * `expo-notifications` so the SDK doesn't hard-depend on Expo —\n * consumers running on a non-Expo runtime (e.g. a server test harness)\n * can still import the SDK without Metro blowing up.\n *\n * Exposes:\n * - permissionStatus — \"granted\" / \"denied\" / \"undetermined\"\n * - devicePushToken — native APNs/FCM token (undefined until register)\n * - register() — request permission, fetch token, register with Truth\n * - unregister() — revoke the device\n * - addReceivedListener / addResponseListener — re-exports of expo's\n * - getBadgeCount / setBadgeCount — re-exports of expo's\n *\n * Web push (VAPID subscription) lands in Phase 3.\n */\n\n\"use client\";\n\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport {\n isWebPushSupported,\n registerServiceWorker,\n subscribeToPush,\n subscriptionToJSON,\n} from \"../web-push\";\n\n// `expo-notifications` is an optional peer dep. We lazy-import it via\n// a string-indirection so TypeScript + tsup don't try to resolve its\n// types at build time (the SDK ships without a hard dep so non-Expo\n// consumers — web, Node services — can still import from @hipnation-truth/sdk/react).\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype ExpoNotificationsModule = any;\n\nasync function loadExpo(): Promise<ExpoNotificationsModule | null> {\n // Hermes rejects dynamic `import()`, so use Metro's `require()` which\n // is provided in every React Native module's scope. `expo-notifications`\n // is an optional peer dep — non-Expo consumers (web, Node) hit the\n // catch and we return null. tsup keeps this as a literal `require()`\n // (rather than its `__require` shim) because expo-notifications is\n // listed in `external` in tsup.config.ts — Metro's static analyzer\n // needs the literal call site to bundle the module.\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n return require(\"expo-notifications\") as ExpoNotificationsModule;\n } catch {\n return null;\n }\n}\n\nexport type PermissionStatus =\n | \"granted\"\n | \"denied\"\n | \"undetermined\"\n | \"unknown\";\n\nexport interface UseNotificationsOptions {\n /** Truth API base URL, e.g. https://app.truth.communication-hub.com */\n apiBaseUrl: string;\n /** `hn_live_*` API key for the caller's application. */\n apiKey: string;\n /** Current user id — used when registering the device. */\n userId: string | null | undefined;\n /** Optional app version string stored on the device row. */\n appVersion?: string;\n /**\n * Auto-register on mount when permission is already granted.\n * Default: true. Set false if you want to control the registration\n * lifecycle yourself.\n */\n autoRegister?: boolean;\n /** VAPID public key for web push. Fetched automatically if omitted. */\n vapidPublicKey?: string;\n /** Path to the service worker file. Default: \"/truth-sw.js\" */\n serviceWorkerPath?: string;\n /**\n * iOS only — which APNs endpoint the device's token will be valid\n * for. Determined by the build's `aps-environment` entitlement\n * (development ⇒ sandbox, production ⇒ production). Detect at the\n * call site via\n * `Application.getIosPushNotificationServiceEnvironmentAsync()`\n * from `expo-application` and pass the normalised value here. The\n * SDK can't read entitlements itself without taking on a native\n * peer dep. Server falls back to the app-level default if omitted.\n */\n apnsEnvironment?: \"sandbox\" | \"production\";\n}\n\nexport interface UseNotificationsResult {\n permissionStatus: PermissionStatus;\n devicePushToken: string | null;\n register: () => Promise<{ ok: boolean; reason?: string }>;\n unregister: () => Promise<void>;\n addReceivedListener: (listener: (n: unknown) => void) => () => void;\n addResponseListener: (listener: (r: unknown) => void) => () => void;\n getBadgeCount: () => Promise<number>;\n setBadgeCount: (count: number) => Promise<void>;\n}\n\nexport function useNotifications(\n options: UseNotificationsOptions,\n): UseNotificationsResult {\n const [permissionStatus, setPermissionStatus] =\n useState<PermissionStatus>(\"unknown\");\n const [devicePushToken, setDevicePushToken] = useState<string | null>(null);\n const expoRef = useRef<ExpoNotificationsModule | null>(null);\n const isWebRef = useRef(false);\n const vapidKeyRef = useRef<string | null>(options.vapidPublicKey ?? null);\n\n useEffect(() => {\n let mounted = true;\n void (async () => {\n const expo = await loadExpo();\n if (!mounted) {\n return;\n }\n expoRef.current = expo;\n\n if (expo) {\n try {\n const perm = await expo.getPermissionsAsync();\n if (!mounted) {\n return;\n }\n setPermissionStatus(mapStatus(perm?.status));\n } catch {\n setPermissionStatus(\"unknown\");\n }\n return;\n }\n\n if (isWebPushSupported()) {\n isWebRef.current = true;\n if (!vapidKeyRef.current) {\n try {\n const res = await fetch(\n `${options.apiBaseUrl}/api/notifications/vapid-key`,\n {\n headers: {\n Accept: \"application/json\",\n \"X-API-Key\": options.apiKey,\n },\n },\n );\n if (res.ok) {\n const data = await res.json();\n vapidKeyRef.current = data.vapidPublicKey ?? null;\n }\n } catch {\n // best-effort\n }\n }\n if (!mounted) {\n return;\n }\n const webPerm =\n typeof Notification !== \"undefined\"\n ? Notification.permission\n : \"default\";\n setPermissionStatus(\n webPerm === \"granted\"\n ? \"granted\"\n : webPerm === \"denied\"\n ? \"denied\"\n : \"undetermined\",\n );\n } else {\n setPermissionStatus(\"unknown\");\n }\n })();\n return () => {\n mounted = false;\n };\n }, [options.apiBaseUrl, options.apiKey]);\n\n const register = useCallback(async () => {\n if (!options.userId) {\n return { ok: false, reason: \"missing_userId\" };\n }\n\n // Web push path\n if (isWebRef.current) {\n const vapidKey = vapidKeyRef.current;\n if (!vapidKey) {\n return { ok: false, reason: \"no_vapid_key\" };\n }\n\n const webPerm = await Notification.requestPermission();\n setPermissionStatus(\n webPerm === \"granted\"\n ? \"granted\"\n : webPerm === \"denied\"\n ? \"denied\"\n : \"undetermined\",\n );\n if (webPerm !== \"granted\") {\n return { ok: false, reason: \"permission_denied\" };\n }\n\n try {\n const swPath = options.serviceWorkerPath ?? \"/truth-sw.js\";\n const registration = await registerServiceWorker(swPath);\n await navigator.serviceWorker.ready;\n const subscription = await subscribeToPush(registration, vapidKey);\n const subJSON = subscriptionToJSON(subscription);\n setDevicePushToken(subscription.endpoint);\n\n const res = await fetch(\n `${options.apiBaseUrl}/api/notifications/devices/register`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": options.apiKey,\n },\n body: JSON.stringify({\n userId: options.userId,\n platform: \"web\",\n webPushSubscription: subJSON,\n appVersion: options.appVersion,\n locale: navigator.language,\n timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,\n }),\n },\n );\n if (!res.ok) {\n const text = await res.text().catch(() => \"\");\n return {\n ok: false,\n reason: `register_failed_${res.status}: ${text.slice(0, 120)}`,\n };\n }\n return { ok: true };\n } catch (err) {\n return {\n ok: false,\n reason: `web_push_error: ${err instanceof Error ? err.message : String(err)}`,\n };\n }\n }\n\n // Native (expo-notifications) path\n const expo = expoRef.current ?? (await loadExpo());\n expoRef.current = expo;\n if (!expo) {\n return { ok: false, reason: \"expo_notifications_missing\" };\n }\n\n let perm = await expo.getPermissionsAsync();\n if (perm?.status !== \"granted\") {\n perm = await expo.requestPermissionsAsync();\n }\n setPermissionStatus(mapStatus(perm?.status));\n if (perm?.status !== \"granted\") {\n return { ok: false, reason: \"permission_denied\" };\n }\n\n const tokenResp = await expo.getDevicePushTokenAsync();\n const nativeToken = tokenResp?.data;\n const platform = detectPlatform(tokenResp?.type);\n if (!nativeToken || (platform !== \"ios\" && platform !== \"android\")) {\n return { ok: false, reason: \"no_native_token\" };\n }\n\n setDevicePushToken(nativeToken);\n\n const res = await fetch(\n `${options.apiBaseUrl}/api/notifications/devices/register`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": options.apiKey,\n },\n body: JSON.stringify({\n userId: options.userId,\n platform,\n nativeToken,\n appVersion: options.appVersion,\n locale:\n typeof navigator !== \"undefined\" ? navigator.language : undefined,\n timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,\n ...(platform === \"ios\" && options.apnsEnvironment\n ? { apnsEnvironment: options.apnsEnvironment }\n : {}),\n }),\n },\n );\n if (!res.ok) {\n const text = await res.text().catch(() => \"\");\n return {\n ok: false,\n reason: `register_failed_${res.status}: ${text.slice(0, 120)}`,\n };\n }\n return { ok: true };\n }, [\n options.apiBaseUrl,\n options.apiKey,\n options.userId,\n options.appVersion,\n options.serviceWorkerPath,\n ]);\n\n const unregister = useCallback(async () => {\n if (!devicePushToken) {\n return;\n }\n await fetch(`${options.apiBaseUrl}/api/notifications/devices/unregister`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": options.apiKey,\n },\n body: JSON.stringify({ nativeToken: devicePushToken }),\n }).catch(() => {\n /* non-fatal */\n });\n setDevicePushToken(null);\n }, [options.apiBaseUrl, options.apiKey, devicePushToken]);\n\n const addReceivedListener = useCallback(\n (listener: (n: unknown) => void): (() => void) => {\n if (isWebRef.current) {\n if (\n typeof navigator === \"undefined\" ||\n !(\"serviceWorker\" in navigator)\n ) {\n return () => {};\n }\n const handler = (event: MessageEvent) => {\n if (event.data?.type === \"TRUTH_PUSH_RECEIVED\") {\n listener(event.data.payload);\n }\n };\n navigator.serviceWorker.addEventListener(\"message\", handler);\n return () =>\n navigator.serviceWorker.removeEventListener(\"message\", handler);\n }\n const expo = expoRef.current;\n if (!expo?.addNotificationReceivedListener) {\n return () => {};\n }\n const sub = expo.addNotificationReceivedListener(listener);\n return () => sub.remove?.();\n },\n [],\n );\n\n const addResponseListener = useCallback(\n (listener: (r: unknown) => void): (() => void) => {\n if (isWebRef.current) {\n if (\n typeof navigator === \"undefined\" ||\n !(\"serviceWorker\" in navigator)\n ) {\n return () => {};\n }\n const handler = (event: MessageEvent) => {\n if (event.data?.type === \"TRUTH_PUSH_TAPPED\") {\n listener(event.data.payload);\n }\n };\n navigator.serviceWorker.addEventListener(\"message\", handler);\n return () =>\n navigator.serviceWorker.removeEventListener(\"message\", handler);\n }\n const expo = expoRef.current;\n if (!expo?.addNotificationResponseReceivedListener) {\n return () => {};\n }\n const sub = expo.addNotificationResponseReceivedListener(listener);\n return () => sub.remove?.();\n },\n [],\n );\n\n const getBadgeCount = useCallback(async (): Promise<number> => {\n const expo = expoRef.current;\n if (!expo?.getBadgeCountAsync) {\n return 0;\n }\n return (await expo.getBadgeCountAsync()) ?? 0;\n }, []);\n\n const setBadgeCount = useCallback(async (count: number): Promise<void> => {\n const expo = expoRef.current;\n if (!expo?.setBadgeCountAsync) {\n return;\n }\n await expo.setBadgeCountAsync(count);\n }, []);\n\n // Auto-register once on mount when conditions are met\n const autoRegister = options.autoRegister !== false;\n useEffect(() => {\n if (!autoRegister) {\n return;\n }\n if (permissionStatus !== \"granted\") {\n return;\n }\n if (devicePushToken) {\n return;\n }\n if (!options.userId) {\n return;\n }\n void register();\n }, [\n autoRegister,\n permissionStatus,\n devicePushToken,\n options.userId,\n register,\n ]);\n\n return {\n permissionStatus,\n devicePushToken,\n register,\n unregister,\n addReceivedListener,\n addResponseListener,\n getBadgeCount,\n setBadgeCount,\n };\n}\n\nfunction mapStatus(status: string | undefined): PermissionStatus {\n if (status === \"granted\") {\n return \"granted\";\n }\n if (status === \"denied\") {\n return \"denied\";\n }\n if (status === \"undetermined\") {\n return \"undetermined\";\n }\n return \"unknown\";\n}\n\nfunction detectPlatform(\n tokenType: string | undefined,\n): \"ios\" | \"android\" | \"web\" | \"unknown\" {\n // expo-notifications >= 0.27 returns \"ios\" / \"android\" / \"web\"\n // directly. Older releases (and Firebase docs) used \"apns\" / \"fcm\";\n // accept both for back-compat with apps still on old SDKs.\n if (tokenType === \"ios\" || tokenType === \"apns\") {\n return \"ios\";\n }\n if (tokenType === \"android\" || tokenType === \"fcm\") {\n return \"android\";\n }\n if (tokenType === \"web\") {\n return \"web\";\n }\n return \"unknown\";\n}\n","/**\n * Web Push helpers for browser environments.\n *\n * Handles service worker registration, VAPID push subscription, and\n * message forwarding from the service worker to the main thread.\n */\n\nexport interface WebPushConfig {\n vapidPublicKey: string;\n serviceWorkerPath?: string;\n}\n\nexport function isWebPushSupported(): boolean {\n return (\n typeof window !== \"undefined\" &&\n \"serviceWorker\" in navigator &&\n \"PushManager\" in window\n );\n}\n\nexport async function registerServiceWorker(\n path = \"/truth-sw.js\",\n): Promise<ServiceWorkerRegistration> {\n return navigator.serviceWorker.register(path);\n}\n\nexport async function subscribeToPush(\n registration: ServiceWorkerRegistration,\n vapidPublicKey: string,\n): Promise<PushSubscription> {\n const existing = await registration.pushManager.getSubscription();\n if (existing) {\n return existing;\n }\n\n return registration.pushManager.subscribe({\n userVisibleOnly: true,\n applicationServerKey: urlBase64ToUint8Array(\n vapidPublicKey,\n ) as unknown as ArrayBuffer,\n });\n}\n\nexport function subscriptionToJSON(sub: PushSubscription): {\n endpoint: string;\n keys: { p256dh: string; auth: string };\n} {\n const json = sub.toJSON();\n return {\n endpoint: sub.endpoint,\n keys: {\n p256dh: json.keys?.p256dh ?? \"\",\n auth: json.keys?.auth ?? \"\",\n },\n };\n}\n\nfunction urlBase64ToUint8Array(base64String: string): Uint8Array {\n const padding = \"=\".repeat((4 - (base64String.length % 4)) % 4);\n const base64 = (base64String + padding).replace(/-/g, \"+\").replace(/_/g, \"/\");\n const rawData = atob(base64);\n const outputArray = new Uint8Array(rawData.length);\n for (let i = 0; i < rawData.length; ++i) {\n outputArray[i] = rawData.charCodeAt(i);\n }\n return outputArray;\n}\n\nexport function onServiceWorkerMessage(\n type: string,\n callback: (payload: unknown) => void,\n): () => void {\n if (typeof navigator === \"undefined\" || !(\"serviceWorker\" in navigator)) {\n return () => {};\n }\n const handler = (event: MessageEvent) => {\n if (event.data?.type === type) {\n callback(event.data.payload);\n }\n };\n navigator.serviceWorker.addEventListener(\"message\", handler);\n return () => navigator.serviceWorker.removeEventListener(\"message\", handler);\n}\n","/**\n * React hook for patient family-member lookup — Truth SDK.\n *\n * Replaces the CommHub Hasura `useFamilyMembersQuery` (backed by\n * `queries/family_members.graphql`). Queries the Convex\n * `patients:listFamilyMembers` function, which returns the set of\n * patients that share a `familyId` OR share at least one phone number\n * with the reference patient, excluding the reference patient themselves.\n *\n * Arguments mirror the Hasura `FamilyMembers` GraphQL query variables:\n * - `familyId` → `$familyId` (Hint family/account id)\n * - `phoneNumbers` → `$phoneNumbers` (patient's phone numbers)\n * - `excludeHintId` → `$excludePatientId` (skip the reference patient)\n *\n * Must be used within a `<TruthProvider />`.\n *\n * @example\n * ```tsx\n * import { usePatientFamilyMembers } from '@hipnation-truth/sdk/react/patient-family';\n *\n * function FamilyPanel({ hintId, familyId, phones }: Props) {\n * const { data: members, loading } = usePatientFamilyMembers({\n * familyId,\n * phoneNumbers: phones,\n * excludeHintId: hintId,\n * });\n * if (loading) return <Spinner />;\n * return members?.map((m) => <div key={m._id}>{m.firstName} {m.lastName}</div>);\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { UseQueryResult } from \"./conversations\";\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\n/**\n * Shape of a single patient row returned by `patients:listFamilyMembers`.\n * Mirrors the fields CommHub's `FamilyMembersQuery` selected from Hasura:\n * - `id` → `_id` (Convex document id)\n * - `name` → `firstName + \" \" + lastName`\n * - `hint_id` → `hintId`\n * - `elation_id` → `elationId`\n * - `family_id` → `familyId`\n * - `phone_numbers` → `phones[].number`\n */\nexport interface FamilyMemberRow {\n _id: string;\n _creationTime: number;\n firstName: string;\n lastName: string;\n elationId?: string;\n hintId?: string;\n familyId?: string;\n phones: Array<{ type?: string; number: string }>;\n membershipStatus?: string;\n sources: string[];\n lastSyncedAt: string;\n}\n\n/** Arguments for `usePatientFamilyMembers`. */\nexport interface UsePatientFamilyMembersInput {\n /**\n * Hint family/account id — when present, all patients sharing this id\n * are returned (matches the Hasura `family_id` column).\n *\n * BACKFILL NEEDED: `familyId` was added to the Convex `patients` schema\n * in this PR. Existing patient rows will return empty for this branch\n * until the Hint patient upsert path (upsertFromHint / basic-refresh)\n * is updated to write this field, or a one-time backfill script runs.\n * The phone-number fallback continues to work in the interim.\n */\n familyId?: string | null;\n /**\n * Patient's phone numbers — patients sharing at least one phone number\n * are included as family members (mirrors Hasura `phone_numbers._overlap`).\n */\n phoneNumbers?: string[] | null;\n /**\n * Hint patient ID of the reference patient to exclude from results\n * (mirrors Hasura `$excludePatientId`).\n */\n excludeHintId?: string | null;\n}\n\n// ---------------------------------------------------------------------------\n// Convex function reference\n// ---------------------------------------------------------------------------\n\nconst patientsFamilyMembersRef = makeFunctionReference<\n \"query\",\n {\n familyId?: string;\n phoneNumbers?: string[];\n excludeHintId?: string;\n },\n FamilyMemberRow[]\n>(\"patients:listFamilyMembers\");\n\n// ---------------------------------------------------------------------------\n// Skip sentinel\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Subscribe to family members of a patient in real time.\n *\n * Returns all patients that share the same `familyId` OR share at least one\n * phone number with the reference patient, sorted by name. The reference\n * patient is excluded via `excludeHintId`.\n *\n * Pass `undefined` (or an object where all fields are undefined/null) to\n * skip the query — returns `{ data: undefined, loading: false }`.\n *\n * @param input - Query inputs. The query is skipped if `input` is undefined\n * or all fields are falsy.\n */\nfunction usePatientFamilyMembers(\n input: UsePatientFamilyMembersInput | undefined,\n): UseQueryResult<FamilyMemberRow[]> {\n const hasFamilyId = !!input?.familyId;\n const hasPhoneNumbers = !!(input?.phoneNumbers && input.phoneNumbers.length > 0);\n const shouldQuery = hasFamilyId || hasPhoneNumbers;\n\n const args = shouldQuery\n ? {\n ...(input?.familyId ? { familyId: input.familyId } : {}),\n ...(input?.phoneNumbers && input.phoneNumbers.length > 0\n ? { phoneNumbers: input.phoneNumbers }\n : {}),\n ...(input?.excludeHintId ? { excludeHintId: input.excludeHintId } : {}),\n }\n : SKIP;\n\n const result = useQuery(\n patientsFamilyMembersRef as FunctionReference<\"query\">,\n args,\n ) as FamilyMemberRow[] | undefined;\n\n if (!shouldQuery) {\n return { data: undefined, loading: false, error: undefined };\n }\n\n return {\n data: result,\n loading: result === undefined,\n error: undefined,\n };\n}\n\nexport { usePatientFamilyMembers };\n","/**\n * React hook for Truth SDK — reactive patient search.\n *\n * Wraps the `patients:search` Convex query so CommHub consumers\n * (and other frontends) can search patients by name, phone, or email\n * with live-updating results and optional office scoping, without\n * managing Convex subscriptions themselves.\n *\n * **Hook contract:** returns `UseQueryResult<PatientSearchResult[]>`.\n * - `data` is `undefined` while loading or when the query is skipped\n * (empty `query` string or `query === undefined`).\n * - `loading` is `true` only while a real subscription is in-flight.\n * - `error` is reserved for SDK-side validation; Convex query errors\n * propagate as React errors and should be caught with an error\n * boundary.\n *\n * **Backed by `patients:search`** — added in agent-A-search.\n * Multi-word queries split on whitespace; every token must match at\n * least one of firstName / lastName / email / phone digits.\n *\n * Must be used within `<TruthProvider />` (see `./provider`).\n *\n * @example\n * ```tsx\n * import { usePatientSearch } from '@hipnation-truth/sdk/react/patient-search';\n *\n * function PatientPicker({ officeId }: { officeId?: string }) {\n * const [query, setQuery] = useState('');\n * const { data: patients, loading } = usePatientSearch({ query, officeId });\n * return (\n * <>\n * <input value={query} onChange={e => setQuery(e.target.value)} />\n * {loading && <Spinner />}\n * {patients?.map(p => <PatientRow key={p.id} patient={p} />)}\n * </>\n * );\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { UseQueryResult } from \"./conversations\";\n\n// ---------------------------------------------------------------------------\n// Return shape\n// ---------------------------------------------------------------------------\n\n/**\n * A single patient result from `patients:search`.\n *\n * Fields are normalised across the `patients` + `hintPatients` tables\n * so callers get a flat, consistent object regardless of which source\n * contributed the match.\n */\nexport interface PatientSearchResult {\n /** Convex `_id` of the matching `patients` or `hintPatients` row. */\n id: string;\n /** Hint patient id — present when the row originates from Hint. */\n hintId: string | undefined;\n firstName: string;\n lastName: string;\n /** ISO date string (YYYY-MM-DD) — undefined when not available. */\n dob: string | undefined;\n /** Email addresses associated with the patient. */\n emails: string[];\n /**\n * Raw phone strings from the source table. Callers should\n * strip non-digits for dialling; the first entry is the primary\n * contact number.\n */\n phones: string[];\n /** Hint office id — present for Hint-sourced results. */\n officeId: string | undefined;\n /** Human-readable office name — present when officeId is set. */\n officeName: string | undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Options\n// ---------------------------------------------------------------------------\n\nexport interface UsePatientSearchOptions {\n /**\n * The search term entered by the user. Multi-word queries are split\n * on whitespace and every token must match. Pass `\"\"` or `undefined`\n * to skip the query and get `{ data: undefined, loading: false }`.\n */\n query: string | undefined;\n /**\n * When supplied, restricts results to patients whose authoritative\n * Hint office id matches. Maps to the `officeId` arg on the Convex\n * query and uses the `hintPatients.by_officeId` index path.\n */\n officeId?: string | null;\n /**\n * `\"fuzzy\"` (default) uses Convex full-text search indexes for name\n * queries; `\"exact\"` uses a bounded scan-filter only. Most callers\n * should leave this at the default.\n */\n mode?: \"fuzzy\" | \"exact\";\n /** Maximum number of results to return (server caps at 200). */\n limit?: number;\n}\n\n// ---------------------------------------------------------------------------\n// Convex function reference\n// ---------------------------------------------------------------------------\n\n// String-keyed reference to `patients:search` — decoupled from the\n// consuming app's generated `convex/_generated/api`.\nconst patientsSearchRef = makeFunctionReference<\n \"query\",\n {\n query: string;\n officeId?: string;\n limit?: number;\n mode?: \"fuzzy\" | \"exact\";\n },\n PatientSearchResult[]\n>(\"patients:search\");\n\n// ---------------------------------------------------------------------------\n// Skipped-query sentinel\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Subscribe to a live patient search backed by `patients:search` on the\n * Truth Convex deployment.\n *\n * Results update reactively whenever the underlying `patients` or\n * `hintPatients` tables change — no polling required.\n *\n * @param options - Search options (see `UsePatientSearchOptions`).\n * @returns `UseQueryResult<PatientSearchResult[]>` — always defined\n * once a non-empty `query` is provided and the subscription resolves.\n */\nexport function usePatientSearch(\n options: UsePatientSearchOptions,\n): UseQueryResult<PatientSearchResult[]> {\n const trimmedQuery = (options.query ?? \"\").trim();\n const skipped = trimmedQuery.length === 0;\n\n const result = useQuery(\n patientsSearchRef as FunctionReference<\"query\">,\n skipped\n ? SKIP\n : {\n query: trimmedQuery,\n ...(options.officeId ? { officeId: options.officeId } : {}),\n ...(options.mode ? { mode: options.mode } : {}),\n ...(options.limit !== undefined ? { limit: options.limit } : {}),\n },\n ) as PatientSearchResult[] | undefined;\n\n if (skipped) {\n return { data: undefined, loading: false, error: undefined };\n }\n\n return {\n data: result,\n loading: result === undefined,\n error: undefined,\n };\n}\n","/**\n * Bulk patient lookup by Convex ids.\n *\n * Backs the inbox card render in CommHub — one reactive subscription\n * resolves names + office + EHR ids for the entire visible list\n * instead of N per-row queries.\n *\n * Must be used within `<TruthProvider />`.\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport { useMemo } from \"react\";\nimport type { Patient } from \"../types/patient\";\nimport type { UseQueryResult } from \"./conversations\";\n\nconst patientsGetByIdsRef = makeFunctionReference<\n \"query\",\n { ids: string[] },\n Patient[]\n>(\"patients:getByIds\");\n\nconst patientsGetByPhonesRef = makeFunctionReference<\n \"query\",\n { phoneDigits: string[] },\n Array<{ phoneDigits: string; patient: Patient }>\n>(\"patients:getByPhones\");\n\nconst SKIP = \"skip\" as const;\n\n/**\n * Subscribe to a list of patients by their Convex ids. Returns a\n * `Record<patientId, Patient>` for O(1) lookup at render time.\n *\n * Pass an empty array (or `undefined`) to skip the query. Missing ids\n * are absent from the map (silently dropped server-side).\n */\nexport function usePatientsByIds(\n ids: string[] | null | undefined,\n): UseQueryResult<Record<string, Patient>> {\n const stableIds = useMemo(() => {\n const arr = ids ?? [];\n return [...new Set(arr)].sort();\n }, [ids]);\n const skipped = stableIds.length === 0;\n\n const result = useQuery(\n patientsGetByIdsRef as FunctionReference<\"query\">,\n skipped ? SKIP : { ids: stableIds },\n ) as Patient[] | undefined;\n\n const mapped = useMemo(() => {\n if (result === undefined) return undefined;\n return Object.fromEntries(\n result.map((p) => {\n const id =\n (p as { id?: string; _id?: string }).id ??\n (p as { _id?: string })._id ??\n \"\";\n return [String(id), p];\n }),\n );\n }, [result]);\n\n if (skipped) {\n return { data: {}, loading: false, error: undefined };\n }\n return {\n data: mapped,\n loading: mapped === undefined,\n error: undefined,\n };\n}\n\n/**\n * Bulk patient lookup by phone numbers (digits or formatted). Returns\n * `Record<phoneDigits, Patient>` for O(1) lookup by digits-only key.\n */\nexport function usePatientsByPhones(\n phones: string[] | null | undefined,\n): UseQueryResult<Record<string, Patient>> {\n const stableDigits = useMemo(() => {\n const arr = phones ?? [];\n const digits = arr\n .map((p) => p.replace(/\\D+/g, \"\"))\n .filter((s) => s.length > 0);\n return [...new Set(digits)].sort();\n }, [phones]);\n const skipped = stableDigits.length === 0;\n\n const result = useQuery(\n patientsGetByPhonesRef as FunctionReference<\"query\">,\n skipped ? SKIP : { phoneDigits: stableDigits },\n ) as Array<{ phoneDigits: string; patient: Patient }> | undefined;\n\n const mapped = useMemo(() => {\n if (result === undefined) return undefined;\n return Object.fromEntries(result.map((r) => [r.phoneDigits, r.patient]));\n }, [result]);\n\n if (skipped) {\n return { data: {}, loading: false, error: undefined };\n }\n return {\n data: mapped,\n loading: mapped === undefined,\n error: undefined,\n };\n}\n","/**\n * TruthProvider — Convex React provider pre-configured for Truth.\n *\n * Wraps children in a ConvexProvider connected to the correct\n * Truth Convex deployment for the given environment.\n *\n * @example\n * ```tsx\n * import { TruthProvider } from '@hipnation-truth/sdk/react';\n *\n * function App() {\n * return (\n * <TruthProvider environment=\"sandbox\">\n * <MyApp />\n * </TruthProvider>\n * );\n * }\n * ```\n */\n\n\"use client\";\n\nimport { ConvexProvider, ConvexReactClient } from \"convex/react\";\nimport type { ReactNode } from \"react\";\nimport { createElement, useMemo } from \"react\";\n\n// Mirrors CONVEX_URLS in client.ts. UAT shares prod resources.\n//\n// Exported so `resolveConvexUrl` can be unit-tested without booting\n// React — a regression in this map shipped SDK 0.4.1 that pointed\n// UAT at sandbox Convex. The test in provider.test.ts locks every\n// stage → URL pair down.\nexport const CONVEX_URLS: Record<string, string> = {\n local: \"https://courteous-duck-623.convex.cloud\",\n staging: \"https://courteous-duck-623.convex.cloud\",\n stg: \"https://courteous-duck-623.convex.cloud\",\n sandbox: \"https://courteous-duck-623.convex.cloud\",\n uat: \"https://gallant-gecko-217.convex.cloud\",\n production: \"https://gallant-gecko-217.convex.cloud\",\n};\n\n/**\n * Resolve the Convex URL for a given environment. Honors an explicit\n * override and falls back to sandbox for unknown environments.\n */\nexport function resolveConvexUrl(\n environment: string | undefined,\n override?: string,\n): string {\n if (override) {\n return override;\n }\n const env = environment ?? \"sandbox\";\n return CONVEX_URLS[env] ?? CONVEX_URLS.sandbox;\n}\n\ninterface TruthProviderProps {\n /** Truth environment — determines which Convex deployment to connect to */\n environment?: string;\n /** Override the Convex URL directly */\n convexUrl?: string;\n children: ReactNode;\n}\n\nfunction TruthProvider({\n environment = \"sandbox\",\n convexUrl,\n children,\n}: TruthProviderProps) {\n const url = resolveConvexUrl(environment, convexUrl);\n\n const client = useMemo(() => new ConvexReactClient(url), [url]);\n\n return createElement(ConvexProvider, { client }, children);\n}\n\nexport { TruthProvider };\nexport type { TruthProviderProps };\n","/**\n * React hooks for conversation reminders — bulk lookup keyed by\n * conversation id. Wraps the public `reminders:listPendingByConversationIds`\n * Convex query so CommHub's inbox can render the reminder clock icon\n * with one reactive subscription for the entire visible list.\n *\n * Must be used within `<TruthProvider />` (see `./provider`).\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { Reminder } from \"../types/reminder\";\nimport type { UseQueryResult } from \"./conversations\";\n\nconst remindersListPendingByConversationIdsRef = makeFunctionReference<\n \"query\",\n { conversationIds: string[] },\n Reminder[]\n>(\"reminders:listPendingByConversationIds\");\n\nconst SKIP = \"skip\" as const;\n\n/**\n * Subscribe to the latest pending reminder for each of the given\n * conversation ids. Returns a `Record<conversationId, Reminder>` —\n * conversations with no pending reminder are absent from the map.\n *\n * Pass an empty array (or `undefined`) to skip the query — useful while\n * the inbox list is still loading.\n *\n * @example\n * ```tsx\n * const ids = conversations.map((c) => c.id);\n * const { data: remindersByConv } = useRemindersForConversations(ids);\n * const r = remindersByConv?.[conv.id]; // Reminder | undefined\n * ```\n */\nexport function useRemindersForConversations(\n conversationIds: string[] | null | undefined,\n): UseQueryResult<Record<string, Reminder>> {\n const ids = conversationIds ?? [];\n const skipped = ids.length === 0;\n\n const result = useQuery(\n remindersListPendingByConversationIdsRef as FunctionReference<\"query\">,\n skipped ? SKIP : { conversationIds: ids },\n ) as Reminder[] | undefined;\n\n const mapped =\n result === undefined\n ? undefined\n : Object.fromEntries(result.map((r) => [r.conversationId, r]));\n\n if (skipped) {\n return { data: {}, loading: false, error: undefined };\n }\n return {\n data: mapped,\n loading: mapped === undefined,\n error: undefined,\n };\n}\n","/**\n * React hooks for conversation tasks (My Tasks surface).\n *\n * Provides a reactive `useMutation` wrapper around the\n * `conversationTasks:markSeen` Convex mutation so CommHub can replace\n * `useMark_Event_Activity_SeenMutation` from `@/generated/graphql`\n * with a Truth SDK equivalent without touching `react.ts` exports (the\n * central agent wires those).\n *\n * Pattern mirrors the existing hooks in `./conversations.ts`:\n * - `makeFunctionReference` for decoupled Convex references\n * - SKIP sentinel for conditional execution\n * - Returns a stable callback, not a live subscription\n *\n * Must be used within `<TruthProvider />`.\n */\n\n\"use client\";\n\nimport { useMutation } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport { useCallback } from \"react\";\n\n// ---------------------------------------------------------------------------\n// Convex mutation reference — decoupled from the consuming app's codegen\n// ---------------------------------------------------------------------------\n\nconst conversationTasksMarkSeenRef = makeFunctionReference<\n \"mutation\",\n { taskId: string; userId: string },\n { ok: true }\n>(\"conversationTasks:markSeen\");\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Returns a stable `markSeen(taskId, userId)` callback backed by the\n * `conversationTasks:markSeen` Convex mutation. Replaces the Hasura\n * `useMark_Event_Activity_SeenMutation` in CommHub's My Tasks surface.\n *\n * @example\n * ```tsx\n * const markSeen = useConversationTaskMarkSeen();\n *\n * // When a task row is pressed:\n * if (!task.seenBy.includes(userId)) {\n * markSeen(task.id, userId).catch(console.error);\n * }\n * ```\n */\nfunction useConversationTaskMarkSeen(): (\n taskId: string,\n userId: string,\n) => Promise<{ ok: true }> {\n const mutate = useMutation(\n conversationTasksMarkSeenRef as FunctionReference<\"mutation\">,\n );\n\n return useCallback(\n (taskId: string, userId: string) =>\n mutate({ taskId, userId }) as Promise<{ ok: true }>,\n [mutate],\n );\n}\n\nexport { useConversationTaskMarkSeen };\n","/**\n * React tracking hook — fire-and-forget event tracking from client components.\n *\n * Creates a lightweight Tracker instance scoped to the browser session\n * and exposes a useTrack() hook for emitting events to Truth's Kinesis stream.\n *\n * @example\n * ```tsx\n * import { useTruth } from '@hipnation-truth/sdk/react';\n *\n * function ConversationView({ conversationId }) {\n * const truth = useTruth();\n *\n * useEffect(() => {\n * truth.track('conversation.marked_read.v1', {\n * read_by_actor_id: userId,\n * unread_count_before: 5,\n * unread_count_after: 0,\n * }, { subject: { conversation_id: conversationId } });\n * }, [conversationId]);\n * }\n * ```\n */\n\n\"use client\";\n\nimport type { ReactNode } from \"react\";\nimport { createContext, createElement, useContext, useMemo } from \"react\";\nimport type {\n EventPayloadMap,\n EventType,\n TrackOptions,\n} from \"../tracking/events\";\nimport { Tracker } from \"../tracking/tracker\";\nimport type { ActorContext } from \"../types/config\";\n\n// ---------------------------------------------------------------------------\n// Context\n// ---------------------------------------------------------------------------\n\ninterface TruthTrackingContextValue {\n track: <T extends EventType>(\n eventType: T,\n payload: EventPayloadMap[T],\n options?: TrackOptions,\n ) => void;\n identify: (actorId: string, actorType: ActorContext[\"actorType\"]) => void;\n}\n\nconst TruthTrackingContext = createContext<TruthTrackingContextValue | null>(\n null,\n);\n\n// ---------------------------------------------------------------------------\n// Provider\n// ---------------------------------------------------------------------------\n\ninterface TruthTrackingProviderProps {\n /** Truth environment — determines API URL for event delivery */\n environment?: string;\n /** Event source identifier */\n source?: string;\n /** Source version (git SHA) */\n sourceVersion?: string;\n /** Default tenant ID */\n tenantId?: string;\n /** API key for authentication */\n apiKey?: string;\n children: ReactNode;\n}\n\nfunction TruthTrackingProvider({\n environment = \"sandbox\",\n source = \"communication-hub.frontend\",\n sourceVersion = \"unknown\",\n tenantId = \"hipnation\",\n apiKey = \"\",\n children,\n}: TruthTrackingProviderProps) {\n const value = useMemo<TruthTrackingContextValue>(() => {\n const tracker = new Tracker({\n apiKey,\n environment,\n source,\n sourceVersion,\n tenantId,\n batchSize: 10,\n flushIntervalMs: 5000,\n });\n\n return {\n track: (eventType, payload, options) => {\n tracker.track(eventType, payload, options);\n },\n identify: (actorId, actorType) => {\n tracker.setActor({ actorId, actorType });\n },\n };\n }, [apiKey, environment, source, sourceVersion, tenantId]);\n\n return createElement(TruthTrackingContext.Provider, { value }, children);\n}\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Access the Truth tracking context. Must be within a TruthTrackingProvider.\n * Returns `{ track, identify }` for emitting events to Kinesis.\n */\nfunction useTruth(): TruthTrackingContextValue {\n const ctx = useContext(TruthTrackingContext);\n if (!ctx) {\n throw new Error(\"useTruth must be used within a TruthTrackingProvider\");\n }\n return ctx;\n}\n\nexport { TruthTrackingProvider, useTruth };\nexport type { TruthTrackingProviderProps, TruthTrackingContextValue };\n","/**\n * Event tracker with batching, retry, and flush support.\n *\n * Buffers events in an internal queue and flushes them to the Truth API\n * endpoint. Flushes occur when the buffer reaches `batchSize` or every\n * `flushIntervalMs` milliseconds, whichever comes first.\n */\n\nimport type { ActorContext } from \"../types/config\";\nimport type {\n EventEnvelope,\n EventPayloadMap,\n EventType,\n TrackOptions,\n} from \"./events\";\n\n// ---------------------------------------------------------------------------\n// UUID v7 helper (no external dependency)\n// ---------------------------------------------------------------------------\n\n/**\n * Generates a UUID v7 string. Uses crypto.randomUUID where available,\n * falling back to a timestamp + random bytes implementation.\n *\n * UUID v7 layout (RFC 9562):\n * 48 bits - unix timestamp (ms)\n * 4 bits - version (0b0111)\n * 12 bits - random\n * 2 bits - variant (0b10)\n * 62 bits - random\n */\nfunction generateUuidV7(): string {\n const now = Date.now();\n\n // 6 bytes of timestamp\n const timeBytes = new Uint8Array(6);\n let ts = now;\n for (let i = 5; i >= 0; i--) {\n timeBytes[i] = ts & 0xff;\n ts = Math.floor(ts / 256);\n }\n\n // 10 bytes of random\n const randomBytes = new Uint8Array(10);\n if (\n typeof globalThis.crypto !== \"undefined\" &&\n globalThis.crypto.getRandomValues\n ) {\n globalThis.crypto.getRandomValues(randomBytes);\n } else {\n for (let i = 0; i < 10; i++) {\n randomBytes[i] = Math.floor(Math.random() * 256);\n }\n }\n\n // Assemble 16 bytes\n const bytes = new Uint8Array(16);\n bytes.set(timeBytes, 0);\n bytes.set(randomBytes, 6);\n\n // Set version (bits 48-51 to 0b0111)\n bytes[6] = (bytes[6] & 0x0f) | 0x70;\n\n // Set variant (bits 64-65 to 0b10)\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n\n // Format as hex string with dashes\n const hex = Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n\n return [\n hex.slice(0, 8),\n hex.slice(8, 12),\n hex.slice(12, 16),\n hex.slice(16, 20),\n hex.slice(20, 32),\n ].join(\"-\");\n}\n\n// ---------------------------------------------------------------------------\n// Environment-based API URL resolution\n// ---------------------------------------------------------------------------\n\n// Environment → Truth API base URL.\n//\n// Topology:\n// local / staging / stg / sandbox → sandbox Truth (app.sandbox.*)\n// uat / production → production Truth (app.truth.*)\n//\n// UAT shares production resources (same Fly Postgres + Hasura + Truth +\n// Convex + EHR tokens as prod). Staging is the isolated sandbox env.\nconst API_URLS: Record<string, string> = {\n local: \"http://localhost:3000\",\n staging: \"https://app.sandbox.communication-hub.com\",\n stg: \"https://app.sandbox.communication-hub.com\",\n sandbox: \"https://app.sandbox.communication-hub.com\",\n uat: \"https://app.truth.communication-hub.com\",\n production: \"https://app.truth.communication-hub.com\",\n};\n\n// ---------------------------------------------------------------------------\n// Constants\n// ---------------------------------------------------------------------------\n\nconst DEFAULT_BATCH_SIZE = 25;\nconst DEFAULT_FLUSH_INTERVAL_MS = 5_000;\nconst MAX_RETRIES = 3;\nconst BASE_RETRY_DELAY_MS = 500;\n\n// ---------------------------------------------------------------------------\n// Tracker configuration\n// ---------------------------------------------------------------------------\n\ninterface TrackerConfig {\n apiKey: string;\n environment: string;\n source: string;\n sourceVersion: string;\n tenantId: string;\n batchSize: number;\n flushIntervalMs: number;\n apiBaseUrl?: string;\n}\n\n// ---------------------------------------------------------------------------\n// Tracker class\n// ---------------------------------------------------------------------------\n\nclass Tracker {\n private readonly config: TrackerConfig;\n readonly apiUrl: string;\n private queue: EventEnvelope[] = [];\n private flushTimer: ReturnType<typeof setInterval> | null = null;\n private defaultActor: ActorContext | undefined;\n private isFlushing = false;\n private isShutdown = false;\n\n constructor(config: TrackerConfig) {\n this.config = config;\n this.apiUrl =\n config.apiBaseUrl ?? API_URLS[config.environment] ?? API_URLS.local;\n\n this.startFlushInterval();\n this.registerShutdownHooks();\n }\n\n /**\n * Set the default actor context for subsequent events.\n */\n setActor(actor: ActorContext): void {\n this.defaultActor = actor;\n }\n\n /**\n * Enqueue a typed event for delivery. This is fire-and-forget from\n * the caller's perspective -- events are buffered and flushed in batches.\n */\n track<T extends EventType>(\n eventType: T,\n payload: EventPayloadMap[T],\n options?: TrackOptions,\n ): void {\n if (this.isShutdown) {\n return;\n }\n\n const now = new Date().toISOString();\n\n const envelope: EventEnvelope = {\n event_id: generateUuidV7(),\n event_type: eventType,\n schema_version: 1,\n occurred_at: options?.occurredAt ?? now,\n received_at: now,\n source: this.config.source,\n source_version: this.config.sourceVersion,\n tenant_id: options?.tenantId ?? this.config.tenantId,\n actor:\n options?.actor ??\n (this.defaultActor\n ? {\n actor_id: this.defaultActor.actorId,\n actor_type: this.defaultActor.actorType,\n }\n : undefined),\n subject: options?.subject,\n compliance: options?.compliance,\n payload: payload as unknown as Record<string, unknown>,\n };\n\n this.queue.push(envelope);\n\n if (this.queue.length >= this.config.batchSize) {\n void this.flush();\n }\n }\n\n /**\n * Force an immediate flush of all buffered events.\n * Returns a promise that resolves when the flush completes.\n */\n async flush(): Promise<void> {\n if (this.queue.length === 0 || this.isFlushing) {\n return;\n }\n\n this.isFlushing = true;\n const batch = this.queue.splice(0, this.config.batchSize);\n\n try {\n await this.sendBatch(batch);\n } catch {\n // Re-queue events that failed to send (prepend to maintain ordering)\n this.queue.unshift(...batch);\n } finally {\n this.isFlushing = false;\n }\n\n // If there are still events in the queue, flush again\n if (this.queue.length >= this.config.batchSize) {\n await this.flush();\n }\n }\n\n /**\n * Gracefully shut down the tracker. Flushes remaining events and\n * clears the flush interval.\n */\n async shutdown(): Promise<void> {\n this.isShutdown = true;\n this.stopFlushInterval();\n await this.flush();\n }\n\n /**\n * Send a batch of events to the Truth API with exponential backoff retry.\n */\n private async sendBatch(batch: EventEnvelope[]): Promise<void> {\n let lastError: unknown;\n\n for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {\n try {\n const response = await fetch(`${this.apiUrl}/api/events/ingest`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": this.config.apiKey,\n },\n body: JSON.stringify({ events: batch }),\n });\n\n if (response.ok) {\n return;\n }\n\n // Don't retry 4xx client errors (except 429)\n if (\n response.status >= 400 &&\n response.status < 500 &&\n response.status !== 429\n ) {\n return;\n }\n\n lastError = new Error(\n `HTTP ${response.status}: ${response.statusText}`,\n );\n } catch (error) {\n lastError = error;\n }\n\n // Exponential backoff with jitter\n if (attempt < MAX_RETRIES) {\n const delay = BASE_RETRY_DELAY_MS * 2 ** attempt;\n const jitter = Math.random() * delay * 0.5;\n await sleep(delay + jitter);\n }\n }\n\n throw lastError;\n }\n\n private startFlushInterval(): void {\n if (this.config.flushIntervalMs > 0) {\n this.flushTimer = setInterval(() => {\n void this.flush();\n }, this.config.flushIntervalMs);\n\n // Unref the timer so it doesn't prevent Node.js from exiting\n if (typeof this.flushTimer === \"object\" && \"unref\" in this.flushTimer) {\n this.flushTimer.unref();\n }\n }\n }\n\n private stopFlushInterval(): void {\n if (this.flushTimer !== null) {\n clearInterval(this.flushTimer);\n this.flushTimer = null;\n }\n }\n\n private registerShutdownHooks(): void {\n // Only register in Node.js environments\n if (typeof globalThis.process !== \"undefined\" && globalThis.process.on) {\n const shutdownHandler = () => {\n void this.shutdown();\n };\n\n globalThis.process.on(\"beforeExit\", shutdownHandler);\n globalThis.process.on(\"SIGTERM\", shutdownHandler);\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport {\n Tracker,\n generateUuidV7,\n DEFAULT_BATCH_SIZE,\n DEFAULT_FLUSH_INTERVAL_MS,\n};\nexport type { TrackerConfig };\n","/**\n * useUserSettings — reactive hook for per-user notification preferences\n * stored in the Truth `userSettings` Convex table.\n *\n * Must be used within `<TruthProvider />` (see `./provider`).\n *\n * @example\n * ```tsx\n * const { data: settings, loading } = useUserSettings(userId);\n * // settings?.notificationsEnabled — boolean or undefined (no row yet)\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { UseQueryResult } from \"./conversations\";\nimport type { UserSettings } from \"../resources/user-settings\";\n\n// ---------------------------------------------------------------------------\n// Convex function reference\n// ---------------------------------------------------------------------------\n\nconst userSettingsGetByUserIdRef = makeFunctionReference<\n \"query\",\n { userId: string },\n UserSettings | null\n>(\"userSettings:getByUserId\");\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\n/**\n * Subscribe to the `userSettings` row for `userId`. Returns `null` when\n * no row exists (treat as all-defaults). Returns `undefined` while the\n * Convex query is in-flight.\n *\n * Pass `null` or `undefined` as `userId` to skip the query (e.g. while\n * auth is loading).\n */\nexport function useUserSettings(\n userId: string | null | undefined,\n): UseQueryResult<UserSettings | null> {\n const skip = !userId;\n\n const result = useQuery(\n userSettingsGetByUserIdRef as FunctionReference<\"query\">,\n skip ? SKIP : { userId: userId as string },\n ) as UserSettings | null | undefined;\n\n if (skip) {\n return { data: null, loading: false, error: undefined };\n }\n\n return {\n data: result ?? null,\n loading: result === undefined,\n error: undefined,\n };\n}\n","/**\n * React hook for fetching an authenticated Dialpad voicemail URL.\n *\n * Provides an imperative `fetchUrl` callback that wraps\n * `client.messages.getVoicemailUrl()` in React state so CommHub's\n * `AudioPlayer` can replace its urql `useMutation` with a single hook\n * call, without adding a query library dependency.\n *\n * The TruthClient instance is passed directly so the hook is testable\n * and doesn't require a global singleton or React context.\n *\n * @example\n * ```tsx\n * import { useVoicemailUrl } from '@hipnation-truth/sdk/react/voicemail';\n * import { getTruthClient } from '@/lib/truthClient';\n *\n * function AudioPlayer({ uri }: { uri: string }) {\n * const { fetchUrl, url, isLoading, error } = useVoicemailUrl(getTruthClient());\n *\n * const handlePlay = async () => {\n * const authenticated = await fetchUrl(uri);\n * if (authenticated) { ... }\n * };\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useCallback, useRef, useState } from \"react\";\nimport type { TruthClient } from \"../client\";\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\nexport interface UseVoicemailUrlResult {\n /** Trigger the URL fetch. Resolves with the clean playback URL or null on error. */\n fetchUrl: (voicemailLink: string) => Promise<string | null>;\n /** The most recently fetched URL (null until first successful fetch). */\n url: string | null;\n /** True while the fetch is in-flight. */\n isLoading: boolean;\n /** Error message from the last failed fetch, or null. */\n error: string | null;\n}\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Provides an imperative `fetchUrl` callback that authenticates a Dialpad\n * voicemail link via the Truth SDK `messages.getVoicemailUrl()` method.\n *\n * @param client The TruthClient instance (e.g., from `getTruthClient()`).\n */\nexport function useVoicemailUrl(client: TruthClient): UseVoicemailUrlResult {\n const [url, setUrl] = useState<string | null>(null);\n const [isLoading, setIsLoading] = useState(false);\n const [error, setError] = useState<string | null>(null);\n\n // Guard against concurrent fetches (user double-tapping play).\n const inFlightRef = useRef(false);\n\n const fetchUrl = useCallback(\n async (voicemailLink: string): Promise<string | null> => {\n if (inFlightRef.current) return null;\n inFlightRef.current = true;\n setIsLoading(true);\n setError(null);\n\n try {\n const result = await client.messages.getVoicemailUrl(voicemailLink);\n setUrl(result.url);\n return result.url;\n } catch (err) {\n const msg = err instanceof Error ? err.message : \"Unknown error\";\n setError(msg);\n return null;\n } finally {\n setIsLoading(false);\n inFlightRef.current = false;\n }\n },\n [client],\n );\n\n return { fetchUrl, url, isLoading, error };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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cAAM,SAAS,MAAM,OAAO,SAAS,gBAAgB,aAAa;AAClE,eAAO,OAAO,GAAG;AACjB,eAAO,OAAO;AAAA,MAChB,SAAS,KAAK;AACZ,cAAM,MAAM,eAAe,QAAQ,IAAI,UAAU;AACjD,iBAAS,GAAG;AACZ,eAAO;AAAA,MACT,UAAE;AACA,qBAAa,KAAK;AAClB,oBAAY,UAAU;AAAA,MACxB;AAAA,IACF;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AAEA,SAAO,EAAE,UAAU,KAAK,WAAW,MAAM;AAC3C;","names":["import_react","import_server","import_react","import_server","SKIP","toResult","import_react","import_server","import_react","_a","res","import_react","import_server","SKIP","import_react","import_server","SKIP","import_react","import_server","SKIP","import_react","import_react","import_server","SKIP","import_react","import_server","import_react","import_react","import_server","SKIP","import_react"]}
|
|
1
|
+
{"version":3,"sources":["../src/react.ts","../src/react/calls.ts","../src/react/conversation-by-id.ts","../src/react/conversations.ts","../src/react/hooks.ts","../src/react/notifications.ts","../src/web-push.ts","../src/react/patient-family.ts","../src/react/patient-search.ts","../src/react/patients-bulk.ts","../src/react/provider.ts","../src/react/reminders.ts","../src/react/tasks.ts","../src/react/tracking.ts","../src/tracking/tracker.ts","../src/react/user-settings.ts","../src/react/voicemail.ts"],"sourcesContent":["/**\n * @hipnation-truth/sdk/react\n *\n * React hooks and provider for Truth SDK — real-time Convex-backed data.\n *\n * @example\n * ```tsx\n * import { TruthProvider, usePatients, useAppointments } from '@hipnation-truth/sdk/react';\n *\n * function App() {\n * return (\n * <TruthProvider environment=\"sandbox\">\n * <PatientList />\n * </TruthProvider>\n * );\n * }\n *\n * function PatientList() {\n * const patients = usePatients({ limit: 20 });\n * if (!patients) return <div>Loading...</div>;\n * return patients.map(p => <div key={p._id}>{p.firstName}</div>);\n * }\n * ```\n */\n\n// Dialpad call event hooks (Convex-backed)\nexport type {\n DialpadCallLogRow,\n DialpadCallRow,\n UseActiveCallsOptions,\n} from \"./react/calls\";\nexport {\n ACTIVE_CALL_STATES,\n CONNECTED_CALL_STATES,\n DialpadCallState,\n RINGING_CALL_STATES,\n TERMINAL_CALL_STATES,\n useActiveCalls,\n useDialpadCallByCallId,\n useDialpadCallLog,\n useDialpadCallsForConversation,\n} from \"./react/calls\";\n// Conversation-by-id lookup\nexport { useConversationById } from \"./react/conversation-by-id\";\n// Reactive conversation / message hooks (Convex-backed, PR #110 schema)\nexport type {\n ConversationListItem,\n ConversationMessageRow,\n ConversationNoteRow,\n ConversationRow,\n ConversationTaskForUserRow,\n ConversationTaskRow,\n UseConversationsFilters,\n UseMessagesOptions,\n UseQueryResult,\n} from \"./react/conversations\";\nexport {\n useConversationByPhonePair,\n useConversationNotes,\n useConversationNotesByPhonePair,\n useConversations,\n useConversationTasks,\n useConversationTasksByPhonePair,\n useConversationTasksForUser,\n useMessages,\n useUnreadAggregate,\n useUnreadCount,\n} from \"./react/conversations\";\n// Re-export types consumers commonly need\nexport type {\n ConversationMessage,\n Physician,\n UseAppointmentListOptions,\n UseConversationMessagesOptions,\n UsePatientBasicOptions,\n UsePatientBasicResult,\n UsePatientListOptions,\n UsePatientMedicalOptions,\n UsePatientPhotoOptions,\n} from \"./react/hooks\";\n// Patient / Appointment / Physician / Medical / Messages hooks\nexport {\n useAppointment,\n useAppointmentByElationId,\n useAppointments,\n useConversationMessages,\n usePatient,\n usePatientBasic,\n usePatientByElationId,\n usePatientByHintId,\n usePatientMedical,\n usePatientPhoto,\n usePatients,\n usePharmacyByNcpdpId,\n usePhysicianByElationId,\n usePhysiciansByElationIds,\n} from \"./react/hooks\";\n// Notifications\nexport type {\n PermissionStatus,\n UseNotificationsOptions,\n UseNotificationsResult,\n} from \"./react/notifications\";\nexport { useNotifications } from \"./react/notifications\";\n// Patient family members\nexport type {\n FamilyMemberRow,\n UsePatientFamilyMembersInput,\n} from \"./react/patient-family\";\nexport { usePatientFamilyMembers } from \"./react/patient-family\";\n// Patient search (multi-mode, office-scoped)\nexport type {\n PatientSearchResult,\n UsePatientSearchOptions,\n} from \"./react/patient-search\";\nexport { usePatientSearch } from \"./react/patient-search\";\n// Bulk patient lookup\nexport {\n usePatientsByIds,\n usePatientsByPhones,\n} from \"./react/patients-bulk\";\nexport type { TruthProviderProps } from \"./react/provider\";\n// Provider\nexport { TruthProvider } from \"./react/provider\";\n// Reminders\nexport { useRemindersForConversations } from \"./react/reminders\";\n// Conversation-task seen tracking\nexport { useConversationTaskMarkSeen } from \"./react/tasks\";\nexport type {\n TruthTrackingContextValue,\n TruthTrackingProviderProps,\n} from \"./react/tracking\";\n// Tracking\nexport { TruthTrackingProvider, useTruth } from \"./react/tracking\";\n// User settings\nexport { useUserSettings } from \"./react/user-settings\";\n// Voicemail URL resolver\nexport { useVoicemailUrl } from \"./react/voicemail\";\n// Re-export event types for track() calls\nexport type {\n EventPayloadMap,\n EventType,\n TrackOptions,\n} from \"./tracking/events\";\nexport type { Appointment, AppointmentListOptions } from \"./types/appointment\";\nexport type { Patient, PatientListOptions } from \"./types/patient\";\n","/**\n * React hooks for Dialpad call events — replaces CommHub's Hasura\n * `useDialpad_EventsSubscription`. Powered by the new\n * `dialpadCallEvents` + `dialpadCallEventLog` Convex tables that the\n * Truth webhook handler writes on every call state transition.\n *\n * Same `{ data, loading, error }` contract as the other hooks in this\n * SDK; same `skip` semantics on missing arguments.\n *\n * @example\n * ```tsx\n * import { useActiveCalls, useDialpadCallByCallId } from '@hipnation-truth/sdk/react';\n *\n * function IncomingBanner() {\n * const { data: active } = useActiveCalls();\n * if (!active?.length) return null;\n * return <Banner calls={active} />;\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { UseQueryResult } from \"./conversations\";\n\n// ---------------------------------------------------------------------------\n// Call state — single source of truth, mirrors the Dialpad webhook\n// `state` field and the values written to the Convex `dialpadCallEvents`\n// table by the Truth webhook handler. Consumers can switch on the\n// const-as-enum or compare the row's raw `state` string directly.\n// ---------------------------------------------------------------------------\n\nexport const DialpadCallState = {\n Calling: \"calling\",\n Ringing: \"ringing\",\n Connected: \"connected\",\n Hold: \"hold\",\n Hangup: \"hangup\",\n Missed: \"missed\",\n VoicemailUploaded: \"voicemail_uploaded\",\n} as const;\nexport type DialpadCallState =\n (typeof DialpadCallState)[keyof typeof DialpadCallState];\n\n/** Ringing states — caller hasn't been picked up yet. */\nexport const RINGING_CALL_STATES: ReadonlySet<DialpadCallState> = new Set([\n DialpadCallState.Calling,\n DialpadCallState.Ringing,\n]);\n\n/** Connected states — call is live (or on hold). */\nexport const CONNECTED_CALL_STATES: ReadonlySet<DialpadCallState> = new Set([\n DialpadCallState.Connected,\n]);\n\n/**\n * Active states — anything not terminal. Default filter for\n * `useActiveCalls`; the union of ringing + connected. `hold` is NOT\n * included by default because the IncomingCalls UI doesn't surface\n * held calls in the inbox banner.\n */\nexport const ACTIVE_CALL_STATES: ReadonlySet<DialpadCallState> = new Set([\n ...RINGING_CALL_STATES,\n ...CONNECTED_CALL_STATES,\n]);\n\n/** Terminal states — call has ended. Used to exclude rows from active lists. */\nexport const TERMINAL_CALL_STATES: ReadonlySet<DialpadCallState> = new Set([\n DialpadCallState.Hangup,\n DialpadCallState.Missed,\n DialpadCallState.VoicemailUploaded,\n]);\n\n// ---------------------------------------------------------------------------\n// Row shapes — mirror the Convex `dialpadCallEvents` table on the Truth\n// schema. Once the consuming app regenerates `convex/_generated/api.d.ts`\n// these can be inferred.\n// ---------------------------------------------------------------------------\n\nexport interface DialpadCallRow {\n _id: string;\n _creationTime: number;\n callId: string;\n state: string;\n direction: string | null;\n fromNumber: string | null;\n toNumber: string | null;\n phonePair: string | null;\n conversationId: string | null;\n patientId: string | null;\n voicemailLink: string | null;\n voicemailDurationSec: number | null;\n transcript: unknown | null;\n duration: number | null;\n occurredAt: string;\n updatedAt: string;\n}\n\nexport interface DialpadCallLogRow {\n _id: string;\n _creationTime: number;\n callId: string;\n state: string;\n direction: string | null;\n payload: unknown | null;\n occurredAt: string;\n}\n\n// ---------------------------------------------------------------------------\n// Convex refs\n// ---------------------------------------------------------------------------\n\nconst listActiveRef = makeFunctionReference<\n \"query\",\n { limit?: number; terminalStates?: string[] },\n DialpadCallRow[]\n>(\"dialpadCallEvents:listActive\");\n\nconst listForConversationRef = makeFunctionReference<\n \"query\",\n { conversationId: string; limit?: number },\n DialpadCallRow[]\n>(\"dialpadCallEvents:listForConversation\");\n\nconst getByCallIdRef = makeFunctionReference<\n \"query\",\n { callId: string },\n DialpadCallRow | null\n>(\"dialpadCallEvents:getByCallId\");\n\nconst listLogForCallIdRef = makeFunctionReference<\n \"query\",\n { callId: string; limit?: number },\n DialpadCallLogRow[]\n>(\"dialpadCallEvents:listLogForCallId\");\n\n// ---------------------------------------------------------------------------\n// Helpers — duplicated from conversations.ts to keep this file independent\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\nfunction toResult<T>(\n value: T | undefined,\n skipped: boolean,\n): UseQueryResult<T> {\n if (skipped) {\n return { data: undefined, loading: false, error: undefined };\n }\n return {\n data: value,\n loading: value === undefined,\n error: undefined,\n };\n}\n\n// ---------------------------------------------------------------------------\n// Hooks\n// ---------------------------------------------------------------------------\n\nexport interface UseActiveCallsOptions {\n /** Page cap. Default 50. */\n limit?: number;\n /**\n * Terminal states excluded from the active list. Override only if\n * Dialpad introduces a new state.\n */\n terminalStates?: string[];\n}\n\n/**\n * Live list of every Dialpad call that hasn't reached a terminal state\n * — used by CommHub's IncomingCalls banner. Updates as the webhook\n * handler patches the per-call row.\n */\nexport function useActiveCalls(\n options?: UseActiveCallsOptions,\n): UseQueryResult<DialpadCallRow[]> {\n const result = useQuery(listActiveRef, options ?? {});\n return toResult(result, false);\n}\n\n/**\n * All calls (most-recent-first) on a single conversation. Pass the\n * Convex conversation `_id` (e.g. from `useConversationByPhonePair`).\n */\nexport function useDialpadCallsForConversation(\n conversationId: string | null | undefined,\n options?: { limit?: number },\n): UseQueryResult<DialpadCallRow[]> {\n const skipped = !conversationId;\n const result = useQuery(\n listForConversationRef,\n skipped\n ? SKIP\n : {\n conversationId: conversationId as string,\n limit: options?.limit,\n },\n );\n return toResult(result, skipped);\n}\n\n/** Single call lookup by Dialpad `call_id`. */\nexport function useDialpadCallByCallId(\n callId: string | null | undefined,\n): UseQueryResult<DialpadCallRow | null> {\n const skipped = !callId;\n const result = useQuery(\n getByCallIdRef,\n skipped ? SKIP : { callId: callId as string },\n );\n return toResult(result, skipped);\n}\n\n/**\n * Full audit history of state transitions for a call — backs the\n * hold / recording / transcription timeline in the right panel.\n */\nexport function useDialpadCallLog(\n callId: string | null | undefined,\n options?: { limit?: number },\n): UseQueryResult<DialpadCallLogRow[]> {\n const skipped = !callId;\n const result = useQuery(\n listLogForCallIdRef,\n skipped ? SKIP : { callId: callId as string, limit: options?.limit },\n );\n return toResult(result, skipped);\n}\n","/**\n * React hook for a single conversation lookup by Convex id — Truth SDK.\n *\n * Replaces CommHub's `useGet_Conversation_EventQuery` (backed by the\n * Hasura `Get_Conversation_Event` GraphQL query). Queries the Convex\n * `conversations:getById` function which returns the raw conversation\n * row, or `null` when the id doesn't exist.\n *\n * The returned `ConversationRow` carries the same phone-pair fields as\n * `useConversationByPhonePair` — `patientPhone`, `providerPhone`,\n * `phonePair`, `patientId` — but is keyed on the Convex document `_id`\n * rather than the pair. Use this when you have a Convex id from a push\n * notification or a deep-link route param.\n *\n * Must be used within a `<TruthProvider />`.\n *\n * @example\n * ```tsx\n * import { useConversationById } from '@hipnation-truth/sdk/react/conversation-by-id';\n *\n * function ConversationScreen({ id }: { id: string }) {\n * const { data: conversation, loading } = useConversationById(id);\n * if (loading) return <Spinner />;\n * if (!conversation) return <NotFound />;\n * return <View>{conversation.patientPhone}</View>;\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { ConversationRow, UseQueryResult } from \"./conversations\";\n\n// ---------------------------------------------------------------------------\n// Convex function reference\n// ---------------------------------------------------------------------------\n\nconst conversationsGetByIdRef = makeFunctionReference<\n \"query\",\n { id: string },\n ConversationRow | null\n>(\"conversations:getById\");\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Subscribe to a single conversation by its Convex document id. Returns\n * `null` when no conversation exists for that id.\n *\n * `data` is `undefined` while loading or when `id` is falsy (skip). Pass\n * `null` or `undefined` to skip the query — useful when the route param\n * hasn't resolved yet.\n */\nfunction useConversationById(\n id: string | null | undefined,\n): UseQueryResult<ConversationRow | null> {\n const skipped = !id;\n const result = useQuery(\n conversationsGetByIdRef as FunctionReference<\"query\">,\n skipped ? \"skip\" : { id: id as string },\n ) as ConversationRow | null | undefined;\n\n if (skipped) {\n return { data: undefined, loading: false, error: undefined };\n }\n return {\n data: result,\n loading: result === undefined,\n error: undefined,\n };\n}\n\nexport { useConversationById };\n","/**\n * React hooks for Truth SDK — reactive conversation + message data.\n *\n * Wraps Convex queries owned by the Truth backend so consumers (CommHub\n * Expo frontend) get live-updating conversation lists, message threads,\n * and unread totals without managing subscriptions themselves.\n *\n * **Hook contract:** every hook in this file returns\n * `{ data, loading, error }`. `data` is `undefined` while loading and\n * also when the underlying query is `\"skip\"`'d (caller didn't pass the\n * required arg yet). `loading` is `true` only while a real subscription\n * is in-flight; once `data` resolves (even to `null`) `loading` flips\n * to `false`. `error` is reserved for SDK-side validation — Convex\n * itself throws on subscribe rather than returning an error value, so\n * unhandled query errors propagate as React errors and should be caught\n * with an error boundary.\n *\n * **Backed by PR #110 schema (commit `41dbb59` on\n * `feat/migrate-dialpad-webhook-and-messages-to-truth`):**\n * - `conversations:listForUser` → `useConversations`\n * - `conversations:getUnreadTotalForUser` → `useUnreadCount`\n * - `conversations:getByPhonePair` → `useConversationByPhonePair`\n * - `conversationMessages:getByConversationId` → `useMessages`\n *\n * **Deliberately NOT shipped here (no backend query yet — flagged in\n * PR #111 body for follow-up):**\n * - Single-conversation-by-id lookup. Convex `conversations` has no\n * `get(id)` query — only `getByPhonePair`. CommHub uses phonePair as\n * the primary handle, so the byPair flavor is what consumers need;\n * if id-based lookup is needed later we can add a `conversations:get`\n * in Truth.\n * - Participants / \"seen by\" hook. The migrated schema has no\n * `conversationParticipants` table — read state is per-user via\n * `conversationReads` and there's no public list query for it yet.\n * - `markRead` mutation. PR #110 declares `markRead` as\n * `internalMutation`, so the frontend can't invoke it directly — it\n * has to go through a Truth HTTP endpoint, or webhook-migrator needs\n * to flip it to a public `mutation`. Flagged for a follow-up.\n *\n * Must be used within `<TruthProvider />` (see `./provider`).\n *\n * @example\n * ```tsx\n * import {\n * useConversations,\n * useConversationByPhonePair,\n * useMessages,\n * useUnreadCount,\n * } from '@hipnation-truth/sdk/react';\n *\n * function Inbox({ userId }: { userId: string }) {\n * const { data: convos, loading } = useConversations({ userId });\n * const { data: unread } = useUnreadCount(userId);\n * if (loading) return <Spinner />;\n * return (\n * <div>\n * <Badge count={unread ?? 0} />\n * {convos?.map((c) => <ConvoRow key={c.id} convo={c} />)}\n * </div>\n * );\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport { useMemo } from \"react\";\n\n// ---------------------------------------------------------------------------\n// Shared types\n// ---------------------------------------------------------------------------\n//\n// Mirror the shapes returned by the Convex queries on PR #110. Once\n// the consuming app regenerates `convex/_generated/api.d.ts` against\n// the merged schema these can be replaced with inferred types — for\n// today they give consumers useful autocomplete.\n\n/**\n * Row shape returned by `conversations:listForUser` — joins the base\n * `conversations` row with the caller's per-user `conversationReads`\n * entry so unread state lives on each item.\n */\nexport interface ConversationListItem {\n /** Convex id of the conversation row. */\n id: string;\n patientPhone: string;\n providerPhone: string;\n /** Sorted-digit-pair key (commutative — same regardless of direction). */\n phonePair: string;\n /** Truth-side patient id, when the kinesis pipeline could resolve it. */\n patientId: string | null;\n /** ISO timestamp of the most recent message in the conversation. */\n lastMessageAt: string;\n /** Caller's unread count for this conversation. */\n unreadCount: number;\n /** ISO timestamp of when the caller last marked the conversation read. */\n lastReadAt: string | null;\n}\n\n/**\n * Raw `conversations` table row — what `getByPhonePair` returns. No\n * unread / read joining; for that, prefer `useConversations`.\n */\nexport interface ConversationRow {\n _id: string;\n _creationTime: number;\n patientPhone: string;\n providerPhone: string;\n phonePair: string;\n patientId: string | null;\n lastEventId: string | null;\n lastMessageAt: string;\n createdAt: string;\n}\n\n/**\n * Message row returned by `conversationMessages:getByConversationId` —\n * calls + SMS merged chronologically. Mirrors `ConversationMessage`\n * already exported from the SDK; re-exported here under a clearer name\n * for the new hook surface.\n */\n/**\n * Conversation note row — single entry shown in the right-panel notes\n * tab in CommHub. Replaces the urql Hasura `internal_activities` row\n * with `type='note'`.\n */\nexport interface ConversationNoteRow {\n /** Convex id of the conversationNotes row. */\n id: string;\n conversationId: string;\n /** Dialpad event the note is attached to, when present. */\n eventId: string | null;\n author: string;\n content: string;\n /** Free-form status flag — matches CommHub's existing schema. */\n status: string | null;\n /** Free-form `type` discriminator — matches CommHub's existing schema. */\n type: string | null;\n createdAt: string;\n updatedAt: string;\n}\n\n/**\n * Conversation task row — single entry shown in the right-panel tasks\n * tab. Replaces the urql Hasura `internal_activities` row with\n * `type='task'`.\n */\nexport interface ConversationTaskRow {\n id: string;\n conversationId: string;\n eventId: string | null;\n author: string;\n description: string;\n status: \"pending\" | \"completed\";\n priority: \"high\" | \"medium\" | \"low\" | null;\n assignee: string | null;\n resolvedBy: string | null;\n type: string | null;\n createdAt: string;\n updatedAt: string;\n}\n\n/**\n * Cross-conversation task row used by the \"My Tasks\" tab. Adds the\n * conversation's normalized phone pair so the UI can render the\n * patient handle without a second reactive query.\n */\nexport interface ConversationTaskForUserRow extends ConversationTaskRow {\n /** Normalized `(patient_phone | provider_phone)` for the task's conversation. */\n phonePair: string | null;\n /**\n * Set of user ids who have seen this task (opened the detail panel).\n * Replaces the Hasura `event_activities.seen` boolean — use\n * `seenBy.includes(userId)` to derive the per-user boolean.\n */\n seenBy: string[];\n /**\n * Which backend table the task lives in:\n * - `\"conversation\"` — `conversationTasks`, tied to a conversation.\n * - `\"standalone\"` — the general `tasks` table (New Task modal with\n * a patient but no conversation). For standalone rows\n * `conversationId` is `\"\"` and `phonePair` is `null`; status /\n * priority mutations must route to the `tasks` resource.\n */\n source: \"conversation\" | \"standalone\";\n}\n\nexport interface ConversationMessageRow {\n kind: \"call\" | \"sms\";\n id: string;\n providerId: string;\n state: string | null;\n direction: string | null;\n fromNumber: string | null;\n toNumber: string | null;\n voicemailLink: string | null;\n duration: number | null;\n text: string | null;\n mms: boolean;\n mmsUrl: string | null;\n messageStatus: string | null;\n occurredAt: string;\n conversationId: string | null;\n patientId: string | null;\n}\n\nexport interface UseQueryResult<T> {\n /**\n * Query data. `undefined` while loading or while the query is\n * intentionally skipped (e.g. caller hasn't supplied `userId` yet).\n */\n data: T | undefined;\n /** True only while a real subscription is in-flight. */\n loading: boolean;\n /**\n * Reserved for client-side validation errors surfaced by the SDK\n * itself. Convex query errors propagate as React errors and should\n * be caught with an error boundary.\n */\n error: Error | undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Convex function references — string-keyed, decoupled from the\n// consuming app's generated `convex/_generated/api`. Names below match\n// the queries on PR #110 (commit 41dbb59) verbatim.\n// ---------------------------------------------------------------------------\n\nconst conversationsListForUserRef = makeFunctionReference<\n \"query\",\n { userId: string; limit?: number },\n ConversationListItem[]\n>(\"conversations:listForUser\");\n\nconst conversationsSearchForUserRef = makeFunctionReference<\n \"query\",\n { userId: string; search: string; limit?: number },\n ConversationListItem[]\n>(\"conversations:searchForUser\");\n\nconst conversationsGetUnreadTotalForUserRef = makeFunctionReference<\n \"query\",\n { userId: string },\n number\n>(\"conversations:getUnreadTotalForUser\");\n\nconst conversationsGetUnreadAggregateForUserRef = makeFunctionReference<\n \"query\",\n { userId: string; providerPhones?: string[] },\n { totalMessages: number; conversationCount: number }\n>(\"conversations:getUnreadAggregateForUser\");\n\nconst conversationsGetByPhonePairRef = makeFunctionReference<\n \"query\",\n { phonePair: string },\n ConversationRow | null\n>(\"conversations:getByPhonePair\");\n\n// Lives on the existing `conversationMessages` module (pre-#110). PR\n// #110 added `conversationId` indexing on `messageSms` / `messageCalls`,\n// so this query is now reliable for the new conversations table.\nconst conversationMessagesGetByConversationIdRef = makeFunctionReference<\n \"query\",\n { conversationId: string; limit?: number },\n ConversationMessageRow[]\n>(\"conversationMessages:getByConversationId\");\n\n// Notes + tasks for a conversation thread (Truth #730). Both lists are\n// keyed on the Convex conversation `_id` — same id that comes back\n// from `useConversations`.\nconst conversationNotesListForConversationRef = makeFunctionReference<\n \"query\",\n { conversationId: string },\n ConversationNoteRow[]\n>(\"conversationNotes:listForConversation\");\n\nconst conversationTasksListForConversationRef = makeFunctionReference<\n \"query\",\n { conversationId: string },\n ConversationTaskRow[]\n>(\"conversationTasks:listForConversation\");\n\n// Phone-pair-keyed variants (Truth SDK 0.10.0). CommHub holds the\n// conversation by `(patient_phone, provider_phone)` and shouldn't have\n// to chain `useConversationByPhonePair → useConversationNotes`. These\n// resolve the conversation server-side and return the same row shape.\nconst conversationNotesListByPhonePairRef = makeFunctionReference<\n \"query\",\n { phonePair: string },\n ConversationNoteRow[]\n>(\"conversationNotes:listByPhonePair\");\n\nconst conversationTasksListByPhonePairRef = makeFunctionReference<\n \"query\",\n { phonePair: string },\n ConversationTaskRow[]\n>(\"conversationTasks:listByPhonePair\");\n\nconst conversationTasksListForUserRef = makeFunctionReference<\n \"query\",\n { userId: string; limit?: number },\n ConversationTaskForUserRow[]\n>(\"conversationTasks:listForUser\");\n\n// ---------------------------------------------------------------------------\n// Skipped-query sentinel + result shaping\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\nfunction toResult<T>(\n value: T | undefined,\n skipped: boolean,\n): UseQueryResult<T> {\n if (skipped) {\n return { data: undefined, loading: false, error: undefined };\n }\n return {\n data: value,\n loading: value === undefined,\n error: undefined,\n };\n}\n\n// ---------------------------------------------------------------------------\n// Filters + options\n// ---------------------------------------------------------------------------\n\nexport interface UseConversationsFilters {\n /**\n * Truth user id (the Better Auth subject). Pass `undefined` to skip\n * the query — useful when the auth session is still loading.\n */\n userId: string | null | undefined;\n /**\n * Optional search term — when non-empty, switches the hook from\n * `conversations:listForUser` to `conversations:searchForUser`, which\n * fans out two parallel Convex full-text patient searches\n * (firstName + lastName) and returns the matching conversations the\n * user can see, sorted by recency. Whitespace-only values are treated\n * as empty.\n */\n search?: string;\n /** Page size. Server caps at 100 by default. */\n limit?: number;\n}\n\nexport interface UseMessagesOptions {\n /** Page size. Server caps at 200 by default. */\n limit?: number;\n}\n\n// ---------------------------------------------------------------------------\n// Hooks\n// ---------------------------------------------------------------------------\n\n/**\n * Subscribe to a user's conversations — joined with their per-conversation\n * unread count, sorted by most recent message. Updates live as new\n * SMS / calls land in Convex and the webhook bumps `unreadCount`.\n *\n * Returns `{ data: undefined, loading: false }` while the auth session\n * resolves (`userId === undefined`); not an error, just a skip.\n */\nfunction useConversations(\n filters: UseConversationsFilters,\n): UseQueryResult<ConversationListItem[]> {\n const trimmedSearch = filters.search?.trim() ?? \"\";\n const isSearchMode = trimmedSearch.length > 0;\n const skipped = !filters.userId;\n\n const listResult = useQuery(\n conversationsListForUserRef as FunctionReference<\"query\">,\n skipped || isSearchMode\n ? SKIP\n : {\n userId: filters.userId as string,\n limit: filters.limit,\n },\n ) as ConversationListItem[] | undefined;\n\n const searchResult = useQuery(\n conversationsSearchForUserRef as FunctionReference<\"query\">,\n skipped || !isSearchMode\n ? SKIP\n : {\n userId: filters.userId as string,\n search: trimmedSearch,\n limit: filters.limit,\n },\n ) as ConversationListItem[] | undefined;\n\n return toResult(isSearchMode ? searchResult : listResult, skipped);\n}\n\n/**\n * Look up a single conversation by its normalized phonePair (sorted\n * digit-pair, e.g. `\"5125550123|6505551234\"`). Returns `null` if no\n * conversation exists yet for that pair.\n *\n * Use the `phonePairKey(patientPhone, providerPhone)` helper exposed by\n * the Truth Convex module to derive the key from raw phone strings.\n */\nfunction useConversationByPhonePair(\n phonePair: string | null | undefined,\n): UseQueryResult<ConversationRow | null> {\n const skipped = !phonePair;\n const result = useQuery(\n conversationsGetByPhonePairRef as FunctionReference<\"query\">,\n skipped ? SKIP : { phonePair: phonePair as string },\n ) as ConversationRow | null | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Subscribe to a paginated message stream for a single conversation.\n * Calls + SMS merged chronologically, newest-first. Live as the\n * kinesis consumer writes to `messageCalls` / `messageSms`.\n *\n * Pass the Convex `_id` of the conversation row (from\n * `useConversations` / `useConversationByPhonePair`) as\n * `conversationId`.\n */\nfunction useMessages(\n conversationId: string | null | undefined,\n options?: UseMessagesOptions,\n): UseQueryResult<ConversationMessageRow[]> {\n const skipped = !conversationId;\n const result = useQuery(\n conversationMessagesGetByConversationIdRef as FunctionReference<\"query\">,\n skipped\n ? SKIP\n : {\n conversationId: conversationId as string,\n limit: options?.limit,\n },\n ) as ConversationMessageRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Subscribe to the user's total unread message count across all\n * conversations. Backs the inbox tab badge in CommHub.\n *\n * Returns `0` when the underlying query resolves with no unread\n * messages, `undefined` while loading or when `userId` is missing.\n */\nfunction useUnreadCount(\n userId: string | null | undefined,\n): UseQueryResult<number> {\n const skipped = !userId;\n const result = useQuery(\n conversationsGetUnreadTotalForUserRef as FunctionReference<\"query\">,\n skipped ? SKIP : { userId: userId as string },\n ) as number | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Server-side aggregate of the user's unread total + count of\n * conversations with at least one unread. Optionally filtered by\n * `providerPhones` (e.g. selected offices) — the aggregate is\n * computed in Convex, no client-side fetch-and-filter.\n */\nfunction useUnreadAggregate(\n userId: string | null | undefined,\n options?: { providerPhones?: string[] },\n): UseQueryResult<{ totalMessages: number; conversationCount: number }> {\n const skipped = !userId;\n const phones = options?.providerPhones;\n const stablePhones = useMemoizedPhones(phones);\n const result = useQuery(\n conversationsGetUnreadAggregateForUserRef as FunctionReference<\"query\">,\n skipped\n ? SKIP\n : {\n userId: userId as string,\n providerPhones: stablePhones,\n },\n ) as { totalMessages: number; conversationCount: number } | undefined;\n return toResult(result, skipped);\n}\n\n// Memoize the phones array so a new reference each render doesn't\n// retrigger the Convex subscription. Sort to canonicalize identity.\nfunction useMemoizedPhones(phones: string[] | undefined): string[] | undefined {\n const key = phones ? [...phones].sort().join(\"|\") : \"\";\n return useMemo(\n () => (phones && phones.length ? [...phones].sort() : undefined),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [key],\n );\n}\n\n/**\n * Subscribe to all notes attached to a conversation, newest first.\n * Replaces the urql Hasura `useConversation_NotesSubscription` in\n * CommHub (Truth #730). Pass `null`/`undefined` to skip the query.\n */\nfunction useConversationNotes(\n conversationId: string | null | undefined,\n): UseQueryResult<ConversationNoteRow[]> {\n const skipped = !conversationId;\n const result = useQuery(\n conversationNotesListForConversationRef as FunctionReference<\"query\">,\n skipped ? SKIP : { conversationId: conversationId as string },\n ) as ConversationNoteRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Subscribe to all tasks attached to a conversation, newest first.\n * Replaces the urql Hasura `useConversation_TasksSubscription` in\n * CommHub (Truth #730).\n */\nfunction useConversationTasks(\n conversationId: string | null | undefined,\n): UseQueryResult<ConversationTaskRow[]> {\n const skipped = !conversationId;\n const result = useQuery(\n conversationTasksListForConversationRef as FunctionReference<\"query\">,\n skipped ? SKIP : { conversationId: conversationId as string },\n ) as ConversationTaskRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Same as `useConversationNotes` but keyed on the normalized\n * `(patient_phone | provider_phone)` pair — saves the caller from\n * chaining a separate `useConversationByPhonePair` call. Returns\n * `[]` if no conversation exists for the pair yet.\n */\nfunction useConversationNotesByPhonePair(\n phonePair: string | null | undefined,\n): UseQueryResult<ConversationNoteRow[]> {\n const skipped = !phonePair;\n const result = useQuery(\n conversationNotesListByPhonePairRef as FunctionReference<\"query\">,\n skipped ? SKIP : { phonePair: phonePair as string },\n ) as ConversationNoteRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\n/**\n * Same as `useConversationTasks` but keyed on phonePair.\n */\n/**\n * Subscribe to every conversation task where the caller is the\n * assignee or the author. Backs CommHub's \"My Tasks\" tab.\n */\nfunction useConversationTasksForUser(\n userId: string | null | undefined,\n options?: { limit?: number },\n): UseQueryResult<ConversationTaskForUserRow[]> {\n const skipped = !userId;\n const result = useQuery(\n conversationTasksListForUserRef as FunctionReference<\"query\">,\n skipped ? SKIP : { userId: userId as string, limit: options?.limit },\n ) as ConversationTaskForUserRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\nfunction useConversationTasksByPhonePair(\n phonePair: string | null | undefined,\n): UseQueryResult<ConversationTaskRow[]> {\n const skipped = !phonePair;\n const result = useQuery(\n conversationTasksListByPhonePairRef as FunctionReference<\"query\">,\n skipped ? SKIP : { phonePair: phonePair as string },\n ) as ConversationTaskRow[] | undefined;\n\n return toResult(result, skipped);\n}\n\nexport {\n useConversationByPhonePair,\n useConversationNotes,\n useConversationNotesByPhonePair,\n useConversations,\n useConversationTasks,\n useConversationTasksByPhonePair,\n useConversationTasksForUser,\n useMessages,\n useUnreadAggregate,\n useUnreadCount,\n};\n","/**\n * React hooks for Truth SDK — real-time Convex-backed data access.\n *\n * These hooks use Convex React subscriptions for live-updating data.\n * Must be used within a ConvexProvider (see TruthProvider).\n *\n * @example\n * ```tsx\n * import { usePatients, useAppointments } from '@hipnation-truth/sdk/react';\n *\n * function PatientList() {\n * const patients = usePatients({ limit: 20 });\n * return patients?.map(p => <div key={p._id}>{p.firstName} {p.lastName}</div>);\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport { useEffect } from \"react\";\n\n// ---------------------------------------------------------------------------\n// Convex function references\n// ---------------------------------------------------------------------------\n// We use string-based references to avoid importing the generated API\n// (which lives in the consuming app, not the SDK). Convex supports this\n// via the `api` module pattern, but for a published SDK we use\n// makeFunctionReference to stay decoupled from the app's codegen.\n\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\n\n// Patient query references\nconst patientsListRef = makeFunctionReference<\n \"query\",\n {\n search?: string;\n lastName?: string;\n cursor?: string;\n limit?: number;\n },\n unknown[]\n>(\"patients:list\");\n\nconst patientsGetRef = makeFunctionReference<\n \"query\",\n { id: string },\n unknown | null\n>(\"patients:get\");\n\nconst patientsByElationIdRef = makeFunctionReference<\n \"query\",\n { elationId: string },\n unknown | null\n>(\"patients:getByElationId\");\n\nconst patientsByHintIdRef = makeFunctionReference<\n \"query\",\n { hintId: string },\n unknown | null\n>(\"patients:getByHintId\");\n\n// Appointment query references\nconst appointmentsListRef = makeFunctionReference<\n \"query\",\n {\n patientId?: string;\n status?: string;\n startDate?: string;\n endDate?: string;\n limit?: number;\n },\n unknown[]\n>(\"appointments:list\");\n\nconst appointmentsGetRef = makeFunctionReference<\n \"query\",\n { id: string },\n unknown | null\n>(\"appointments:get\");\n\nconst appointmentsByElationIdRef = makeFunctionReference<\n \"query\",\n { elationId: string },\n unknown | null\n>(\"appointments:getByElationId\");\n\n// ---------------------------------------------------------------------------\n// Patient hooks\n// ---------------------------------------------------------------------------\n\ninterface UsePatientListOptions {\n search?: string;\n lastName?: string;\n limit?: number;\n}\n\n/**\n * Subscribe to a list of patients with optional search and filtering.\n * Returns undefined while loading, then an array of patients.\n */\nfunction usePatients(options?: UsePatientListOptions) {\n return useQuery(patientsListRef as FunctionReference<\"query\">, options ?? {});\n}\n\n/**\n * Subscribe to a single patient by Convex document ID.\n */\nfunction usePatient(id: string) {\n return useQuery(patientsGetRef as FunctionReference<\"query\">, { id });\n}\n\n/**\n * Subscribe to a patient by their Elation ID.\n */\nfunction usePatientByElationId(elationId: string) {\n return useQuery(patientsByElationIdRef as FunctionReference<\"query\">, {\n elationId,\n });\n}\n\n/**\n * Subscribe to a patient by their Hint ID.\n */\nfunction usePatientByHintId(hintId: string) {\n return useQuery(patientsByHintIdRef as FunctionReference<\"query\">, {\n hintId,\n });\n}\n\n// ---------------------------------------------------------------------------\n// Appointment hooks\n// ---------------------------------------------------------------------------\n\ninterface UseAppointmentListOptions {\n patientId?: string;\n status?: string;\n startDate?: string;\n endDate?: string;\n limit?: number;\n}\n\n/**\n * Subscribe to a list of appointments with optional filtering.\n */\nfunction useAppointments(options?: UseAppointmentListOptions) {\n return useQuery(\n appointmentsListRef as FunctionReference<\"query\">,\n options ?? {},\n );\n}\n\n/**\n * Subscribe to a single appointment by Convex document ID.\n */\nfunction useAppointment(id: string) {\n return useQuery(appointmentsGetRef as FunctionReference<\"query\">, { id });\n}\n\n/**\n * Subscribe to an appointment by its Elation ID.\n */\nfunction useAppointmentByElationId(elationId: string) {\n return useQuery(appointmentsByElationIdRef as FunctionReference<\"query\">, {\n elationId,\n });\n}\n\n// ---------------------------------------------------------------------------\n// Physician hooks\n// ---------------------------------------------------------------------------\n\ninterface Physician {\n _id: string;\n elationId: number;\n firstName?: string;\n lastName?: string;\n npi?: string;\n credentials?: string;\n specialties?: string[];\n practice?: number;\n email?: string;\n phone?: string;\n lastSyncedAt: string;\n}\n\nconst physiciansGetByElationIdsRef = makeFunctionReference<\n \"query\",\n { ids: number[] },\n Physician[]\n>(\"physicians:getByElationIds\");\n\nconst physiciansGetByElationIdRef = makeFunctionReference<\n \"query\",\n { id: number },\n Physician | null\n>(\"physicians:getByElationId\");\n\n/**\n * Resolve a batch of physicians by their Elation IDs. Returns an array\n * of physicians that exist in Convex; missing ids are silently dropped.\n *\n * Use this to resolve medication `prescribing_physician` / appointment\n * physician ids to display names, avoiding the per-physician HTTP\n * round-trip through Truth's Elation proxy.\n *\n * Pass `undefined` (or an empty array) to skip the query — the hook\n * returns `undefined` until you pass a populated list.\n */\nfunction usePhysiciansByElationIds(ids: number[] | undefined) {\n return useQuery(\n physiciansGetByElationIdsRef as FunctionReference<\"query\">,\n ids && ids.length > 0 ? { ids } : \"skip\",\n );\n}\n\n/**\n * Subscribe to a single physician by their Elation ID.\n */\nfunction usePhysicianByElationId(id: number | undefined) {\n return useQuery(\n physiciansGetByElationIdRef as FunctionReference<\"query\">,\n id !== undefined ? { id } : \"skip\",\n );\n}\n\n// ---------------------------------------------------------------------------\n// Patient medical hooks (medications, problems, allergies, appointments)\n// ---------------------------------------------------------------------------\n\nconst medicationsByPatientRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown[]\n>(\"medicalRecords:getMedicationsByElationPatient\");\n\nconst problemsByPatientRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown[]\n>(\"medicalRecords:getProblemsByElationPatient\");\n\nconst allergiesByPatientRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown[]\n>(\"medicalRecords:getAllergiesByElationPatient\");\n\nconst appointmentsByPatientRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown[]\n>(\"medicalRecords:getAppointmentsByElationPatient\");\n\ninterface UsePatientMedicalOptions {\n /**\n * Base URL of the Truth API. Required to trigger the background refresh\n * (e.g. `https://app.truth.communication-hub.com`). If omitted, the hook\n * only reads from Convex and does not refresh.\n */\n apiBaseUrl?: string;\n /** API key used for the refresh call. */\n apiKey?: string;\n /**\n * If true, suppress the background refresh. Useful when you know the\n * data was just refreshed by another component on the page.\n */\n skipRefresh?: boolean;\n}\n\n/**\n * Composite hook that returns a patient's medical records — medications,\n * problems, allergies, appointments — from the Convex cache.\n *\n * On mount (and when `elationId` changes) fires a background refresh\n * against Truth's `/api/patients/medical/refresh` endpoint so stale data\n * is pulled in without blocking render. Returns cached data immediately;\n * Convex subscription updates the UI when refresh completes.\n */\nfunction usePatientMedical(\n elationId: number | undefined,\n options?: UsePatientMedicalOptions,\n) {\n const medications = useQuery(\n medicationsByPatientRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n ) as unknown[] | undefined;\n\n const problems = useQuery(\n problemsByPatientRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n ) as unknown[] | undefined;\n\n const allergies = useQuery(\n allergiesByPatientRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n ) as unknown[] | undefined;\n\n const appointments = useQuery(\n appointmentsByPatientRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n ) as unknown[] | undefined;\n\n useEffect(() => {\n if (elationId === undefined || options?.skipRefresh) {\n return;\n }\n const apiBaseUrl = options?.apiBaseUrl;\n const apiKey = options?.apiKey;\n if (!apiBaseUrl || !apiKey) {\n return;\n }\n\n const controller = new AbortController();\n void fetch(`${apiBaseUrl}/api/patients/medical/refresh`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": apiKey,\n },\n body: JSON.stringify({ elationId }),\n signal: controller.signal,\n }).catch(() => {\n /* background refresh — failures are non-fatal */\n });\n\n return () => controller.abort();\n }, [elationId, options?.apiBaseUrl, options?.apiKey, options?.skipRefresh]);\n\n return { medications, problems, allergies, appointments };\n}\n\n// ---------------------------------------------------------------------------\n// Patient basic hooks (Elation demographics + Hint memberships/account)\n// ---------------------------------------------------------------------------\n//\n// Replaces the CommHub `getPatientBasicDetails` Hasura Action. Reads\n// from Convex-cached rows (populated by the patients-backfill cron +\n// `/api/patients/basic/refresh` on-demand), falling back to an empty\n// state until the background refresh completes.\n\nconst elationPatientByIdRef = makeFunctionReference<\n \"query\",\n { elationId: number },\n unknown | null\n>(\"elationPatients:getByElationId\");\n\nconst hintPatientByIdRef = makeFunctionReference<\n \"query\",\n { hintId: string },\n unknown | null\n>(\"hintPatients:getByHintId\");\n\nconst pharmacyByNcpdpRef = makeFunctionReference<\n \"query\",\n { ncpdpId: string },\n unknown | null\n>(\"elationPharmacies:getByNcpdpId\");\n\nconst patientPhotoByIdRef = makeFunctionReference<\n \"query\",\n { elationPatientId: number },\n unknown | null\n>(\"elationPatientPhotos:getByElationPatientId\");\n\ninterface UsePatientBasicOptions {\n /** Truth API base URL used for the background refresh. */\n apiBaseUrl?: string;\n /** API key for the refresh call. */\n apiKey?: string;\n /** Suppress the background refresh. */\n skipRefresh?: boolean;\n}\n\ninterface UsePatientBasicResult {\n /**\n * Raw Elation patient payload (matches the shape Elation's\n * `/patients/{id}` returns — `first_name`, `last_name`, `phones`, etc).\n * Returns `undefined` while the cache miss is loading, `null` if the\n * patient isn't in Convex yet (first-open-after-backfill).\n */\n elationPatient: Record<string, unknown> | null | undefined;\n /**\n * Raw Hint patient payload (matches the shape Hint's\n * `/provider/patients/{id}` returns — `memberships`, `account`,\n * `phones`, etc).\n */\n hintPatient: Record<string, unknown> | null | undefined;\n /**\n * The full Convex row for Elation (includes `elationId`, `lastSyncedAt`,\n * `photoS3Key`, `preferredPharmacyNcpdpId`, etc). Use this when you\n * need the structured/typed fields rather than the raw Elation payload.\n */\n elationRow: Record<string, unknown> | null | undefined;\n /**\n * The full Convex row for Hint. Similar relationship to `hintPatient`.\n */\n hintRow: Record<string, unknown> | null | undefined;\n /** True while either cache miss is still pending. */\n loading: boolean;\n}\n\n/**\n * Composite hook returning a patient's basic details — Hint demographics\n * + memberships + account, Elation demographics + clinical metadata —\n * from the Convex cache.\n *\n * On mount (and when inputs change) fires a background refresh against\n * `/api/patients/basic/refresh` so stale rows get pulled fresh without\n * blocking render. Returns cached data immediately; Convex subscription\n * updates the UI when refresh completes.\n */\nfunction usePatientBasic(\n input: { hintId?: string; elationId?: number },\n options?: UsePatientBasicOptions,\n): UsePatientBasicResult {\n const elationRow = useQuery(\n elationPatientByIdRef as FunctionReference<\"query\">,\n input.elationId !== undefined ? { elationId: input.elationId } : \"skip\",\n ) as Record<string, unknown> | null | undefined;\n\n const hintRow = useQuery(\n hintPatientByIdRef as FunctionReference<\"query\">,\n input.hintId !== undefined ? { hintId: input.hintId } : \"skip\",\n ) as Record<string, unknown> | null | undefined;\n\n useEffect(() => {\n if (options?.skipRefresh) {\n return;\n }\n if (!input.hintId && input.elationId === undefined) {\n return;\n }\n const apiBaseUrl = options?.apiBaseUrl;\n const apiKey = options?.apiKey;\n if (!apiBaseUrl || !apiKey) {\n return;\n }\n\n const controller = new AbortController();\n void fetch(`${apiBaseUrl}/api/patients/basic/refresh`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": apiKey,\n },\n body: JSON.stringify({\n hintId: input.hintId,\n elationId: input.elationId,\n }),\n signal: controller.signal,\n }).catch(() => {\n /* background refresh — failures are non-fatal */\n });\n\n return () => controller.abort();\n }, [\n input.hintId,\n input.elationId,\n options?.apiBaseUrl,\n options?.apiKey,\n options?.skipRefresh,\n ]);\n\n // Surface the raw payload under elationPatient/hintPatient so consumers\n // can keep using the exact Elation/Hint field names (first_name,\n // memberships, account.past_due_in_cents) with zero mapping.\n const elationPatient =\n elationRow === undefined\n ? undefined\n : elationRow === null\n ? null\n : ((elationRow.raw as Record<string, unknown>) ?? null);\n const hintPatient =\n hintRow === undefined\n ? undefined\n : hintRow === null\n ? null\n : ((hintRow.raw as Record<string, unknown>) ?? null);\n\n const elationLoading =\n input.elationId !== undefined && elationRow === undefined;\n const hintLoading = input.hintId !== undefined && hintRow === undefined;\n\n return {\n elationPatient,\n hintPatient,\n elationRow,\n hintRow,\n loading: elationLoading || hintLoading,\n };\n}\n\n/**\n * Read a shared pharmacy row by NCPDP id. Backs the pharmacy card in\n * the patient panel (from Elation's preferred_pharmacy reference).\n */\nfunction usePharmacyByNcpdpId(ncpdpId: string | undefined) {\n return useQuery(\n pharmacyByNcpdpRef as FunctionReference<\"query\">,\n ncpdpId ? { ncpdpId } : \"skip\",\n );\n}\n\ninterface UsePatientPhotoOptions {\n apiBaseUrl?: string;\n apiKey?: string;\n skipRefresh?: boolean;\n}\n\n/**\n * Subscribe to a patient's profile photo (s3Key + metadata). The\n * consumer constructs a download URL via the Truth attachments resource\n * (signed S3 URL). Fires a background refresh to pull the latest photo\n * binary from Elation + upload to S3.\n */\nfunction usePatientPhoto(\n elationId: number | undefined,\n options?: UsePatientPhotoOptions,\n) {\n const photo = useQuery(\n patientPhotoByIdRef as FunctionReference<\"query\">,\n elationId !== undefined ? { elationPatientId: elationId } : \"skip\",\n );\n\n useEffect(() => {\n if (options?.skipRefresh) {\n return;\n }\n if (elationId === undefined) {\n return;\n }\n const apiBaseUrl = options?.apiBaseUrl;\n const apiKey = options?.apiKey;\n if (!apiBaseUrl || !apiKey) {\n return;\n }\n\n const controller = new AbortController();\n void fetch(`${apiBaseUrl}/api/patients/photo/refresh`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": apiKey,\n },\n body: JSON.stringify({ elationId }),\n signal: controller.signal,\n }).catch(() => {\n /* non-fatal */\n });\n\n return () => controller.abort();\n }, [elationId, options?.apiBaseUrl, options?.apiKey, options?.skipRefresh]);\n\n return photo;\n}\n\n// ---------------------------------------------------------------------------\n// Conversation messages (Dialpad calls + SMS merged)\n// ---------------------------------------------------------------------------\n\ninterface ConversationMessage {\n kind: \"call\" | \"sms\";\n id: string;\n providerId: string;\n state: string | null;\n direction: string | null;\n fromNumber: string | null;\n toNumber: string | null;\n voicemailLink: string | null;\n duration: number | null;\n text: string | null;\n mms: boolean;\n mmsUrl: string | null;\n messageStatus: string | null;\n occurredAt: string;\n conversationId: string | null;\n patientId: string | null;\n}\n\nconst messagesByPhonesRef = makeFunctionReference<\n \"query\",\n { phoneA: string; phoneB: string; limit?: number },\n ConversationMessage[]\n>(\"conversationMessages:getByPhones\");\n\nconst messagesByConversationIdRef = makeFunctionReference<\n \"query\",\n { conversationId: string; limit?: number },\n ConversationMessage[]\n>(\"conversationMessages:getByConversationId\");\n\ninterface UseConversationMessagesOptions {\n /** Max items to return (default 200). */\n limit?: number;\n}\n\n/**\n * Subscribe to a conversation's calls + SMS merged chronologically.\n * Pass the patient phone + the provider phone — Truth computes a\n * normalized pair key server-side so formatting differences don't\n * matter.\n *\n * Returns `undefined` while loading, then `ConversationMessage[]`\n * sorted newest-first. Updates live as new webhook events land in\n * Convex via the kinesis consumer.\n */\nfunction useConversationMessages(\n input: { phoneA?: string; phoneB?: string; conversationId?: string },\n options?: UseConversationMessagesOptions,\n): ConversationMessage[] | undefined {\n const hasPair = !!input.phoneA && !!input.phoneB;\n const byPair = useQuery(\n messagesByPhonesRef as FunctionReference<\"query\">,\n hasPair\n ? {\n phoneA: input.phoneA as string,\n phoneB: input.phoneB as string,\n limit: options?.limit,\n }\n : \"skip\",\n ) as ConversationMessage[] | undefined;\n\n const byConvo = useQuery(\n messagesByConversationIdRef as FunctionReference<\"query\">,\n !hasPair && input.conversationId\n ? { conversationId: input.conversationId, limit: options?.limit }\n : \"skip\",\n ) as ConversationMessage[] | undefined;\n\n return hasPair ? byPair : byConvo;\n}\n\nexport {\n usePatients,\n usePatient,\n usePatientByElationId,\n usePatientByHintId,\n useAppointments,\n useAppointment,\n useAppointmentByElationId,\n usePhysiciansByElationIds,\n usePhysicianByElationId,\n usePatientMedical,\n usePatientBasic,\n usePharmacyByNcpdpId,\n usePatientPhoto,\n useConversationMessages,\n};\nexport type { ConversationMessage, UseConversationMessagesOptions };\nexport type {\n UsePatientListOptions,\n UseAppointmentListOptions,\n UsePatientMedicalOptions,\n UsePatientBasicOptions,\n UsePatientBasicResult,\n UsePatientPhotoOptions,\n Physician,\n};\n","/**\n * useNotifications — Truth SDK React hook for push notifications.\n *\n * Shape-compatible with `expo-notifications` where practical so\n * consumers port with minimal change. The hook dynamically imports\n * `expo-notifications` so the SDK doesn't hard-depend on Expo —\n * consumers running on a non-Expo runtime (e.g. a server test harness)\n * can still import the SDK without Metro blowing up.\n *\n * Exposes:\n * - permissionStatus — \"granted\" / \"denied\" / \"undetermined\"\n * - devicePushToken — native APNs/FCM token (undefined until register)\n * - register() — request permission, fetch token, register with Truth\n * - unregister() — revoke the device\n * - addReceivedListener / addResponseListener — re-exports of expo's\n * - getBadgeCount / setBadgeCount — re-exports of expo's\n *\n * Web push (VAPID subscription) lands in Phase 3.\n */\n\n\"use client\";\n\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport {\n isWebPushSupported,\n registerServiceWorker,\n subscribeToPush,\n subscriptionToJSON,\n} from \"../web-push\";\n\n// `expo-notifications` is an optional peer dep. We lazy-import it via\n// a string-indirection so TypeScript + tsup don't try to resolve its\n// types at build time (the SDK ships without a hard dep so non-Expo\n// consumers — web, Node services — can still import from @hipnation-truth/sdk/react).\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype ExpoNotificationsModule = any;\n\nasync function loadExpo(): Promise<ExpoNotificationsModule | null> {\n // Hermes rejects dynamic `import()`, so use Metro's `require()` which\n // is provided in every React Native module's scope. `expo-notifications`\n // is an optional peer dep — non-Expo consumers (web, Node) hit the\n // catch and we return null. tsup keeps this as a literal `require()`\n // (rather than its `__require` shim) because expo-notifications is\n // listed in `external` in tsup.config.ts — Metro's static analyzer\n // needs the literal call site to bundle the module.\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n return require(\"expo-notifications\") as ExpoNotificationsModule;\n } catch {\n return null;\n }\n}\n\nexport type PermissionStatus =\n | \"granted\"\n | \"denied\"\n | \"undetermined\"\n | \"unknown\";\n\nexport interface UseNotificationsOptions {\n /** Truth API base URL, e.g. https://app.truth.communication-hub.com */\n apiBaseUrl: string;\n /** `hn_live_*` API key for the caller's application. */\n apiKey: string;\n /** Current user id — used when registering the device. */\n userId: string | null | undefined;\n /** Optional app version string stored on the device row. */\n appVersion?: string;\n /**\n * Auto-register on mount when permission is already granted.\n * Default: true. Set false if you want to control the registration\n * lifecycle yourself.\n */\n autoRegister?: boolean;\n /** VAPID public key for web push. Fetched automatically if omitted. */\n vapidPublicKey?: string;\n /** Path to the service worker file. Default: \"/truth-sw.js\" */\n serviceWorkerPath?: string;\n /**\n * iOS only — which APNs endpoint the device's token will be valid\n * for. Determined by the build's `aps-environment` entitlement\n * (development ⇒ sandbox, production ⇒ production). Detect at the\n * call site via\n * `Application.getIosPushNotificationServiceEnvironmentAsync()`\n * from `expo-application` and pass the normalised value here. The\n * SDK can't read entitlements itself without taking on a native\n * peer dep. Server falls back to the app-level default if omitted.\n */\n apnsEnvironment?: \"sandbox\" | \"production\";\n}\n\nexport interface UseNotificationsResult {\n permissionStatus: PermissionStatus;\n devicePushToken: string | null;\n register: () => Promise<{ ok: boolean; reason?: string }>;\n unregister: () => Promise<void>;\n addReceivedListener: (listener: (n: unknown) => void) => () => void;\n addResponseListener: (listener: (r: unknown) => void) => () => void;\n getBadgeCount: () => Promise<number>;\n setBadgeCount: (count: number) => Promise<void>;\n}\n\nexport function useNotifications(\n options: UseNotificationsOptions,\n): UseNotificationsResult {\n const [permissionStatus, setPermissionStatus] =\n useState<PermissionStatus>(\"unknown\");\n const [devicePushToken, setDevicePushToken] = useState<string | null>(null);\n const expoRef = useRef<ExpoNotificationsModule | null>(null);\n const isWebRef = useRef(false);\n const vapidKeyRef = useRef<string | null>(options.vapidPublicKey ?? null);\n\n useEffect(() => {\n let mounted = true;\n void (async () => {\n const expo = await loadExpo();\n if (!mounted) {\n return;\n }\n expoRef.current = expo;\n\n if (expo) {\n try {\n const perm = await expo.getPermissionsAsync();\n if (!mounted) {\n return;\n }\n setPermissionStatus(mapStatus(perm?.status));\n } catch {\n setPermissionStatus(\"unknown\");\n }\n return;\n }\n\n if (isWebPushSupported()) {\n isWebRef.current = true;\n if (!vapidKeyRef.current) {\n try {\n const res = await fetch(\n `${options.apiBaseUrl}/api/notifications/vapid-key`,\n {\n headers: {\n Accept: \"application/json\",\n \"X-API-Key\": options.apiKey,\n },\n },\n );\n if (res.ok) {\n const data = await res.json();\n vapidKeyRef.current = data.vapidPublicKey ?? null;\n }\n } catch {\n // best-effort\n }\n }\n if (!mounted) {\n return;\n }\n const webPerm =\n typeof Notification !== \"undefined\"\n ? Notification.permission\n : \"default\";\n setPermissionStatus(\n webPerm === \"granted\"\n ? \"granted\"\n : webPerm === \"denied\"\n ? \"denied\"\n : \"undetermined\",\n );\n } else {\n setPermissionStatus(\"unknown\");\n }\n })();\n return () => {\n mounted = false;\n };\n }, [options.apiBaseUrl, options.apiKey]);\n\n const register = useCallback(async () => {\n if (!options.userId) {\n return { ok: false, reason: \"missing_userId\" };\n }\n\n // Web push path\n if (isWebRef.current) {\n const vapidKey = vapidKeyRef.current;\n if (!vapidKey) {\n return { ok: false, reason: \"no_vapid_key\" };\n }\n\n const webPerm = await Notification.requestPermission();\n setPermissionStatus(\n webPerm === \"granted\"\n ? \"granted\"\n : webPerm === \"denied\"\n ? \"denied\"\n : \"undetermined\",\n );\n if (webPerm !== \"granted\") {\n return { ok: false, reason: \"permission_denied\" };\n }\n\n try {\n const swPath = options.serviceWorkerPath ?? \"/truth-sw.js\";\n const registration = await registerServiceWorker(swPath);\n await navigator.serviceWorker.ready;\n const subscription = await subscribeToPush(registration, vapidKey);\n const subJSON = subscriptionToJSON(subscription);\n setDevicePushToken(subscription.endpoint);\n\n const res = await fetch(\n `${options.apiBaseUrl}/api/notifications/devices/register`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": options.apiKey,\n },\n body: JSON.stringify({\n userId: options.userId,\n platform: \"web\",\n webPushSubscription: subJSON,\n appVersion: options.appVersion,\n locale: navigator.language,\n timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,\n }),\n },\n );\n if (!res.ok) {\n const text = await res.text().catch(() => \"\");\n return {\n ok: false,\n reason: `register_failed_${res.status}: ${text.slice(0, 120)}`,\n };\n }\n return { ok: true };\n } catch (err) {\n return {\n ok: false,\n reason: `web_push_error: ${err instanceof Error ? err.message : String(err)}`,\n };\n }\n }\n\n // Native (expo-notifications) path\n const expo = expoRef.current ?? (await loadExpo());\n expoRef.current = expo;\n if (!expo) {\n return { ok: false, reason: \"expo_notifications_missing\" };\n }\n\n let perm = await expo.getPermissionsAsync();\n if (perm?.status !== \"granted\") {\n perm = await expo.requestPermissionsAsync();\n }\n setPermissionStatus(mapStatus(perm?.status));\n if (perm?.status !== \"granted\") {\n return { ok: false, reason: \"permission_denied\" };\n }\n\n const tokenResp = await expo.getDevicePushTokenAsync();\n const nativeToken = tokenResp?.data;\n const platform = detectPlatform(tokenResp?.type);\n if (!nativeToken || (platform !== \"ios\" && platform !== \"android\")) {\n return { ok: false, reason: \"no_native_token\" };\n }\n\n setDevicePushToken(nativeToken);\n\n const res = await fetch(\n `${options.apiBaseUrl}/api/notifications/devices/register`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": options.apiKey,\n },\n body: JSON.stringify({\n userId: options.userId,\n platform,\n nativeToken,\n appVersion: options.appVersion,\n locale:\n typeof navigator !== \"undefined\" ? navigator.language : undefined,\n timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,\n ...(platform === \"ios\" && options.apnsEnvironment\n ? { apnsEnvironment: options.apnsEnvironment }\n : {}),\n }),\n },\n );\n if (!res.ok) {\n const text = await res.text().catch(() => \"\");\n return {\n ok: false,\n reason: `register_failed_${res.status}: ${text.slice(0, 120)}`,\n };\n }\n return { ok: true };\n }, [\n options.apiBaseUrl,\n options.apiKey,\n options.userId,\n options.appVersion,\n options.serviceWorkerPath,\n ]);\n\n const unregister = useCallback(async () => {\n if (!devicePushToken) {\n return;\n }\n await fetch(`${options.apiBaseUrl}/api/notifications/devices/unregister`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": options.apiKey,\n },\n body: JSON.stringify({ nativeToken: devicePushToken }),\n }).catch(() => {\n /* non-fatal */\n });\n setDevicePushToken(null);\n }, [options.apiBaseUrl, options.apiKey, devicePushToken]);\n\n const addReceivedListener = useCallback(\n (listener: (n: unknown) => void): (() => void) => {\n if (isWebRef.current) {\n if (\n typeof navigator === \"undefined\" ||\n !(\"serviceWorker\" in navigator)\n ) {\n return () => {};\n }\n const handler = (event: MessageEvent) => {\n if (event.data?.type === \"TRUTH_PUSH_RECEIVED\") {\n listener(event.data.payload);\n }\n };\n navigator.serviceWorker.addEventListener(\"message\", handler);\n return () =>\n navigator.serviceWorker.removeEventListener(\"message\", handler);\n }\n const expo = expoRef.current;\n if (!expo?.addNotificationReceivedListener) {\n return () => {};\n }\n const sub = expo.addNotificationReceivedListener(listener);\n return () => sub.remove?.();\n },\n [],\n );\n\n const addResponseListener = useCallback(\n (listener: (r: unknown) => void): (() => void) => {\n if (isWebRef.current) {\n if (\n typeof navigator === \"undefined\" ||\n !(\"serviceWorker\" in navigator)\n ) {\n return () => {};\n }\n const handler = (event: MessageEvent) => {\n if (event.data?.type === \"TRUTH_PUSH_TAPPED\") {\n listener(event.data.payload);\n }\n };\n navigator.serviceWorker.addEventListener(\"message\", handler);\n return () =>\n navigator.serviceWorker.removeEventListener(\"message\", handler);\n }\n const expo = expoRef.current;\n if (!expo?.addNotificationResponseReceivedListener) {\n return () => {};\n }\n const sub = expo.addNotificationResponseReceivedListener(listener);\n return () => sub.remove?.();\n },\n [],\n );\n\n const getBadgeCount = useCallback(async (): Promise<number> => {\n const expo = expoRef.current;\n if (!expo?.getBadgeCountAsync) {\n return 0;\n }\n return (await expo.getBadgeCountAsync()) ?? 0;\n }, []);\n\n const setBadgeCount = useCallback(async (count: number): Promise<void> => {\n const expo = expoRef.current;\n if (!expo?.setBadgeCountAsync) {\n return;\n }\n await expo.setBadgeCountAsync(count);\n }, []);\n\n // Auto-register once on mount when conditions are met\n const autoRegister = options.autoRegister !== false;\n useEffect(() => {\n if (!autoRegister) {\n return;\n }\n if (permissionStatus !== \"granted\") {\n return;\n }\n if (devicePushToken) {\n return;\n }\n if (!options.userId) {\n return;\n }\n void register();\n }, [\n autoRegister,\n permissionStatus,\n devicePushToken,\n options.userId,\n register,\n ]);\n\n return {\n permissionStatus,\n devicePushToken,\n register,\n unregister,\n addReceivedListener,\n addResponseListener,\n getBadgeCount,\n setBadgeCount,\n };\n}\n\nfunction mapStatus(status: string | undefined): PermissionStatus {\n if (status === \"granted\") {\n return \"granted\";\n }\n if (status === \"denied\") {\n return \"denied\";\n }\n if (status === \"undetermined\") {\n return \"undetermined\";\n }\n return \"unknown\";\n}\n\nfunction detectPlatform(\n tokenType: string | undefined,\n): \"ios\" | \"android\" | \"web\" | \"unknown\" {\n // expo-notifications >= 0.27 returns \"ios\" / \"android\" / \"web\"\n // directly. Older releases (and Firebase docs) used \"apns\" / \"fcm\";\n // accept both for back-compat with apps still on old SDKs.\n if (tokenType === \"ios\" || tokenType === \"apns\") {\n return \"ios\";\n }\n if (tokenType === \"android\" || tokenType === \"fcm\") {\n return \"android\";\n }\n if (tokenType === \"web\") {\n return \"web\";\n }\n return \"unknown\";\n}\n","/**\n * Web Push helpers for browser environments.\n *\n * Handles service worker registration, VAPID push subscription, and\n * message forwarding from the service worker to the main thread.\n */\n\nexport interface WebPushConfig {\n vapidPublicKey: string;\n serviceWorkerPath?: string;\n}\n\nexport function isWebPushSupported(): boolean {\n return (\n typeof window !== \"undefined\" &&\n \"serviceWorker\" in navigator &&\n \"PushManager\" in window\n );\n}\n\nexport async function registerServiceWorker(\n path = \"/truth-sw.js\",\n): Promise<ServiceWorkerRegistration> {\n return navigator.serviceWorker.register(path);\n}\n\nexport async function subscribeToPush(\n registration: ServiceWorkerRegistration,\n vapidPublicKey: string,\n): Promise<PushSubscription> {\n const existing = await registration.pushManager.getSubscription();\n if (existing) {\n return existing;\n }\n\n return registration.pushManager.subscribe({\n userVisibleOnly: true,\n applicationServerKey: urlBase64ToUint8Array(\n vapidPublicKey,\n ) as unknown as ArrayBuffer,\n });\n}\n\nexport function subscriptionToJSON(sub: PushSubscription): {\n endpoint: string;\n keys: { p256dh: string; auth: string };\n} {\n const json = sub.toJSON();\n return {\n endpoint: sub.endpoint,\n keys: {\n p256dh: json.keys?.p256dh ?? \"\",\n auth: json.keys?.auth ?? \"\",\n },\n };\n}\n\nfunction urlBase64ToUint8Array(base64String: string): Uint8Array {\n const padding = \"=\".repeat((4 - (base64String.length % 4)) % 4);\n const base64 = (base64String + padding).replace(/-/g, \"+\").replace(/_/g, \"/\");\n const rawData = atob(base64);\n const outputArray = new Uint8Array(rawData.length);\n for (let i = 0; i < rawData.length; ++i) {\n outputArray[i] = rawData.charCodeAt(i);\n }\n return outputArray;\n}\n\nexport function onServiceWorkerMessage(\n type: string,\n callback: (payload: unknown) => void,\n): () => void {\n if (typeof navigator === \"undefined\" || !(\"serviceWorker\" in navigator)) {\n return () => {};\n }\n const handler = (event: MessageEvent) => {\n if (event.data?.type === type) {\n callback(event.data.payload);\n }\n };\n navigator.serviceWorker.addEventListener(\"message\", handler);\n return () => navigator.serviceWorker.removeEventListener(\"message\", handler);\n}\n","/**\n * React hook for patient family-member lookup — Truth SDK.\n *\n * Replaces the CommHub Hasura `useFamilyMembersQuery` (backed by\n * `queries/family_members.graphql`). Queries the Convex\n * `patients:listFamilyMembers` function, which returns the set of\n * patients that share a `familyId` OR share at least one phone number\n * with the reference patient, excluding the reference patient themselves.\n *\n * Arguments mirror the Hasura `FamilyMembers` GraphQL query variables:\n * - `familyId` → `$familyId` (Hint family/account id)\n * - `phoneNumbers` → `$phoneNumbers` (patient's phone numbers)\n * - `excludeHintId` → `$excludePatientId` (skip the reference patient)\n *\n * Must be used within a `<TruthProvider />`.\n *\n * @example\n * ```tsx\n * import { usePatientFamilyMembers } from '@hipnation-truth/sdk/react/patient-family';\n *\n * function FamilyPanel({ hintId, familyId, phones }: Props) {\n * const { data: members, loading } = usePatientFamilyMembers({\n * familyId,\n * phoneNumbers: phones,\n * excludeHintId: hintId,\n * });\n * if (loading) return <Spinner />;\n * return members?.map((m) => <div key={m._id}>{m.firstName} {m.lastName}</div>);\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { UseQueryResult } from \"./conversations\";\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\n/**\n * Shape of a single patient row returned by `patients:listFamilyMembers`.\n * Mirrors the fields CommHub's `FamilyMembersQuery` selected from Hasura:\n * - `id` → `_id` (Convex document id)\n * - `name` → `firstName + \" \" + lastName`\n * - `hint_id` → `hintId`\n * - `elation_id` → `elationId`\n * - `family_id` → `familyId`\n * - `phone_numbers` → `phones[].number`\n */\nexport interface FamilyMemberRow {\n _id: string;\n _creationTime: number;\n firstName: string;\n lastName: string;\n elationId?: string;\n hintId?: string;\n familyId?: string;\n phones: Array<{ type?: string; number: string }>;\n membershipStatus?: string;\n sources: string[];\n lastSyncedAt: string;\n}\n\n/** Arguments for `usePatientFamilyMembers`. */\nexport interface UsePatientFamilyMembersInput {\n /**\n * Hint family/account id — when present, all patients sharing this id\n * are returned (matches the Hasura `family_id` column).\n *\n * BACKFILL NEEDED: `familyId` was added to the Convex `patients` schema\n * in this PR. Existing patient rows will return empty for this branch\n * until the Hint patient upsert path (upsertFromHint / basic-refresh)\n * is updated to write this field, or a one-time backfill script runs.\n * The phone-number fallback continues to work in the interim.\n */\n familyId?: string | null;\n /**\n * Patient's phone numbers — patients sharing at least one phone number\n * are included as family members (mirrors Hasura `phone_numbers._overlap`).\n */\n phoneNumbers?: string[] | null;\n /**\n * Hint patient ID of the reference patient to exclude from results\n * (mirrors Hasura `$excludePatientId`).\n */\n excludeHintId?: string | null;\n}\n\n// ---------------------------------------------------------------------------\n// Convex function reference\n// ---------------------------------------------------------------------------\n\nconst patientsFamilyMembersRef = makeFunctionReference<\n \"query\",\n {\n familyId?: string;\n phoneNumbers?: string[];\n excludeHintId?: string;\n },\n FamilyMemberRow[]\n>(\"patients:listFamilyMembers\");\n\n// ---------------------------------------------------------------------------\n// Skip sentinel\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Subscribe to family members of a patient in real time.\n *\n * Returns all patients that share the same `familyId` OR share at least one\n * phone number with the reference patient, sorted by name. The reference\n * patient is excluded via `excludeHintId`.\n *\n * Pass `undefined` (or an object where all fields are undefined/null) to\n * skip the query — returns `{ data: undefined, loading: false }`.\n *\n * @param input - Query inputs. The query is skipped if `input` is undefined\n * or all fields are falsy.\n */\nfunction usePatientFamilyMembers(\n input: UsePatientFamilyMembersInput | undefined,\n): UseQueryResult<FamilyMemberRow[]> {\n const hasFamilyId = !!input?.familyId;\n const hasPhoneNumbers = !!(input?.phoneNumbers && input.phoneNumbers.length > 0);\n const shouldQuery = hasFamilyId || hasPhoneNumbers;\n\n const args = shouldQuery\n ? {\n ...(input?.familyId ? { familyId: input.familyId } : {}),\n ...(input?.phoneNumbers && input.phoneNumbers.length > 0\n ? { phoneNumbers: input.phoneNumbers }\n : {}),\n ...(input?.excludeHintId ? { excludeHintId: input.excludeHintId } : {}),\n }\n : SKIP;\n\n const result = useQuery(\n patientsFamilyMembersRef as FunctionReference<\"query\">,\n args,\n ) as FamilyMemberRow[] | undefined;\n\n if (!shouldQuery) {\n return { data: undefined, loading: false, error: undefined };\n }\n\n return {\n data: result,\n loading: result === undefined,\n error: undefined,\n };\n}\n\nexport { usePatientFamilyMembers };\n","/**\n * React hook for Truth SDK — reactive patient search.\n *\n * Wraps the `patients:search` Convex query so CommHub consumers\n * (and other frontends) can search patients by name, phone, or email\n * with live-updating results and optional office scoping, without\n * managing Convex subscriptions themselves.\n *\n * **Hook contract:** returns `UseQueryResult<PatientSearchResult[]>`.\n * - `data` is `undefined` while loading or when the query is skipped\n * (empty `query` string or `query === undefined`).\n * - `loading` is `true` only while a real subscription is in-flight.\n * - `error` is reserved for SDK-side validation; Convex query errors\n * propagate as React errors and should be caught with an error\n * boundary.\n *\n * **Backed by `patients:search`** — added in agent-A-search.\n * Multi-word queries split on whitespace; every token must match at\n * least one of firstName / lastName / email / phone digits.\n *\n * Must be used within `<TruthProvider />` (see `./provider`).\n *\n * @example\n * ```tsx\n * import { usePatientSearch } from '@hipnation-truth/sdk/react/patient-search';\n *\n * function PatientPicker({ officeId }: { officeId?: string }) {\n * const [query, setQuery] = useState('');\n * const { data: patients, loading } = usePatientSearch({ query, officeId });\n * return (\n * <>\n * <input value={query} onChange={e => setQuery(e.target.value)} />\n * {loading && <Spinner />}\n * {patients?.map(p => <PatientRow key={p.id} patient={p} />)}\n * </>\n * );\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { UseQueryResult } from \"./conversations\";\n\n// ---------------------------------------------------------------------------\n// Return shape\n// ---------------------------------------------------------------------------\n\n/**\n * A single patient result from `patients:search`.\n *\n * Fields are normalised across the `patients` + `hintPatients` tables\n * so callers get a flat, consistent object regardless of which source\n * contributed the match.\n */\nexport interface PatientSearchResult {\n /** Convex `_id` of the matching `patients` or `hintPatients` row. */\n id: string;\n /** Hint patient id — present when the row originates from Hint. */\n hintId: string | undefined;\n firstName: string;\n lastName: string;\n /** ISO date string (YYYY-MM-DD) — undefined when not available. */\n dob: string | undefined;\n /** Email addresses associated with the patient. */\n emails: string[];\n /**\n * Raw phone strings from the source table. Callers should\n * strip non-digits for dialling; the first entry is the primary\n * contact number.\n */\n phones: string[];\n /** Hint office id — present for Hint-sourced results. */\n officeId: string | undefined;\n /** Human-readable office name — present when officeId is set. */\n officeName: string | undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Options\n// ---------------------------------------------------------------------------\n\nexport interface UsePatientSearchOptions {\n /**\n * The search term entered by the user. Multi-word queries are split\n * on whitespace and every token must match. Pass `\"\"` or `undefined`\n * to skip the query and get `{ data: undefined, loading: false }`.\n */\n query: string | undefined;\n /**\n * When supplied, restricts results to patients whose authoritative\n * Hint office id matches. Maps to the `officeId` arg on the Convex\n * query and uses the `hintPatients.by_officeId` index path.\n */\n officeId?: string | null;\n /**\n * `\"fuzzy\"` (default) uses Convex full-text search indexes for name\n * queries; `\"exact\"` uses a bounded scan-filter only. Most callers\n * should leave this at the default.\n */\n mode?: \"fuzzy\" | \"exact\";\n /** Maximum number of results to return (server caps at 200). */\n limit?: number;\n}\n\n// ---------------------------------------------------------------------------\n// Convex function reference\n// ---------------------------------------------------------------------------\n\n// String-keyed reference to `patients:search` — decoupled from the\n// consuming app's generated `convex/_generated/api`.\nconst patientsSearchRef = makeFunctionReference<\n \"query\",\n {\n query: string;\n officeId?: string;\n limit?: number;\n mode?: \"fuzzy\" | \"exact\";\n },\n PatientSearchResult[]\n>(\"patients:search\");\n\n// ---------------------------------------------------------------------------\n// Skipped-query sentinel\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Subscribe to a live patient search backed by `patients:search` on the\n * Truth Convex deployment.\n *\n * Results update reactively whenever the underlying `patients` or\n * `hintPatients` tables change — no polling required.\n *\n * @param options - Search options (see `UsePatientSearchOptions`).\n * @returns `UseQueryResult<PatientSearchResult[]>` — always defined\n * once a non-empty `query` is provided and the subscription resolves.\n */\nexport function usePatientSearch(\n options: UsePatientSearchOptions,\n): UseQueryResult<PatientSearchResult[]> {\n const trimmedQuery = (options.query ?? \"\").trim();\n const skipped = trimmedQuery.length === 0;\n\n const result = useQuery(\n patientsSearchRef as FunctionReference<\"query\">,\n skipped\n ? SKIP\n : {\n query: trimmedQuery,\n ...(options.officeId ? { officeId: options.officeId } : {}),\n ...(options.mode ? { mode: options.mode } : {}),\n ...(options.limit !== undefined ? { limit: options.limit } : {}),\n },\n ) as PatientSearchResult[] | undefined;\n\n if (skipped) {\n return { data: undefined, loading: false, error: undefined };\n }\n\n return {\n data: result,\n loading: result === undefined,\n error: undefined,\n };\n}\n","/**\n * Bulk patient lookup by Convex ids.\n *\n * Backs the inbox card render in CommHub — one reactive subscription\n * resolves names + office + EHR ids for the entire visible list\n * instead of N per-row queries.\n *\n * Must be used within `<TruthProvider />`.\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport { useMemo } from \"react\";\nimport type { Patient } from \"../types/patient\";\nimport type { UseQueryResult } from \"./conversations\";\n\nconst patientsGetByIdsRef = makeFunctionReference<\n \"query\",\n { ids: string[] },\n Patient[]\n>(\"patients:getByIds\");\n\nconst patientsGetByPhonesRef = makeFunctionReference<\n \"query\",\n { phoneDigits: string[] },\n Array<{ phoneDigits: string; patient: Patient }>\n>(\"patients:getByPhones\");\n\nconst SKIP = \"skip\" as const;\n\n/**\n * Subscribe to a list of patients by their Convex ids. Returns a\n * `Record<patientId, Patient>` for O(1) lookup at render time.\n *\n * Pass an empty array (or `undefined`) to skip the query. Missing ids\n * are absent from the map (silently dropped server-side).\n */\nexport function usePatientsByIds(\n ids: string[] | null | undefined,\n): UseQueryResult<Record<string, Patient>> {\n const stableIds = useMemo(() => {\n const arr = ids ?? [];\n return [...new Set(arr)].sort();\n }, [ids]);\n const skipped = stableIds.length === 0;\n\n const result = useQuery(\n patientsGetByIdsRef as FunctionReference<\"query\">,\n skipped ? SKIP : { ids: stableIds },\n ) as Patient[] | undefined;\n\n const mapped = useMemo(() => {\n if (result === undefined) return undefined;\n return Object.fromEntries(\n result.map((p) => {\n const id =\n (p as { id?: string; _id?: string }).id ??\n (p as { _id?: string })._id ??\n \"\";\n return [String(id), p];\n }),\n );\n }, [result]);\n\n if (skipped) {\n return { data: {}, loading: false, error: undefined };\n }\n return {\n data: mapped,\n loading: mapped === undefined,\n error: undefined,\n };\n}\n\n/**\n * Bulk patient lookup by phone numbers (digits or formatted). Returns\n * `Record<phoneDigits, Patient>` for O(1) lookup by digits-only key.\n */\nexport function usePatientsByPhones(\n phones: string[] | null | undefined,\n): UseQueryResult<Record<string, Patient>> {\n const stableDigits = useMemo(() => {\n const arr = phones ?? [];\n const digits = arr\n .map((p) => p.replace(/\\D+/g, \"\"))\n .filter((s) => s.length > 0);\n return [...new Set(digits)].sort();\n }, [phones]);\n const skipped = stableDigits.length === 0;\n\n const result = useQuery(\n patientsGetByPhonesRef as FunctionReference<\"query\">,\n skipped ? SKIP : { phoneDigits: stableDigits },\n ) as Array<{ phoneDigits: string; patient: Patient }> | undefined;\n\n const mapped = useMemo(() => {\n if (result === undefined) return undefined;\n return Object.fromEntries(result.map((r) => [r.phoneDigits, r.patient]));\n }, [result]);\n\n if (skipped) {\n return { data: {}, loading: false, error: undefined };\n }\n return {\n data: mapped,\n loading: mapped === undefined,\n error: undefined,\n };\n}\n","/**\n * TruthProvider — Convex React provider pre-configured for Truth.\n *\n * Wraps children in a ConvexProvider connected to the correct\n * Truth Convex deployment for the given environment.\n *\n * @example\n * ```tsx\n * import { TruthProvider } from '@hipnation-truth/sdk/react';\n *\n * function App() {\n * return (\n * <TruthProvider environment=\"sandbox\">\n * <MyApp />\n * </TruthProvider>\n * );\n * }\n * ```\n */\n\n\"use client\";\n\nimport { ConvexProvider, ConvexReactClient } from \"convex/react\";\nimport type { ReactNode } from \"react\";\nimport { createElement, useMemo } from \"react\";\n\n// Mirrors CONVEX_URLS in client.ts. UAT shares prod resources.\n//\n// Exported so `resolveConvexUrl` can be unit-tested without booting\n// React — a regression in this map shipped SDK 0.4.1 that pointed\n// UAT at sandbox Convex. The test in provider.test.ts locks every\n// stage → URL pair down.\nexport const CONVEX_URLS: Record<string, string> = {\n local: \"https://courteous-duck-623.convex.cloud\",\n staging: \"https://courteous-duck-623.convex.cloud\",\n stg: \"https://courteous-duck-623.convex.cloud\",\n sandbox: \"https://courteous-duck-623.convex.cloud\",\n uat: \"https://gallant-gecko-217.convex.cloud\",\n production: \"https://gallant-gecko-217.convex.cloud\",\n};\n\n/**\n * Resolve the Convex URL for a given environment. Honors an explicit\n * override and falls back to sandbox for unknown environments.\n */\nexport function resolveConvexUrl(\n environment: string | undefined,\n override?: string,\n): string {\n if (override) {\n return override;\n }\n const env = environment ?? \"sandbox\";\n return CONVEX_URLS[env] ?? CONVEX_URLS.sandbox;\n}\n\ninterface TruthProviderProps {\n /** Truth environment — determines which Convex deployment to connect to */\n environment?: string;\n /** Override the Convex URL directly */\n convexUrl?: string;\n children: ReactNode;\n}\n\nfunction TruthProvider({\n environment = \"sandbox\",\n convexUrl,\n children,\n}: TruthProviderProps) {\n const url = resolveConvexUrl(environment, convexUrl);\n\n const client = useMemo(() => new ConvexReactClient(url), [url]);\n\n return createElement(ConvexProvider, { client }, children);\n}\n\nexport { TruthProvider };\nexport type { TruthProviderProps };\n","/**\n * React hooks for conversation reminders — bulk lookup keyed by\n * conversation id. Wraps the public `reminders:listPendingByConversationIds`\n * Convex query so CommHub's inbox can render the reminder clock icon\n * with one reactive subscription for the entire visible list.\n *\n * Must be used within `<TruthProvider />` (see `./provider`).\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { Reminder } from \"../types/reminder\";\nimport type { UseQueryResult } from \"./conversations\";\n\nconst remindersListPendingByConversationIdsRef = makeFunctionReference<\n \"query\",\n { conversationIds: string[] },\n Reminder[]\n>(\"reminders:listPendingByConversationIds\");\n\nconst SKIP = \"skip\" as const;\n\n/**\n * Subscribe to the latest pending reminder for each of the given\n * conversation ids. Returns a `Record<conversationId, Reminder>` —\n * conversations with no pending reminder are absent from the map.\n *\n * Pass an empty array (or `undefined`) to skip the query — useful while\n * the inbox list is still loading.\n *\n * @example\n * ```tsx\n * const ids = conversations.map((c) => c.id);\n * const { data: remindersByConv } = useRemindersForConversations(ids);\n * const r = remindersByConv?.[conv.id]; // Reminder | undefined\n * ```\n */\nexport function useRemindersForConversations(\n conversationIds: string[] | null | undefined,\n): UseQueryResult<Record<string, Reminder>> {\n const ids = conversationIds ?? [];\n const skipped = ids.length === 0;\n\n const result = useQuery(\n remindersListPendingByConversationIdsRef as FunctionReference<\"query\">,\n skipped ? SKIP : { conversationIds: ids },\n ) as Reminder[] | undefined;\n\n const mapped =\n result === undefined\n ? undefined\n : Object.fromEntries(result.map((r) => [r.conversationId, r]));\n\n if (skipped) {\n return { data: {}, loading: false, error: undefined };\n }\n return {\n data: mapped,\n loading: mapped === undefined,\n error: undefined,\n };\n}\n","/**\n * React hooks for conversation tasks (My Tasks surface).\n *\n * Provides a reactive `useMutation` wrapper around the\n * `conversationTasks:markSeen` Convex mutation so CommHub can replace\n * `useMark_Event_Activity_SeenMutation` from `@/generated/graphql`\n * with a Truth SDK equivalent without touching `react.ts` exports (the\n * central agent wires those).\n *\n * Pattern mirrors the existing hooks in `./conversations.ts`:\n * - `makeFunctionReference` for decoupled Convex references\n * - SKIP sentinel for conditional execution\n * - Returns a stable callback, not a live subscription\n *\n * Must be used within `<TruthProvider />`.\n */\n\n\"use client\";\n\nimport { useMutation } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport { useCallback } from \"react\";\n\n// ---------------------------------------------------------------------------\n// Convex mutation reference — decoupled from the consuming app's codegen\n// ---------------------------------------------------------------------------\n\nconst conversationTasksMarkSeenRef = makeFunctionReference<\n \"mutation\",\n { taskId: string; userId: string },\n { ok: true }\n>(\"conversationTasks:markSeen\");\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Returns a stable `markSeen(taskId, userId)` callback backed by the\n * `conversationTasks:markSeen` Convex mutation. Replaces the Hasura\n * `useMark_Event_Activity_SeenMutation` in CommHub's My Tasks surface.\n *\n * @example\n * ```tsx\n * const markSeen = useConversationTaskMarkSeen();\n *\n * // When a task row is pressed:\n * if (!task.seenBy.includes(userId)) {\n * markSeen(task.id, userId).catch(console.error);\n * }\n * ```\n */\nfunction useConversationTaskMarkSeen(): (\n taskId: string,\n userId: string,\n) => Promise<{ ok: true }> {\n const mutate = useMutation(\n conversationTasksMarkSeenRef as FunctionReference<\"mutation\">,\n );\n\n return useCallback(\n (taskId: string, userId: string) =>\n mutate({ taskId, userId }) as Promise<{ ok: true }>,\n [mutate],\n );\n}\n\nexport { useConversationTaskMarkSeen };\n","/**\n * React tracking hook — fire-and-forget event tracking from client components.\n *\n * Creates a lightweight Tracker instance scoped to the browser session\n * and exposes a useTrack() hook for emitting events to Truth's Kinesis stream.\n *\n * @example\n * ```tsx\n * import { useTruth } from '@hipnation-truth/sdk/react';\n *\n * function ConversationView({ conversationId }) {\n * const truth = useTruth();\n *\n * useEffect(() => {\n * truth.track('conversation.marked_read.v1', {\n * read_by_actor_id: userId,\n * unread_count_before: 5,\n * unread_count_after: 0,\n * }, { subject: { conversation_id: conversationId } });\n * }, [conversationId]);\n * }\n * ```\n */\n\n\"use client\";\n\nimport type { ReactNode } from \"react\";\nimport { createContext, createElement, useContext, useMemo } from \"react\";\nimport type {\n EventPayloadMap,\n EventType,\n TrackOptions,\n} from \"../tracking/events\";\nimport { Tracker } from \"../tracking/tracker\";\nimport type { ActorContext } from \"../types/config\";\n\n// ---------------------------------------------------------------------------\n// Context\n// ---------------------------------------------------------------------------\n\ninterface TruthTrackingContextValue {\n track: <T extends EventType>(\n eventType: T,\n payload: EventPayloadMap[T],\n options?: TrackOptions,\n ) => void;\n identify: (actorId: string, actorType: ActorContext[\"actorType\"]) => void;\n}\n\nconst TruthTrackingContext = createContext<TruthTrackingContextValue | null>(\n null,\n);\n\n// ---------------------------------------------------------------------------\n// Provider\n// ---------------------------------------------------------------------------\n\ninterface TruthTrackingProviderProps {\n /** Truth environment — determines API URL for event delivery */\n environment?: string;\n /** Event source identifier */\n source?: string;\n /** Source version (git SHA) */\n sourceVersion?: string;\n /** Default tenant ID */\n tenantId?: string;\n /** API key for authentication */\n apiKey?: string;\n children: ReactNode;\n}\n\nfunction TruthTrackingProvider({\n environment = \"sandbox\",\n source = \"communication-hub.frontend\",\n sourceVersion = \"unknown\",\n tenantId = \"hipnation\",\n apiKey = \"\",\n children,\n}: TruthTrackingProviderProps) {\n const value = useMemo<TruthTrackingContextValue>(() => {\n const tracker = new Tracker({\n apiKey,\n environment,\n source,\n sourceVersion,\n tenantId,\n batchSize: 10,\n flushIntervalMs: 5000,\n });\n\n return {\n track: (eventType, payload, options) => {\n tracker.track(eventType, payload, options);\n },\n identify: (actorId, actorType) => {\n tracker.setActor({ actorId, actorType });\n },\n };\n }, [apiKey, environment, source, sourceVersion, tenantId]);\n\n return createElement(TruthTrackingContext.Provider, { value }, children);\n}\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Access the Truth tracking context. Must be within a TruthTrackingProvider.\n * Returns `{ track, identify }` for emitting events to Kinesis.\n */\nfunction useTruth(): TruthTrackingContextValue {\n const ctx = useContext(TruthTrackingContext);\n if (!ctx) {\n throw new Error(\"useTruth must be used within a TruthTrackingProvider\");\n }\n return ctx;\n}\n\nexport { TruthTrackingProvider, useTruth };\nexport type { TruthTrackingProviderProps, TruthTrackingContextValue };\n","/**\n * Event tracker with batching, retry, and flush support.\n *\n * Buffers events in an internal queue and flushes them to the Truth API\n * endpoint. Flushes occur when the buffer reaches `batchSize` or every\n * `flushIntervalMs` milliseconds, whichever comes first.\n */\n\nimport type { ActorContext } from \"../types/config\";\nimport type {\n EventEnvelope,\n EventPayloadMap,\n EventType,\n TrackOptions,\n} from \"./events\";\n\n// ---------------------------------------------------------------------------\n// UUID v7 helper (no external dependency)\n// ---------------------------------------------------------------------------\n\n/**\n * Generates a UUID v7 string. Uses crypto.randomUUID where available,\n * falling back to a timestamp + random bytes implementation.\n *\n * UUID v7 layout (RFC 9562):\n * 48 bits - unix timestamp (ms)\n * 4 bits - version (0b0111)\n * 12 bits - random\n * 2 bits - variant (0b10)\n * 62 bits - random\n */\nfunction generateUuidV7(): string {\n const now = Date.now();\n\n // 6 bytes of timestamp\n const timeBytes = new Uint8Array(6);\n let ts = now;\n for (let i = 5; i >= 0; i--) {\n timeBytes[i] = ts & 0xff;\n ts = Math.floor(ts / 256);\n }\n\n // 10 bytes of random\n const randomBytes = new Uint8Array(10);\n if (\n typeof globalThis.crypto !== \"undefined\" &&\n globalThis.crypto.getRandomValues\n ) {\n globalThis.crypto.getRandomValues(randomBytes);\n } else {\n for (let i = 0; i < 10; i++) {\n randomBytes[i] = Math.floor(Math.random() * 256);\n }\n }\n\n // Assemble 16 bytes\n const bytes = new Uint8Array(16);\n bytes.set(timeBytes, 0);\n bytes.set(randomBytes, 6);\n\n // Set version (bits 48-51 to 0b0111)\n bytes[6] = (bytes[6] & 0x0f) | 0x70;\n\n // Set variant (bits 64-65 to 0b10)\n bytes[8] = (bytes[8] & 0x3f) | 0x80;\n\n // Format as hex string with dashes\n const hex = Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n\n return [\n hex.slice(0, 8),\n hex.slice(8, 12),\n hex.slice(12, 16),\n hex.slice(16, 20),\n hex.slice(20, 32),\n ].join(\"-\");\n}\n\n// ---------------------------------------------------------------------------\n// Environment-based API URL resolution\n// ---------------------------------------------------------------------------\n\n// Environment → Truth API base URL.\n//\n// Topology:\n// local / staging / stg / sandbox → sandbox Truth (app.sandbox.*)\n// uat / production → production Truth (app.truth.*)\n//\n// UAT shares production resources (same Fly Postgres + Hasura + Truth +\n// Convex + EHR tokens as prod). Staging is the isolated sandbox env.\nconst API_URLS: Record<string, string> = {\n local: \"http://localhost:3000\",\n staging: \"https://app.sandbox.communication-hub.com\",\n stg: \"https://app.sandbox.communication-hub.com\",\n sandbox: \"https://app.sandbox.communication-hub.com\",\n uat: \"https://app.truth.communication-hub.com\",\n production: \"https://app.truth.communication-hub.com\",\n};\n\n// ---------------------------------------------------------------------------\n// Constants\n// ---------------------------------------------------------------------------\n\nconst DEFAULT_BATCH_SIZE = 25;\nconst DEFAULT_FLUSH_INTERVAL_MS = 5_000;\nconst MAX_RETRIES = 3;\nconst BASE_RETRY_DELAY_MS = 500;\n\n// ---------------------------------------------------------------------------\n// Tracker configuration\n// ---------------------------------------------------------------------------\n\ninterface TrackerConfig {\n apiKey: string;\n environment: string;\n source: string;\n sourceVersion: string;\n tenantId: string;\n batchSize: number;\n flushIntervalMs: number;\n apiBaseUrl?: string;\n}\n\n// ---------------------------------------------------------------------------\n// Tracker class\n// ---------------------------------------------------------------------------\n\nclass Tracker {\n private readonly config: TrackerConfig;\n readonly apiUrl: string;\n private queue: EventEnvelope[] = [];\n private flushTimer: ReturnType<typeof setInterval> | null = null;\n private defaultActor: ActorContext | undefined;\n private isFlushing = false;\n private isShutdown = false;\n\n constructor(config: TrackerConfig) {\n this.config = config;\n this.apiUrl =\n config.apiBaseUrl ?? API_URLS[config.environment] ?? API_URLS.local;\n\n this.startFlushInterval();\n this.registerShutdownHooks();\n }\n\n /**\n * Set the default actor context for subsequent events.\n */\n setActor(actor: ActorContext): void {\n this.defaultActor = actor;\n }\n\n /**\n * Enqueue a typed event for delivery. This is fire-and-forget from\n * the caller's perspective -- events are buffered and flushed in batches.\n */\n track<T extends EventType>(\n eventType: T,\n payload: EventPayloadMap[T],\n options?: TrackOptions,\n ): void {\n if (this.isShutdown) {\n return;\n }\n\n const now = new Date().toISOString();\n\n const envelope: EventEnvelope = {\n event_id: generateUuidV7(),\n event_type: eventType,\n schema_version: 1,\n occurred_at: options?.occurredAt ?? now,\n received_at: now,\n source: this.config.source,\n source_version: this.config.sourceVersion,\n tenant_id: options?.tenantId ?? this.config.tenantId,\n actor:\n options?.actor ??\n (this.defaultActor\n ? {\n actor_id: this.defaultActor.actorId,\n actor_type: this.defaultActor.actorType,\n }\n : undefined),\n subject: options?.subject,\n compliance: options?.compliance,\n payload: payload as unknown as Record<string, unknown>,\n };\n\n this.queue.push(envelope);\n\n if (this.queue.length >= this.config.batchSize) {\n void this.flush();\n }\n }\n\n /**\n * Force an immediate flush of all buffered events.\n * Returns a promise that resolves when the flush completes.\n */\n async flush(): Promise<void> {\n if (this.queue.length === 0 || this.isFlushing) {\n return;\n }\n\n this.isFlushing = true;\n const batch = this.queue.splice(0, this.config.batchSize);\n\n try {\n await this.sendBatch(batch);\n } catch {\n // Re-queue events that failed to send (prepend to maintain ordering)\n this.queue.unshift(...batch);\n } finally {\n this.isFlushing = false;\n }\n\n // If there are still events in the queue, flush again\n if (this.queue.length >= this.config.batchSize) {\n await this.flush();\n }\n }\n\n /**\n * Gracefully shut down the tracker. Flushes remaining events and\n * clears the flush interval.\n */\n async shutdown(): Promise<void> {\n this.isShutdown = true;\n this.stopFlushInterval();\n await this.flush();\n }\n\n /**\n * Send a batch of events to the Truth API with exponential backoff retry.\n */\n private async sendBatch(batch: EventEnvelope[]): Promise<void> {\n let lastError: unknown;\n\n for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {\n try {\n const response = await fetch(`${this.apiUrl}/api/events/ingest`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-Key\": this.config.apiKey,\n },\n body: JSON.stringify({ events: batch }),\n });\n\n if (response.ok) {\n return;\n }\n\n // Don't retry 4xx client errors (except 429)\n if (\n response.status >= 400 &&\n response.status < 500 &&\n response.status !== 429\n ) {\n return;\n }\n\n lastError = new Error(\n `HTTP ${response.status}: ${response.statusText}`,\n );\n } catch (error) {\n lastError = error;\n }\n\n // Exponential backoff with jitter\n if (attempt < MAX_RETRIES) {\n const delay = BASE_RETRY_DELAY_MS * 2 ** attempt;\n const jitter = Math.random() * delay * 0.5;\n await sleep(delay + jitter);\n }\n }\n\n throw lastError;\n }\n\n private startFlushInterval(): void {\n if (this.config.flushIntervalMs > 0) {\n this.flushTimer = setInterval(() => {\n void this.flush();\n }, this.config.flushIntervalMs);\n\n // Unref the timer so it doesn't prevent Node.js from exiting\n if (typeof this.flushTimer === \"object\" && \"unref\" in this.flushTimer) {\n this.flushTimer.unref();\n }\n }\n }\n\n private stopFlushInterval(): void {\n if (this.flushTimer !== null) {\n clearInterval(this.flushTimer);\n this.flushTimer = null;\n }\n }\n\n private registerShutdownHooks(): void {\n // Only register in Node.js environments\n if (typeof globalThis.process !== \"undefined\" && globalThis.process.on) {\n const shutdownHandler = () => {\n void this.shutdown();\n };\n\n globalThis.process.on(\"beforeExit\", shutdownHandler);\n globalThis.process.on(\"SIGTERM\", shutdownHandler);\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport {\n Tracker,\n generateUuidV7,\n DEFAULT_BATCH_SIZE,\n DEFAULT_FLUSH_INTERVAL_MS,\n};\nexport type { TrackerConfig };\n","/**\n * useUserSettings — reactive hook for per-user notification preferences\n * stored in the Truth `userSettings` Convex table.\n *\n * Must be used within `<TruthProvider />` (see `./provider`).\n *\n * @example\n * ```tsx\n * const { data: settings, loading } = useUserSettings(userId);\n * // settings?.notificationsEnabled — boolean or undefined (no row yet)\n * ```\n */\n\n\"use client\";\n\nimport { useQuery } from \"convex/react\";\nimport type { FunctionReference } from \"convex/server\";\nimport { makeFunctionReference } from \"convex/server\";\nimport type { UseQueryResult } from \"./conversations\";\nimport type { UserSettings } from \"../resources/user-settings\";\n\n// ---------------------------------------------------------------------------\n// Convex function reference\n// ---------------------------------------------------------------------------\n\nconst userSettingsGetByUserIdRef = makeFunctionReference<\n \"query\",\n { userId: string },\n UserSettings | null\n>(\"userSettings:getByUserId\");\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\nconst SKIP = \"skip\" as const;\n\n/**\n * Subscribe to the `userSettings` row for `userId`. Returns `null` when\n * no row exists (treat as all-defaults). Returns `undefined` while the\n * Convex query is in-flight.\n *\n * Pass `null` or `undefined` as `userId` to skip the query (e.g. while\n * auth is loading).\n */\nexport function useUserSettings(\n userId: string | null | undefined,\n): UseQueryResult<UserSettings | null> {\n const skip = !userId;\n\n const result = useQuery(\n userSettingsGetByUserIdRef as FunctionReference<\"query\">,\n skip ? SKIP : { userId: userId as string },\n ) as UserSettings | null | undefined;\n\n if (skip) {\n return { data: null, loading: false, error: undefined };\n }\n\n return {\n data: result ?? null,\n loading: result === undefined,\n error: undefined,\n };\n}\n","/**\n * React hook for fetching an authenticated Dialpad voicemail URL.\n *\n * Provides an imperative `fetchUrl` callback that wraps\n * `client.messages.getVoicemailUrl()` in React state so CommHub's\n * `AudioPlayer` can replace its urql `useMutation` with a single hook\n * call, without adding a query library dependency.\n *\n * The TruthClient instance is passed directly so the hook is testable\n * and doesn't require a global singleton or React context.\n *\n * @example\n * ```tsx\n * import { useVoicemailUrl } from '@hipnation-truth/sdk/react/voicemail';\n * import { getTruthClient } from '@/lib/truthClient';\n *\n * function AudioPlayer({ uri }: { uri: string }) {\n * const { fetchUrl, url, isLoading, error } = useVoicemailUrl(getTruthClient());\n *\n * const handlePlay = async () => {\n * const authenticated = await fetchUrl(uri);\n * if (authenticated) { ... }\n * };\n * }\n * ```\n */\n\n\"use client\";\n\nimport { useCallback, useRef, useState } from \"react\";\nimport type { TruthClient } from \"../client\";\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\nexport interface UseVoicemailUrlResult {\n /** Trigger the URL fetch. Resolves with the clean playback URL or null on error. */\n fetchUrl: (voicemailLink: string) => Promise<string | null>;\n /** The most recently fetched URL (null until first successful fetch). */\n url: string | null;\n /** True while the fetch is in-flight. */\n isLoading: boolean;\n /** Error message from the last failed fetch, or null. */\n error: string | null;\n}\n\n// ---------------------------------------------------------------------------\n// Hook\n// ---------------------------------------------------------------------------\n\n/**\n * Provides an imperative `fetchUrl` callback that authenticates a Dialpad\n * voicemail link via the Truth SDK `messages.getVoicemailUrl()` method.\n *\n * @param client The TruthClient instance (e.g., from `getTruthClient()`).\n */\nexport function useVoicemailUrl(client: TruthClient): UseVoicemailUrlResult {\n const [url, setUrl] = useState<string | null>(null);\n const [isLoading, setIsLoading] = useState(false);\n const [error, setError] = useState<string | null>(null);\n\n // Guard against concurrent fetches (user double-tapping play).\n const inFlightRef = useRef(false);\n\n const fetchUrl = useCallback(\n async (voicemailLink: string): Promise<string | null> => {\n if (inFlightRef.current) return null;\n inFlightRef.current = true;\n setIsLoading(true);\n setError(null);\n\n try {\n const result = await client.messages.getVoicemailUrl(voicemailLink);\n setUrl(result.url);\n return result.url;\n } catch (err) {\n const msg = err instanceof Error ? err.message : \"Unknown error\";\n setError(msg);\n return null;\n } finally {\n setIsLoading(false);\n inFlightRef.current = false;\n }\n },\n [client],\n );\n\n return { fetchUrl, url, isLoading, error };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACuBA,mBAAyB;AACzB,oBAAsC;AAU/B,IAAM,mBAAmB;AAAA,EAC9B,SAAS;AAAA,EACT,SAAS;AAAA,EACT,WAAW;AAAA,EACX,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,mBAAmB;AACrB;AAKO,IAAM,sBAAqD,oBAAI,IAAI;AAAA,EACxE,iBAAiB;AAAA,EACjB,iBAAiB;AACnB,CAAC;AAGM,IAAM,wBAAuD,oBAAI,IAAI;AAAA,EAC1E,iBAAiB;AACnB,CAAC;AAQM,IAAM,qBAAoD,oBAAI,IAAI;AAAA,EACvE,GAAG;AAAA,EACH,GAAG;AACL,CAAC;AAGM,IAAM,uBAAsD,oBAAI,IAAI;AAAA,EACzE,iBAAiB;AAAA,EACjB,iBAAiB;AAAA,EACjB,iBAAiB;AACnB,CAAC;AAyCD,IAAM,oBAAgB,qCAIpB,8BAA8B;AAEhC,IAAM,6BAAyB,qCAI7B,uCAAuC;AAEzC,IAAM,qBAAiB,qCAIrB,+BAA+B;AAEjC,IAAM,0BAAsB,qCAI1B,oCAAoC;AAMtC,IAAM,OAAO;AAEb,SAAS,SACP,OACA,SACmB;AACnB,MAAI,SAAS;AACX,WA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QAAI,yBAAwB,IAAI;AAGtD,QAAM,kBAAc,uBAAO,KAAK;AAEhC,QAAM,eAAW;AAAA,IACf,CAAO,kBAAkD;AACvD,UAAI,YAAY,QAAS,QAAO;AAChC,kBAAY,UAAU;AACtB,mBAAa,IAAI;AACjB,eAAS,IAAI;AAEb,UAAI;AACF,cAAM,SAAS,MAAM,OAAO,SAAS,gBAAgB,aAAa;AAClE,eAAO,OAAO,GAAG;AACjB,eAAO,OAAO;AAAA,MAChB,SAAS,KAAK;AACZ,cAAM,MAAM,eAAe,QAAQ,IAAI,UAAU;AACjD,iBAAS,GAAG;AACZ,eAAO;AAAA,MACT,UAAE;AACA,qBAAa,KAAK;AAClB,oBAAY,UAAU;AAAA,MACxB;AAAA,IACF;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AAEA,SAAO,EAAE,UAAU,KAAK,WAAW,MAAM;AAC3C;","names":["import_react","import_server","import_react","import_server","SKIP","toResult","import_react","import_server","import_react","_a","res","import_react","import_server","SKIP","import_react","import_server","SKIP","import_react","import_server","SKIP","import_react","import_react","import_server","SKIP","import_react","import_server","import_react","import_react","import_server","SKIP","import_react"]}
|