๐ Built using [Crystal](https://github.com/stravu/crystal) Co-Authored-By: Crystal <crystal@stravu.com>
3.3 KiB
3.3 KiB
State Management Guidelines
โ ๏ธ IMPORTANT: Crystal follows a targeted update pattern for state management to minimize unnecessary re-renders and network requests.
Overview
Crystal uses a combination of Zustand stores, IPC events, and targeted updates to manage application state efficiently. The application prioritizes specific, targeted updates over global refreshes to improve performance and user experience.
Key Principles
- Targeted Updates: Always update only the specific data that changed
- Event-Driven Updates: Use IPC events to communicate changes between processes
- Avoid Global Refreshes: Never reload entire lists when only one item changes
- Database as Source of Truth: Frontend state should reflect backend state, not override it
State Update Patterns
Session Updates
// โ BAD: Global refresh
const handleSessionCreated = () => {
loadProjectsWithSessions(); // Reloads everything
};
// โ
GOOD: Targeted update
const handleSessionCreated = (newSession: Session) => {
setProjectsWithSessions(prevProjects => {
return prevProjects.map(project => {
if (project.id === newSession.projectId) {
return {
...project,
sessions: [...project.sessions, newSession]
};
}
return project;
});
});
};
Project Updates
// โ BAD: Reload all projects
const handleProjectDeleted = () => {
fetchProjects(); // Network request for all projects
};
// โ
GOOD: Remove from local state
const handleProjectDeleted = () => {
setProjects(prev => prev.filter(p => p.id !== deletedId));
};
IPC Event Handling
The application uses IPC events to synchronize state between the main process and renderer:
Session Events
session:created- Add new session to appropriate projectsession:updated- Update specific session propertiessession:deleted- Remove session from project list
Project Events (if implemented)
project:created- Add new project to listproject:updated- Update specific project propertiesproject:deleted- Remove project from list
When Global Refreshes Are Acceptable
- Initial Load: When component mounts for the first time
- User-Triggered Refresh: When user explicitly requests a refresh
- Error Recovery: After connection loss or critical errors
- Complex State Changes: When multiple interdependent items change
Implementation Examples
DraggableProjectTreeView.tsx
- Uses targeted updates for session creation, update, and deletion
- Only reloads all data on initial mount or when critical errors occur
- Maintains local state synchronized with backend through IPC events
ProjectSelector.tsx
- Updates project list locally when projects are deleted
- Falls back to refresh only when necessary (e.g., complex updates)
Best Practices
- Use State Setters with Callbacks: Always use the callback form of setState to ensure you're working with the latest state
- Merge Updates: When updating objects, spread existing properties to preserve data
- Handle Edge Cases: Always check if the item exists before updating
- Log State Changes: Add console logs for debugging state updates in development
- Validate IPC Data: Ensure IPC events contain expected data structure