.agents/skills/zustand/references/slice-organization.md
Key aggregation files:
src/store/chat/initialState.ts: Aggregate all slice initial statessrc/store/chat/store.ts: Define top-level ChatStore, combine all slice actionssrc/store/chat/selectors.ts: Export all slice selectorssrc/store/chat/helpers.ts: Chat helper functions// src/store/chat/initialState.ts
import { ChatTopicState, initialTopicState } from './slices/topic/initialState';
import { ChatMessageState, initialMessageState } from './slices/message/initialState';
export type ChatStoreState = ChatTopicState & ChatMessageState & ...
export const initialState: ChatStoreState = {
...initialMessageState,
...initialTopicState,
...
};
// src/store/chat/store.ts
export interface ChatStoreAction
extends ChatMessageAction, ChatTopicAction, ...
const createStore: StateCreator<ChatStore, [['zustand/devtools', never]]> = (...params) => ({
...initialState,
...chatMessage(...params),
...chatTopic(...params),
});
export const useChatStore = createWithEqualityFn<ChatStore>()(
subscribeWithSelector(devtools(createStore)),
shallow
);
src/store/chat/slices/
└── [sliceName]/
├── action.ts # Define actions (or actions/ directory)
├── initialState.ts # State structure and initial values
├── reducer.ts # (Optional) Reducer pattern
├── selectors.ts # Define selectors
└── index.ts # (Optional) Re-exports
export interface ChatTopicState {
activeTopicId?: string;
topicMaps: Record<string, ChatTopic[]>;
topicsInit: boolean;
topicLoadingIds: string[];
}
export const initialTopicState: ChatTopicState = {
activeTopicId: undefined,
topicMaps: {},
topicsInit: false,
topicLoadingIds: [],
};
const currentTopics = (s: ChatStoreState): ChatTopic[] | undefined => s.topicMaps[s.activeId];
const getTopicById =
(id: string) =>
(s: ChatStoreState): ChatTopic | undefined =>
currentTopics(s)?.find((topic) => topic.id === id);
// Core pattern: Use xxxSelectors aggregate
export const topicSelectors = {
currentTopics,
getTopicById,
};
src/store/chat/slices/aiChat/
├── actions/
│ ├── generateAIChat.ts
│ ├── rag.ts
│ ├── memory.ts
│ └── index.ts
├── initialState.ts
└── selectors.ts
topicMaps: Record<string, ChatTopic[]>;
messagesMap: Record<string, ChatMessage[]>;
messageLoadingIds: string[]
topicLoadingIds: string[]
activeId: string
activeTopicId?: string