-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: creates persisted store for Active Project Id using Zustand #67
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2dce654
chore: add zustand
ErikSin b5e542c
chore: createPersistedStateFunction
ErikSin 82dbbfc
chore: persisted active project Id
ErikSin 448518e
chore: utility function to createStoreHooks
ErikSin 9e6eb23
chore: create active project Id provider
ErikSin 51558d1
chore: update app with new provider
ErikSin 55b9113
chore: update use of hook
ErikSin 4e8e063
chore: remove unneccesary set state
ErikSin 723ef1c
chore: create example use of zustand store with active project id
ErikSin f7a1cca
chore: remove duplicate providers
ErikSin 2a8ae4c
chore: remove console log
ErikSin 5b69aa7
chore: change function casing
ErikSin 89de469
chore: update casing
ErikSin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,8 @@ import { useLayoutEffect } from 'react' | |||||
import { useOwnDeviceInfo } from '@comapeo/core-react' | ||||||
import { createFileRoute, useNavigate } from '@tanstack/react-router' | ||||||
|
||||||
import { useActiveProjectIdStoreState } from '../contexts/ActiveProjectIdProvider' | ||||||
|
||||||
export const Route = createFileRoute('/')({ | ||||||
component: RouteComponent, | ||||||
}) | ||||||
|
@@ -12,14 +14,25 @@ export const Route = createFileRoute('/')({ | |||||
function RouteComponent() { | ||||||
const navigate = useNavigate() | ||||||
const { data } = useOwnDeviceInfo() | ||||||
const hasCreatedDeviceName = data?.name !== undefined | ||||||
const hasCreatedDeviceName = data.name !== undefined | ||||||
const activeProjectId = useActiveProjectIdStoreState( | ||||||
(store) => store.activeProjectId, | ||||||
) | ||||||
|
||||||
console.log({ activeProjectId }) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
useLayoutEffect(() => { | ||||||
if (!hasCreatedDeviceName) { | ||||||
navigate({ to: '/Onboarding' }) | ||||||
} else { | ||||||
navigate({ to: '/tab1' }) | ||||||
return | ||||||
} | ||||||
|
||||||
if (!activeProjectId) { | ||||||
navigate({ to: '/Onboarding/CreateJoinProjectScreen' }) | ||||||
return | ||||||
} | ||||||
|
||||||
navigate({ to: '/tab1' }) | ||||||
}) | ||||||
|
||||||
return null | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my comment before was maybe not very clear, I did not mean for you to change the case of CreateACtiveProjectIdStore