diff --git a/src/app/page.tsx b/src/app/page.tsx index e85d8c4..e0d4b59 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -2,7 +2,7 @@ import { SidebarTrigger } from "@/components/ui/sidebar"; import { ReactNode, useState } from "react"; import { useLiveQuery } from "dexie-react-hooks"; -import { db } from "@/lib/db"; +import { addProject, db, deleteProject } from "@/lib/db"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; import { CopyIcon, EditIcon, EllipsisIcon, InfoIcon, ListCheckIcon, PencilIcon, PlusIcon, TrashIcon } from "lucide-react"; @@ -95,7 +95,7 @@ const RenameProjectDialog = ({ project }: { project: Project }) => { const DeleteProjectDialog = ({ project }: { project: Project }) => { const handleDelete = async () => { - await db.projects.delete(project.uuid); + deleteProject(project.uuid); }; return ( @@ -122,15 +122,27 @@ const ProjectDropdown = ({ project }: { project: Project }): ReactNode => { const isMobile = useIsMobile(); const handleDuplicate = async () => { + let originProject = await db.projects.where('uuid').equals(project.origin).first(); + if (!originProject) originProject = project; + let newProjectTitle = originProject.title; + + const duplication = await db.duplications.where('uuid').equals(originProject.uuid).first(); + if (!duplication) return; + newProjectTitle = `${originProject.title} (${duplication.count + 1})`; + await db.duplications.update(duplication.uuid, { + ...duplication, + count: duplication.count + 1 + }); + const newProject = { ...project, uuid: generateUUID(), creationDate: Date.now(), editDate: Date.now(), - title: project.title.includes("Copy of") ? project.title : `Copy of ${project.title}`, - origin: project.origin + title: newProjectTitle, + origin: originProject.uuid }; - await db.projects.add(newProject); + addProject(newProject as Project); }; return ( @@ -239,14 +251,14 @@ export default function Home(): ReactNode { const newProjectSubmit = async (data: z.infer) => { const date = Date.now(); - await db.projects.add({ + addProject({ uuid: generateUUID(), creationDate: date, editDate: date, title: data.title, description: data.description, origin: "" - }); + } as Project); }; return ( diff --git a/src/app/persistence-provider.tsx b/src/app/persistence-provider.tsx index c1a14cf..c620d35 100644 --- a/src/app/persistence-provider.tsx +++ b/src/app/persistence-provider.tsx @@ -27,9 +27,8 @@ const PersistenceProvider = ({ useEffect(() => { const tryToPersist = async () => { const isPersistent = await isStoragePersisted(); - console.log(isPersistent); if (!isPersistent) { - if ((localStorage.getItem('persistence-status') != "persisted" && localStorage.getItem('persistence-status') == undefined) || localStorage.getItem('persistence-status') === "") { + if ((localStorage.getItem('persistence-status') !== "persisted" && localStorage.getItem('persistence-status') === undefined) || localStorage.getItem('persistence-status') === "") { const persistenceStatus = await tryPersistWithoutPromtingUser(); localStorage.setItem("persistence-status", persistenceStatus); if (persistenceStatus == "never") { diff --git a/src/components/dashboard/Dashboard.tsx b/src/components/dashboard/Dashboard.tsx index 446adc6..1098e70 100644 --- a/src/components/dashboard/Dashboard.tsx +++ b/src/components/dashboard/Dashboard.tsx @@ -94,11 +94,11 @@ export const Dashboard = (): ReactNode => { - ClipFusion GitHub Repository + ClipFusion GitHub -

ClipFusion GitHub Repository

+

ClipFusion GitHub

diff --git a/src/lib/db.ts b/src/lib/db.ts index 9bca7ce..0911a83 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -1,7 +1,21 @@ import EditorDB from "@/types/EditorDB"; +import Project from "@/types/Project"; export const db = new EditorDB(); +export function addProject(project: Project) { + db.projects.add(project); + db.duplications.add({ + uuid: project.uuid, + count: 0 + }); +} + +export function deleteProject(uuid: string) { + db.projects.delete(uuid); + db.duplications.delete(uuid); +} + // StorageManager code from https://dexie.org/docs/StorageManager /** Check if storage is persisted already. diff --git a/src/types/Duplication.ts b/src/types/Duplication.ts new file mode 100644 index 0000000..334c180 --- /dev/null +++ b/src/types/Duplication.ts @@ -0,0 +1,7 @@ +import { Entity } from "dexie"; +import EditorDB from "./EditorDB"; + +export default class Duplication extends Entity { + uuid!: string; + count!: number; +} \ No newline at end of file diff --git a/src/types/EditorDB.ts b/src/types/EditorDB.ts index 9cffa7c..562778d 100644 --- a/src/types/EditorDB.ts +++ b/src/types/EditorDB.ts @@ -1,14 +1,18 @@ import Dexie, { type EntityTable } from "dexie"; import Project from "./Project"; +import Duplication from "./Duplication"; export default class EditorDB extends Dexie { projects!: EntityTable; + duplications!: EntityTable; constructor() { super('EditorDB'); this.version(1).stores({ - projects: 'uuid, project' + projects: 'uuid, project', + duplications: 'uuid, count' }); this.projects.mapToClass(Project); + this.duplications.mapToClass(Duplication); } }