Merge pull request #502 from buster-so/big-nate/bus-1371-icon-greyed-out

create a stream dynamically on change
This commit is contained in:
Nate Kelley 2025-07-14 12:42:17 -06:00 committed by GitHub
commit 4f1799c0cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 9 deletions

View File

@ -9,6 +9,7 @@ import {
import { ELECTRIC_BASE_URL } from './config';
import { useSupabaseContext } from '@/context/Supabase';
import { useEffect, useMemo, useRef } from 'react';
import { useMemoizedFn } from '../../hooks';
export type ElectricShapeOptions<T extends Row<unknown> = Row<unknown>> = Omit<
Parameters<typeof useElectricShape<T>>[0],
@ -60,7 +61,7 @@ export const useShapeStream = <T extends Row<unknown> = Row<unknown>>(
const memoParams = useMemo(() => params, [JSON.stringify(params)]);
const abortRef = useRef<AbortController>();
const stream = useMemo(() => {
const createStream = useMemoizedFn(() => {
abortRef.current?.abort();
const controller = new AbortController();
abortRef.current = controller;
@ -72,13 +73,15 @@ export const useShapeStream = <T extends Row<unknown> = Row<unknown>>(
...opts,
signal: controller.signal
});
}, [memoParams, accessToken]);
});
useEffect(() => {
if (!subscribe) {
return;
}
const stream = createStream();
let hasSyncedInitial = false;
const handler = (messages: Message<T>[]) => {
@ -107,13 +110,13 @@ export const useShapeStream = <T extends Row<unknown> = Row<unknown>>(
};
function tearDown() {
unsubscribe();
unsubscribe?.();
abortRef.current?.abort();
}
}, [
stream,
subscribe,
memoParams,
accessToken,
subscribe,
operations.join(','), // primitive dep
onUpdate,
shouldUnsubscribe

View File

@ -33,14 +33,14 @@ export const useTrackAndUpdateMessageChanges = (
) => {
const { onUpdateChatMessage, onUpdateChat } = useChatUpdate();
const getChatMemoized = useGetChatMemoized();
const subscribe = !!chatId && !!messageId && messageId !== 'undefined';
const shape = useMemo(
() => messageShape({ chatId: chatId || '', messageId }),
[chatId, messageId]
);
const subscribe = !!chatId && !!messageId && messageId !== 'undefined';
return useShapeStream(
shape,
updateOperations,
@ -61,7 +61,7 @@ export const useTrackAndUpdateMessageChanges = (
}
if (iChatMessage.is_completed) {
prefetchGetListChats()
prefetchGetListChats();
}
}
callback?.(iChatMessage);