mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-05-12 22:31:48 +00:00
aggregate content from same space
This commit is contained in:
parent
ad79064317
commit
490ad332ff
3 changed files with 58 additions and 38 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { db } from "@/server/db";
|
||||
import { db } from '@/server/db';
|
||||
import {
|
||||
contentToSpace,
|
||||
sessions,
|
||||
|
|
@ -6,28 +6,28 @@ import {
|
|||
StoredContent,
|
||||
storedContent,
|
||||
users,
|
||||
} from "@/server/db/schema";
|
||||
import { eq, inArray } from "drizzle-orm";
|
||||
import { cookies, headers } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import Sidebar from "@/components/Sidebar/index";
|
||||
import Main from "@/components/Main";
|
||||
import MessagePoster from "./MessagePoster";
|
||||
import { transformContent } from "../../types/memory";
|
||||
import { MemoryProvider } from "@/contexts/MemoryContext";
|
||||
import Content from "./content";
|
||||
} from '@/server/db/schema';
|
||||
import { eq, inArray } from 'drizzle-orm';
|
||||
import { cookies, headers } from 'next/headers';
|
||||
import { redirect } from 'next/navigation';
|
||||
import Sidebar from '@/components/Sidebar/index';
|
||||
import Main from '@/components/Main';
|
||||
import MessagePoster from './MessagePoster';
|
||||
import { transformContent } from '../../types/memory';
|
||||
import { MemoryProvider } from '@/contexts/MemoryContext';
|
||||
import Content from './content';
|
||||
|
||||
export const runtime = "edge";
|
||||
export const runtime = 'edge';
|
||||
|
||||
export default async function Home() {
|
||||
const token =
|
||||
cookies().get("next-auth.session-token")?.value ??
|
||||
cookies().get("__Secure-authjs.session-token")?.value ??
|
||||
cookies().get("authjs.session-token")?.value ??
|
||||
headers().get("Authorization")?.replace("Bearer ", "");
|
||||
cookies().get('next-auth.session-token')?.value ??
|
||||
cookies().get('__Secure-authjs.session-token')?.value ??
|
||||
cookies().get('authjs.session-token')?.value ??
|
||||
headers().get('Authorization')?.replace('Bearer ', '');
|
||||
|
||||
if (!token) {
|
||||
return redirect("/api/auth/signin");
|
||||
return redirect('/api/auth/signin');
|
||||
}
|
||||
|
||||
const session = await db
|
||||
|
|
@ -36,7 +36,7 @@ export default async function Home() {
|
|||
.where(eq(sessions.sessionToken, token!));
|
||||
|
||||
if (!session || session.length === 0) {
|
||||
return redirect("/api/auth/signin");
|
||||
return redirect('/api/auth/signin');
|
||||
}
|
||||
|
||||
const [userData] = await db
|
||||
|
|
@ -46,7 +46,7 @@ export default async function Home() {
|
|||
.limit(1);
|
||||
|
||||
if (!userData) {
|
||||
return redirect("/api/auth/signin");
|
||||
return redirect('/api/auth/signin');
|
||||
}
|
||||
|
||||
// Fetch all content for the user
|
||||
|
|
@ -60,18 +60,18 @@ export default async function Home() {
|
|||
contents.length > 0 ? await transformContent(contents) : [];
|
||||
|
||||
collectedSpaces.push({
|
||||
id: 1,
|
||||
title: "Test",
|
||||
id: 2,
|
||||
title: 'Test',
|
||||
content: [
|
||||
{
|
||||
id: 1,
|
||||
content: "Test",
|
||||
title: "Vscode",
|
||||
description: "Test",
|
||||
url: "https://vscode-remake.vercel.app/",
|
||||
content: 'Test',
|
||||
title: 'Vscode',
|
||||
description: 'Test',
|
||||
url: 'https://vscode-remake.vercel.app/',
|
||||
savedAt: new Date(),
|
||||
baseUrl: "https://vscode-remake.vercel.app/",
|
||||
image: "https://vscode-remake.vercel.app/favicon.svg",
|
||||
baseUrl: 'https://vscode-remake.vercel.app/',
|
||||
image: 'https://vscode-remake.vercel.app/favicon.svg',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
"use client";
|
||||
import React, { useCallback } from "react";
|
||||
import { CollectedSpaces } from "../../types/memory";
|
||||
'use client';
|
||||
import React, { useCallback } from 'react';
|
||||
import { CollectedSpaces } from '../../types/memory';
|
||||
|
||||
// temperory (will change)
|
||||
export const MemoryContext = React.createContext<{
|
||||
|
|
@ -31,6 +31,8 @@ export const MemoryProvider: React.FC<
|
|||
[spaces],
|
||||
);
|
||||
|
||||
console.log(spaces);
|
||||
|
||||
return (
|
||||
<MemoryContext.Provider value={{ spaces, addSpace, deleteSpace }}>
|
||||
{children}
|
||||
|
|
@ -41,7 +43,7 @@ export const MemoryProvider: React.FC<
|
|||
export const useMemory = () => {
|
||||
const context = React.useContext(MemoryContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useMemory must be used within a MemoryProvider");
|
||||
throw new Error('useMemory must be used within a MemoryProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ import { eq, inArray, or } from 'drizzle-orm';
|
|||
export const transformContent = async (
|
||||
content: StoredContent[],
|
||||
): Promise<CollectedSpaces[]> => {
|
||||
const collectedSpaces = await db
|
||||
// Retrieve spaces and their associated content from the database
|
||||
const spacesWithContent = await db
|
||||
.select({
|
||||
id: space.id,
|
||||
name: space.name,
|
||||
|
|
@ -21,13 +22,30 @@ export const transformContent = async (
|
|||
)
|
||||
.all();
|
||||
|
||||
const result = collectedSpaces.map((s) => ({
|
||||
id: s.id,
|
||||
title: s.name,
|
||||
content: content.filter((c) => c.id === s.contentId),
|
||||
}));
|
||||
// Group content by id
|
||||
const contentById = content.reduce(
|
||||
(acc, c) => {
|
||||
acc[c.id] = acc[c.id] || [];
|
||||
acc[c.id].push(c);
|
||||
return acc;
|
||||
},
|
||||
{} as { [key: number]: StoredContent[] },
|
||||
);
|
||||
|
||||
return result;
|
||||
// Aggregate content for each space
|
||||
const aggregatedSpaces = spacesWithContent.reduce(
|
||||
(acc, space) => {
|
||||
if (!acc[space.id]) {
|
||||
acc[space.id] = { id: space.id, title: space.name, content: [] };
|
||||
}
|
||||
acc[space.id].content.push(...contentById[space.contentId!]);
|
||||
return acc;
|
||||
},
|
||||
{} as { [key: number]: CollectedSpaces },
|
||||
);
|
||||
|
||||
// Convert the aggregated spaces object to an array
|
||||
return Object.values(aggregatedSpaces);
|
||||
};
|
||||
|
||||
export type CollectedSpaces = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue