@knocklabs/client 0.20.4 → 0.21.1
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/CHANGELOG.md +48 -0
- package/dist/cjs/api.js +1 -1
- package/dist/cjs/clients/feed/feed.js +1 -1
- package/dist/cjs/clients/feed/feed.js.map +1 -1
- package/dist/cjs/clients/feed/utils.js.map +1 -1
- package/dist/cjs/clients/guide/client.js +1 -1
- package/dist/cjs/clients/guide/client.js.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/esm/api.mjs +1 -1
- package/dist/esm/clients/feed/feed.mjs +2 -1
- package/dist/esm/clients/feed/feed.mjs.map +1 -1
- package/dist/esm/clients/feed/utils.mjs.map +1 -1
- package/dist/esm/clients/guide/client.mjs +212 -190
- package/dist/esm/clients/guide/client.mjs.map +1 -1
- package/dist/esm/index.mjs +11 -10
- package/dist/types/clients/feed/feed.d.ts.map +1 -1
- package/dist/types/clients/feed/interfaces.d.ts +35 -3
- package/dist/types/clients/feed/interfaces.d.ts.map +1 -1
- package/dist/types/clients/feed/utils.d.ts +1 -0
- package/dist/types/clients/feed/utils.d.ts.map +1 -1
- package/dist/types/clients/guide/client.d.ts +4 -1
- package/dist/types/clients/guide/client.d.ts.map +1 -1
- package/dist/types/clients/guide/index.d.ts +2 -2
- package/dist/types/clients/guide/index.d.ts.map +1 -1
- package/dist/types/clients/guide/types.d.ts +12 -1
- package/dist/types/clients/guide/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/clients/feed/feed.ts +2 -1
- package/src/clients/feed/interfaces.ts +35 -6
- package/src/clients/feed/utils.ts +11 -2
- package/src/clients/guide/client.ts +89 -52
- package/src/clients/guide/index.ts +7 -1
- package/src/clients/guide/types.ts +21 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkStateIfThrottled,\n findDefaultGroup,\n formatFilters,\n formatGroupStage,\n formatState,\n mockDefaultGroup,\n newUrl,\n predicateUrlPatterns,\n predicateUrlRules,\n} from \"./helpers\";\nimport {\n Any,\n ConstructorOpts,\n DebugState,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideLivePreviewUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n SelectGuideOpts,\n SelectGuidesOpts,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\n// Maximum number of retry attempts for channel subscription\nconst SUBSCRIBE_RETRY_LIMIT = 3;\n\n// Debug query param keys\nexport const DEBUG_QUERY_PARAMS = {\n GUIDE_KEY: \"knock_guide_key\",\n PREVIEW_SESSION_ID: \"knock_preview_session_id\",\n};\n\nconst DEBUG_STORAGE_KEY = \"knock_guide_debug\";\n\n// Return the global window object if defined, so to safely guard against SSR.\nconst checkForWindow = () => {\n if (typeof window !== \"undefined\") {\n return window;\n }\n};\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\n// Detect debug params from URL or local storage\nconst detectDebugParams = (): DebugState => {\n const win = checkForWindow();\n if (!win || !win.location) {\n return { forcedGuideKey: null, previewSessionId: null };\n }\n\n const urlParams = new URLSearchParams(win.location.search);\n const urlGuideKey = urlParams.get(DEBUG_QUERY_PARAMS.GUIDE_KEY);\n const urlPreviewSessionId = urlParams.get(\n DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID,\n );\n\n // If URL params exist, persist them to localStorage and return them\n if (urlGuideKey || urlPreviewSessionId) {\n if (win.localStorage) {\n try {\n const debugState = {\n forcedGuideKey: urlGuideKey,\n previewSessionId: urlPreviewSessionId,\n };\n win.localStorage.setItem(DEBUG_STORAGE_KEY, JSON.stringify(debugState));\n } catch {\n // Silently fail in privacy mode\n }\n }\n return {\n forcedGuideKey: urlGuideKey,\n previewSessionId: urlPreviewSessionId,\n };\n }\n\n // Check local storage if no URL params\n let storedGuideKey = null;\n let storedPreviewSessionId = null;\n\n if (win.localStorage) {\n try {\n const storedDebugState = win.localStorage.getItem(DEBUG_STORAGE_KEY);\n if (storedDebugState) {\n const parsedDebugState = safeJsonParseDebugParams(storedDebugState);\n storedGuideKey = parsedDebugState.forcedGuideKey;\n storedPreviewSessionId = parsedDebugState.previewSessionId;\n }\n } catch {\n // Silently fail in privacy mode\n }\n }\n\n return {\n forcedGuideKey: storedGuideKey,\n previewSessionId: storedPreviewSessionId,\n };\n};\n\nconst safeJsonParseDebugParams = (value: string): DebugState => {\n try {\n const parsed = JSON.parse(value);\n return {\n forcedGuideKey: parsed?.forcedGuideKey ?? null,\n previewSessionId: parsed?.previewSessionId ?? null,\n };\n } catch {\n return {\n forcedGuideKey: null,\n previewSessionId: null,\n };\n }\n};\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = [...defaultGroup.display_sequence];\n const location = state.location;\n\n // If in debug mode, put the forced guide at the beginning of the display sequence.\n if (state.debug.forcedGuideKey) {\n const forcedKeyIndex = displaySequence.indexOf(state.debug.forcedGuideKey);\n if (forcedKeyIndex > -1) {\n displaySequence.splice(forcedKeyIndex, 1);\n }\n displaySequence.unshift(state.debug.forcedGuideKey);\n }\n\n for (const [index, guideKey] of displaySequence.entries()) {\n let guide = state.guides[guideKey];\n\n // Use preview guide if it exists and matches the forced guide key\n if (\n state.debug.forcedGuideKey === guideKey &&\n state.previewGuides[guideKey]\n ) {\n guide = state.previewGuides[guideKey];\n }\n\n if (!guide) continue;\n\n const affirmed = predicate(guide, {\n location,\n filters,\n debug: state.debug,\n });\n\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n debug: DebugState;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {}, debug = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n // Bypass filtering if the debugged guide matches the given filters.\n // This should always run AFTER checking the filters but BEFORE\n // checking archived status and location rules.\n if (debug.forcedGuideKey === guide.key) {\n return true;\n }\n\n if (!guide.active) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const url = location ? newUrl(location) : undefined;\n\n const urlRules = guide.activation_url_rules || [];\n const urlPatterns = guide.activation_url_patterns || [];\n\n // A guide can have either activation url rules XOR url patterns, but not both.\n if (url && urlRules.length > 0) {\n const allowed = predicateUrlRules(url, urlRules);\n if (!allowed) return false;\n } else if (url && urlPatterns.length > 0) {\n const allowed = predicateUrlPatterns(url, urlPatterns);\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n \"guide.live_preview_updated\",\n ];\n private subscribeRetryCount = 0;\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const {\n trackLocationFromWindow = true,\n throttleCheckInterval = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = options;\n const win = checkForWindow();\n\n const location = trackLocationFromWindow ? win?.location?.href : undefined;\n\n const debug = detectDebugParams();\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n previewGuides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n debug,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n if (throttleCheckInterval) {\n // Start the counter loop to increment at an interval.\n this.startCounterInterval(throttleCheckInterval);\n }\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval(delay: number) {\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeLocationChangeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.log(\"[Guide] .fetch\");\n this.knock.failIfNotAuthenticated();\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n this.knock.log(\"[Guide] Fetching all eligible guides\");\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const { entries, guide_groups: groups, guide_group_display_logs } = data;\n\n this.knock.log(\"[Guide] Loading fetched guides\");\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs: guide_group_display_logs || {},\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const debugState = this.store.state.debug;\n const params = {\n ...this.targetParams,\n user_id: this.knock.userId,\n force_all_guides: debugState.forcedGuideKey ? true : undefined,\n preview_session_id: debugState.previewSessionId || undefined,\n };\n\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n // Reset retry count for new subscription attempt\n this.subscribeRetryCount = 0;\n\n newChannel\n .join()\n .receive(\"ok\", () => {\n this.knock.log(\"[Guide] Successfully joined channel\");\n })\n .receive(\"error\", (resp) => {\n this.knock.log(\n `[Guide] Failed to join channel: ${JSON.stringify(resp)}`,\n );\n this.handleChannelJoinError();\n })\n .receive(\"timeout\", () => {\n this.knock.log(\"[Guide] Channel join timed out\");\n this.handleChannelJoinError();\n });\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n private handleChannelJoinError() {\n // Prevent phx channel from retrying forever in case of either network or\n // other errors (e.g. auth error, invalid channel etc)\n if (this.subscribeRetryCount >= SUBSCRIBE_RETRY_LIMIT) {\n this.knock.log(\n `[Guide] Channel join max retry limit reached: ${this.subscribeRetryCount}`,\n );\n this.unsubscribe();\n return;\n }\n\n this.subscribeRetryCount++;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n case \"guide.live_preview_updated\":\n return this.updatePreviewGuide(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string, additionalParams: Partial<StoreState> = {}) {\n this.knock.log(`[Guide] .setLocation (loc=${href})`);\n\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.knock.log(\"[Guide] Updating the tracked location\");\n this.store.setState((state) => {\n // Clear preview guides if no longer in preview mode\n const previewGuides = additionalParams?.debug?.previewSessionId\n ? state.previewGuides\n : {};\n\n return {\n ...state,\n ...additionalParams,\n previewGuides,\n location: href,\n };\n });\n }\n\n exitDebugMode() {\n this.knock.log(\"[Guide] Exiting debug mode\");\n\n // Clear localStorage debug params\n const win = checkForWindow();\n if (win?.localStorage) {\n try {\n win.localStorage.removeItem(DEBUG_STORAGE_KEY);\n } catch {\n // Silently fail in privacy mode\n }\n }\n\n // Clear debug state from store\n this.store.setState((state) => ({\n ...state,\n debug: { forcedGuideKey: null, previewSessionId: null },\n previewGuides: {}, // Clear preview guides when exiting debug mode\n }));\n\n // Remove URL query params if present\n // Only update the URL if params need to be cleared to avoid unnecessary navigations\n if (win?.location) {\n const url = new URL(win.location.href);\n if (\n url.searchParams.has(DEBUG_QUERY_PARAMS.GUIDE_KEY) ||\n url.searchParams.has(DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID)\n ) {\n url.searchParams.delete(DEBUG_QUERY_PARAMS.GUIDE_KEY);\n url.searchParams.delete(DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID);\n win.location.href = url.toString();\n }\n }\n }\n\n //\n // Store selector\n //\n\n selectGuides<C = Any>(\n state: StoreState,\n filters: SelectFilterParams = {},\n opts: SelectGuidesOpts = {},\n ): KnockGuide<C>[] {\n this.knock.log(\n `[Guide] .selectGuides (filters: ${formatFilters(filters)}; state: ${formatState(state)})`,\n );\n\n const selectedGuide = this.selectGuide(state, filters, opts);\n if (!selectedGuide) {\n return [];\n }\n\n // There should be at least one guide to return here now.\n const guides = [...select(state, filters).values()];\n\n if (!opts.includeThrottled && checkStateIfThrottled(state)) {\n const unthrottledGuides = guides.filter(\n (g) => g.bypass_global_group_limit,\n );\n const throttledCount = guides.length - unthrottledGuides.length;\n this.knock.log(\n `[Guide] Throttling ${throttledCount} guides from selection, and returning ${unthrottledGuides.length} guides`,\n );\n\n return unthrottledGuides;\n }\n\n this.knock.log(`[Guide] Returning ${guides.length} guides from selection`);\n return guides;\n }\n\n selectGuide<C = Any>(\n state: StoreState,\n filters: SelectFilterParams = {},\n opts: SelectGuideOpts = {},\n ): KnockGuide<C> | undefined {\n this.knock.log(\n `[Guide] .selectGuide (filters: ${formatFilters(filters)}; state: ${formatState(state)})`,\n );\n if (\n Object.keys(state.guides).length === 0 &&\n Object.keys(state.previewGuides).length === 0\n ) {\n this.knock.log(\"[Guide] Exiting selection (no guides)\");\n return undefined;\n }\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection found zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n this.knock.log(\n `[Guide] Selection found: \\`${guide.key}\\` (total: ${result.size})`,\n );\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n this.knock.log(`[Guide] Returning the unthrottled guide: ${guide.key}`);\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined unless explicitly given the option to include throttled.\n if (!opts.includeThrottled && checkStateIfThrottled(state)) {\n this.knock.log(`[Guide] Throttling the selected guide: ${guide.key}`);\n return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Adding to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n\n const ret = this.stage.resolved === guide.key ? guide : undefined;\n this.knock.log(\n `[Guide] Returning \\`${ret?.key}\\` (stage: ${formatGroupStage(this.stage)})`,\n );\n return ret;\n }\n\n case \"closed\": {\n const ret = this.stage.resolved === guide.key ? guide : undefined;\n this.knock.log(\n `[Guide] Returning \\`${ret?.key}\\` (stage: ${formatGroupStage(this.stage)})`,\n );\n return ret;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n this.knock.log(\"[Guide] .closePendingGroupStage\");\n if (!this.stage || this.stage.status === \"closed\") return;\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n // If in debug mode, try to resolve the forced guide, otherwise return the first non-undefined guide.\n let resolved = undefined;\n if (this.store.state.debug.forcedGuideKey) {\n resolved = this.stage.ordered.find(\n (x) => x === this.store.state.debug.forcedGuideKey,\n );\n }\n\n if (!resolved) {\n resolved = this.stage.ordered.find((x) => x !== undefined);\n }\n\n this.knock.log(\n `[Guide] Closing the current group stage: resolved=${resolved}`,\n );\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved,\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n this.knock.log(\"[Guide] .patchClosedGroupStage\");\n if (this.stage?.status !== \"closed\") return;\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n this.knock.log(\"[Guide] .clearGroupStage\");\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helpers to open and close the group stage to return the select result\n // immediately.\n private _selectGuide(\n state: StoreState,\n filters: SelectFilterParams = {},\n opts: SelectGuideOpts = {},\n ) {\n this.openGroupStage();\n\n this.selectGuide(state, filters, opts);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters, opts);\n }\n\n private _selectGuides(\n state: StoreState,\n filters: SelectFilterParams = {},\n opts: SelectGuidesOpts = {},\n ) {\n this.openGroupStage();\n\n this.selectGuides(state, filters, opts);\n this.closePendingGroupStage();\n\n return this.selectGuides(state, filters, opts);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}; Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n // If debugging this guide, return the first step regardless of archive status\n if (self.store.state.debug.forcedGuideKey === this.key) {\n return this.steps[0];\n }\n\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_url_patterns =\n remoteGuide.activation_url_patterns.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({\n pathname: rule.pathname ?? undefined,\n search: rule.search ?? undefined,\n }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams: GenericData = {\n ...this.targetParams,\n ...filterParams,\n };\n\n // Append debug params\n const debugState = this.store.state.debug;\n if (debugState.forcedGuideKey) {\n combinedParams.force_all_guides = true;\n }\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n let guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // If updated, return the guide as a new object so useStore can trigger.\n guide = updatedStep ? { ...guide, steps } : guide;\n\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n // Can be used for scoping guide messages.\n tenant: this.targetParams.tenant,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n private updatePreviewGuide({ data }: GuideLivePreviewUpdatedEvent) {\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const previewGuides = { ...state.previewGuides, [guide.key]: guide };\n return { ...state, previewGuides };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n this.knock.log(`[Guide] .handleLocationChange`);\n const win = checkForWindow();\n if (!win?.location) return;\n\n const href = win.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Detected a location change: ${href}`);\n\n // If entering debug mode, fetch all guides.\n const currentDebugParams = this.store.state.debug;\n const newDebugParams = detectDebugParams();\n\n this.setLocation(href, { debug: newDebugParams });\n\n // If debug state has changed, refetch guides and resubscribe to the websocket channel\n const debugStateChanged = this.checkDebugStateChanged(\n currentDebugParams,\n newDebugParams,\n );\n\n if (debugStateChanged) {\n this.knock.log(\n `[Guide] Debug state changed, refetching guides and resubscribing to the websocket channel`,\n );\n this.fetch();\n this.subscribe();\n }\n };\n\n // Returns whether debug params have changed. For guide key, we only check\n // presence since the exact value has no impact on fetch/subscribe\n private checkDebugStateChanged(a: DebugState, b: DebugState): boolean {\n return (\n Boolean(a.forcedGuideKey) !== Boolean(b.forcedGuideKey) ||\n a.previewSessionId !== b.previewSessionId\n );\n }\n\n private listenForLocationChangesFromWindow() {\n const win = checkForWindow();\n if (win?.history && win?.addEventListener) {\n // 1. Listen for browser back/forward button clicks.\n win.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n win.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = win.history.pushState;\n const replaceStateFn = win.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n win.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n win.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n removeLocationChangeEventListeners() {\n const win = checkForWindow();\n if (!win?.history || !win?.removeEventListener) return;\n\n win.removeEventListener(\"popstate\", this.handleLocationChange);\n win.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n win.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n win.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","SUBSCRIBE_RETRY_LIMIT","DEBUG_QUERY_PARAMS","DEBUG_STORAGE_KEY","checkForWindow","guidesApiRootPath","userId","detectDebugParams","win","urlParams","urlGuideKey","urlPreviewSessionId","debugState","storedGuideKey","storedPreviewSessionId","storedDebugState","parsedDebugState","safeJsonParseDebugParams","value","parsed","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","forcedKeyIndex","index","guideKey","guide","predicate","debug","s","url","newUrl","urlRules","urlPatterns","predicateUrlRules","predicateUrlPatterns","KnockGuideClient","knock","channelId","targetParams","options","__publicField","href","currentDebugParams","newDebugParams","trackLocationFromWindow","throttleCheckInterval","_a","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guide_group_display_logs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","resp","event","additionalParams","previewGuides","formatFilters","formatState","guides","checkStateIfThrottled","unthrottledGuides","throttledCount","ret","formatGroupStage","timeoutId","resolved","x","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","rule","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guideGroupDisplayLogs","DEFAULT_GROUP_KEY","_","guideGroups","unthrottled","throttled","acc","a","b","pushStateFn","replaceStateFn","target","history","args"],"mappings":"kVAuDMA,EAAoC,GAIpCC,EAAqC,GAAK,IAG1CC,EAAwB,EAGjBC,EAAqB,CAChC,UAAW,kBACX,mBAAoB,0BACtB,EAEMC,EAAoB,oBAGpBC,EAAiB,IAAM,CACvB,GAAA,OAAO,OAAW,IACb,OAAA,MAEX,EA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AK,YAAc,QAEjB,KAAK,iBACHA,EAAA,QAAQ,aAAe,KAAK,eAChC,KAAK,eAAiB,QACxB,CAEJ"}
|
|
1
|
+
{"version":3,"file":"client.js","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkStateIfThrottled,\n findDefaultGroup,\n formatFilters,\n formatGroupStage,\n formatState,\n mockDefaultGroup,\n newUrl,\n predicateUrlPatterns,\n predicateUrlRules,\n} from \"./helpers\";\nimport {\n Any,\n ConstructorOpts,\n DebugState,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideLivePreviewUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n SelectGuideOpts,\n SelectGuidesOpts,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\n// Maximum number of retry attempts for channel subscription\nconst SUBSCRIBE_RETRY_LIMIT = 3;\n\n// Debug query param keys\nexport const DEBUG_QUERY_PARAMS = {\n GUIDE_KEY: \"knock_guide_key\",\n PREVIEW_SESSION_ID: \"knock_preview_session_id\",\n};\n\nconst DEBUG_STORAGE_KEY = \"knock_guide_debug\";\n\n// Return the global window object if defined, so to safely guard against SSR.\nconst checkForWindow = () => {\n if (typeof window !== \"undefined\") {\n return window;\n }\n};\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\n// Detect debug params from URL or local storage\nconst detectDebugParams = (): DebugState => {\n const win = checkForWindow();\n if (!win || !win.location) {\n return { forcedGuideKey: null, previewSessionId: null };\n }\n\n const urlParams = new URLSearchParams(win.location.search);\n const urlGuideKey = urlParams.get(DEBUG_QUERY_PARAMS.GUIDE_KEY);\n const urlPreviewSessionId = urlParams.get(\n DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID,\n );\n\n // If URL params exist, persist them to localStorage and return them\n if (urlGuideKey || urlPreviewSessionId) {\n if (win.localStorage) {\n try {\n const debugState = {\n forcedGuideKey: urlGuideKey,\n previewSessionId: urlPreviewSessionId,\n };\n win.localStorage.setItem(DEBUG_STORAGE_KEY, JSON.stringify(debugState));\n } catch {\n // Silently fail in privacy mode\n }\n }\n return {\n forcedGuideKey: urlGuideKey,\n previewSessionId: urlPreviewSessionId,\n };\n }\n\n // Check local storage if no URL params\n let storedGuideKey = null;\n let storedPreviewSessionId = null;\n\n if (win.localStorage) {\n try {\n const storedDebugState = win.localStorage.getItem(DEBUG_STORAGE_KEY);\n if (storedDebugState) {\n const parsedDebugState = safeJsonParseDebugParams(storedDebugState);\n storedGuideKey = parsedDebugState.forcedGuideKey;\n storedPreviewSessionId = parsedDebugState.previewSessionId;\n }\n } catch {\n // Silently fail in privacy mode\n }\n }\n\n return {\n forcedGuideKey: storedGuideKey,\n previewSessionId: storedPreviewSessionId,\n };\n};\n\nconst safeJsonParseDebugParams = (value: string): DebugState => {\n try {\n const parsed = JSON.parse(value);\n return {\n forcedGuideKey: parsed?.forcedGuideKey ?? null,\n previewSessionId: parsed?.previewSessionId ?? null,\n };\n } catch {\n return {\n forcedGuideKey: null,\n previewSessionId: null,\n };\n }\n};\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.previewGuides[guideKey] || state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, filters, {\n location,\n ineligibleGuides: state.ineligibleGuides,\n debug: state.debug,\n });\n\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = Pick<\n StoreState,\n \"location\" | \"ineligibleGuides\" | \"debug\"\n>;\n\nconst predicate = (\n guide: KnockGuide,\n filters: SelectFilterParams,\n { location, ineligibleGuides = {}, debug = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n // If in debug mode with a forced guide key, bypass other filtering and always\n // return true for that guide only. This should always run AFTER checking the\n // filters but BEFORE checking archived status and location rules.\n if (debug.forcedGuideKey) {\n return debug.forcedGuideKey === guide.key;\n }\n\n const ineligible = ineligibleGuides[guide.key];\n if (ineligible) {\n return false;\n }\n\n if (!guide.active) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n return checkActivatable(guide, location);\n};\n\nexport const checkActivatable = (\n guide: KnockGuide,\n location: string | undefined,\n) => {\n const url = location ? newUrl(location) : undefined;\n\n const urlRules = guide.activation_url_rules || [];\n const urlPatterns = guide.activation_url_patterns || [];\n\n // A guide can have either activation url rules XOR url patterns, but not both.\n if (url && urlRules.length > 0) {\n const allowed = predicateUrlRules(url, urlRules);\n if (!allowed) return false;\n } else if (url && urlPatterns.length > 0) {\n const allowed = predicateUrlPatterns(url, urlPatterns);\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n \"guide.live_preview_updated\",\n ];\n private subscribeRetryCount = 0;\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const {\n trackLocationFromWindow = true,\n // TODO(KNO-11523): Remove once we ship guide toolbar v2, and offload as\n // much debugging specific logic and responsibilities to toolbar.\n trackDebugParams = false,\n throttleCheckInterval = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = options;\n const win = checkForWindow();\n\n const location = trackLocationFromWindow ? win?.location?.href : undefined;\n const debug = trackDebugParams ? detectDebugParams() : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n ineligibleGuides: {},\n previewGuides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n debug,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n if (throttleCheckInterval) {\n // Start the counter loop to increment at an interval.\n this.startCounterInterval(throttleCheckInterval);\n }\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval(delay: number) {\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeLocationChangeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.log(\"[Guide] .fetch\");\n this.knock.failIfNotAuthenticated();\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n this.knock.log(\"[Guide] Fetching all eligible guides\");\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const {\n entries,\n guide_groups: groups,\n guide_group_display_logs,\n ineligible_guides,\n } = data;\n\n this.knock.log(\"[Guide] Loading fetched guides\");\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs: guide_group_display_logs || {},\n guides: byKey(entries.map((g) => this.localCopy(g))),\n ineligibleGuides: byKey(ineligible_guides || []),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const debugState = this.store.state.debug;\n const params = {\n ...this.targetParams,\n user_id: this.knock.userId,\n force_all_guides:\n debugState?.forcedGuideKey || debugState?.debugging ? true : undefined,\n preview_session_id: debugState?.previewSessionId || undefined,\n };\n\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n // Reset retry count for new subscription attempt\n this.subscribeRetryCount = 0;\n\n newChannel\n .join()\n .receive(\"ok\", () => {\n this.knock.log(\"[Guide] Successfully joined channel\");\n })\n .receive(\"error\", (resp) => {\n this.knock.log(\n `[Guide] Failed to join channel: ${JSON.stringify(resp)}`,\n );\n this.handleChannelJoinError();\n })\n .receive(\"timeout\", () => {\n this.knock.log(\"[Guide] Channel join timed out\");\n this.handleChannelJoinError();\n });\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n private handleChannelJoinError() {\n // Prevent phx channel from retrying forever in case of either network or\n // other errors (e.g. auth error, invalid channel etc)\n if (this.subscribeRetryCount >= SUBSCRIBE_RETRY_LIMIT) {\n this.knock.log(\n `[Guide] Channel join max retry limit reached: ${this.subscribeRetryCount}`,\n );\n this.unsubscribe();\n return;\n }\n\n this.subscribeRetryCount++;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n // TODO(KNO-11489): Include an ineligible guide in the socket payload too\n // and process it when handling socket events in real time.\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n case \"guide.live_preview_updated\":\n return this.updatePreviewGuide(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string, additionalParams: Partial<StoreState> = {}) {\n this.knock.log(`[Guide] .setLocation (loc=${href})`);\n\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.knock.log(\"[Guide] Updating the tracked location\");\n this.store.setState((state) => {\n // Clear preview guides if no longer in preview mode\n const previewGuides = additionalParams?.debug?.previewSessionId\n ? state.previewGuides\n : {};\n\n return {\n ...state,\n ...additionalParams,\n previewGuides,\n location: href,\n };\n });\n }\n\n exitDebugMode() {\n this.knock.log(\"[Guide] Exiting debug mode\");\n\n // Clear localStorage debug params\n const win = checkForWindow();\n if (win?.localStorage) {\n try {\n win.localStorage.removeItem(DEBUG_STORAGE_KEY);\n } catch {\n // Silently fail in privacy mode\n }\n }\n\n // Clear debug state from store\n this.store.setState((state) => ({\n ...state,\n debug: { forcedGuideKey: null, previewSessionId: null },\n previewGuides: {}, // Clear preview guides when exiting debug mode\n }));\n\n // Remove URL query params if present\n // Only update the URL if params need to be cleared to avoid unnecessary navigations\n if (win?.location) {\n const url = new URL(win.location.href);\n if (\n url.searchParams.has(DEBUG_QUERY_PARAMS.GUIDE_KEY) ||\n url.searchParams.has(DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID)\n ) {\n url.searchParams.delete(DEBUG_QUERY_PARAMS.GUIDE_KEY);\n url.searchParams.delete(DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID);\n win.location.href = url.toString();\n }\n }\n }\n\n setDebug(debugOpts?: Omit<DebugState, \"debugging\">) {\n this.knock.log(\"[Guide] .setDebug()\");\n const shouldRefetch = !this.store.state.debug?.debugging;\n\n this.store.setState((state) => ({\n ...state,\n debug: { ...debugOpts, debugging: true },\n }));\n\n if (shouldRefetch) {\n this.knock.log(\n `[Guide] Start debugging, refetching guides and resubscribing to the websocket channel`,\n );\n this.fetch();\n this.subscribe();\n }\n }\n\n unsetDebug() {\n this.knock.log(\"[Guide] .unsetDebug()\");\n const shouldRefetch = this.store.state.debug?.debugging;\n\n this.store.setState((state) => ({ ...state, debug: undefined }));\n\n if (shouldRefetch) {\n this.knock.log(\n `[Guide] Stop debugging, refetching guides and resubscribing to the websocket channel`,\n );\n this.fetch();\n this.subscribe();\n }\n }\n\n //\n // Store selector\n //\n\n selectGuides<C = Any>(\n state: StoreState,\n filters: SelectFilterParams = {},\n opts: SelectGuidesOpts = {},\n ): KnockGuide<C>[] {\n this.knock.log(\n `[Guide] .selectGuides (filters: ${formatFilters(filters)}; state: ${formatState(state)})`,\n );\n\n const selectedGuide = this.selectGuide(state, filters, opts);\n if (!selectedGuide) {\n return [];\n }\n\n // There should be at least one guide to return here now.\n const guides = [...select(state, filters).values()];\n\n if (!opts.includeThrottled && checkStateIfThrottled(state)) {\n const unthrottledGuides = guides.filter(\n (g) => g.bypass_global_group_limit,\n );\n const throttledCount = guides.length - unthrottledGuides.length;\n this.knock.log(\n `[Guide] Throttling ${throttledCount} guides from selection, and returning ${unthrottledGuides.length} guides`,\n );\n\n return unthrottledGuides;\n }\n\n this.knock.log(`[Guide] Returning ${guides.length} guides from selection`);\n return guides;\n }\n\n selectGuide<C = Any>(\n state: StoreState,\n filters: SelectFilterParams = {},\n opts: SelectGuideOpts = {},\n ): KnockGuide<C> | undefined {\n this.knock.log(\n `[Guide] .selectGuide (filters: ${formatFilters(filters)}; state: ${formatState(state)})`,\n );\n if (\n Object.keys(state.guides).length === 0 &&\n Object.keys(state.previewGuides).length === 0\n ) {\n this.knock.log(\"[Guide] Exiting selection (no guides)\");\n return undefined;\n }\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection found zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n this.knock.log(\n `[Guide] Selection found: \\`${guide.key}\\` (total: ${result.size})`,\n );\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n this.knock.log(`[Guide] Returning the unthrottled guide: ${guide.key}`);\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined unless explicitly given the option to include throttled.\n if (!opts.includeThrottled && checkStateIfThrottled(state)) {\n this.knock.log(`[Guide] Throttling the selected guide: ${guide.key}`);\n return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Adding to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n\n const ret = this.stage.resolved === guide.key ? guide : undefined;\n this.knock.log(\n `[Guide] Returning \\`${ret?.key}\\` (stage: ${formatGroupStage(this.stage)})`,\n );\n return ret;\n }\n\n case \"closed\": {\n const ret = this.stage.resolved === guide.key ? guide : undefined;\n this.knock.log(\n `[Guide] Returning \\`${ret?.key}\\` (stage: ${formatGroupStage(this.stage)})`,\n );\n return ret;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n this.knock.log(\"[Guide] .closePendingGroupStage\");\n if (!this.stage || this.stage.status === \"closed\") return;\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n // Resolve to the first non-undefined guide in the stage.\n const resolved = this.stage.ordered.find((x) => x !== undefined);\n\n this.knock.log(\n `[Guide] Closing the current group stage: resolved=${resolved}`,\n );\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved,\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n this.knock.log(\"[Guide] .patchClosedGroupStage\");\n if (this.stage?.status !== \"closed\") return;\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n this.knock.log(\"[Guide] .clearGroupStage\");\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helpers to open and close the group stage to return the select result\n // immediately.\n private _selectGuide(\n state: StoreState,\n filters: SelectFilterParams = {},\n opts: SelectGuideOpts = {},\n ) {\n this.openGroupStage();\n\n this.selectGuide(state, filters, opts);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters, opts);\n }\n\n private _selectGuides(\n state: StoreState,\n filters: SelectFilterParams = {},\n opts: SelectGuidesOpts = {},\n ) {\n this.openGroupStage();\n\n this.selectGuides(state, filters, opts);\n this.closePendingGroupStage();\n\n return this.selectGuides(state, filters, opts);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}; Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n // If debugging this guide, return the first step regardless of archive status\n if (self.store.state.debug?.forcedGuideKey === this.key) {\n return this.steps[0];\n }\n\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_url_patterns =\n remoteGuide.activation_url_patterns.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({\n pathname: rule.pathname ?? undefined,\n search: rule.search ?? undefined,\n }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams: GenericData = {\n ...this.targetParams,\n ...filterParams,\n };\n\n // Append debug params\n const debugState = this.store.state.debug;\n if (debugState?.forcedGuideKey || debugState?.debugging) {\n combinedParams.force_all_guides = true;\n }\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n let guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // If updated, return the guide as a new object so useStore can trigger.\n guide = updatedStep ? { ...guide, steps } : guide;\n\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n // Can be used for scoping guide messages.\n tenant: this.targetParams.tenant,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n private updatePreviewGuide({ data }: GuideLivePreviewUpdatedEvent) {\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const previewGuides = { ...state.previewGuides, [guide.key]: guide };\n return { ...state, previewGuides };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n this.knock.log(`[Guide] .handleLocationChange`);\n const win = checkForWindow();\n if (!win?.location) return;\n\n const href = win.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Detected a location change: ${href}`);\n\n if (!this.options.trackDebugParams) {\n this.setLocation(href);\n return;\n }\n\n // TODO(KNO-11523): Remove below once we ship toolbar v2.\n\n // If entering debug mode, fetch all guides.\n const currentDebugParams = this.store.state.debug || {};\n const newDebugParams = detectDebugParams();\n\n this.setLocation(href, { debug: newDebugParams });\n\n // If debug state has changed, refetch guides and resubscribe to the websocket channel\n const debugStateChanged = this.checkDebugStateChanged(\n currentDebugParams,\n newDebugParams,\n );\n\n if (debugStateChanged) {\n this.knock.log(\n `[Guide] Debug state changed, refetching guides and resubscribing to the websocket channel`,\n );\n this.fetch();\n this.subscribe();\n }\n };\n\n // Returns whether debug params have changed. For guide key, we only check\n // presence since the exact value has no impact on fetch/subscribe\n private checkDebugStateChanged(a: DebugState, b: DebugState): boolean {\n return (\n Boolean(a.forcedGuideKey) !== Boolean(b.forcedGuideKey) ||\n a.previewSessionId !== b.previewSessionId\n );\n }\n\n private listenForLocationChangesFromWindow() {\n const win = checkForWindow();\n if (win?.history && win?.addEventListener) {\n // 1. Listen for browser back/forward button clicks.\n win.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n win.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = win.history.pushState;\n const replaceStateFn = win.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n win.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n win.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n removeLocationChangeEventListeners() {\n const win = checkForWindow();\n if (!win?.history || !win?.removeEventListener) return;\n\n win.removeEventListener(\"popstate\", this.handleLocationChange);\n win.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n win.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n win.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","SUBSCRIBE_RETRY_LIMIT","DEBUG_QUERY_PARAMS","DEBUG_STORAGE_KEY","checkForWindow","guidesApiRootPath","userId","detectDebugParams","win","urlParams","urlGuideKey","urlPreviewSessionId","debugState","storedGuideKey","storedPreviewSessionId","storedDebugState","parsedDebugState","safeJsonParseDebugParams","value","parsed","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","ineligibleGuides","debug","s","checkActivatable","url","newUrl","urlRules","urlPatterns","predicateUrlRules","predicateUrlPatterns","KnockGuideClient","knock","channelId","targetParams","options","__publicField","href","currentDebugParams","newDebugParams","trackLocationFromWindow","trackDebugParams","throttleCheckInterval","_a","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guide_group_display_logs","ineligible_guides","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","resp","event","additionalParams","previewGuides","debugOpts","shouldRefetch","formatFilters","formatState","guides","checkStateIfThrottled","unthrottledGuides","throttledCount","ret","formatGroupStage","timeoutId","resolved","x","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","rule","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guideGroupDisplayLogs","DEFAULT_GROUP_KEY","_","guideGroups","unthrottled","throttled","acc","a","b","pushStateFn","replaceStateFn","target","history","args"],"mappings":"kVAuDMA,EAAoC,GAIpCC,EAAqC,GAAK,IAG1CC,EAAwB,EAGjBC,EAAqB,CAChC,UAAW,kBACX,mBAAoB,0BACtB,EAEMC,EAAoB,oBAGpBC,EAAiB,IAAM,CACvB,GAAA,OAAO,OAAW,IACb,OAAA,MAEX,EAEaC,EAAqBC,GAChC,aAAaA,CAAM,UAGfC,EAAoB,IAAkB,CAC1C,MAAMC,EAAMJ,EAAe,EAC3B,GAAI,CAACI,GAAO,CAACA,EAAI,SACf,MAAO,CAAE,eAAgB,KAAM,iBAAkB,IAAK,EAGxD,MAAMC,EAAY,IAAI,gBAAgBD,EAAI,SAAS,MAAM,EACnDE,EAAcD,EAAU,IAAIP,EAAmB,SAAS,EACxDS,EAAsBF,EAAU,IACpCP,EAAmB,kBACrB,EAGA,GAAIQ,GAAeC,EAAqB,CACtC,GAAIH,EAAI,aACF,GAAA,CACF,MAAMI,EAAa,CACjB,eAAgBF,EAChB,iBAAkB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AAA,EAAK,UAAW,EAACA,GAAA,MAAAA,EAAK,uBAEvBA,EAAA,oBAAoB,WAAY,KAAK,oBAAoB,EACzDA,EAAA,oBAAoB,aAAc,KAAK,oBAAoB,EAE3D,KAAK,cACHA,EAAA,QAAQ,UAAY,KAAK,YAC7B,KAAK,YAAc,QAEjB,KAAK,iBACHA,EAAA,QAAQ,aAAe,KAAK,eAChC,KAAK,eAAiB,QACxB,CAEJ"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=require("./clients/feed/index.js"),u=require("./knock.js"),e=require("./clients/guide/client.js"),n=require("./clients/objects/constants.js"),t=require("./networkStatus.js"),r=require("./clients/feed/feed.js");exports.FeedClient=i.default;exports.default=u.default;exports.DEBUG_QUERY_PARAMS=e.DEBUG_QUERY_PARAMS;exports.KnockGuideClient=e.KnockGuideClient;exports.checkActivatable=e.checkActivatable;exports.TENANT_OBJECT_COLLECTION=n.TENANT_OBJECT_COLLECTION;exports.NetworkStatus=t.NetworkStatus;exports.isRequestInFlight=t.isRequestInFlight;exports.Feed=r.default;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/api.mjs
CHANGED
|
@@ -56,7 +56,7 @@ class k {
|
|
|
56
56
|
return i.isNetworkError(e) ? !0 : e.response ? e.response.status >= 500 && e.response.status <= 599 || e.response.status === 429 : !1;
|
|
57
57
|
}
|
|
58
58
|
getKnockClientHeader() {
|
|
59
|
-
return "Knock/ClientJS 0.
|
|
59
|
+
return "Knock/ClientJS 0.21.1";
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
export {
|
|
@@ -9,7 +9,8 @@ import { SocketEventType as y } from "./socket-manager.mjs";
|
|
|
9
9
|
import b from "./store.mjs";
|
|
10
10
|
import { mergeDateRangeParams as f, getFormattedTriggerData as I } from "./utils.mjs";
|
|
11
11
|
const C = {
|
|
12
|
-
archived: "exclude"
|
|
12
|
+
archived: "exclude",
|
|
13
|
+
mode: "compact"
|
|
13
14
|
}, U = 2e3, w = "client_";
|
|
14
15
|
class P {
|
|
15
16
|
constructor(t, e, s, n) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feed.mjs","sources":["../../../../src/clients/feed/feed.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport EventEmitter from \"eventemitter2\";\nimport { nanoid } from \"nanoid\";\n\nimport { isValidUuid } from \"../../helpers\";\nimport Knock from \"../../knock\";\nimport { NetworkStatus, isRequestInFlight } from \"../../networkStatus\";\nimport {\n BulkUpdateMessagesInChannelProperties,\n MessageEngagementStatus,\n} from \"../messages/interfaces\";\n\nimport {\n FeedClientOptions,\n FeedItem,\n FeedMetadata,\n FeedResponse,\n FetchFeedOptions,\n FetchFeedOptionsForRequest,\n} from \"./interfaces\";\nimport {\n FeedSocketManager,\n SocketEventPayload,\n SocketEventType,\n} from \"./socket-manager\";\nimport createStore, { FeedStore } from \"./store\";\nimport {\n BindableFeedEvent,\n FeedEvent,\n FeedEventCallback,\n FeedEventPayload,\n FeedItemOrItems,\n FeedMessagesReceivedPayload,\n FeedRealTimeCallback,\n} from \"./types\";\nimport { getFormattedTriggerData, mergeDateRangeParams } from \"./utils\";\n\n// Default options to apply\nconst feedClientDefaults: Pick<FeedClientOptions, \"archived\"> = {\n archived: \"exclude\",\n};\n\nconst DEFAULT_DISCONNECT_DELAY = 2000;\n\nconst CLIENT_REF_ID_PREFIX = \"client_\";\n\nclass Feed {\n public readonly defaultOptions: FeedClientOptions;\n public readonly referenceId: string;\n public unsubscribeFromSocketEvents: (() => void) | undefined = undefined;\n private socketManager: FeedSocketManager | undefined;\n private userFeedId: string;\n private broadcaster: EventEmitter;\n private broadcastChannel!: BroadcastChannel | null;\n private disconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private hasSubscribedToRealTimeUpdates: boolean = false;\n private visibilityChangeHandler: () => void = () => {};\n private visibilityChangeListenerConnected: boolean = false;\n\n // The raw store instance, used for binding in React and other environments\n public store: FeedStore;\n\n constructor(\n readonly knock: Knock,\n readonly feedId: string,\n options: FeedClientOptions,\n socketManager: FeedSocketManager | undefined,\n ) {\n if (!feedId || !isValidUuid(feedId)) {\n this.knock.log(\n \"[Feed] Invalid or missing feedId provided to the Feed constructor. The feed should be a UUID of an in-app feed channel (`in_app_feed`) found in the Knock dashboard. Please provide a valid feedId to the Feed constructor.\",\n true,\n );\n }\n\n this.feedId = feedId;\n this.userFeedId = this.buildUserFeedId();\n this.referenceId = CLIENT_REF_ID_PREFIX + nanoid();\n this.socketManager = socketManager;\n this.store = createStore();\n this.broadcaster = new EventEmitter({ wildcard: true, delimiter: \".\" });\n this.defaultOptions = {\n ...feedClientDefaults,\n ...mergeDateRangeParams(options),\n };\n this.knock.log(`[Feed] Initialized a feed on channel ${feedId}`);\n\n // Attempt to setup a realtime connection (does not join)\n this.initializeRealtimeConnection();\n\n this.setupBroadcastChannel();\n }\n\n /**\n * Used to reinitialize a current feed instance, which is useful when reauthenticating users\n */\n reinitialize(socketManager?: FeedSocketManager) {\n this.socketManager = socketManager;\n\n // Reinitialize the user feed id incase the userId changed\n this.userFeedId = this.buildUserFeedId();\n\n // Reinitialize the real-time connection\n this.initializeRealtimeConnection();\n\n // Reinitialize our broadcast channel\n this.setupBroadcastChannel();\n }\n\n /**\n * Cleans up a feed instance by destroying the store and disconnecting\n * an open socket connection.\n */\n teardown() {\n this.knock.log(\"[Feed] Tearing down feed instance\");\n\n this.socketManager?.leave(this);\n\n this.tearDownVisibilityListeners();\n\n if (this.disconnectTimer) {\n clearTimeout(this.disconnectTimer);\n this.disconnectTimer = null;\n }\n\n if (this.broadcastChannel) {\n this.broadcastChannel.close();\n }\n }\n\n /** Tears down an instance and removes it entirely from the feed manager */\n dispose() {\n this.knock.log(\"[Feed] Disposing of feed instance\");\n this.teardown();\n this.broadcaster.removeAllListeners();\n this.knock.feeds.removeInstance(this);\n }\n\n /*\n Initializes a real-time connection to Knock, connecting the websocket for the\n current ApiClient instance if the socket is not already connected.\n */\n listenForUpdates() {\n this.knock.log(\"[Feed] Connecting to real-time service\");\n\n this.hasSubscribedToRealTimeUpdates = true;\n\n // If the user is not authenticated, then do nothing\n if (!this.knock.isAuthenticated()) {\n this.knock.log(\n \"[Feed] User is not authenticated, skipping listening for updates\",\n );\n return;\n }\n\n this.unsubscribeFromSocketEvents = this.socketManager?.join(this);\n }\n\n /* Binds a handler to be invoked when event occurs */\n on(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.on(eventName, callback);\n }\n\n off(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.off(eventName, callback);\n }\n\n getState() {\n return this.store.getState();\n }\n\n async markAsSeen(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"seen\",\n { seen_at: now },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"seen\");\n }\n\n async markAllAsSeen() {\n // To mark all of the messages as seen we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unseen_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unseen`\n // items by removing everything from view.\n const { metadata, items, ...state } = this.store.getState();\n\n const isViewingOnlyUnseen = this.defaultOptions.status === \"unseen\";\n\n // If we're looking at the unseen view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnseen) {\n state.resetStore({\n ...metadata,\n total_count: 0,\n unseen_count: 0,\n });\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n state.setMetadata({ ...metadata, unseen_count: 0 });\n\n const attrs = { seen_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n state.setItemAttrs(itemIds, attrs);\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"seen\");\n this.emitEvent(\"all_seen\", items);\n\n return result;\n }\n\n async markAsUnseen(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unseen\",\n { seen_at: null },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unseen\");\n }\n\n async markAsRead(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"read\",\n { read_at: now },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"read\");\n }\n\n async markAllAsRead() {\n // To mark all of the messages as read we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unread_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unread_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unread`\n // items by removing everything from view.\n const { metadata, items, ...state } = this.store.getState();\n\n const isViewingOnlyUnread = this.defaultOptions.status === \"unread\";\n\n // If we're looking at the unread view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnread) {\n state.resetStore({\n ...metadata,\n total_count: 0,\n unread_count: 0,\n });\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n state.setMetadata({ ...metadata, unread_count: 0 });\n\n const attrs = { read_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n state.setItemAttrs(itemIds, attrs);\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"read\");\n this.emitEvent(\"all_read\", items);\n\n return result;\n }\n\n async markAsUnread(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unread\",\n { read_at: null },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unread\");\n }\n\n async markAsInteracted(\n itemOrItems: FeedItemOrItems,\n metadata?: Record<string, string>,\n ) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"interacted\",\n {\n read_at: now,\n interacted_at: now,\n },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"interacted\", metadata);\n }\n\n /*\n Marking one or more items as archived should:\n\n - Decrement the badge count for any unread / unseen items\n - Remove the item from the feed list when the `archived` flag is \"exclude\" (default)\n\n TODO: how do we handle rollbacks?\n */\n async markAsArchived(itemOrItems: FeedItemOrItems) {\n const state = this.store.getState();\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n\n const itemIds: string[] = items.map((item) => item.id);\n\n /*\n In the code here we want to optimistically update counts and items\n that are persisted such that we can display updates immediately on the feed\n without needing to make a network request.\n\n Note: right now this does *not* take into account offline handling or any extensive retry\n logic, so rollbacks aren't considered. That probably needs to be a future consideration for\n this library.\n\n Scenarios to consider:\n\n ## Feed scope to archived *only*\n\n - Counts should not be decremented\n - Items should not be removed\n\n ## Feed scoped to exclude archived items (the default)\n\n - Counts should be decremented\n - Items should be removed\n\n ## Feed scoped to include archived items as well\n\n - Counts should not be decremented\n - Items should not be removed\n */\n\n if (shouldOptimisticallyRemoveItems) {\n // If any of the items are unseen or unread, then capture as we'll want to decrement\n // the counts for these in the metadata we have\n const unseenCount = items.filter((i) => !i.seen_at).length;\n const unreadCount = items.filter((i) => !i.read_at).length;\n\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n // Ensure that the counts don't ever go below 0 on archiving where the client state\n // gets out of sync with the server state\n total_count: Math.max(0, state.metadata.total_count - items.length),\n unseen_count: Math.max(0, state.metadata.unseen_count - unseenCount),\n unread_count: Math.max(0, state.metadata.unread_count - unreadCount),\n };\n\n // Remove the archiving entries\n const entriesToSet = state.items.filter(\n (item) => !itemIds.includes(item.id),\n );\n\n state.setResult({\n entries: entriesToSet,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n return this.makeStatusUpdate(itemOrItems, \"archived\");\n }\n\n async markAllAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { items, ...state } = this.store.getState();\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Reset the store to clear out all of items and reset the badge count\n state.resetStore();\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n const itemIds = items.map((i) => i.id);\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n this.emitEvent(\"all_archived\", items);\n\n return result;\n }\n\n async markAllReadAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { items, ...state } = this.store.getState();\n // Filter items to only include those that are unread\n const unreadItems = items.filter((item) => item.read_at === null);\n // Mark all the unread items as archived and read\n const itemIds = unreadItems.map((i) => i.id);\n state.setItemAttrs(itemIds, {\n archived_at: new Date().toISOString(),\n });\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Remove all the read items from the store and reset the badge count\n const remainingItems = items.filter((item) => !itemIds.includes(item.id));\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n total_count: remainingItems.length,\n unread_count: 0,\n };\n\n state.setResult({\n entries: remainingItems,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n // this.emitEvent(\"all_archived\", readItems);\n\n return result;\n }\n\n async markAsUnarchived(itemOrItems: FeedItemOrItems) {\n const state = this.store.getState();\n\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n\n const itemIds: string[] = items.map((item) => item.id);\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"only\";\n\n if (shouldOptimisticallyRemoveItems) {\n // If any of the items are unseen or unread, then capture as we'll want to decrement\n // the counts for these in the metadata we have\n const unseenCount = items.filter((i) => !i.seen_at).length;\n const unreadCount = items.filter((i) => !i.read_at).length;\n\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n // Ensure that the counts don't ever go below 0 on unarchiving where the client state\n // gets out of sync with the server state\n total_count: Math.max(0, state.metadata.total_count - items.length),\n unseen_count: Math.max(0, state.metadata.unseen_count - unseenCount),\n unread_count: Math.max(0, state.metadata.unread_count - unreadCount),\n };\n\n // Remove the unarchived entries\n const entriesToSet = state.items.filter(\n (item) => !itemIds.includes(item.id),\n );\n\n state.setResult({\n entries: entriesToSet,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n } else {\n this.optimisticallyPerformStatusUpdate(itemOrItems, \"unarchived\", {\n archived_at: null,\n });\n }\n\n return this.makeStatusUpdate(itemOrItems, \"unarchived\");\n }\n\n /* Fetches the feed content, appending it to the store */\n async fetch(options: FetchFeedOptions = {}) {\n const { networkStatus, ...state } = this.store.getState();\n\n // If the user is not authenticated, then do nothing\n if (!this.knock.isAuthenticated()) {\n this.knock.log(\"[Feed] User is not authenticated, skipping fetch\");\n return;\n }\n\n // If there's an existing request in flight, then do nothing\n if (isRequestInFlight(networkStatus)) {\n this.knock.log(\"[Feed] Request is in flight, skipping fetch\");\n return;\n }\n\n // Set the loading type based on the request type it is\n state.setNetworkStatus(options.__loadingType ?? NetworkStatus.loading);\n\n // trigger_data should be a JSON string for the API\n // this function will format the trigger data if it's an object\n // https://docs.knock.app/reference#get-feed\n const formattedTriggerData = getFormattedTriggerData({\n ...this.defaultOptions,\n ...options,\n });\n\n // Always include the default params, if they have been set\n const queryParams: FetchFeedOptionsForRequest = {\n ...this.defaultOptions,\n ...mergeDateRangeParams(options),\n trigger_data: formattedTriggerData,\n // Unset options that should not be sent to the API\n __loadingType: undefined,\n __fetchSource: undefined,\n __experimentalCrossBrowserUpdates: undefined,\n auto_manage_socket_connection: undefined,\n auto_manage_socket_connection_delay: undefined,\n };\n\n const result = await this.knock.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,\n params: queryParams,\n });\n\n if (result.statusCode === \"error\" || !result.body) {\n state.setNetworkStatus(NetworkStatus.error);\n\n return {\n status: result.statusCode,\n data: result.error || result.body,\n };\n }\n\n const response = {\n entries: result.body.entries,\n meta: result.body.meta,\n page_info: result.body.page_info,\n };\n\n if (options.before) {\n const opts = { shouldSetPage: false, shouldAppend: true };\n state.setResult(response, opts);\n } else if (options.after) {\n const opts = { shouldSetPage: true, shouldAppend: true };\n state.setResult(response, opts);\n } else {\n state.setResult(response);\n }\n\n // Legacy `messages.new` event, should be removed in a future version\n this.broadcast(\"messages.new\", response);\n\n // Broadcast the appropriate event type depending on the fetch source\n const feedEventType: FeedEvent =\n options.__fetchSource === \"socket\"\n ? \"items.received.realtime\"\n : \"items.received.page\";\n\n const eventPayload = {\n items: response.entries as FeedItem[],\n metadata: response.meta as FeedMetadata,\n event: feedEventType,\n };\n\n this.broadcast(eventPayload.event, eventPayload);\n\n return { data: response, status: result.statusCode };\n }\n\n async fetchNextPage(options: FetchFeedOptions = {}) {\n // Attempts to fetch the next page of results (if we have any)\n const { pageInfo } = this.store.getState();\n\n if (!pageInfo.after) {\n // Nothing more to fetch\n return;\n }\n\n this.fetch({\n ...options,\n after: pageInfo.after,\n __loadingType: NetworkStatus.fetchMore,\n });\n }\n\n get socketChannelTopic(): string {\n return `feeds:${this.userFeedId}`;\n }\n\n private broadcast(\n eventName: FeedEvent,\n data: FeedResponse | FeedEventPayload,\n ) {\n this.broadcaster.emit(eventName, data);\n }\n\n // Invoked when a new real-time message comes in from the socket\n private async onNewMessageReceived({ data }: FeedMessagesReceivedPayload) {\n this.knock.log(\"[Feed] Received new real-time message\");\n\n // Handle the new message coming in\n const { items, ...state } = this.store.getState();\n const currentHead: FeedItem | undefined = items[0];\n\n // Optimistically set the badge counts\n const metadata = data[this.referenceId]?.metadata;\n if (metadata) {\n state.setMetadata(metadata);\n }\n\n // Fetch the items before the current head (if it exists)\n this.fetch({ before: currentHead?.__cursor, __fetchSource: \"socket\" });\n }\n\n private buildUserFeedId() {\n return `${this.feedId}:${this.knock.userId}`;\n }\n\n private optimisticallyPerformStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n attrs: object,\n badgeCountAttr?: \"unread_count\" | \"unseen_count\",\n ) {\n const state = this.store.getState();\n const normalizedItems = Array.isArray(itemOrItems)\n ? itemOrItems\n : [itemOrItems];\n const itemIds = normalizedItems.map((item) => item.id);\n\n if (badgeCountAttr) {\n const { metadata } = state;\n\n // We only want to update the counts of items that have not already been counted towards the\n // badge count total to avoid updating the badge count unnecessarily.\n const itemsToUpdate = normalizedItems.filter((item) => {\n switch (type) {\n case \"seen\":\n return item.seen_at === null;\n case \"unseen\":\n return item.seen_at !== null;\n case \"read\":\n case \"interacted\":\n return item.read_at === null;\n case \"unread\":\n return item.read_at !== null;\n default:\n return true;\n }\n });\n\n // This is a hack to determine the direction of whether we're\n // adding or removing from the badge count\n const direction = type.startsWith(\"un\")\n ? itemsToUpdate.length\n : -itemsToUpdate.length;\n\n state.setMetadata({\n ...metadata,\n [badgeCountAttr]: Math.max(0, metadata[badgeCountAttr] + direction),\n });\n }\n\n // Update the items with the given attributes\n state.setItemAttrs(itemIds, attrs);\n }\n\n private async makeStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n metadata?: Record<string, string>,\n ) {\n // Always treat items as a batch to use the corresponding batch endpoint\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n const itemIds = items.map((item) => item.id);\n\n const result = await this.knock.messages.batchUpdateStatuses(\n itemIds,\n type,\n { metadata },\n );\n\n // Emit the event that these items had their statuses changed\n // Note: we do this after the update to ensure that the server event actually completed\n this.emitEvent(type, items);\n\n return result;\n }\n\n private async makeBulkStatusUpdate(\n status: BulkUpdateMessagesInChannelProperties[\"status\"],\n ) {\n // The base scope for the call should take into account all of the options currently\n // set on the feed, as well as being scoped for the current user. We do this so that\n // we ONLY make changes to the messages that are currently in view on this feed, and not\n // all messages that exist.\n const options = {\n user_ids: [this.knock.userId!],\n engagement_status:\n this.defaultOptions.status !== \"all\"\n ? this.defaultOptions.status\n : undefined,\n archived: this.defaultOptions.archived,\n has_tenant: this.defaultOptions.has_tenant,\n tenants: this.defaultOptions.tenant\n ? [this.defaultOptions.tenant]\n : undefined,\n };\n\n return await this.knock.messages.bulkUpdateAllStatusesInChannel({\n channelId: this.feedId,\n status,\n options,\n });\n }\n\n private setupBroadcastChannel() {\n // Attempt to bind to listen to other events from this feed in different tabs\n // Note: here we ensure `self` is available (it's not in server rendered envs)\n this.broadcastChannel =\n typeof self !== \"undefined\" && \"BroadcastChannel\" in self\n ? new BroadcastChannel(`knock:feed:${this.userFeedId}`)\n : null;\n\n // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast\n // channel (iff it's enabled and exists)\n if (\n this.broadcastChannel &&\n this.defaultOptions.__experimentalCrossBrowserUpdates === true\n ) {\n this.broadcastChannel.onmessage = (e) => {\n switch (e.data.type) {\n case \"items:archived\":\n case \"items:unarchived\":\n case \"items:seen\":\n case \"items:unseen\":\n case \"items:read\":\n case \"items:unread\":\n case \"items:all_read\":\n case \"items:all_seen\":\n case \"items:all_archived\":\n // When items are updated in any other tab, simply refetch to get the latest state\n // to make sure that the state gets updated accordingly. In the future here we could\n // maybe do this optimistically without the fetch.\n return this.fetch();\n default:\n return null;\n }\n };\n }\n }\n\n private broadcastOverChannel(type: string, payload: GenericData) {\n // The broadcastChannel may not be available in non-browser environments\n if (!this.broadcastChannel) {\n return;\n }\n\n // Here we stringify our payload and try and send as JSON such that we\n // don't get any `An object could not be cloned` errors when trying to broadcast\n try {\n const stringifiedPayload = JSON.parse(JSON.stringify(payload));\n\n this.broadcastChannel.postMessage({\n type,\n payload: stringifiedPayload,\n });\n } catch (e) {\n console.warn(`Could not broadcast ${type}, got error: ${e}`);\n }\n }\n\n private initializeRealtimeConnection() {\n // In server environments we might not have a socket connection\n if (!this.socketManager) return;\n\n if (this.defaultOptions.auto_manage_socket_connection) {\n this.setUpVisibilityListeners();\n }\n\n // If we're initializing but they have previously opted to listen to real-time updates\n // then we will automatically reconnect on their behalf\n if (this.hasSubscribedToRealTimeUpdates && this.knock.isAuthenticated()) {\n this.unsubscribeFromSocketEvents = this.socketManager?.join(this);\n }\n }\n\n async handleSocketEvent(payload: SocketEventPayload) {\n switch (payload.event) {\n case SocketEventType.NewMessage:\n this.onNewMessageReceived(payload);\n return;\n default: {\n const _exhaustiveCheck: never = payload.event;\n return;\n }\n }\n }\n\n /**\n * Listen for changes to document visibility and automatically disconnect\n * or reconnect the socket after a delay\n */\n private setUpVisibilityListeners() {\n if (\n typeof document === \"undefined\" ||\n this.visibilityChangeListenerConnected\n ) {\n return;\n }\n\n this.visibilityChangeHandler = this.handleVisibilityChange.bind(this);\n this.visibilityChangeListenerConnected = true;\n document.addEventListener(\"visibilitychange\", this.visibilityChangeHandler);\n }\n\n private tearDownVisibilityListeners() {\n if (typeof document === \"undefined\") return;\n\n document.removeEventListener(\n \"visibilitychange\",\n this.visibilityChangeHandler,\n );\n this.visibilityChangeListenerConnected = false;\n }\n\n private emitEvent(\n type:\n | MessageEngagementStatus\n | \"all_read\"\n | \"all_seen\"\n | \"all_archived\"\n | \"unread\"\n | \"unseen\"\n | \"unarchived\",\n items: FeedItem[],\n ) {\n // Handle both `items.` and `items:` format for events for compatibility reasons\n this.broadcaster.emit(`items.${type}`, { items });\n this.broadcaster.emit(`items:${type}`, { items });\n // Internal events only need `items:`\n this.broadcastOverChannel(`items:${type}`, { items });\n }\n\n private handleVisibilityChange() {\n const disconnectDelay =\n this.defaultOptions.auto_manage_socket_connection_delay ??\n DEFAULT_DISCONNECT_DELAY;\n\n const client = this.knock.client();\n\n if (document.visibilityState === \"hidden\") {\n // When the tab is hidden, clean up the socket connection after a delay\n this.disconnectTimer = setTimeout(() => {\n client.socket?.disconnect();\n this.disconnectTimer = null;\n }, disconnectDelay);\n } else if (document.visibilityState === \"visible\") {\n // When the tab is visible, clear the disconnect timer if active to cancel disconnecting\n // This handles cases where the tab is only briefly hidden to avoid unnecessary disconnects\n if (this.disconnectTimer) {\n clearTimeout(this.disconnectTimer);\n this.disconnectTimer = null;\n }\n\n // If the socket is not connected, try to reconnect\n if (!client.socket?.isConnected()) {\n client.socket?.connect();\n }\n }\n }\n}\n\nexport default Feed;\n"],"names":["feedClientDefaults","DEFAULT_DISCONNECT_DELAY","CLIENT_REF_ID_PREFIX","Feed","knock","feedId","options","socketManager","__publicField","isValidUuid","nanoid","createStore","EventEmitter","mergeDateRangeParams","_a","eventName","callback","itemOrItems","now","metadata","items","state","attrs","itemIds","item","result","shouldOptimisticallyRemoveItems","unseenCount","i","unreadCount","updatedMetadata","entriesToSet","remainingItems","networkStatus","isRequestInFlight","NetworkStatus","formattedTriggerData","getFormattedTriggerData","queryParams","response","opts","feedEventType","eventPayload","pageInfo","data","currentHead","type","badgeCountAttr","normalizedItems","itemsToUpdate","direction","status","e","payload","stringifiedPayload","SocketEventType","disconnectDelay","client","_b"],"mappings":";;;;;;;;;;AAsCA,MAAMA,IAA0D;AAAA,EAC9D,UAAU;AACZ,GAEMC,IAA2B,KAE3BC,IAAuB;AAE7B,MAAMC,EAAK;AAAA,EAgBT,YACWC,GACAC,GACTC,GACAC,GACA;AApBc,IAAAC,EAAA;AACA,IAAAA,EAAA;AACT,IAAAA,EAAA;AACC,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA,yBAAwD;AACxD,IAAAA,EAAA,wCAA0C;AAC1C,IAAAA,EAAA,iCAAsC,MAAM;AAAA,IAAC;AAC7C,IAAAA,EAAA,2CAA6C;AAG9C;AAAA,IAAAA,EAAA;AAGI,SAAA,QAAAJ,GACA,KAAA,SAAAC,IAIL,CAACA,KAAU,CAACI,EAAYJ,CAAM,MAChC,KAAK,MAAM;AAAA,MACT;AAAA,MACA;AAAA,IACF,GAGF,KAAK,SAASA,GACT,KAAA,aAAa,KAAK,gBAAgB,GAClC,KAAA,cAAcH,IAAuBQ,EAAO,GACjD,KAAK,gBAAgBH,GACrB,KAAK,QAAQI,EAAY,GACpB,KAAA,cAAc,IAAIC,EAAa,EAAE,UAAU,IAAM,WAAW,KAAK,GACtE,KAAK,iBAAiB;AAAA,MACpB,GAAGZ;AAAA,MACH,GAAGa,EAAqBP,CAAO;AAAA,IACjC,GACA,KAAK,MAAM,IAAI,wCAAwCD,CAAM,EAAE,GAG/D,KAAK,6BAA6B,GAElC,KAAK,sBAAsB;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM7B,aAAaE,GAAmC;AAC9C,SAAK,gBAAgBA,GAGhB,KAAA,aAAa,KAAK,gBAAgB,GAGvC,KAAK,6BAA6B,GAGlC,KAAK,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EAAe,kBAClB4C,IAAAD,EAAO,WAAP,QAAAC,EAAe;AAAA,EAEnB;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"feed.mjs","sources":["../../../../src/clients/feed/feed.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport EventEmitter from \"eventemitter2\";\nimport { nanoid } from \"nanoid\";\n\nimport { isValidUuid } from \"../../helpers\";\nimport Knock from \"../../knock\";\nimport { NetworkStatus, isRequestInFlight } from \"../../networkStatus\";\nimport {\n BulkUpdateMessagesInChannelProperties,\n MessageEngagementStatus,\n} from \"../messages/interfaces\";\n\nimport {\n FeedClientOptions,\n FeedItem,\n FeedMetadata,\n FeedResponse,\n FetchFeedOptions,\n FetchFeedOptionsForRequest,\n} from \"./interfaces\";\nimport {\n FeedSocketManager,\n SocketEventPayload,\n SocketEventType,\n} from \"./socket-manager\";\nimport createStore, { FeedStore } from \"./store\";\nimport {\n BindableFeedEvent,\n FeedEvent,\n FeedEventCallback,\n FeedEventPayload,\n FeedItemOrItems,\n FeedMessagesReceivedPayload,\n FeedRealTimeCallback,\n} from \"./types\";\nimport { getFormattedTriggerData, mergeDateRangeParams } from \"./utils\";\n\n// Default options to apply\nconst feedClientDefaults: Pick<FeedClientOptions, \"archived\" | \"mode\"> = {\n archived: \"exclude\",\n mode: \"compact\",\n};\n\nconst DEFAULT_DISCONNECT_DELAY = 2000;\n\nconst CLIENT_REF_ID_PREFIX = \"client_\";\n\nclass Feed {\n public readonly defaultOptions: FeedClientOptions;\n public readonly referenceId: string;\n public unsubscribeFromSocketEvents: (() => void) | undefined = undefined;\n private socketManager: FeedSocketManager | undefined;\n private userFeedId: string;\n private broadcaster: EventEmitter;\n private broadcastChannel!: BroadcastChannel | null;\n private disconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private hasSubscribedToRealTimeUpdates: boolean = false;\n private visibilityChangeHandler: () => void = () => {};\n private visibilityChangeListenerConnected: boolean = false;\n\n // The raw store instance, used for binding in React and other environments\n public store: FeedStore;\n\n constructor(\n readonly knock: Knock,\n readonly feedId: string,\n options: FeedClientOptions,\n socketManager: FeedSocketManager | undefined,\n ) {\n if (!feedId || !isValidUuid(feedId)) {\n this.knock.log(\n \"[Feed] Invalid or missing feedId provided to the Feed constructor. The feed should be a UUID of an in-app feed channel (`in_app_feed`) found in the Knock dashboard. Please provide a valid feedId to the Feed constructor.\",\n true,\n );\n }\n\n this.feedId = feedId;\n this.userFeedId = this.buildUserFeedId();\n this.referenceId = CLIENT_REF_ID_PREFIX + nanoid();\n this.socketManager = socketManager;\n this.store = createStore();\n this.broadcaster = new EventEmitter({ wildcard: true, delimiter: \".\" });\n this.defaultOptions = {\n ...feedClientDefaults,\n ...mergeDateRangeParams(options),\n };\n this.knock.log(`[Feed] Initialized a feed on channel ${feedId}`);\n\n // Attempt to setup a realtime connection (does not join)\n this.initializeRealtimeConnection();\n\n this.setupBroadcastChannel();\n }\n\n /**\n * Used to reinitialize a current feed instance, which is useful when reauthenticating users\n */\n reinitialize(socketManager?: FeedSocketManager) {\n this.socketManager = socketManager;\n\n // Reinitialize the user feed id incase the userId changed\n this.userFeedId = this.buildUserFeedId();\n\n // Reinitialize the real-time connection\n this.initializeRealtimeConnection();\n\n // Reinitialize our broadcast channel\n this.setupBroadcastChannel();\n }\n\n /**\n * Cleans up a feed instance by destroying the store and disconnecting\n * an open socket connection.\n */\n teardown() {\n this.knock.log(\"[Feed] Tearing down feed instance\");\n\n this.socketManager?.leave(this);\n\n this.tearDownVisibilityListeners();\n\n if (this.disconnectTimer) {\n clearTimeout(this.disconnectTimer);\n this.disconnectTimer = null;\n }\n\n if (this.broadcastChannel) {\n this.broadcastChannel.close();\n }\n }\n\n /** Tears down an instance and removes it entirely from the feed manager */\n dispose() {\n this.knock.log(\"[Feed] Disposing of feed instance\");\n this.teardown();\n this.broadcaster.removeAllListeners();\n this.knock.feeds.removeInstance(this);\n }\n\n /*\n Initializes a real-time connection to Knock, connecting the websocket for the\n current ApiClient instance if the socket is not already connected.\n */\n listenForUpdates() {\n this.knock.log(\"[Feed] Connecting to real-time service\");\n\n this.hasSubscribedToRealTimeUpdates = true;\n\n // If the user is not authenticated, then do nothing\n if (!this.knock.isAuthenticated()) {\n this.knock.log(\n \"[Feed] User is not authenticated, skipping listening for updates\",\n );\n return;\n }\n\n this.unsubscribeFromSocketEvents = this.socketManager?.join(this);\n }\n\n /* Binds a handler to be invoked when event occurs */\n on(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.on(eventName, callback);\n }\n\n off(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.off(eventName, callback);\n }\n\n getState() {\n return this.store.getState();\n }\n\n async markAsSeen(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"seen\",\n { seen_at: now },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"seen\");\n }\n\n async markAllAsSeen() {\n // To mark all of the messages as seen we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unseen_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unseen`\n // items by removing everything from view.\n const { metadata, items, ...state } = this.store.getState();\n\n const isViewingOnlyUnseen = this.defaultOptions.status === \"unseen\";\n\n // If we're looking at the unseen view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnseen) {\n state.resetStore({\n ...metadata,\n total_count: 0,\n unseen_count: 0,\n });\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n state.setMetadata({ ...metadata, unseen_count: 0 });\n\n const attrs = { seen_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n state.setItemAttrs(itemIds, attrs);\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"seen\");\n this.emitEvent(\"all_seen\", items);\n\n return result;\n }\n\n async markAsUnseen(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unseen\",\n { seen_at: null },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unseen\");\n }\n\n async markAsRead(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"read\",\n { read_at: now },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"read\");\n }\n\n async markAllAsRead() {\n // To mark all of the messages as read we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unread_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unread_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unread`\n // items by removing everything from view.\n const { metadata, items, ...state } = this.store.getState();\n\n const isViewingOnlyUnread = this.defaultOptions.status === \"unread\";\n\n // If we're looking at the unread view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnread) {\n state.resetStore({\n ...metadata,\n total_count: 0,\n unread_count: 0,\n });\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n state.setMetadata({ ...metadata, unread_count: 0 });\n\n const attrs = { read_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n state.setItemAttrs(itemIds, attrs);\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"read\");\n this.emitEvent(\"all_read\", items);\n\n return result;\n }\n\n async markAsUnread(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unread\",\n { read_at: null },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unread\");\n }\n\n async markAsInteracted(\n itemOrItems: FeedItemOrItems,\n metadata?: Record<string, string>,\n ) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"interacted\",\n {\n read_at: now,\n interacted_at: now,\n },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"interacted\", metadata);\n }\n\n /*\n Marking one or more items as archived should:\n\n - Decrement the badge count for any unread / unseen items\n - Remove the item from the feed list when the `archived` flag is \"exclude\" (default)\n\n TODO: how do we handle rollbacks?\n */\n async markAsArchived(itemOrItems: FeedItemOrItems) {\n const state = this.store.getState();\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n\n const itemIds: string[] = items.map((item) => item.id);\n\n /*\n In the code here we want to optimistically update counts and items\n that are persisted such that we can display updates immediately on the feed\n without needing to make a network request.\n\n Note: right now this does *not* take into account offline handling or any extensive retry\n logic, so rollbacks aren't considered. That probably needs to be a future consideration for\n this library.\n\n Scenarios to consider:\n\n ## Feed scope to archived *only*\n\n - Counts should not be decremented\n - Items should not be removed\n\n ## Feed scoped to exclude archived items (the default)\n\n - Counts should be decremented\n - Items should be removed\n\n ## Feed scoped to include archived items as well\n\n - Counts should not be decremented\n - Items should not be removed\n */\n\n if (shouldOptimisticallyRemoveItems) {\n // If any of the items are unseen or unread, then capture as we'll want to decrement\n // the counts for these in the metadata we have\n const unseenCount = items.filter((i) => !i.seen_at).length;\n const unreadCount = items.filter((i) => !i.read_at).length;\n\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n // Ensure that the counts don't ever go below 0 on archiving where the client state\n // gets out of sync with the server state\n total_count: Math.max(0, state.metadata.total_count - items.length),\n unseen_count: Math.max(0, state.metadata.unseen_count - unseenCount),\n unread_count: Math.max(0, state.metadata.unread_count - unreadCount),\n };\n\n // Remove the archiving entries\n const entriesToSet = state.items.filter(\n (item) => !itemIds.includes(item.id),\n );\n\n state.setResult({\n entries: entriesToSet,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n return this.makeStatusUpdate(itemOrItems, \"archived\");\n }\n\n async markAllAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { items, ...state } = this.store.getState();\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Reset the store to clear out all of items and reset the badge count\n state.resetStore();\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n const itemIds = items.map((i) => i.id);\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n this.emitEvent(\"all_archived\", items);\n\n return result;\n }\n\n async markAllReadAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { items, ...state } = this.store.getState();\n // Filter items to only include those that are unread\n const unreadItems = items.filter((item) => item.read_at === null);\n // Mark all the unread items as archived and read\n const itemIds = unreadItems.map((i) => i.id);\n state.setItemAttrs(itemIds, {\n archived_at: new Date().toISOString(),\n });\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Remove all the read items from the store and reset the badge count\n const remainingItems = items.filter((item) => !itemIds.includes(item.id));\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n total_count: remainingItems.length,\n unread_count: 0,\n };\n\n state.setResult({\n entries: remainingItems,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n // this.emitEvent(\"all_archived\", readItems);\n\n return result;\n }\n\n async markAsUnarchived(itemOrItems: FeedItemOrItems) {\n const state = this.store.getState();\n\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n\n const itemIds: string[] = items.map((item) => item.id);\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"only\";\n\n if (shouldOptimisticallyRemoveItems) {\n // If any of the items are unseen or unread, then capture as we'll want to decrement\n // the counts for these in the metadata we have\n const unseenCount = items.filter((i) => !i.seen_at).length;\n const unreadCount = items.filter((i) => !i.read_at).length;\n\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n // Ensure that the counts don't ever go below 0 on unarchiving where the client state\n // gets out of sync with the server state\n total_count: Math.max(0, state.metadata.total_count - items.length),\n unseen_count: Math.max(0, state.metadata.unseen_count - unseenCount),\n unread_count: Math.max(0, state.metadata.unread_count - unreadCount),\n };\n\n // Remove the unarchived entries\n const entriesToSet = state.items.filter(\n (item) => !itemIds.includes(item.id),\n );\n\n state.setResult({\n entries: entriesToSet,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n } else {\n this.optimisticallyPerformStatusUpdate(itemOrItems, \"unarchived\", {\n archived_at: null,\n });\n }\n\n return this.makeStatusUpdate(itemOrItems, \"unarchived\");\n }\n\n /* Fetches the feed content, appending it to the store */\n async fetch(options: FetchFeedOptions = {}) {\n const { networkStatus, ...state } = this.store.getState();\n\n // If the user is not authenticated, then do nothing\n if (!this.knock.isAuthenticated()) {\n this.knock.log(\"[Feed] User is not authenticated, skipping fetch\");\n return;\n }\n\n // If there's an existing request in flight, then do nothing\n if (isRequestInFlight(networkStatus)) {\n this.knock.log(\"[Feed] Request is in flight, skipping fetch\");\n return;\n }\n\n // Set the loading type based on the request type it is\n state.setNetworkStatus(options.__loadingType ?? NetworkStatus.loading);\n\n // trigger_data should be a JSON string for the API\n // this function will format the trigger data if it's an object\n // https://docs.knock.app/reference#get-feed\n const formattedTriggerData = getFormattedTriggerData({\n ...this.defaultOptions,\n ...options,\n });\n\n // Always include the default params, if they have been set\n const queryParams: FetchFeedOptionsForRequest = {\n ...this.defaultOptions,\n ...mergeDateRangeParams(options),\n trigger_data: formattedTriggerData,\n // Unset options that should not be sent to the API\n __loadingType: undefined,\n __fetchSource: undefined,\n __experimentalCrossBrowserUpdates: undefined,\n auto_manage_socket_connection: undefined,\n auto_manage_socket_connection_delay: undefined,\n };\n\n const result = await this.knock.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,\n params: queryParams,\n });\n\n if (result.statusCode === \"error\" || !result.body) {\n state.setNetworkStatus(NetworkStatus.error);\n\n return {\n status: result.statusCode,\n data: result.error || result.body,\n };\n }\n\n const response = {\n entries: result.body.entries,\n meta: result.body.meta,\n page_info: result.body.page_info,\n };\n\n if (options.before) {\n const opts = { shouldSetPage: false, shouldAppend: true };\n state.setResult(response, opts);\n } else if (options.after) {\n const opts = { shouldSetPage: true, shouldAppend: true };\n state.setResult(response, opts);\n } else {\n state.setResult(response);\n }\n\n // Legacy `messages.new` event, should be removed in a future version\n this.broadcast(\"messages.new\", response);\n\n // Broadcast the appropriate event type depending on the fetch source\n const feedEventType: FeedEvent =\n options.__fetchSource === \"socket\"\n ? \"items.received.realtime\"\n : \"items.received.page\";\n\n const eventPayload = {\n items: response.entries as FeedItem[],\n metadata: response.meta as FeedMetadata,\n event: feedEventType,\n };\n\n this.broadcast(eventPayload.event, eventPayload);\n\n return { data: response, status: result.statusCode };\n }\n\n async fetchNextPage(options: FetchFeedOptions = {}) {\n // Attempts to fetch the next page of results (if we have any)\n const { pageInfo } = this.store.getState();\n\n if (!pageInfo.after) {\n // Nothing more to fetch\n return;\n }\n\n this.fetch({\n ...options,\n after: pageInfo.after,\n __loadingType: NetworkStatus.fetchMore,\n });\n }\n\n get socketChannelTopic(): string {\n return `feeds:${this.userFeedId}`;\n }\n\n private broadcast(\n eventName: FeedEvent,\n data: FeedResponse | FeedEventPayload,\n ) {\n this.broadcaster.emit(eventName, data);\n }\n\n // Invoked when a new real-time message comes in from the socket\n private async onNewMessageReceived({ data }: FeedMessagesReceivedPayload) {\n this.knock.log(\"[Feed] Received new real-time message\");\n\n // Handle the new message coming in\n const { items, ...state } = this.store.getState();\n const currentHead: FeedItem | undefined = items[0];\n\n // Optimistically set the badge counts\n const metadata = data[this.referenceId]?.metadata;\n if (metadata) {\n state.setMetadata(metadata);\n }\n\n // Fetch the items before the current head (if it exists)\n this.fetch({ before: currentHead?.__cursor, __fetchSource: \"socket\" });\n }\n\n private buildUserFeedId() {\n return `${this.feedId}:${this.knock.userId}`;\n }\n\n private optimisticallyPerformStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n attrs: object,\n badgeCountAttr?: \"unread_count\" | \"unseen_count\",\n ) {\n const state = this.store.getState();\n const normalizedItems = Array.isArray(itemOrItems)\n ? itemOrItems\n : [itemOrItems];\n const itemIds = normalizedItems.map((item) => item.id);\n\n if (badgeCountAttr) {\n const { metadata } = state;\n\n // We only want to update the counts of items that have not already been counted towards the\n // badge count total to avoid updating the badge count unnecessarily.\n const itemsToUpdate = normalizedItems.filter((item) => {\n switch (type) {\n case \"seen\":\n return item.seen_at === null;\n case \"unseen\":\n return item.seen_at !== null;\n case \"read\":\n case \"interacted\":\n return item.read_at === null;\n case \"unread\":\n return item.read_at !== null;\n default:\n return true;\n }\n });\n\n // This is a hack to determine the direction of whether we're\n // adding or removing from the badge count\n const direction = type.startsWith(\"un\")\n ? itemsToUpdate.length\n : -itemsToUpdate.length;\n\n state.setMetadata({\n ...metadata,\n [badgeCountAttr]: Math.max(0, metadata[badgeCountAttr] + direction),\n });\n }\n\n // Update the items with the given attributes\n state.setItemAttrs(itemIds, attrs);\n }\n\n private async makeStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n metadata?: Record<string, string>,\n ) {\n // Always treat items as a batch to use the corresponding batch endpoint\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n const itemIds = items.map((item) => item.id);\n\n const result = await this.knock.messages.batchUpdateStatuses(\n itemIds,\n type,\n { metadata },\n );\n\n // Emit the event that these items had their statuses changed\n // Note: we do this after the update to ensure that the server event actually completed\n this.emitEvent(type, items);\n\n return result;\n }\n\n private async makeBulkStatusUpdate(\n status: BulkUpdateMessagesInChannelProperties[\"status\"],\n ) {\n // The base scope for the call should take into account all of the options currently\n // set on the feed, as well as being scoped for the current user. We do this so that\n // we ONLY make changes to the messages that are currently in view on this feed, and not\n // all messages that exist.\n const options = {\n user_ids: [this.knock.userId!],\n engagement_status:\n this.defaultOptions.status !== \"all\"\n ? this.defaultOptions.status\n : undefined,\n archived: this.defaultOptions.archived,\n has_tenant: this.defaultOptions.has_tenant,\n tenants: this.defaultOptions.tenant\n ? [this.defaultOptions.tenant]\n : undefined,\n };\n\n return await this.knock.messages.bulkUpdateAllStatusesInChannel({\n channelId: this.feedId,\n status,\n options,\n });\n }\n\n private setupBroadcastChannel() {\n // Attempt to bind to listen to other events from this feed in different tabs\n // Note: here we ensure `self` is available (it's not in server rendered envs)\n this.broadcastChannel =\n typeof self !== \"undefined\" && \"BroadcastChannel\" in self\n ? new BroadcastChannel(`knock:feed:${this.userFeedId}`)\n : null;\n\n // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast\n // channel (iff it's enabled and exists)\n if (\n this.broadcastChannel &&\n this.defaultOptions.__experimentalCrossBrowserUpdates === true\n ) {\n this.broadcastChannel.onmessage = (e) => {\n switch (e.data.type) {\n case \"items:archived\":\n case \"items:unarchived\":\n case \"items:seen\":\n case \"items:unseen\":\n case \"items:read\":\n case \"items:unread\":\n case \"items:all_read\":\n case \"items:all_seen\":\n case \"items:all_archived\":\n // When items are updated in any other tab, simply refetch to get the latest state\n // to make sure that the state gets updated accordingly. In the future here we could\n // maybe do this optimistically without the fetch.\n return this.fetch();\n default:\n return null;\n }\n };\n }\n }\n\n private broadcastOverChannel(type: string, payload: GenericData) {\n // The broadcastChannel may not be available in non-browser environments\n if (!this.broadcastChannel) {\n return;\n }\n\n // Here we stringify our payload and try and send as JSON such that we\n // don't get any `An object could not be cloned` errors when trying to broadcast\n try {\n const stringifiedPayload = JSON.parse(JSON.stringify(payload));\n\n this.broadcastChannel.postMessage({\n type,\n payload: stringifiedPayload,\n });\n } catch (e) {\n console.warn(`Could not broadcast ${type}, got error: ${e}`);\n }\n }\n\n private initializeRealtimeConnection() {\n // In server environments we might not have a socket connection\n if (!this.socketManager) return;\n\n if (this.defaultOptions.auto_manage_socket_connection) {\n this.setUpVisibilityListeners();\n }\n\n // If we're initializing but they have previously opted to listen to real-time updates\n // then we will automatically reconnect on their behalf\n if (this.hasSubscribedToRealTimeUpdates && this.knock.isAuthenticated()) {\n this.unsubscribeFromSocketEvents = this.socketManager?.join(this);\n }\n }\n\n async handleSocketEvent(payload: SocketEventPayload) {\n switch (payload.event) {\n case SocketEventType.NewMessage:\n this.onNewMessageReceived(payload);\n return;\n default: {\n const _exhaustiveCheck: never = payload.event;\n return;\n }\n }\n }\n\n /**\n * Listen for changes to document visibility and automatically disconnect\n * or reconnect the socket after a delay\n */\n private setUpVisibilityListeners() {\n if (\n typeof document === \"undefined\" ||\n this.visibilityChangeListenerConnected\n ) {\n return;\n }\n\n this.visibilityChangeHandler = this.handleVisibilityChange.bind(this);\n this.visibilityChangeListenerConnected = true;\n document.addEventListener(\"visibilitychange\", this.visibilityChangeHandler);\n }\n\n private tearDownVisibilityListeners() {\n if (typeof document === \"undefined\") return;\n\n document.removeEventListener(\n \"visibilitychange\",\n this.visibilityChangeHandler,\n );\n this.visibilityChangeListenerConnected = false;\n }\n\n private emitEvent(\n type:\n | MessageEngagementStatus\n | \"all_read\"\n | \"all_seen\"\n | \"all_archived\"\n | \"unread\"\n | \"unseen\"\n | \"unarchived\",\n items: FeedItem[],\n ) {\n // Handle both `items.` and `items:` format for events for compatibility reasons\n this.broadcaster.emit(`items.${type}`, { items });\n this.broadcaster.emit(`items:${type}`, { items });\n // Internal events only need `items:`\n this.broadcastOverChannel(`items:${type}`, { items });\n }\n\n private handleVisibilityChange() {\n const disconnectDelay =\n this.defaultOptions.auto_manage_socket_connection_delay ??\n DEFAULT_DISCONNECT_DELAY;\n\n const client = this.knock.client();\n\n if (document.visibilityState === \"hidden\") {\n // When the tab is hidden, clean up the socket connection after a delay\n this.disconnectTimer = setTimeout(() => {\n client.socket?.disconnect();\n this.disconnectTimer = null;\n }, disconnectDelay);\n } else if (document.visibilityState === \"visible\") {\n // When the tab is visible, clear the disconnect timer if active to cancel disconnecting\n // This handles cases where the tab is only briefly hidden to avoid unnecessary disconnects\n if (this.disconnectTimer) {\n clearTimeout(this.disconnectTimer);\n this.disconnectTimer = null;\n }\n\n // If the socket is not connected, try to reconnect\n if (!client.socket?.isConnected()) {\n client.socket?.connect();\n }\n }\n }\n}\n\nexport default Feed;\n"],"names":["feedClientDefaults","DEFAULT_DISCONNECT_DELAY","CLIENT_REF_ID_PREFIX","Feed","knock","feedId","options","socketManager","__publicField","isValidUuid","nanoid","createStore","EventEmitter","mergeDateRangeParams","_a","eventName","callback","itemOrItems","now","metadata","items","state","attrs","itemIds","item","result","shouldOptimisticallyRemoveItems","unseenCount","i","unreadCount","updatedMetadata","entriesToSet","remainingItems","networkStatus","isRequestInFlight","NetworkStatus","formattedTriggerData","getFormattedTriggerData","queryParams","response","opts","feedEventType","eventPayload","pageInfo","data","currentHead","type","badgeCountAttr","normalizedItems","itemsToUpdate","direction","status","e","payload","stringifiedPayload","SocketEventType","disconnectDelay","client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":["../../../../src/clients/feed/utils.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"utils.mjs","sources":["../../../../src/clients/feed/utils.ts"],"sourcesContent":["import type {\n FeedClientOptions,\n FeedItem,\n FetchFeedOptionsForRequest,\n} from \"./interfaces\";\n\nexport function deduplicateItems(items: FeedItem[]): FeedItem[] {\n const seen: Record<string, boolean> = {};\n const values: FeedItem[] = [];\n\n return items.reduce((acc, item) => {\n if (seen[item.id]) {\n return acc;\n }\n\n seen[item.id] = true;\n return [...acc, item];\n }, values);\n}\n\nexport function sortItems(items: FeedItem[]) {\n return items.sort((a, b) => {\n return (\n new Date(b.inserted_at).getTime() - new Date(a.inserted_at).getTime()\n );\n });\n}\n\ntype DateRangeParams = Pick<\n FetchFeedOptionsForRequest,\n \"inserted_at.gte\" | \"inserted_at.lte\" | \"inserted_at.gt\" | \"inserted_at.lt\"\n>;\n\nexport function mergeDateRangeParams(options: FeedClientOptions) {\n const { inserted_at_date_range, ...rest } = options;\n\n if (!inserted_at_date_range) {\n return rest;\n }\n\n const dateRangeParams: DateRangeParams = {};\n\n // Determine which operators to use based on the inclusive flag\n const isInclusive = inserted_at_date_range.inclusive ?? false;\n\n // For start date: use gte if inclusive, gt if not\n if (inserted_at_date_range.start) {\n const startOperator = isInclusive ? \"inserted_at.gte\" : \"inserted_at.gt\";\n dateRangeParams[startOperator] = inserted_at_date_range.start;\n }\n\n // For end date: use lte if inclusive, lt if not\n if (inserted_at_date_range.end) {\n const endOperator = isInclusive ? \"inserted_at.lte\" : \"inserted_at.lt\";\n dateRangeParams[endOperator] = inserted_at_date_range.end;\n }\n\n return { ...rest, ...dateRangeParams };\n}\n\n// If the trigger data is an object, stringify it to conform to API expectations\n// https://docs.knock.app/reference#get-feed\n// We also want to be careful to check for string values already,\n// because this was a bug (KNO-7843) and customers had to manually stringify their trigger data\nexport function getFormattedTriggerData(options: FeedClientOptions) {\n // If the trigger data is an object, stringify it to conform to API expectations\n if (typeof options?.trigger_data === \"object\") {\n return JSON.stringify(options.trigger_data);\n }\n\n // For when the trigger data is already formatted as a string by the user\n if (typeof options?.trigger_data === \"string\") {\n return options.trigger_data;\n }\n\n return undefined;\n}\n"],"names":["deduplicateItems","items","seen","values","acc","item","sortItems","a","b","mergeDateRangeParams","options","inserted_at_date_range","rest","dateRangeParams","isInclusive","startOperator","endOperator","getFormattedTriggerData"],"mappings":"AAMO,SAASA,EAAiBC,GAA+B;AAC9D,QAAMC,IAAgC,CAAC,GACjCC,IAAqB,CAAC;AAE5B,SAAOF,EAAM,OAAO,CAACG,GAAKC,MACpBH,EAAKG,EAAK,EAAE,IACPD,KAGJF,EAAAG,EAAK,EAAE,IAAI,IACT,CAAC,GAAGD,GAAKC,CAAI,IACnBF,CAAM;AACX;AAEO,SAASG,EAAUL,GAAmB;AAC3C,SAAOA,EAAM,KAAK,CAACM,GAAGC,MAElB,IAAI,KAAKA,EAAE,WAAW,EAAE,YAAY,IAAI,KAAKD,EAAE,WAAW,EAAE,QAAQ,CAEvE;AACH;AAOO,SAASE,EAAqBC,GAA4B;AAC/D,QAAM,EAAE,wBAAAC,GAAwB,GAAGC,EAAA,IAASF;AAE5C,MAAI,CAACC;AACI,WAAAC;AAGT,QAAMC,IAAmC,CAAC,GAGpCC,IAAcH,EAAuB,aAAa;AAGxD,MAAIA,EAAuB,OAAO;AAC1B,UAAAI,IAAgBD,IAAc,oBAAoB;AACxC,IAAAD,EAAAE,CAAa,IAAIJ,EAAuB;AAAA,EAAA;AAI1D,MAAIA,EAAuB,KAAK;AACxB,UAAAK,IAAcF,IAAc,oBAAoB;AACtC,IAAAD,EAAAG,CAAW,IAAIL,EAAuB;AAAA,EAAA;AAGxD,SAAO,EAAE,GAAGC,GAAM,GAAGC,EAAgB;AACvC;AAMO,SAASI,EAAwBP,GAA4B;AAE9D,MAAA,QAAOA,KAAA,gBAAAA,EAAS,iBAAiB;AAC5B,WAAA,KAAK,UAAUA,EAAQ,YAAY;AAIxC,MAAA,QAAOA,KAAA,gBAAAA,EAAS,iBAAiB;AACnC,WAAOA,EAAQ;AAInB;"}
|