MCPcopy Index your code
hub / github.com/coder/coder / prependToInfiniteChatsCache

Function prependToInfiniteChatsCache

site/src/api/queries/chats.ts:115–135  ·  view source on GitHub ↗
(
	queryClient: QueryClient,
	chat: TypesGen.Chat,
)

Source from the content-addressed store, hash-verified

113 * runs independently on each page.
114 */
115export const prependToInfiniteChatsCache = (
116 queryClient: QueryClient,
117 chat: TypesGen.Chat,
118) => {
119 queryClient.setQueriesData<InfiniteChatsCacheData>(
120 { queryKey: chatsKey, predicate: isChatListQuery },
121 (prev) => {
122 if (!prev?.pages) return prev;
123 // Check across ALL pages to avoid duplicates.
124 const exists = prev.pages.some((page) =>
125 page.some((c) => c.id === chat.id),
126 );
127 if (exists) return prev;
128 // Only prepend to the first page.
129 const nextPages = prev.pages.map((page, i) =>
130 i === 0 ? [chat, ...page] : page,
131 );
132 return { ...prev, pages: nextPages };
133 },
134 );
135};
136
137/**
138 * Reads the flat list of chats from the first matching infinite query

Callers 2

connectFunction · 0.90
chats.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected