|
| 1 | +# DataManager Hooks |
| 2 | + |
| 3 | +This directory contains React hooks for the DataManager library, following modern React patterns with TanStack Query for data fetching. |
| 4 | + |
| 5 | +## Available Hooks |
| 6 | + |
| 7 | +### `useActions` |
| 8 | + |
| 9 | +A hook for fetching available actions from the DataManager API using TanStack Query. |
| 10 | + |
| 11 | +#### Features |
| 12 | + |
| 13 | +- **Automatic caching**: Actions are cached for 5 minutes by default |
| 14 | +- **Lazy loading**: Only fetches when enabled (e.g., when dropdown is opened) |
| 15 | +- **Error handling**: Built-in error states |
| 16 | +- **Loading states**: Provides both initial loading and refetching states |
| 17 | +- **Type safety**: Full TypeScript support |
| 18 | + |
| 19 | +#### Usage |
| 20 | + |
| 21 | +```tsx |
| 22 | +import { useActions } from "../hooks/useActions"; |
| 23 | + |
| 24 | +function ActionsDropdown({ projectId }) { |
| 25 | + const { |
| 26 | + actions, |
| 27 | + isLoading, |
| 28 | + isError, |
| 29 | + error, |
| 30 | + refetch, |
| 31 | + isFetching, |
| 32 | + } = useActions({ |
| 33 | + projectId, // Optional: Used for cache scoping per project |
| 34 | + enabled: isOpen, // Only fetch when dropdown is opened |
| 35 | + staleTime: 5 * 60 * 1000, // Optional: 5 minutes |
| 36 | + cacheTime: 10 * 60 * 1000, // Optional: 10 minutes |
| 37 | + }); |
| 38 | + |
| 39 | + if (isLoading) return <div>Loading...</div>; |
| 40 | + if (isError) return <div>Error: {error.message}</div>; |
| 41 | + |
| 42 | + return ( |
| 43 | + <div> |
| 44 | + {actions.map((action) => ( |
| 45 | + <button key={action.id}>{action.title}</button> |
| 46 | + ))} |
| 47 | + </div> |
| 48 | + ); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +#### Parameters |
| 53 | + |
| 54 | +- `options.projectId` (string, optional): Project ID for scoping the query cache. When provided, actions are cached per project, preventing cache conflicts in multi-project scenarios |
| 55 | +- `options.enabled` (boolean, default: `true`): Whether to enable the query |
| 56 | +- `options.staleTime` (number, default: `5 * 60 * 1000`): Time in ms before data is considered stale |
| 57 | +- `options.cacheTime` (number, default: `10 * 60 * 1000`): Time in ms before unused data is garbage collected |
| 58 | + |
| 59 | +#### Return Value |
| 60 | + |
| 61 | +- `actions` (Action[]): Array of available actions |
| 62 | +- `isLoading` (boolean): True on first load |
| 63 | +- `isFetching` (boolean): True whenever data is being fetched |
| 64 | +- `isError` (boolean): True if the query failed |
| 65 | +- `error` (Error): The error object if query failed |
| 66 | +- `refetch` (function): Function to manually refetch the data |
| 67 | + |
| 68 | +### `useDataManagerUsers` |
| 69 | + |
| 70 | +A hook for fetching users from the DataManager API with infinite pagination support. |
| 71 | + |
| 72 | +See `useUsers.ts` for documentation. |
| 73 | + |
| 74 | +### Other Hooks |
| 75 | + |
| 76 | +- `useFirstMountState`: Utility hook to detect first mount |
| 77 | +- `useUpdateEffect`: Effect hook that skips the first render |
| 78 | + |
| 79 | +## Migration from MobX to TanStack Query |
| 80 | + |
| 81 | +The DataManager is gradually migrating from MobX State Tree flows to TanStack Query hooks for better performance, caching, and developer experience. |
| 82 | + |
| 83 | +### Why TanStack Query? |
| 84 | + |
| 85 | +1. **Automatic caching**: Reduces unnecessary API calls |
| 86 | +2. **Better loading states**: Built-in loading, error, and refetching states |
| 87 | +3. **Background refetching**: Keeps data fresh automatically |
| 88 | +4. **Query invalidation**: Easy cache management |
| 89 | +5. **TypeScript support**: Full type safety out of the box |
| 90 | +6. **React best practices**: Follows modern React patterns recommended in project rules |
| 91 | + |
| 92 | +### Coexistence with MobX |
| 93 | + |
| 94 | +The hooks replace the need for MobX flows for data fetching: |
| 95 | + |
| 96 | +- **Old code**: `store.fetchActions()` is now deprecated but kept for backward compatibility |
| 97 | +- **New code**: Should always use `useActions()` hook |
| 98 | +- **Migration complete**: The actions endpoint is now only called via the `useActions` hook, preventing duplicate API calls |
| 99 | + |
| 100 | +### Example Migration |
| 101 | + |
| 102 | +**Before (MobX):** |
| 103 | +```javascript |
| 104 | +useEffect(() => { |
| 105 | + if (isOpen && actions.length === 0) { |
| 106 | + setIsLoading(true); |
| 107 | + store.fetchActions().finally(() => { |
| 108 | + setIsLoading(false); |
| 109 | + }); |
| 110 | + } |
| 111 | +}, [isOpen, actions.length, store]); |
| 112 | +``` |
| 113 | + |
| 114 | +**After (TanStack Query):** |
| 115 | +```typescript |
| 116 | +const { actions, isLoading, isFetching } = useActions({ |
| 117 | + projectId, // Optional: for cache scoping |
| 118 | + enabled: isOpen, |
| 119 | +}); |
| 120 | +``` |
| 121 | + |
| 122 | +## Best Practices |
| 123 | + |
| 124 | +1. **Use lazy loading**: Set `enabled: false` for data that's not immediately needed |
| 125 | +2. **Scope by projectId**: Always pass `projectId` when working with project-specific data to prevent cache conflicts |
| 126 | +3. **Configure cache times**: Adjust `staleTime` and `cacheTime` based on data freshness requirements |
| 127 | +4. **Handle loading states**: Always provide loading UI for better UX |
| 128 | +5. **Handle errors**: Display user-friendly error messages |
| 129 | +6. **Type everything**: Use TypeScript interfaces for type safety |
| 130 | + |
| 131 | +## QueryClient Setup |
| 132 | + |
| 133 | +The QueryClient is configured at the app level in `App.tsx`: |
| 134 | + |
| 135 | +```typescript |
| 136 | +import { QueryClientProvider } from "@tanstack/react-query"; |
| 137 | +import { queryClient } from "@humansignal/core/lib/utils/query-client"; |
| 138 | + |
| 139 | +<QueryClientProvider client={queryClient}> |
| 140 | + <Provider store={app}> |
| 141 | + {/* App content */} |
| 142 | + </Provider> |
| 143 | +</QueryClientProvider> |
| 144 | +``` |
| 145 | + |
| 146 | +This ensures all hooks have access to the shared query cache. |
| 147 | + |
0 commit comments