Initial import
This commit is contained in:
49
src/store/appStore/context/AppStoreContext.tsx
Executable file
49
src/store/appStore/context/AppStoreContext.tsx
Executable file
@@ -0,0 +1,49 @@
|
||||
import {
|
||||
createContext,
|
||||
useContext,
|
||||
useMemo,
|
||||
useState,
|
||||
type PropsWithChildren,
|
||||
} from 'react';
|
||||
import type { AppPageDescriptor, AppStoreSnapshot } from '../types';
|
||||
|
||||
type AppStoreContextValue = AppStoreSnapshot & {
|
||||
setCurrentPage: (page: AppPageDescriptor) => void;
|
||||
setFocusedComponentId: (focusedComponentId: string | null) => void;
|
||||
};
|
||||
|
||||
const defaultPage: AppPageDescriptor = {
|
||||
id: 'plans:all',
|
||||
title: '메모 / Plan',
|
||||
topMenu: 'plans',
|
||||
section: 'all',
|
||||
};
|
||||
|
||||
const AppStoreContext = createContext<AppStoreContextValue | null>(null);
|
||||
|
||||
export function AppStoreProvider({ children }: PropsWithChildren) {
|
||||
const [currentPage, setCurrentPage] = useState<AppPageDescriptor>(defaultPage);
|
||||
const [focusedComponentId, setFocusedComponentId] = useState<string | null>(null);
|
||||
|
||||
const value = useMemo<AppStoreContextValue>(
|
||||
() => ({
|
||||
currentPage,
|
||||
focusedComponentId,
|
||||
setCurrentPage,
|
||||
setFocusedComponentId,
|
||||
}),
|
||||
[currentPage, focusedComponentId],
|
||||
);
|
||||
|
||||
return <AppStoreContext.Provider value={value}>{children}</AppStoreContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAppStoreContext() {
|
||||
const context = useContext(AppStoreContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error('useAppStoreContext must be used within an AppStoreProvider.');
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
5
src/store/appStore/hooks/useAppStore.ts
Executable file
5
src/store/appStore/hooks/useAppStore.ts
Executable file
@@ -0,0 +1,5 @@
|
||||
import { useAppStoreContext } from '../context/AppStoreContext';
|
||||
|
||||
export function useAppStore() {
|
||||
return useAppStoreContext();
|
||||
}
|
||||
3
src/store/appStore/index.ts
Executable file
3
src/store/appStore/index.ts
Executable file
@@ -0,0 +1,3 @@
|
||||
export * from './context/AppStoreContext';
|
||||
export * from './hooks/useAppStore';
|
||||
export * from './types';
|
||||
11
src/store/appStore/types/index.ts
Executable file
11
src/store/appStore/types/index.ts
Executable file
@@ -0,0 +1,11 @@
|
||||
export type AppPageDescriptor = {
|
||||
id: string;
|
||||
title: string;
|
||||
topMenu: 'docs' | 'apis' | 'plans' | 'chat' | 'play';
|
||||
section: string;
|
||||
};
|
||||
|
||||
export type AppStoreSnapshot = {
|
||||
currentPage: AppPageDescriptor;
|
||||
focusedComponentId: string | null;
|
||||
};
|
||||
1
src/store/index.ts
Executable file
1
src/store/index.ts
Executable file
@@ -0,0 +1 @@
|
||||
export * from './appStore';
|
||||
Reference in New Issue
Block a user