tahamajs/IE / Projects /front /src /components /threads /ThreadContextProvider.tsx
tahamajs's picture
download
raw
1.42 kB
// src/components/threads/ThreadContextProvider.tsx
import React, { createContext, useContext, useMemo } from "react";
import { useThreadSystem } from "@/hooks/useThreadSystem";
import { useThreadContext } from "@/hooks/useThreadContext";
import { useThreadPersistence } from "@/hooks/useThreadPersistence";
interface ThreadContextValue
extends ReturnType<typeof useThreadSystem>,
ReturnType<typeof useThreadContext>,
ReturnType<typeof useThreadPersistence> {}
const ThreadCtx = createContext<ThreadContextValue | null>(null);
interface ThreadContextProviderProps {
chatId: string;
children: React.ReactNode;
}
export function ThreadContextProvider({ chatId, children }: ThreadContextProviderProps) {
const threadSystem = useThreadSystem(chatId);
const threadCtx = useThreadContext(threadSystem.activeThreadId);
const persistence = useThreadPersistence(chatId);
// Memoize the combined value to avoid unnecessary re-renders of consumers
const value = useMemo<ThreadContextValue>(
() => ({
...threadSystem,
...threadCtx,
...persistence,
}),
[threadSystem, threadCtx, persistence]
);
return <ThreadCtx.Provider value={value}>{children}</ThreadCtx.Provider>;
}
export function useThreadContextValue() {
const ctx = useContext(ThreadCtx);
if (!ctx) {
throw new Error("useThreadContextValue must be used within ThreadContextProvider");
}
return ctx;
}

Xet Storage Details

Size:
1.42 kB
·
Xet hash:
9adcf844b446ec0969e24f1631a530d6b9781daf4533790ac26217d74013d413

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.