Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 21x 21x 21x 21x 17x 3x 20x 59x 18x 59x 59x 59x 59x 59x 59x 2x 2x 59x 1x 1x 1x 59x 77x 59x 17x | import { useEffect, useState } from 'react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { CodeXml, Search, RefreshCw, Plus } from 'lucide-react';
import MacrosTable from '@/components/macros/MacrosTable';
import MacroEditorDialog from '@/components/macros/MacroEditorDialog';
import MacroTestDialog from '@/components/macros/MacroTestDialog';
import MacroDetailDialog from '@/components/macros/MacroDetailDialog';
import DeleteMacroDialog from '@/components/macros/DeleteMacroDialog';
import { listMacros, deleteMacro, createMacro, updateMacro } from '@/services/macros.service';
import type { Macro, MacroCreate, MacroUpdate } from '@/types/macro';
import { handleApiError } from '@/lib/api';
export default function MacrosPage() {
const [macros, setMacros] = useState<Macro[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [searchQuery, setSearchQuery] = useState('');
// Dialog states
const [editorOpen, setEditorOpen] = useState(false);
const [testOpen, setTestOpen] = useState(false);
const [detailOpen, setDetailOpen] = useState(false);
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [selectedMacro, setSelectedMacro] = useState<Macro | null>(null);
const fetchMacros = async () => {
setLoading(true);
setError(null);
try {
const data = await listMacros();
setMacros(data);
} catch (err) {
setError(handleApiError(err));
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchMacros();
}, []);
const handleDelete = (macro: Macro) => {
setSelectedMacro(macro);
setDeleteDialogOpen(true);
};
const handleDeleteConfirm = async (macroId: number) => {
await deleteMacro(macroId);
fetchMacros();
};
const handleView = (macro: Macro) => {
setSelectedMacro(macro);
setDetailOpen(true);
};
const handleEdit = (macro: Macro) => {
setSelectedMacro(macro);
setEditorOpen(true);
};
const handleTest = (macro: Macro) => {
setSelectedMacro(macro);
setTestOpen(true);
};
const handleCreate = () => {
setSelectedMacro(null);
setEditorOpen(true);
};
const handleEditorSubmit = async (data: MacroCreate | MacroUpdate) => {
Iif (selectedMacro) {
await updateMacro(selectedMacro.id, data as MacroUpdate);
} else {
await createMacro(data as MacroCreate);
}
fetchMacros();
};
const filteredMacros = macros.filter((macro) =>
macro.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
(macro.description || '').toLowerCase().includes(searchQuery.toLowerCase())
);
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h1 className="text-3xl font-bold">Macros</h1>
<p className="text-muted-foreground mt-2">
Manage SQL macros for custom permission logic
</p>
</div>
<div className="flex gap-2">
<Button
variant="outline"
onClick={fetchMacros}
disabled={loading}
className="gap-2"
>
<RefreshCw className={`h-4 w-4 ${loading ? 'animate-spin' : ''}`} />
Refresh
</Button>
<Button onClick={handleCreate} className="gap-2">
<Plus className="h-4 w-4" />
Create Macro
</Button>
</div>
</div>
<Card>
<CardHeader className="pb-3">
<CardTitle className="flex items-center gap-2">
<CodeXml className="h-5 w-5 text-primary" />
Macro Library
</CardTitle>
<CardDescription>
Custom SQL snippets that can be referenced as @macro_name in permission rules
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex gap-2">
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Search macros by name or description..."
className="pl-9"
/>
</div>
</div>
{error && (
<div className="bg-destructive/10 border border-destructive/20 rounded-lg p-4">
<p className="text-destructive font-medium">Failed to load macros</p>
<p className="text-sm text-muted-foreground mt-1">{error}</p>
<Button onClick={fetchMacros} className="mt-4" size="sm">
Try Again
</Button>
</div>
)}
{loading && macros.length === 0 ? (
<div className="flex items-center justify-center py-12">
<RefreshCw className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
) : (
<MacrosTable
macros={filteredMacros}
onView={handleView}
onEdit={handleEdit}
onDelete={handleDelete}
onTest={handleTest}
/>
)}
</CardContent>
</Card>
{/* Dialogs */}
<MacroEditorDialog
open={editorOpen}
onOpenChange={setEditorOpen}
onSubmit={handleEditorSubmit}
macro={selectedMacro}
/>
<MacroTestDialog
open={testOpen}
onOpenChange={setTestOpen}
macro={selectedMacro}
/>
<MacroDetailDialog
open={detailOpen}
onOpenChange={setDetailOpen}
macro={selectedMacro}
onTest={handleTest}
onEdit={handleEdit}
/>
<DeleteMacroDialog
open={deleteDialogOpen}
onOpenChange={setDeleteDialogOpen}
macro={selectedMacro}
onConfirm={handleDeleteConfirm}
/>
</div>
);
}
|