Skip to content

Commit

Permalink
add search to context
Browse files Browse the repository at this point in the history
  • Loading branch information
yxshv committed Apr 11, 2024
1 parent 43005b3 commit bf4eb47
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default async function Home() {
});

return (
<MemoryProvider spaces={collectedSpaces} freeMemories={freeMemories} cachedMemories={contents}>
<MemoryProvider user={userData} spaces={collectedSpaces} freeMemories={freeMemories} cachedMemories={contents}>
<Content jwt={token} />
{/* <MessagePoster jwt={token} /> */}
</MemoryProvider>
Expand Down
60 changes: 35 additions & 25 deletions apps/web/src/contexts/MemoryContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
import React, { useCallback } from "react";
import { CollectedSpaces } from "../../types/memory";
import { StoredContent, storedContent, StoredSpace } from "@/server/db/schema";
import { addMemory } from "@/actions/db";
import { addMemory, searchMemoriesAndSpaces } from "@/actions/db";
import { User } from "next-auth";

export type SearchResult = {
type: "memory" | "space",
space: StoredSpace,
memory: StoredContent
}

// temperory (will change)
export const MemoryContext = React.createContext<{
Expand All @@ -15,22 +22,25 @@ export const MemoryContext = React.createContext<{
spaces?: number[],
) => Promise<void>;
cachedMemories: StoredContent[];
search: (query: string) => Promise<SearchResult[]>;
}>({
spaces: [],
freeMemories: [],
addMemory: async () => {},
addSpace: async () => {},
deleteSpace: async () => {},
cachedMemories: [],
search: async () => []
});

export const MemoryProvider: React.FC<
{
spaces: StoredSpace[];
freeMemories: StoredContent[];
cachedMemories: StoredContent[]
cachedMemories: StoredContent[];
user: User;
} & React.PropsWithChildren
> = ({ children, spaces: initalSpaces, freeMemories: initialFreeMemories, cachedMemories: initialCachedMemories }) => {
> = ({ children, user, spaces: initalSpaces, freeMemories: initialFreeMemories, cachedMemories: initialCachedMemories }) => {

const [spaces, setSpaces] = React.useState<StoredSpace[]>(initalSpaces);
const [freeMemories, setFreeMemories] =
Expand All @@ -40,37 +50,37 @@ export const MemoryProvider: React.FC<
initialCachedMemories
);

const addSpace = useCallback(
async (space: StoredSpace) => {
setSpaces((prev) => [...prev, space]);
},
[spaces],
);
const deleteSpace = useCallback(
async (id: number) => {
setSpaces((prev) => prev.filter((s) => s.id !== id));
},
[spaces],
);
const addSpace = async (space: StoredSpace) => {
setSpaces((prev) => [...prev, space]);
}

const deleteSpace = async (id: number) => {
setSpaces((prev) => prev.filter((s) => s.id !== id));
}

const search = async (query: string) => {
if (!user.id) {
throw new Error('user id is not define')
}
const data = await searchMemoriesAndSpaces(user.id, query)
return data as SearchResult[]
}

// const fetchMemories = useCallback(async (query: string) => {
// const response = await fetch(`/api/memories?${query}`);
// }, []);

const _addMemory = useCallback(
async (
memory: typeof storedContent.$inferInsert,
spaces: number[] = [],
) => {
const content = await addMemory(memory, spaces);
console.log(content);
},
[freeMemories, spaces],
);
const _addMemory = async (
memory: typeof storedContent.$inferInsert,
spaces: number[] = [],
) => {
const content = await addMemory(memory, spaces);
}

return (
<MemoryContext.Provider
value={{
search,
spaces,
addSpace,
deleteSpace,
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const users = createTable("user", {
image: text("image", { length: 255 }),
});

export type User = typeof users.$inferSelect

export const usersRelations = relations(users, ({ many }) => ({
accounts: many(accounts),
sessions: many(sessions),
Expand Down

0 comments on commit bf4eb47

Please sign in to comment.