diff --git a/builtin-extensions/serverkit-tramo/frontend/components/AutomationsPage.jsx b/builtin-extensions/serverkit-tramo/frontend/components/AutomationsPage.jsx index 84f5d3af..299a9284 100644 --- a/builtin-extensions/serverkit-tramo/frontend/components/AutomationsPage.jsx +++ b/builtin-extensions/serverkit-tramo/frontend/components/AutomationsPage.jsx @@ -3,7 +3,8 @@ import { useParams, useNavigate } from 'react-router-dom'; import { Workflow, RefreshCw, Plus, Trash2, Rocket, Pencil, Play, LayoutTemplate, Server, Power, KeyRound, CheckCircle2, X, Send, Github, MessageSquare, - Zap, ChevronRight, MousePointerClick, Clock, Webhook, Check, + Zap, MousePointerClick, Clock, Webhook, Check, Search, Star, + Upload, FileJson, } from 'lucide-react'; import api from '@/services/api'; import { PageTopbar, Pill } from '@/components/ds'; @@ -20,11 +21,24 @@ import '../styles/tramo-automations.scss'; // Route-driven tabs (manifest maps /automations and /automations/:tab here). const TABS = [ { slug: 'workflows', to: '/automations', label: 'Workflows', end: true }, + { slug: 'templates', to: '/automations/templates', label: 'Templates' }, { slug: 'runs', to: '/automations/runs', label: 'Runs' }, { slug: 'settings', to: '/automations/settings', label: 'Settings' }, ]; const VALID_TABS = TABS.map((t) => t.slug); +// Favorited template ids live in the browser for now (per-device). A server-side +// favorites store can replace this later without touching the card UI. +const FAVORITES_KEY = 'tramo:favorite-templates'; +const readFavorites = () => { + try { + const raw = JSON.parse(localStorage.getItem(FAVORITES_KEY) || '[]'); + return new Set(Array.isArray(raw) ? raw : []); + } catch { + return new Set(); + } +}; + // Presentation for the starter templates (icon + accent + tags). Keyed by the // backend template id; unknown ids fall back to a neutral card so new templates // still render cleanly. @@ -102,8 +116,13 @@ const AutomationsPage = () => { const [newModal, setNewModal] = useState(false); const [newName, setNewName] = useState(''); const [newTrigger, setNewTrigger] = useState('manual'); - const [templateModal, setTemplateModal] = useState(false); const [templates, setTemplates] = useState([]); + const [templatesLoading, setTemplatesLoading] = useState(false); + const [templateSearch, setTemplateSearch] = useState(''); + const [favorites, setFavorites] = useState(readFavorites); + const [importModal, setImportModal] = useState(false); + const [importName, setImportName] = useState(''); + const [importText, setImportText] = useState(''); // Runs tab const [runs, setRuns] = useState([]); @@ -164,11 +183,33 @@ const AutomationsPage = () => { } }, [toast]); + const loadTemplates = useCallback(async () => { + setTemplatesLoading(true); + try { + const data = await api.request('/tramo/templates'); + setTemplates(data.templates || []); + } catch (error) { + toast.error(`Could not load templates: ${error.message}`); + } finally { + setTemplatesLoading(false); + } + }, [toast]); + useEffect(() => { if (activeTab === 'workflows') loadWorkflows(); + else if (activeTab === 'templates') loadTemplates(); else if (activeTab === 'runs') loadRuns(); else if (activeTab === 'settings') loadSettings(); - }, [activeTab, loadWorkflows, loadRuns, loadSettings]); + }, [activeTab, loadWorkflows, loadTemplates, loadRuns, loadSettings]); + + const toggleFavorite = (id) => { + setFavorites((prev) => { + const next = new Set(prev); + if (next.has(id)) next.delete(id); else next.add(id); + try { localStorage.setItem(FAVORITES_KEY, JSON.stringify([...next])); } catch { /* ignore */ } + return next; + }); + }; // ── Workflow actions ── const openNewModal = () => { @@ -197,21 +238,10 @@ const AutomationsPage = () => { } }; - const openTemplateModal = async () => { - setTemplateModal(true); - try { - const data = await api.request('/tramo/templates'); - setTemplates(data.templates || []); - } catch (error) { - toast.error(`Could not load templates: ${error.message}`); - } - }; - const handleFromTemplate = async (template) => { setBusy(true); try { const wf = await api.request(`/tramo/workflows/from-template/${template.id}`, { method: 'POST', body: {} }); - setTemplateModal(false); toast.success(`Created "${wf.name}" from ${template.name}`); navigate(`/automations/edit/${wf.slug}`); } catch (error) { @@ -221,6 +251,52 @@ const AutomationsPage = () => { } }; + const openImportModal = () => { + setImportName(''); + setImportText(''); + setImportModal(true); + }; + + const handleImportFile = async (e) => { + const file = e.target.files?.[0]; + if (!file) return; + try { + setImportText(await file.text()); + if (!importName.trim()) setImportName(file.name.replace(/\.json$/i, '')); + } catch { + toast.error('Could not read that file.'); + } + e.target.value = ''; + }; + + const handleImport = async () => { + let doc; + try { + doc = JSON.parse(importText); + } catch { + toast.error('That is not valid JSON. Paste a tramo workflow document.'); + return; + } + if (!doc || typeof doc !== 'object' || !Array.isArray(doc.nodes)) { + toast.error('That JSON does not look like a workflow (missing a nodes array).'); + return; + } + const name = importName.trim() || doc.name || 'Imported automation'; + setBusy(true); + try { + const wf = await api.request('/tramo/workflows', { + method: 'POST', body: { name, doc: { ...doc, name } }, + }); + setImportModal(false); + toast.success(`Imported "${wf.name}"`); + navigate(`/automations/edit/${wf.slug}`); + } catch (error) { + toast.error(`Could not import: ${error.message}`); + } finally { + setBusy(false); + } + }; + const handleToggleEnabled = async (wf) => { try { await api.request(`/tramo/workflows/${wf.slug}`, { method: 'PUT', body: { enabled: !wf.enabled } }); @@ -403,6 +479,83 @@ const AutomationsPage = () => { }; // ── Renderers ── + const renderTemplates = () => { + const q = templateSearch.trim().toLowerCase(); + const filtered = templates.filter((t) => !q + || t.name.toLowerCase().includes(q) + || (t.description || '').toLowerCase().includes(q)); + // Favorites float to the top; otherwise preserve the incoming order. + const sorted = [...filtered].sort( + (a, b) => (favorites.has(a.id) ? 0 : 1) - (favorites.has(b.id) ? 0 : 1)); + return ( +
+
+
+ + setTemplateSearch(e.target.value)} + /> +
+ + {filtered.length} template{filtered.length === 1 ? '' : 's'} + +
+ {templatesLoading ? ( + + ) : sorted.length === 0 ? ( + + ) : ( +
+ {sorted.map((t) => { + const meta = TEMPLATE_META[t.id] || DEFAULT_TEMPLATE_META; + const { Icon } = meta; + const fav = favorites.has(t.id); + return ( +
+
+ + + + +
+

{t.name}

+ {t.description &&

{t.description}

} + {meta.tags.length > 0 && ( +
+ {meta.tags.map((tag) => ( + {tag} + ))} +
+ )} +
+ +
+
+ ); + })} +
+ )} +
+ ); + }; + const renderWorkflows = () => (
@@ -705,14 +858,17 @@ const AutomationsPage = () => { - ); + } else if (activeTab === 'templates') { + topbarActions = ( + + ); } else if (activeTab === 'runs') { topbarActions = ( + <> + + + )} >

- Pick a ready-made automation. You will land on the visual editor to - customize it and add the credentials its integrations need. + Paste a tramo workflow document (JSON), or load one from a file. It is + created as a new automation you can edit and deploy.

- {templates.length === 0 ? ( - - ) : ( -
- {templates.map((t) => { - const meta = TEMPLATE_META[t.id] || DEFAULT_TEMPLATE_META; - const { Icon } = meta; - return ( - - ); - })} +
+ + setImportName(e.target.value)} + /> +
+
+
+ +
- )} +