diff options
| author | Dhravya <[email protected]> | 2024-04-05 18:05:55 -0700 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-04-05 18:05:55 -0700 |
| commit | 0375128bd96e538051f4ac58a9003fa15198f06b (patch) | |
| tree | 52f346abfe5ed782bed8a1e259327c4a182abe12 /apps/web/src/contexts | |
| parent | (NEEDS MIGRATION)made sidebar functional (diff) | |
| parent | remove unused imports (diff) | |
| download | archived-supermemory-0375128bd96e538051f4ac58a9003fa15198f06b.tar.xz archived-supermemory-0375128bd96e538051f4ac58a9003fa15198f06b.zip | |
changed data structure to fetch spaces
Diffstat (limited to 'apps/web/src/contexts')
| -rw-r--r-- | apps/web/src/contexts/MemoryContext.tsx | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/apps/web/src/contexts/MemoryContext.tsx b/apps/web/src/contexts/MemoryContext.tsx new file mode 100644 index 00000000..6d84f95e --- /dev/null +++ b/apps/web/src/contexts/MemoryContext.tsx @@ -0,0 +1,39 @@ +"use client"; +import React, { useCallback } from "react"; +import { CollectedSpaces } from "../../types/memory"; + +// temperory (will change) +export const MemoryContext = React.createContext<{ + spaces: CollectedSpaces[]; + addSpace: (space: CollectedSpaces) => Promise<void>; +}>({ + spaces: [], + addSpace: async (space) => {}, +}); + +export const MemoryProvider: React.FC< + { spaces: CollectedSpaces[] } & React.PropsWithChildren +> = ({ children, spaces: initalSpaces }) => { + const [spaces, setSpaces] = React.useState<CollectedSpaces[]>(initalSpaces); + + const addSpace = useCallback( + async (space: CollectedSpaces) => { + setSpaces((prev) => [...prev, space]); + }, + [spaces], + ); + + return ( + <MemoryContext.Provider value={{ spaces, addSpace }}> + {children} + </MemoryContext.Provider> + ); +}; + +export const useMemory = () => { + const context = React.useContext(MemoryContext); + if (context === undefined) { + throw new Error("useMemory must be used within a MemoryProvider"); + } + return context; +}; |