From af58e84ae3bde374b7f58e0a48c937c6c21d987e Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Sat, 4 Oct 2025 06:50:27 +0800 Subject: [PATCH] Fix snippets API integration and database table creation --- src/backend/database/db/index.ts | 11 +++++ .../Desktop/Apps/Terminal/SnippetsSidebar.tsx | 48 ++++++++++--------- src/ui/main-axios.ts | 43 +++++++++++++++++ 3 files changed, 79 insertions(+), 23 deletions(-) diff --git a/src/backend/database/db/index.ts b/src/backend/database/db/index.ts index 88cca125..d45fc333 100644 --- a/src/backend/database/db/index.ts +++ b/src/backend/database/db/index.ts @@ -242,6 +242,17 @@ async function initializeCompleteDatabase(): Promise { FOREIGN KEY (user_id) REFERENCES users (id) ); + CREATE TABLE IF NOT EXISTS snippets ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id TEXT NOT NULL, + name TEXT NOT NULL, + content TEXT NOT NULL, + description TEXT, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users (id) + ); + `); migrateSchema(); diff --git a/src/ui/Desktop/Apps/Terminal/SnippetsSidebar.tsx b/src/ui/Desktop/Apps/Terminal/SnippetsSidebar.tsx index c433cb33..b7e8534f 100644 --- a/src/ui/Desktop/Apps/Terminal/SnippetsSidebar.tsx +++ b/src/ui/Desktop/Apps/Terminal/SnippetsSidebar.tsx @@ -11,10 +11,15 @@ import { CardTitle, } from "@/components/ui/card"; import { Plus, Play, Edit, Trash2, X } from "lucide-react"; -import axios from "axios"; import { toast } from "sonner"; import { useTranslation } from "react-i18next"; import { useConfirmation } from "@/hooks/use-confirmation.ts"; +import { + getSnippets, + createSnippet, + updateSnippet, + deleteSnippet, +} from "@/ui/main-axios"; import type { Snippet, SnippetData } from "../../../../types/index.js"; interface SnippetsSidebarProps { @@ -53,9 +58,9 @@ export function SnippetsSidebar({ const fetchSnippets = async () => { try { setLoading(true); - const response = await axios.get("/snippets"); - // Defensive: ensure response.data is an array - setSnippets(Array.isArray(response.data) ? response.data : []); + const data = await getSnippets(); + // Defensive: ensure data is an array + setSnippets(Array.isArray(data) ? data : []); } catch (err) { toast.error(t("snippets.failedToFetch")); setSnippets([]); @@ -82,23 +87,20 @@ export function SnippetsSidebar({ setShowDialog(true); }; - const handleDelete = async (snippet: Snippet) => { - const confirmed = await confirmWithToast({ - title: t("snippets.deleteConfirmTitle"), - description: t("snippets.deleteConfirmDescription", { - name: snippet.name, - }), - }); - - if (confirmed) { - try { - await axios.delete(`/snippets/${snippet.id}`); - toast.success(t("snippets.deleteSuccess")); - fetchSnippets(); - } catch (err) { - toast.error(t("snippets.deleteFailed")); - } - } + const handleDelete = (snippet: Snippet) => { + confirmWithToast( + t("snippets.deleteConfirmDescription", { name: snippet.name }), + async () => { + try { + await deleteSnippet(snippet.id); + toast.success(t("snippets.deleteSuccess")); + fetchSnippets(); + } catch (err) { + toast.error(t("snippets.deleteFailed")); + } + }, + "destructive", + ); }; const handleSubmit = async () => { @@ -116,10 +118,10 @@ export function SnippetsSidebar({ try { if (editingSnippet) { - await axios.put(`/snippets/${editingSnippet.id}`, formData); + await updateSnippet(editingSnippet.id, formData); toast.success(t("snippets.updateSuccess")); } else { - await axios.post("/snippets", formData); + await createSnippet(formData); toast.success(t("snippets.createSuccess")); } setShowDialog(false); diff --git a/src/ui/main-axios.ts b/src/ui/main-axios.ts index c1f2a660..cc1fd8dd 100644 --- a/src/ui/main-axios.ts +++ b/src/ui/main-axios.ts @@ -2155,3 +2155,46 @@ export async function deployCredentialToHost( throw handleApiError(error, "deploy credential to host"); } } + +// ============================================================================ +// SNIPPETS API +// ============================================================================ + +export async function getSnippets(): Promise { + try { + const response = await authApi.get("/snippets"); + return response.data; + } catch (error) { + throw handleApiError(error, "fetch snippets"); + } +} + +export async function createSnippet(snippetData: any): Promise { + try { + const response = await authApi.post("/snippets", snippetData); + return response.data; + } catch (error) { + throw handleApiError(error, "create snippet"); + } +} + +export async function updateSnippet( + snippetId: number, + snippetData: any, +): Promise { + try { + const response = await authApi.put(`/snippets/${snippetId}`, snippetData); + return response.data; + } catch (error) { + throw handleApiError(error, "update snippet"); + } +} + +export async function deleteSnippet(snippetId: number): Promise { + try { + const response = await authApi.delete(`/snippets/${snippetId}`); + return response.data; + } catch (error) { + throw handleApiError(error, "delete snippet"); + } +}