Web State Management (Zustand & React Query)
Relevant source files
The following files were used as context for generating this wiki page:
The Ever Teams web application employs a hybrid state management strategy, leveraging two distinct libraries for separate concerns: Jotai for global client-side UI state and React Query for managing server state. This dual approach provides a clear separation between ephemeral UI state (like modal visibility or form data) and cached data from the server (like tasks, projects, and user information).
Jotai offers a minimalistic, atom-based API for managing shared state within the React tree. React Query, on the other hand, excels at fetching, caching, synchronizing, and updating server data, providing a robust framework for handling API interactions, including loading states, error handling, and cache invalidation. This architecture ensures that components are decoupled from data-fetching logic and that server data is always fresh and efficiently managed.
Global UI State Management with Jotai
Jotai is the chosen library for managing global, client-side state. It uses a system of "atoms," which are small, isolated pieces of state. This approach is favored for its simplicity, scalability, and excellent developer experience. All global state atoms are strictly organized by feature domain.
Sources: apps/web/core/stores/README.md
Architecture and Folder Structure
To maintain a clean and discoverable state management system, all Jotai stores are organized by domain within the core/stores/ directory. Each subfolder represents a specific feature area.
core/stores/
├── auth/ # Auth roles, permissions, invitations
├── common/ # Shared layout & UI states
├── integrations/ # GitHub, GitLab, tenants
├── projects/ # Project-related stores
├── tasks/ # All task-related atoms
├── teams/ # Organization teams and member state
├── timer/ # Timer, logs, daily plans, time slots
├── user/ # Authenticated user data, avatars, employees
├── index.ts # Optional: central export hub
└── README.md # Documentation
This structure enforces separation of concerns and prepares the codebase for potential future migration into a dedicated packages/stores workspace.
Sources: apps/web/core/stores/README.md:8-21
Naming Conventions
Strict naming conventions are enforced to ensure clarity and prevent collisions between atoms.
| Type | Prefix | Example |
|---|---|---|
| Atom for primitives | xxxAtom | userAtom, menuOpenAtom |
| Derived atoms (get-only) | xxxDerived | activeProjectDerived |
| Selector atoms (filters) | xxxFilterAtom | taskFilterAtom |
| State with getter logic | getXxxAtom | getActiveTeamAtom |
Sources: apps/web/core/stores/README.md:25-34
Consuming State in Components
Components access Jotai state using the useAtom or useAtomValue hooks. This creates a subscription, and the component will re-render whenever the atom's value changes.
The following diagram shows how a component reads from a Jotai atom.
An example of this can be seen in the task details page, where the component reads the currently active team.
// apps/web/app/[locale]/(main)/task/[id]/page.tsx:20
const activeTeam = useAtomValue(activeTeamState);
Sources: apps/web/app/[locale]/(main)/task/[id]/page.tsx:20, apps/web/core/components/features/projects/add-or-edit-project/steps/review-summary.tsx:30
Server State Management with React Query
React Query is used to manage the entire lifecycle of server state. It simplifies data fetching, caching, and mutations by encapsulating this logic within custom hooks. This keeps components clean and focused on rendering UI.
Sources: apps/web/core/hooks/integrations/use-github-integration.ts, apps/web/core/hooks/tasks/use-issue-types.ts
Core Concepts in Practice
The application makes extensive use of useQuery for data fetching and useMutation for data modification, all wrapped in feature-specific custom hooks.
Data Fetching with useQuery
The useQuery hook is used to fetch and cache data from the API. It requires a unique queryKey and a queryFn that returns a promise. The enabled option is often used to prevent a query from running until certain conditions are met (e.g., required IDs are available).
// apps/web/core/hooks/integrations/use-github-integration.ts:32-42
const metadataQuery = useQuery({
queryKey: metadataIntegrationId
? queryKeys.integrations.github.metadata(tenantId, organizationId, metadataIntegrationId)
: ['integrations', 'github', 'metadata', 'disabled'],
queryFn: () => {
if (!metadataIntegrationId) throw new Error('Integration ID is required for metadata');
return githubService.getGithubIntegrationMetadata({ integrationId: metadataIntegrationId });
},
enabled: !!metadataIntegrationId,
staleTime: 1000 * 60 * 30, // Cache for 30 minutes
gcTime: 1000 * 60 * 60 // Keep in cache for 1 hour
});
Sources: apps/web/core/hooks/integrations/use-github-integration.ts:32-42, apps/web/core/hooks/tasks/use-issue-types.ts:28-42
Data Mutation with useMutation
The useMutation hook is used for creating, updating, or deleting data. The mutationFn performs the API call. A key feature is the onSuccess callback, which is used to invalidate cached queries, ensuring the UI is updated with fresh data after a mutation.
// apps/web/core/hooks/tasks/use-issue-types.ts:60-71
const createIssueTypeMutation = useMutation({
mutationFn: (data: IIssueTypesCreate) => {
if (!tenantId || !teamId) {
throw new Error('Required parameters missing: tenantId, teamId is required');
}
const requestData = { ...data, organizationTeamId: teamId };
return issueTypeService.createIssueType(requestData);
},
onSuccess: () => {
teamId &&
queryClient.invalidateQueries({
queryKey: queryKeys.issueTypes.byTeam(teamId)
});
}
});
Sources: apps/web/core/hooks/tasks/use-issue-types.ts:60-71, apps/web/core/hooks/integrations/use-github-integration.ts:110-132
Data Fetching and Mutation Flow
The following diagrams illustrate the typical data flows for queries and mutations.
This sequence diagram shows how a component uses a custom hook to fetch data, which is managed by React Query.
This sequence diagram shows the process of a data mutation and subsequent cache invalidation.
Integrating Jotai and React Query
A common pattern in the application is to synchronize data fetched by React Query into a Jotai atom. This decouples UI components from the data-fetching layer. Components can subscribe to a simple Jotai atom, while a custom hook manages the complex logic of fetching data with React Query and updating that atom.
Synchronization Pattern
The useIssueType hook demonstrates this pattern. It uses a useQuery to fetch issue types and a useConditionalUpdateEffect to populate the issueTypesListState Jotai atom with the fetched data.
The flow is as follows:
This logic is implemented in the use-issue-types.ts hook.
// apps/web/core/hooks/tasks/use-issue-types.ts:46-53
useConditionalUpdateEffect(
() => {
if (issueTypesQuery.data) {
setIssueTypes(issueTypesQuery.data.items);
}
},
[issueTypesQuery.data],
Boolean(issueTypes?.length)
);
This pattern ensures that components remain simple and only need to be aware of the Jotai atom, while the custom hook handles the server state logic.
Sources: apps/web/core/hooks/tasks/use-issue-types.ts:46-53, apps/web/core/hooks/integrations/use-github-integration.ts:53-63
Key Hooks and Stores
The following table summarizes some of the key state management elements found in the codebase.
| Name | Type | Purpose | Source File |
|---|---|---|---|
useGitHubIntegration | Hook | Manages all interactions with the GitHub integration API, including fetching repos and syncing. | apps/web/core/hooks/integrations/use-github-integration.ts |
useIssueType | Hook | Manages CRUD operations and fetching for issue types for a team. | apps/web/core/hooks/tasks/use-issue-types.ts |
useUserQuery | Hook | A wrapper around useQuery to fetch the current authenticated user's data. | apps/web/core/components/features/projects/add-or-edit-project/steps/review-summary.tsx:32 |
activeTeamState | Atom | Holds the state of the currently active team object. | apps/web/app/[locale]/(main)/task/[id]/page.tsx:20 |
detailedTaskState | Atom | Stores the full details of a task being viewed on the task details page. | apps/web/app/[locale]/(main)/task/[id]/page.tsx:22 |
integrationGithubRepositoriesState | Atom | Caches the list of GitHub repositories available for an integration. | apps/web/core/hooks/integrations/use-github-integration.ts:16 |
Conclusion
The state management architecture in Ever Teams is robust and well-structured. By using Jotai for client-side UI state and React Query for server state, the application achieves a clean separation of concerns. This approach leads to more maintainable code, a better developer experience, and a more performant application by leveraging React Query's powerful caching and automatic data synchronization capabilities. The pattern of using custom hooks to encapsulate data logic and sync with Jotai atoms provides a scalable and efficient way to manage state across the application.