import { useMemo, useState } from "react"; import { Link, Navigate, useNavigate, useSearchParams } from "react-router-dom"; import { LuPencil, LuPlay, LuPlus, LuSearch, LuTrash2 } from "react-icons/lu"; import { errorMessage } from "../api/client.js"; import { useDeleteScript, useScripts } from "../api/hooks.js"; import { ScriptIcon } from "../components/ScriptIcon.jsx"; import { TagBadge } from "../components/TagBadge.jsx"; import { scriptTags } from "../lib/script.js"; export function ScriptsPage() { const [params] = useSearchParams(); const navigate = useNavigate(); const editName = params.get("edit"); const { data: scripts = [], isLoading } = useScripts(); const [confirmDelete, setConfirmDelete] = useState(null); const [query, setQuery] = useState(""); const del = useDeleteScript(); const visible = useMemo(() => { const term = query.trim().toLowerCase(); if (!term) return scripts; return scripts.filter((s) => { const name = typeof s === "string" ? s : s.name ?? ""; const description = typeof s === "string" ? "" : s.meta?.description ?? ""; const tags = typeof s === "string" ? [] : scriptTags(s.meta); return ( name.toLowerCase().includes(term) || description.toLowerCase().includes(term) || tags.some((t) => t.toLowerCase().includes(term)) ); }); }, [scripts, query]); if (editName) { return ; } return (

Scripts

Add
{isLoading ? ( ) : scripts.length === 0 ? (

No scripts yet.

) : visible.length === 0 ? (

No scripts match “{query.trim()}”.

) : (
{visible.map((s) => { const name = typeof s === "string" ? s : s.name; const description = typeof s === "string" ? "" : s.meta?.description; const metaError = typeof s === "string" ? null : s.metaError; const hasIcon = typeof s === "string" ? undefined : s.hasIcon; const tags = typeof s === "string" ? [] : scriptTags(s.meta); return (

{name}

{metaError ? ( {metaError} ) : ( description || "—" )}

{tags.length ? (
{tags.map((tag) => ( setQuery(tag)} /> ))}
) : null}
); })}
)} {confirmDelete ? (

Delete {confirmDelete}?

{del.isError ? (

{errorMessage(del.error)}

) : null}
) : null}
); }