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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | 80x 80x 80x 80x 80x 80x 80x 80x 21x 2x 2x 2x 2x 2x 2x 80x 80x 80x 1x 1x 1x 1x 1x 1x 1x 1x 1x 80x 10x 8x | /**
* Macro editor dialog component
*/
import { useState, useEffect } from 'react';
import { AppDialog } from '@/components/common/AppDialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { X, Plus, Info } from 'lucide-react';
import type { Macro, MacroCreate, MacroUpdate } from '@/types/macro';
import { handleApiError } from '@/lib/api';
interface MacroEditorDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
onSubmit: (data: MacroCreate | MacroUpdate) => Promise<void>;
macro?: Macro | null;
}
export default function MacroEditorDialog({
open,
onOpenChange,
onSubmit,
macro,
}: MacroEditorDialogProps) {
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [sqlQuery, setSqlQuery] = useState('');
const [parameters, setParameters] = useState<string[]>([]);
const [newParameter, setNewParameter] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (open) {
Iif (macro) {
setName(macro.name);
setDescription(macro.description || '');
setSqlQuery(macro.sql_query);
try {
setParameters(JSON.parse(macro.parameters));
} catch {
setParameters([]);
}
} else {
setName('');
setDescription('');
setSqlQuery('');
setParameters([]);
}
setError(null);
}
}, [open, macro]);
const handleAddParameter = () => {
if (newParameter && !parameters.includes(newParameter)) {
setParameters([...parameters, newParameter]);
setNewParameter('');
}
};
const handleRemoveParameter = (index: number) => {
setParameters(parameters.filter((_, i) => i !== index));
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError(null);
Iif (!name) {
setError('Macro name is required');
return;
}
Iif (!sqlQuery) {
setError('SQL query is required');
return;
}
setIsSubmitting(true);
try {
await onSubmit({
name,
description,
sql_query: sqlQuery,
parameters,
});
onOpenChange(false);
} catch (err) {
setError(handleApiError(err));
} finally {
setIsSubmitting(false);
}
};
return (
<AppDialog
open={open}
onOpenChange={onOpenChange}
title={macro ? 'Edit Macro' : 'Create Macro'}
description="Define a reusable SQL snippet for permission rules. Use :param_name for parameters."
className="max-w-2xl"
footer={
<>
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isSubmitting}
>
Cancel
</Button>
<Button type="submit" form="macro-editor-form" disabled={isSubmitting}>
{isSubmitting ? (macro ? 'Updating...' : 'Creating...') : (macro ? 'Update Macro' : 'Create Macro')}
</Button>
</>
}
>
<form id="macro-editor-form" onSubmit={handleSubmit} className="space-y-6">
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="name">Macro Name</Label>
<div className="flex items-center gap-2">
<span className="text-muted-foreground">@</span>
<Input
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="has_department_access"
disabled={isSubmitting}
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="description">Description (Optional)</Label>
<Input
id="description"
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Checks if user has access to a specific department"
disabled={isSubmitting}
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="sql">SQL Query</Label>
<Textarea
id="sql"
value={sqlQuery}
onChange={(e) => setSqlQuery(e.target.value)}
placeholder="SELECT 1 FROM departments WHERE id = :dept_id AND manager_id = :user_id"
className="font-mono min-h-[150px]"
disabled={isSubmitting}
/>
<div className="flex items-center gap-2 text-xs text-muted-foreground bg-blue-50/50 p-2 rounded">
<Info className="h-3 w-3" />
<span>Available variables: record.*, user.*, :your_param</span>
</div>
</div>
<div className="space-y-3">
<Label>Parameters</Label>
<div className="flex flex-wrap gap-2 mb-2">
{parameters.map((param, index) => (
<Badge key={index} variant="secondary" className="gap-1 px-2 py-1">
{param}
{!isSubmitting && (
<X
className="h-3 w-3 cursor-pointer hover:text-destructive"
onClick={() => handleRemoveParameter(index)}
/>
)}
</Badge>
))}
{parameters.length === 0 && (
<span className="text-xs text-muted-foreground italic">No parameters defined</span>
)}
</div>
<div className="flex gap-2">
<Input
value={newParameter}
onChange={(e) => setNewParameter(e.target.value)}
placeholder="Parameter name (e.g. dept_id)"
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
handleAddParameter();
}
}}
disabled={isSubmitting}
/>
<Button
type="button"
variant="outline"
size="icon"
onClick={handleAddParameter}
disabled={isSubmitting || !newParameter}
>
<Plus className="h-4 w-4" />
</Button>
</div>
</div>
{error && (
<div className="bg-destructive/10 border border-destructive/20 rounded-lg p-4">
<p className="text-destructive text-sm font-medium">{error}</p>
</div>
)}
</form>
</AppDialog>
);
}
// Internal Badge component if not imported correctly
function Badge({ children, variant = "default", className = "", ...props }: React.HTMLAttributes<HTMLDivElement> & { variant?: "default" | "secondary" | "destructive" | "outline" }) {
const variants = {
default: "bg-primary text-primary-foreground hover:bg-primary/80",
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/80",
outline: "text-foreground",
};
return (
<div className={`inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 ${variants[variant]} ${className}`} {...props}>
{children}
</div>
);
}
|