Loading...
Loading...
Zustand state management guide. Use when working with store code (src/store/**), implementing actions, managing state, or creating slices. Triggers on Zustand store development, state management questions, or action implementation.
npx skill4agent add lobehub/lobe-chat zustandcreateTopicsendMessageinternal_*internal_internal_createTopicinternal_dispatch*internal_dispatchinternal_dispatchTopicsetmessagesMaptopicMapssetinternal_createTopic: async (params) => {
const tmpId = Date.now().toString();
// 1. Immediately update frontend (optimistic)
get().internal_dispatchTopic(
{ type: 'addTopic', value: { ...params, id: tmpId } },
'internal_createTopic'
);
// 2. Call backend service
const topicId = await topicService.createTopic(params);
// 3. Refresh for consistency
await get().refreshTopic();
return topicId;
},createTopicsendMessageinternal_createTopicinternal_updateMessageContentinternal_dispatchTopicinternal_toggleMessageLoadingmessageLoadingIdstopicEditingIdstopicMapsmessagesMapactiveTopicIdtopicsInitreferences/action-patterns.mdreferences/slice-organization.mdStateCreator(set, get, api)#private#set#getStoreSetter<T>@/store/typessetPick<ActionImpl, keyof ActionImpl>create*Slicetype Setter = StoreSetter<HomeStore>;
export const createRecentSlice = (set: Setter, get: () => HomeStore, _api?: unknown) =>
new RecentActionImpl(set, get, _api);
export class RecentActionImpl {
readonly #get: () => HomeStore;
readonly #set: Setter;
constructor(set: Setter, get: () => HomeStore, _api?: unknown) {
void _api;
this.#set = set;
this.#get = get;
}
useFetchRecentTopics = () => {
// ...
};
}
export type RecentAction = Pick<RecentActionImpl, keyof RecentActionImpl>;flattenActionsflattenActionsconst createStore: StateCreator<HomeStore, [['zustand/devtools', never]]> = (...params) => ({
...initialState,
...flattenActions<HomeStoreAction>([
createRecentSlice(...params),
createHomeInputSlice(...params),
]),
});flattenActionsPublicActions<T>type PublicActions<T> = { [K in keyof T]: T[K] };
export type ChatGroupAction = PublicActions<
ChatGroupInternalAction & ChatGroupLifecycleAction & ChatGroupMemberAction & ChatGroupCurdAction
>;
export const chatGroupAction: StateCreator<
ChatGroupStore,
[['zustand/devtools', never]],
[],
ChatGroupAction
> = (...params) =>
flattenActions<ChatGroupAction>([
new ChatGroupInternalAction(...params),
new ChatGroupLifecycleAction(...params),
new ChatGroupMemberAction(...params),
new ChatGroupCurdAction(...params),
]);ChatGroupStoreWithSwitchTopicswitchTopicChatGroupStoreWithRefreshChatGroupStoreWithInternalinternal_dispatchChatGroupStateCreator(set, get, api)#privateset/getflattenActions