-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from refactor-group/display_overarching_goal_i…
…n_coaching_session_selector
- Loading branch information
Showing
6 changed files
with
348 additions
and
46 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
src/lib/providers/overarching-goal-state-store-provider.tsx
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,47 @@ | ||
// The purpose of this provider is to provide compatibility with | ||
// Next.js re-rendering and component caching | ||
"use client"; | ||
|
||
import { type ReactNode, createContext, useRef, useContext } from "react"; | ||
import { type StoreApi, useStore } from "zustand"; | ||
|
||
import { | ||
type OverarchingGoalStateStore, | ||
createOverarchingGoalStateStore, | ||
} from "@/lib/stores/overarching-goal-state-store"; | ||
|
||
export const OverarchingGoalStateStoreContext = | ||
createContext<StoreApi<OverarchingGoalStateStore> | null>(null); | ||
|
||
export interface OverarchingGoalStateStoreProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const OverarchingGoalStateStoreProvider = ({ | ||
children, | ||
}: OverarchingGoalStateStoreProviderProps) => { | ||
const storeRef = useRef<StoreApi<OverarchingGoalStateStore>>(undefined); | ||
if (!storeRef.current) { | ||
storeRef.current = createOverarchingGoalStateStore(); | ||
} | ||
|
||
return ( | ||
<OverarchingGoalStateStoreContext.Provider value={storeRef.current}> | ||
{children} | ||
</OverarchingGoalStateStoreContext.Provider> | ||
); | ||
}; | ||
|
||
export const useOverarchingGoalStateStore = <T,>( | ||
selector: (store: OverarchingGoalStateStore) => T | ||
): T => { | ||
const oagStateStoreContext = useContext(OverarchingGoalStateStoreContext); | ||
|
||
if (!oagStateStoreContext) { | ||
throw new Error( | ||
`useOverarchingGoalStateStore must be used within OverarchingGoalStateStoreProvider` | ||
); | ||
} | ||
|
||
return useStore(oagStateStoreContext, selector); | ||
}; |
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
Oops, something went wrong.