mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-09-08 22:21:07 +00:00
delete spaces and memories
This commit is contained in:
parent
e99041443a
commit
a7d72c798d
4 changed files with 71 additions and 16 deletions
|
|
@ -222,3 +222,43 @@ export async function addMemory(content: typeof storedContent.$inferInsert, spac
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
export async function deleteSpace(id: number) {
|
||||
|
||||
const user = await getUser()
|
||||
|
||||
if (!user) {
|
||||
return null
|
||||
}
|
||||
|
||||
const [deleted] = await db.delete(space)
|
||||
.where(and(eq(space.user, user.id), eq(space.id, id)))
|
||||
.returning();
|
||||
|
||||
await db.delete(contentToSpace)
|
||||
.where(eq(contentToSpace.spaceId, id));
|
||||
|
||||
return deleted
|
||||
|
||||
}
|
||||
|
||||
|
||||
export async function deleteMemory(id: number) {
|
||||
|
||||
|
||||
const user = await getUser()
|
||||
|
||||
if (!user) {
|
||||
return null
|
||||
}
|
||||
|
||||
const [deleted] = await db.delete(storedContent)
|
||||
.where(and(eq(storedContent.user, user.id), eq(storedContent.id, id)))
|
||||
.returning();
|
||||
|
||||
await db.delete(contentToSpace)
|
||||
.where(eq(contentToSpace.contentId, id));
|
||||
|
||||
return deleted
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,6 @@ import { ExpandedSpace } from "./ExpandedSpace";
|
|||
import { StoredContent, StoredSpace } from "@/server/db/schema";
|
||||
import Image from "next/image"
|
||||
import { useDebounce } from "@/hooks/useDebounce";
|
||||
import { searchMemoriesAndSpaces } from "@/actions/db";
|
||||
|
||||
export function MemoriesBar() {
|
||||
const [parent, enableAnimations] = useAutoAnimate();
|
||||
|
|
@ -165,7 +164,7 @@ export function MemoriesBar() {
|
|||
<>
|
||||
{spaces.map((space) => (
|
||||
<SpaceItem
|
||||
onDelete={() => {}}
|
||||
onDelete={() => deleteSpace(space.id)}
|
||||
key={space.id}
|
||||
//onClick={() => setExpandedSpace(space.id)}
|
||||
{...space}
|
||||
|
|
@ -256,7 +255,6 @@ export function SpaceItem({
|
|||
}, [cachedMemories])
|
||||
|
||||
const _name = name.length > 10 ? name.slice(0, 10) + "..." : name
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
ref={itemRef}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
"use client";
|
||||
import React, { useCallback } from "react";
|
||||
import { ChachedSpaceContent, StoredContent, storedContent, StoredSpace } from "@/server/db/schema";
|
||||
import { addMemory, searchMemoriesAndSpaces, addSpace, fetchContentForSpace } from "@/actions/db";
|
||||
import { addMemory, searchMemoriesAndSpaces, addSpace, fetchContentForSpace, deleteSpace, deleteMemory } from "@/actions/db";
|
||||
import { User } from "next-auth";
|
||||
|
||||
export type SearchResult = {
|
||||
|
|
@ -13,20 +13,22 @@ export type SearchResult = {
|
|||
// temperory (will change)
|
||||
export const MemoryContext = React.createContext<{
|
||||
spaces: StoredSpace[];
|
||||
deleteSpace: (id: number) => Promise<void>;
|
||||
freeMemories: StoredContent[];
|
||||
addSpace: typeof addSpace;
|
||||
addMemory: typeof addMemory;
|
||||
cachedMemories: ChachedSpaceContent[];
|
||||
search: typeof searchMemoriesAndSpaces;
|
||||
deleteSpace: typeof deleteSpace;
|
||||
deleteMemory: typeof deleteMemory;
|
||||
}>({
|
||||
spaces: [],
|
||||
freeMemories: [],
|
||||
addMemory: (() => {}) as unknown as (typeof addMemory),
|
||||
addSpace: (async () => {}) as unknown as (typeof addSpace),
|
||||
deleteSpace: async () => {},
|
||||
cachedMemories: [],
|
||||
search: async () => []
|
||||
search: async () => [],
|
||||
deleteMemory: (() => {}) as unknown as (typeof deleteMemory),
|
||||
deleteSpace: (() => {}) as unknown as (typeof deleteSpace)
|
||||
});
|
||||
|
||||
export const MemoryProvider: React.FC<
|
||||
|
|
@ -46,8 +48,22 @@ export const MemoryProvider: React.FC<
|
|||
initialCachedMemories
|
||||
);
|
||||
|
||||
const deleteSpace = async (id: number) => {
|
||||
setSpaces((prev) => prev.filter((s) => s.id !== id));
|
||||
const _deleteSpace: typeof deleteSpace = async (...params) => {
|
||||
const deleted = (await deleteSpace(...params))!
|
||||
|
||||
setSpaces(prev => prev.filter(i => i.id !== deleted.id))
|
||||
setCachedMemories(prev => prev.filter(i => i.space !== deleted.id))
|
||||
|
||||
return deleted
|
||||
}
|
||||
|
||||
const _deleteMemory: typeof deleteMemory = async (...params) => {
|
||||
const deleted = (await deleteMemory(...params))!
|
||||
|
||||
setCachedMemories(prev => prev.filter(i => i.id !== deleted.id))
|
||||
setFreeMemories(prev => prev.filter(i => i.id !== deleted.id))
|
||||
|
||||
return deleted
|
||||
}
|
||||
|
||||
// const fetchMemories = useCallback(async (query: string) => {
|
||||
|
|
@ -94,9 +110,10 @@ export const MemoryProvider: React.FC<
|
|||
search: searchMemoriesAndSpaces,
|
||||
spaces,
|
||||
addSpace: _addSpace,
|
||||
deleteSpace,
|
||||
deleteSpace: _deleteSpace,
|
||||
freeMemories,
|
||||
cachedMemories,
|
||||
deleteMemory: _deleteMemory,
|
||||
addMemory: _addMemory,
|
||||
}}
|
||||
>
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ export const accounts = createTable(
|
|||
id: integer("id").notNull().primaryKey({ autoIncrement: true }),
|
||||
userId: text("userId", { length: 255 })
|
||||
.notNull()
|
||||
.references(() => users.id),
|
||||
.references(() => users.id, { onDelete: 'cascade' }),
|
||||
type: text("type", { length: 255 }).notNull(),
|
||||
provider: text("provider", { length: 255 }).notNull(),
|
||||
providerAccountId: text("providerAccountId", { length: 255 }).notNull(),
|
||||
|
|
@ -60,7 +60,7 @@ export const sessions = createTable(
|
|||
sessionToken: text("sessionToken", { length: 255 }).notNull(),
|
||||
userId: text("userId", { length: 255 })
|
||||
.notNull()
|
||||
.references(() => users.id),
|
||||
.references(() => users.id, { onDelete: 'cascade' }),
|
||||
expires: int("expires", { mode: "timestamp" }).notNull(),
|
||||
},
|
||||
(session) => ({
|
||||
|
|
@ -94,7 +94,7 @@ export const storedContent = createTable(
|
|||
"page",
|
||||
),
|
||||
image: text("image", { length: 255 }),
|
||||
user: text("user", { length: 255 }).references(() => users.id),
|
||||
user: text("user", { length: 255 }).references(() => users.id, { onDelete: 'cascade' }),
|
||||
},
|
||||
(sc) => ({
|
||||
urlIdx: index("storedContent_url_idx").on(sc.url),
|
||||
|
|
@ -109,10 +109,10 @@ export const contentToSpace = createTable(
|
|||
{
|
||||
contentId: integer("contentId")
|
||||
.notNull()
|
||||
.references(() => storedContent.id),
|
||||
.references(() => storedContent.id, { onDelete: 'cascade' }),
|
||||
spaceId: integer("spaceId")
|
||||
.notNull()
|
||||
.references(() => space.id),
|
||||
.references(() => space.id, { onDelete: 'cascade' }),
|
||||
},
|
||||
(cts) => ({
|
||||
compoundKey: primaryKey({ columns: [cts.contentId, cts.spaceId] }),
|
||||
|
|
@ -124,7 +124,7 @@ export const space = createTable(
|
|||
{
|
||||
id: integer("id").notNull().primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull().unique().default("none"),
|
||||
user: text("user", { length: 255 }).references(() => users.id),
|
||||
user: text("user", { length: 255 }).references(() => users.id, { onDelete: 'cascade' }),
|
||||
},
|
||||
(space) => ({
|
||||
nameIdx: index("spaces_name_idx").on(space.name),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue