From e99041443a35d96a2032b511d7db467af3ba27e3 Mon Sep 17 00:00:00 2001 From: yxshv Date: Sat, 13 Apr 2024 22:51:10 +0530 Subject: order by savedAt --- apps/web/src/actions/db.ts | 4 ++-- apps/web/src/app/page.tsx | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'apps/web/src') diff --git a/apps/web/src/actions/db.ts b/apps/web/src/actions/db.ts index 41789689..215f2f87 100644 --- a/apps/web/src/actions/db.ts +++ b/apps/web/src/actions/db.ts @@ -159,7 +159,7 @@ export async function fetchContentForSpace( exists( db.select().from(contentToSpace).where(and(eq(contentToSpace.spaceId, spaceId), eq(contentToSpace.contentId, storedContent.id))), ), - ).orderBy(asc(storedContent.title)) + ).orderBy(asc(storedContent.savedAt)) return range ? await query.limit(range.limit).offset(range.offset) : await query.all() } @@ -188,7 +188,7 @@ export async function fetchFreeMemories( eq(storedContent.user, user.id), ) - ).orderBy(asc(storedContent.title)) + ).orderBy(asc(storedContent.savedAt)) return range ? await query.limit(range.limit).offset(range.offset) : await query.all() diff --git a/apps/web/src/app/page.tsx b/apps/web/src/app/page.tsx index 7a39aafa..7f3abfee 100644 --- a/apps/web/src/app/page.tsx +++ b/apps/web/src/app/page.tsx @@ -18,8 +18,6 @@ import { } from "../../types/memory"; import { MemoryProvider } from "@/contexts/MemoryContext"; import Content from "./content"; -import { searchMemoriesAndSpaces } from "@/actions/db"; -import { getMetaData } from "@/server/helpers"; export const runtime = "edge"; -- cgit v1.2.3 From a7d72c798d18ae55f9a5de13fa8a94346346779d Mon Sep 17 00:00:00 2001 From: yxshv Date: Sat, 13 Apr 2024 23:14:06 +0530 Subject: delete spaces and memories --- apps/web/src/actions/db.ts | 40 +++++++++++++++++++++++++ apps/web/src/components/Sidebar/MemoriesBar.tsx | 4 +-- apps/web/src/contexts/MemoryContext.tsx | 31 ++++++++++++++----- apps/web/src/server/db/schema.ts | 12 ++++---- 4 files changed, 71 insertions(+), 16 deletions(-) (limited to 'apps/web/src') diff --git a/apps/web/src/actions/db.ts b/apps/web/src/actions/db.ts index 215f2f87..a7b5bd47 100644 --- a/apps/web/src/actions/db.ts +++ b/apps/web/src/actions/db.ts @@ -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 + +} diff --git a/apps/web/src/components/Sidebar/MemoriesBar.tsx b/apps/web/src/components/Sidebar/MemoriesBar.tsx index 9b0376e3..aaf40fd9 100644 --- a/apps/web/src/components/Sidebar/MemoriesBar.tsx +++ b/apps/web/src/components/Sidebar/MemoriesBar.tsx @@ -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) => ( {}} + 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 ( Promise; 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, }} > diff --git a/apps/web/src/server/db/schema.ts b/apps/web/src/server/db/schema.ts index f36e7740..f3eafb94 100644 --- a/apps/web/src/server/db/schema.ts +++ b/apps/web/src/server/db/schema.ts @@ -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), -- cgit v1.2.3 From ba47246430be998ba590436852c9744a0da63b65 Mon Sep 17 00:00:00 2001 From: yxshv Date: Sat, 13 Apr 2024 23:54:22 +0530 Subject: fix notes --- apps/web/src/actions/db.ts | 19 ++++++++++++------- apps/web/src/components/Sidebar/AddMemoryDialog.tsx | 6 +++--- apps/web/src/components/Sidebar/FilterCombobox.tsx | 3 +-- apps/web/src/components/Sidebar/MemoriesBar.tsx | 6 +++--- 4 files changed, 19 insertions(+), 15 deletions(-) (limited to 'apps/web/src') diff --git a/apps/web/src/actions/db.ts b/apps/web/src/actions/db.ts index a7b5bd47..66fbc830 100644 --- a/apps/web/src/actions/db.ts +++ b/apps/web/src/actions/db.ts @@ -31,7 +31,7 @@ export async function searchMemoriesAndSpaces(query: string, opts?: { filter?: { }).from(storedContent).where(and( eq(storedContent.user, user.id), like(storedContent.title, `%${query}%`) - )); + )).orderBy(asc(storedContent.savedAt)); const searchSpacesQuery = db.select({ type: sql`'space'`, @@ -42,10 +42,12 @@ export async function searchMemoriesAndSpaces(query: string, opts?: { filter?: { eq(space.user, user.id), like(space.name, `%${query}%`) ) - ); + ).orderBy(asc(space.name)); let queries = []; + console.log('adding'); + [undefined, true].includes(opts?.filter?.memories) && queries.push(searchMemoriesQuery); [undefined, true].includes(opts?.filter?.spaces) && queries.push(searchSpacesQuery); @@ -56,6 +58,8 @@ export async function searchMemoriesAndSpaces(query: string, opts?: { filter?: { } const data = await Promise.all(queries) + + console.log('resp', data) return data.reduce((acc, i) => [...acc, ...i]) as SearchResult[] } catch { @@ -231,12 +235,13 @@ export async function deleteSpace(id: number) { return null } + await db.delete(contentToSpace) + .where(eq(contentToSpace.spaceId, id)); + 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 @@ -252,13 +257,13 @@ export async function deleteMemory(id: number) { return null } + await db.delete(contentToSpace) + .where(eq(contentToSpace.contentId, id)); + 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 } diff --git a/apps/web/src/components/Sidebar/AddMemoryDialog.tsx b/apps/web/src/components/Sidebar/AddMemoryDialog.tsx index 93f4f3a7..90ac46fa 100644 --- a/apps/web/src/components/Sidebar/AddMemoryDialog.tsx +++ b/apps/web/src/components/Sidebar/AddMemoryDialog.tsx @@ -313,15 +313,15 @@ export function SpaceAddPage({ closeDialog }: { closeDialog: () => void }) { ); } -export function MemorySelectedItem({ id, title, url, image, onRemove }: StoredContent & { onRemove: () => void; }) { +export function MemorySelectedItem({ id, title, url, type, image, onRemove }: StoredContent & { onRemove: () => void; }) { return (
- + {title} - {cleanUrl(url)} + {type ==='note' ? 'Note' : cleanUrl(url)}
) } diff --git a/apps/web/src/components/Sidebar/FilterCombobox.tsx b/apps/web/src/components/Sidebar/FilterCombobox.tsx index 30463672..88bb5a8c 100644 --- a/apps/web/src/components/Sidebar/FilterCombobox.tsx +++ b/apps/web/src/components/Sidebar/FilterCombobox.tsx @@ -180,7 +180,6 @@ export function FilterMemories({ const [isSearching, setIsSearching] = React.useState(false) const results = React.useMemo(() => { - console.log("use memo") return searchResults.map(r => r.memory) }, [searchResults]) @@ -258,7 +257,7 @@ export function FilterMemories({
- + {m.title} i.id === m.id) !== undefined} diff --git a/apps/web/src/components/Sidebar/MemoriesBar.tsx b/apps/web/src/components/Sidebar/MemoriesBar.tsx index aaf40fd9..1c061451 100644 --- a/apps/web/src/components/Sidebar/MemoriesBar.tsx +++ b/apps/web/src/components/Sidebar/MemoriesBar.tsx @@ -354,19 +354,19 @@ export function SpaceItem({ c.image).reverse() as string[]} + images={spaceMemories.map((c) => c.type === 'note' ? '/note.svg' : c.image).reverse() as string[]} /> ) : spaceMemories.length > 1 ? ( c.image).reverse() as string[]} + images={spaceMemories.map((c) => c.type === 'note' ? '/note.svg' : c.image).reverse() as string[]} /> ) : spaceMemories.length === 1 ? ( ) : (
-- cgit v1.2.3