File size: 8,144 Bytes
d725335
 
 
 
 
 
 
 
f005306
d725335
 
 
 
 
 
 
a837fd9
 
d725335
 
 
 
 
 
 
 
 
f005306
 
2a9b8d1
d725335
 
 
 
9b858f8
d725335
f005306
d725335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f005306
d725335
 
a837fd9
 
 
2a9b8d1
a837fd9
2a9b8d1
a837fd9
d725335
 
 
 
 
 
 
 
a837fd9
 
2a9b8d1
d725335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f005306
d725335
 
 
 
 
 
 
 
 
 
9b858f8
f005306
d725335
 
 
 
 
f005306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d725335
 
 
 
f005306
 
 
 
 
d725335
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from "react"
import {
  CheckCircle2,
  CircleSlash,
  Loader2,
  Save,
  ShieldCheck,
  Sparkles,
  Trash2,
  Wand2,
} from "lucide-react"
import { useDashboard } from "@/lib/dashboard"
import { Button } from "@/components/ui/button"
import { Textarea } from "@/components/ui/textarea"
import { Badge } from "@/components/ui/badge"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
import { cn } from "@/lib/utils"

export function RuleStudioPanel() {
  const {
    rulesText,
    setRulesText,
    validate,
    validation,
    save,
    setRuleEnabled,
    deleteRule,
    compiler,
    compile,
    compiling,
  } = useDashboard()
  const [instruction, setInstruction] = useState(
    "When a cat appears in the image, notify me.",
  )
  const [armedDelete, setArmedDelete] = useState<string | null>(null)

  return (
    <Tabs defaultValue="compose">
      <div className="flex items-center justify-between">
        <TabsList>
          <TabsTrigger value="compose">
            <Sparkles className="size-3.5" />
            Compose
          </TabsTrigger>
          <TabsTrigger value="source">Source</TabsTrigger>
          <TabsTrigger value="rules">
            Rules
            {validation?.ok && (
              <span className="ml-1 font-mono text-[0.625rem] text-muted-foreground">
                {validation.rules.length}
              </span>
            )}
          </TabsTrigger>
        </TabsList>
        {validation &&
          (validation.ok ? (
            <Badge variant="live">
              <CheckCircle2 className="size-3" />
              valid
            </Badge>
          ) : (
            <Badge variant="danger">
              <CircleSlash className="size-3" />
              invalid
            </Badge>
          ))}
      </div>

      {/* Compose: NL → rules */}
      <TabsContent value="compose">
        <div className="space-y-3">
          <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
            <p className="text-xs text-muted-foreground">
              Describe the automation in plain language. It compiles to validated rules through{" "}
              {compiler.provider} — never executable code.
            </p>
            <Badge variant="default">{compiler.provider}</Badge>
          </div>
          <Textarea
            value={instruction}
            onChange={(e) => setInstruction(e.target.value)}
            className="min-h-24"
            placeholder="When a cat is near the feeder, dispense food once every 30 minutes…"
          />
          <Button onClick={() => compile(instruction)} disabled={compiling || !instruction.trim()}>
            {compiling ? <Loader2 className="size-4 animate-spin" /> : <Wand2 className="size-4" />}
            {compiling
              ? "Compiling…"
              : `Compile with ${compiler.provider}`}
          </Button>
        </div>
      </TabsContent>

      {/* Source: raw editor */}
      <TabsContent value="source">
        <div className="space-y-3">
          <div className="relative">
            <div className="absolute left-0 top-0 z-10 rounded-br-md rounded-tl-md border-b border-r border-border bg-black/40 px-2 py-0.5 font-mono text-[0.625rem] uppercase tracking-wider text-muted-foreground">
              yaml · json
            </div>
            <Textarea
              value={rulesText}
              onChange={(e) => setRulesText(e.target.value)}
              spellCheck={false}
              className="min-h-64 bg-black/40 pt-7 font-mono text-xs leading-relaxed"
            />
          </div>
          {validation && !validation.ok && validation.error && (
            <pre className="max-h-28 overflow-auto rounded-md border border-destructive/30 bg-destructive/5 p-3 font-mono text-[0.6875rem] text-destructive/90 whitespace-pre-wrap">
              {validation.error}
            </pre>
          )}
          <div className="flex gap-2">
            <Button variant="secondary" onClick={validate}>
              <ShieldCheck className="size-4" />
              Validate
            </Button>
            <Button variant="secondary" onClick={save}>
              <Save className="size-4" />
              Save
            </Button>
          </div>
        </div>
      </TabsContent>

      {/* Rules: parsed view */}
      <TabsContent value="rules">
        {validation?.ok && validation.rules.length > 0 ? (
          <div className="space-y-2">
            {validation.rules.map((r) => (
              <div
                key={r.name}
                className="flex flex-col gap-3 rounded-lg border border-border bg-black/20 px-4 py-3 sm:flex-row sm:items-center sm:justify-between"
              >
                <div className="min-w-0">
                  <div className="flex items-center gap-2">
                    <span
                      className={cn(
                        "size-1.5 rounded-full",
                        r.enabled ? "bg-primary" : "bg-muted-foreground/40",
                      )}
                    />
                    <span className="truncate font-mono text-sm text-foreground">{r.name}</span>
                    {r.name.startsWith("example-") && <Badge variant="default">example</Badge>}
                    <Badge variant="default">{r.trigger}</Badge>
                  </div>
                  <div className="mt-1 pl-3.5 text-xs text-muted-foreground">
                    {r.conditions} condition{r.conditions === 1 ? "" : "s"} →{" "}
                    {r.actions.map((a) => `${a.type}:${a.name}`).join(", ")}
                  </div>
                  {r.labels.length > 0 && (
                    <div className="mt-2 flex flex-wrap gap-1 pl-3.5">
                      {r.labels.map((label) => (
                        <span
                          key={label}
                          className="rounded border border-border bg-black/30 px-1.5 py-0.5 font-mono text-[0.625rem] text-muted-foreground"
                        >
                          {label}
                        </span>
                      ))}
                    </div>
                  )}
                </div>
                <div className="flex shrink-0 items-center gap-2">
                  <Label htmlFor={`enabled-${r.name}`} className="text-xs text-muted-foreground">
                    {r.enabled ? "enabled" : "disabled"}
                  </Label>
                  <Switch
                    id={`enabled-${r.name}`}
                    checked={r.enabled}
                    onCheckedChange={(checked) => setRuleEnabled(r.name, checked)}
                  />
                  <Button
                    variant={armedDelete === r.name ? "destructive" : "secondary"}
                    size="sm"
                    onClick={() => {
                      if (armedDelete === r.name) {
                        void deleteRule(r.name)
                        setArmedDelete(null)
                      } else {
                        setArmedDelete(r.name)
                      }
                    }}
                  >
                    <Trash2 className="size-4" />
                    {armedDelete === r.name ? "Confirm" : "Delete"}
                  </Button>
                </div>
              </div>
            ))}
          </div>
        ) : validation?.ok ? (
          <div className="flex flex-col items-center gap-2 py-8 text-center text-muted-foreground">
            <ShieldCheck className="size-7 opacity-40" />
            <p className="text-sm">No rules saved.</p>
          </div>
        ) : (
          <div className="flex flex-col items-center gap-2 py-8 text-center text-muted-foreground">
            <ShieldCheck className="size-7 opacity-40" />
            <p className="text-sm">Validate the source to see parsed rules.</p>
            <Button variant="secondary" size="sm" onClick={validate}>
              Validate now
            </Button>
          </div>
        )}
      </TabsContent>
    </Tabs>
  )
}