import { type Component, createSignal, createResource, For, Show } from "solid-js"; import { rulesApi } from "../api/client"; import type { InterceptionRule } from "../types"; import { Button } from "./ui/Button"; import { Card } from "./ui/Card"; import { Input } from "./ui/Input"; import { Select } from "./ui/Select"; import { Textarea } from "./ui/Textarea"; const RulesList: Component = () => { const [rules, { refetch }] = createResource(rulesApi.list); const [showEditor, setShowEditor] = createSignal(false); const [editingId, setEditingId] = createSignal(null); const [editingMethod, setEditingMethod] = createSignal(""); const [editingAction, setEditingAction] = createSignal<"passthrough" | "modify" | "replace">("passthrough"); const [editingResponse, setEditingResponse] = createSignal(""); const toggleRule = async (rule: InterceptionRule) => { try { await rulesApi.update(rule.id, { is_enabled: !rule.is_enabled, }); refetch(); } catch (err) { console.error("Failed to toggle rule:", err); alert(`Error: ${err}`); } }; const deleteRule = async (id: number) => { if (!confirm("Are you sure you want to delete this rule?")) return; try { if (id === editingId()) { setShowEditor(false); } await rulesApi.delete(id); refetch(); } catch (err) { console.error("Failed to delete rule:", err); alert(`Error: ${err}`); } }; const createNewRule = async () => { if (!editingMethod()) { alert("Please enter a method name"); return; } try { await rulesApi.create({ method_name: editingMethod(), action: editingAction(), custom_response: editingAction() === "replace" ? editingResponse() : undefined, }); setShowEditor(false); setEditingMethod(""); setEditingResponse(""); refetch(); } catch (err) { console.error("Failed to create rule:", err); alert(`Error: ${err}`); } }; const startEdit = (rule: InterceptionRule) => { setEditingId(rule.id); setEditingMethod(rule.method_name); setEditingAction(rule.action); setEditingResponse(rule.custom_response || ""); setShowEditor(true); }; const cancelEdit = () => { setEditingId(null); setEditingMethod(""); setEditingAction("passthrough"); setEditingResponse(""); setShowEditor(false); }; const saveEdit = async () => { const id = editingId(); if (id === null) { await createNewRule(); return; } if (!editingMethod()) { alert("Please enter a method name"); return; } try { await rulesApi.update(id, { method_name: editingMethod(), action: editingAction(), custom_response: editingAction() === "replace" ? editingResponse() : undefined, }); cancelEdit(); refetch(); } catch (err) { console.error("Failed to update rule:", err); alert(`Error: ${err}`); } }; return (

Interception Rules

{editingId() !== null ? "Edit Rule" : "Create New Rule"}

setEditingMethod(e.currentTarget.value)} placeholder="com.linspirer.method.name" />