-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: creates persisted store for Active Project Id using Zustand (#67)
* chore: add zustand * chore: createPersistedStateFunction * chore: persisted active project Id * chore: utility function to createStoreHooks * chore: create active project Id provider * chore: update app with new provider * chore: update use of hook * chore: remove unneccesary set state * chore: create example use of zustand store with active project id * chore: remove duplicate providers * chore: remove console log * chore: change function casing * chore: update casing
- Loading branch information
Showing
9 changed files
with
190 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { CssBaseline, ThemeProvider } from '@mui/material' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { RouterProvider, createRouter } from '@tanstack/react-router' | ||
|
||
import { theme } from './Theme' | ||
import { | ||
ActiveProjectIdProvider, | ||
createActiveProjectIdStore, | ||
} from './contexts/ActiveProjectIdProvider' | ||
import { ApiProvider } from './contexts/ApiContext' | ||
import { IntlProvider } from './contexts/IntlContext' | ||
import { routeTree } from './routeTree.gen' | ||
|
||
const queryClient = new QueryClient() | ||
|
||
const router = createRouter({ routeTree }) | ||
|
||
declare module '@tanstack/react-router' { | ||
interface Register { | ||
router: typeof router | ||
} | ||
} | ||
|
||
const PersistedProjectIdStore = createActiveProjectIdStore({ | ||
persist: true, | ||
}) | ||
|
||
export const App = () => ( | ||
<ThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
<IntlProvider> | ||
<QueryClientProvider client={queryClient}> | ||
<ActiveProjectIdProvider store={PersistedProjectIdStore}> | ||
<ApiProvider> | ||
<RouterProvider router={router} /> | ||
</ApiProvider> | ||
</ActiveProjectIdProvider> | ||
</QueryClientProvider> | ||
</IntlProvider> | ||
</ThemeProvider> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createContext, type ReactNode } from 'react' | ||
import { createStore, type StoreApi } from 'zustand' | ||
import { persist as zustandPersist } from 'zustand/middleware' | ||
|
||
import { createHooks } from './createStoreHooks' | ||
|
||
const PERSISTED_ACTIVE_PROJECT_ID_KEY = 'ActiveProjectId' | ||
|
||
type ActiveProjectId = { activeProjectId: string | undefined } | ||
|
||
const initialActiveProjectId: ActiveProjectId = { | ||
activeProjectId: undefined, | ||
} | ||
|
||
type ActiveProjectIdStore = ReturnType<typeof createActiveProjectIdStore> | ||
|
||
type ActiveProjectIdProviderProps = { | ||
children: ReactNode | ||
store: ActiveProjectIdStore | ||
} | ||
|
||
const ActiveProjectIdContext = createContext<ActiveProjectIdStore | null>(null) | ||
|
||
export const ActiveProjectIdProvider = ({ | ||
children, | ||
store, | ||
}: ActiveProjectIdProviderProps) => { | ||
return ( | ||
<ActiveProjectIdContext.Provider value={store}> | ||
{children} | ||
</ActiveProjectIdContext.Provider> | ||
) | ||
} | ||
|
||
const { useStoreActions, useStoreState } = createHooks(ActiveProjectIdContext) | ||
|
||
export { | ||
useStoreActions as useActiveProjectIdStoreActions, | ||
useStoreState as useActiveProjectIdStoreState, | ||
} | ||
|
||
export function createActiveProjectIdStore({ persist }: { persist: boolean }) { | ||
let store: StoreApi<ActiveProjectId> | ||
|
||
if (!persist) { | ||
store = createStore(() => initialActiveProjectId) | ||
} else { | ||
store = createStore( | ||
zustandPersist(() => initialActiveProjectId, { | ||
name: PERSISTED_ACTIVE_PROJECT_ID_KEY, | ||
}), | ||
) | ||
} | ||
|
||
const actions = { | ||
setActiveProjectId: (newProjectId: string) => | ||
store.setState({ activeProjectId: newProjectId }), | ||
} | ||
|
||
return { store, actions } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useContext } from 'react' | ||
import { useStore, type StoreApi } from 'zustand' | ||
|
||
export function createHooks<TStoreState, TStoreActions>( | ||
context: React.Context<{ | ||
store: StoreApi<TStoreState> | ||
actions: TStoreActions | ||
} | null>, | ||
) { | ||
function useContextValue() { | ||
const value = useContext(context) | ||
if (!value) { | ||
throw new Error('Must set up the provider first') | ||
} | ||
return value | ||
} | ||
|
||
function useStoreState(): TStoreState | ||
function useStoreState<S>(selector: (state: TStoreState) => S): S | ||
function useStoreState<S>(selector?: (state: TStoreState) => S) { | ||
const { store } = useContextValue() | ||
return useStore(store, selector!) | ||
} | ||
|
||
function useStoreActions() { | ||
const { actions } = useContextValue() | ||
return actions | ||
} | ||
|
||
return { useStoreState, useStoreActions } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,9 @@ | ||
import { RouterProvider, createRouter } from '@tanstack/react-router' | ||
import { createRoot } from 'react-dom/client' | ||
|
||
import { routeTree } from './routeTree.gen' | ||
|
||
import './index.css' | ||
|
||
const router = createRouter({ routeTree }) | ||
|
||
declare module '@tanstack/react-router' { | ||
interface Register { | ||
router: typeof router | ||
} | ||
} | ||
import { App } from './App' | ||
|
||
const root = createRoot(document.getElementById('app') as HTMLElement) | ||
|
||
root.render(<RouterProvider router={router} />) | ||
root.render(<App />) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,11 @@ | ||
import { Suspense } from 'react' | ||
import { CssBaseline, ThemeProvider } from '@mui/material' | ||
import CircularProgress from '@mui/material/CircularProgress' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { Outlet, createRootRoute } from '@tanstack/react-router' | ||
|
||
import { theme } from '../Theme' | ||
import { ApiProvider } from '../contexts/ApiContext' | ||
import { IntlProvider } from '../contexts/IntlContext' | ||
|
||
const queryClient = new QueryClient() | ||
|
||
export const Route = createRootRoute({ | ||
component: () => ( | ||
<ThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
<IntlProvider> | ||
<QueryClientProvider client={queryClient}> | ||
<ApiProvider> | ||
<Suspense fallback={<CircularProgress />}> | ||
<Outlet /> | ||
</Suspense> | ||
</ApiProvider> | ||
</QueryClientProvider> | ||
</IntlProvider> | ||
</ThemeProvider> | ||
<Suspense fallback={<CircularProgress />}> | ||
<Outlet /> | ||
</Suspense> | ||
), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters