UNPKG

@lobehub/chat

Version:

Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.

49 lines (40 loc) 1.17 kB
import { produce } from 'immer'; import { ThreadItem } from '@/types/topic'; interface UpdateThreadAction { id: string; type: 'updateThread'; value: Partial<ThreadItem>; } interface DeleteThreadAction { id: string; type: 'deleteThread'; } export type ThreadDispatch = UpdateThreadAction | DeleteThreadAction; export const threadReducer = (state: ThreadItem[] = [], payload: ThreadDispatch): ThreadItem[] => { switch (payload.type) { case 'updateThread': { return produce(state, (draftState) => { const { value, id } = payload; const threadIndex = draftState.findIndex((thread) => thread.id === id); if (threadIndex !== -1) { draftState[threadIndex] = { ...draftState[threadIndex], ...value, updatedAt: new Date(), }; } }); } case 'deleteThread': { return produce(state, (draftState) => { const threadIndex = draftState.findIndex((thread) => thread.id === payload.id); if (threadIndex !== -1) { draftState.splice(threadIndex, 1); } }); } default: { return state; } } };