diff --git a/src/components/SidebarAdd.astro b/src/components/SidebarAdd.astro index b06ba31..f9e9b43 100644 --- a/src/components/SidebarAdd.astro +++ b/src/components/SidebarAdd.astro @@ -23,10 +23,17 @@ const code = `[

All fields marked with * are required.

Import

+

To import prompts from a JSON file, upload a JSON file formatted as below, or download a sample here.

+

Export

+ +

To export prompts to a JSON file, select the prompts using the checkboxes, and click the button below.

+ +

To export all prompts, leave all checkboxes unchecked.

+ Go Back diff --git a/src/pages/add.astro b/src/pages/add.astro index 5b0c2a0..7b05156 100644 --- a/src/pages/add.astro +++ b/src/pages/add.astro @@ -41,7 +41,7 @@ const supabaseKey = import.meta.env.PUBLIC_SUPABASE_ANON_KEY; -
+
- +
+
+

Import Prompts

+

Upload a JSON file to import prompts.

+ + + + +
+ +
+

Export Prompts

+

Select prompts to export, or leave all unchecked to export everything.

+ + +
+ + +
@@ -178,6 +195,70 @@ const supabaseKey = import.meta.env.PUBLIC_SUPABASE_ANON_KEY; errorBox.style.display = 'block'; } }); + + async function loadPromptCheckboxes() { + const promptList = document.getElementById('prompt-list'); + const { data, error } = await supabase.from('prompts').select('id, title'); + + if (error) { + promptList.innerHTML = `

Failed to load prompts: ${error.message}

`; + return; + } + + promptList.innerHTML = data + .map( + prompt => ` + ` + ) + .join(''); + } + + document.getElementById('exportBtn').addEventListener('click', async () => { + const checkboxes = Array.from(document.querySelectorAll('#prompt-list input[type="checkbox"]')); + const selectedIds = checkboxes.filter(cb => cb.checked).map(cb => cb.value); + + const { data, error } = selectedIds.length > 0 + ? await supabase.from('prompts').select('*').in('id', selectedIds) + : await supabase.from('prompts').select('*'); + + const successBox = document.getElementById('success'); + const errorBox = document.getElementById('error'); + successBox.style.display = 'none'; + errorBox.style.display = 'none'; + + if (error) { + errorBox.innerText = `Export failed: ${error.message}`; + errorBox.style.display = 'block'; + return; + } + + const output = data.map(p => ({ + id: p.id, + type: p.type.toLowerCase(), + title: p.title, + description: p.description, + tags: `{${(p.tags || []).join(',')}}`, + createdat: p.createdat, + notes: p.notes || '' + })); + + const blob = new Blob([JSON.stringify(output, null, 2)], { + type: 'application/json' + }); + + const url = URL.createObjectURL(blob); + const link = document.createElement('a'); + link.href = url; + link.download = selectedIds.length > 0 ? 'selected-prompts.json' : 'all-prompts.json'; + link.click(); + URL.revokeObjectURL(url); + }); + + // Load checkboxes on DOM load + loadPromptCheckboxes();