File size: 14,397 Bytes
921d377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
 * RulesPanel β€” personalization rules CRUD.
 *
 * Backend already enforces DSL validation (see
 * personalize/rules.py::validate_rule). We mirror the allowed-key
 * list here so the form can surface local hints, but the final
 * word on validity belongs to the server β€” on 400 we render the
 * server's `data.problems` array inline.
 *
 * Rules are compact JSON blobs. The form exposes:
 *   - name (free text)
 *   - priority (int; lower = higher priority)
 *   - enabled (toggle)
 *   - condition + action (JSON textareas with live parse errors)
 *
 * Advanced users can edit the JSON directly; typical users see
 * a hint listing the allowed keys so they don't have to read the
 * backend source.
 */

import React, { useCallback, useMemo, useState } from "react";
import { Sparkles, Trash2 } from "lucide-react";
import type { InteractiveApi } from "./api";
import { InteractiveApiError } from "./types";
import type { RuleItem } from "./types";
import {
  DangerButton,
  EmptyState,
  ErrorBanner,
  Modal,
  Panel,
  PrimaryButton,
  SecondaryButton,
  SkeletonRow,
  useAsyncResource,
  useToast,
} from "./ui";

export interface RulesPanelProps {
  api: InteractiveApi;
  projectId: string;
}

const ALLOWED_CONDITION_KEYS = [
  "role", "level", "language", "country",
  "has_tag", "mood", "min_affinity", "max_affinity", "metric",
];
const ALLOWED_ACTION_KEYS = [
  "route_to_node", "prefer_tone", "bump_affinity",
];

export function RulesPanel({ api, projectId }: RulesPanelProps) {
  const toast = useToast();
  const resource = useAsyncResource<RuleItem[]>(
    (signal) => api.listRules(projectId, signal),
    [api, projectId],
  );
  const [createOpen, setCreateOpen] = useState(false);
  const [confirmDelete, setConfirmDelete] = useState<RuleItem | null>(null);

  const items = useMemo(() => resource.data || [], [resource.data]);

  const onCreated = useCallback((created: RuleItem) => {
    resource.setData((prev) => {
      const next = [...(prev || []), created];
      next.sort((a, b) => a.priority - b.priority);
      return next;
    });
  }, [resource]);

  const onDelete = useCallback(async () => {
    const target = confirmDelete;
    if (!target) return;
    const snapshot = items;
    resource.setData((prev) => (prev || []).filter((r) => r.id !== target.id));
    setConfirmDelete(null);
    try {
      await api.deleteRule(target.id);
      toast.toast({ variant: "success", title: "Rule deleted" });
    } catch (err) {
      const e = err as InteractiveApiError;
      resource.setData(snapshot);
      toast.toast({
        variant: "error",
        title: "Couldn't delete rule",
        message: e.message || "The rule has been restored.",
      });
    }
  }, [api, confirmDelete, items, resource, toast]);

  return (
    <Panel
      title="Personalization rules"
      subtitle="Declarative rules that bias the runtime router. Lower priority wins."
      actions={
        <PrimaryButton
          onClick={() => setCreateOpen(true)}
          size="sm"
          icon={<Sparkles className="w-4 h-4" aria-hidden />}
        >
          New rule
        </PrimaryButton>
      }
    >
      {resource.error ? (
        <ErrorBanner
          title="Couldn't load rules"
          message={resource.error}
          onRetry={resource.reload}
        />
      ) : resource.loading && !resource.data ? (
        <div className="flex flex-col gap-2" aria-busy="true">
          {Array.from({ length: 3 }).map((_, i) => <SkeletonRow key={i} />)}
        </div>
      ) : items.length === 0 ? (
        <EmptyState
          icon={<Sparkles className="w-12 h-12" aria-hidden />}
          title="No personalization rules"
          description="Rules let you route viewers based on mood, affinity, or audience facets. Great for adaptive paths without coding."
          action={
            <PrimaryButton onClick={() => setCreateOpen(true)} icon={<Sparkles className="w-4 h-4" aria-hidden />}>
              Add your first rule
            </PrimaryButton>
          }
        />
      ) : (
        <ul className="flex flex-col gap-2">
          {items.map((r) => (
            <RuleRow key={r.id} rule={r} onDelete={() => setConfirmDelete(r)} />
          ))}
        </ul>
      )}

      <NewRuleModal
        open={createOpen}
        onClose={() => setCreateOpen(false)}
        api={api}
        projectId={projectId}
        onCreated={(r) => {
          onCreated(r);
          setCreateOpen(false);
          toast.toast({ variant: "success", title: "Rule created", message: r.name });
        }}
      />

      <Modal
        open={!!confirmDelete}
        onClose={() => setConfirmDelete(null)}
        title="Delete rule?"
        footer={
          <>
            <SecondaryButton onClick={() => setConfirmDelete(null)}>Cancel</SecondaryButton>
            <DangerButton onClick={onDelete} icon={<Trash2 className="w-4 h-4" aria-hidden />}>
              Delete
            </DangerButton>
          </>
        }
      >
        <p className="text-sm text-[#cfd8dc]">
          This permanently removes <span className="font-medium">{confirmDelete?.name}</span>.
        </p>
      </Modal>
    </Panel>
  );
}

// ────────────────────────────────────────────────────────────────
// Rule row
// ────────────────────────────────────────────────────────────────

function RuleRow({ rule, onDelete }: { rule: RuleItem; onDelete: () => void }) {
  const condKeys = Object.keys(rule.condition || {});
  const actionKeys = Object.keys(rule.action || {});
  return (
    <li className="bg-[#121212] border border-[#3f3f3f] rounded-md p-3 flex items-start justify-between gap-3">
      <div className="min-w-0 flex-1">
        <div className="flex items-center gap-2 flex-wrap">
          <span className="text-sm font-medium text-[#f1f1f1]">{rule.name}</span>
          <span className="text-[10px] text-[#777]">priority {rule.priority}</span>
          {!rule.enabled && (
            <span className="text-[10px] uppercase tracking-wide text-amber-300 bg-amber-400/10 border border-amber-400/30 rounded px-1.5 py-0.5">
              disabled
            </span>
          )}
        </div>
        <div className="mt-1.5 flex flex-wrap gap-1.5 text-[11px]">
          {condKeys.length > 0 ? condKeys.map((k) => (
            <code key={`c:${k}`} className="bg-[#1f1f1f] border border-[#3f3f3f] rounded px-1.5 py-0.5 text-[#cfd8dc]">
              when {k}
            </code>
          )) : <span className="text-[#777]">no conditions</span>}
          {actionKeys.map((k) => (
            <code key={`a:${k}`} className="bg-[#3ea6ff]/10 border border-[#3ea6ff]/40 rounded px-1.5 py-0.5 text-[#3ea6ff]">
              β†’ {k}
            </code>
          ))}
        </div>
      </div>
      <button
        type="button"
        onClick={onDelete}
        aria-label={`Delete ${rule.name}`}
        className="text-[#aaa] hover:text-red-400 p-1 rounded focus:outline-none focus-visible:ring-2 focus-visible:ring-red-500"
      >
        <Trash2 className="w-4 h-4" aria-hidden />
      </button>
    </li>
  );
}

// ────────────────────────────────────────────────────────────────
// New-rule modal
// ────────────────────────────────────────────────────────────────

function NewRuleModal({
  open, onClose, api, projectId, onCreated,
}: {
  open: boolean;
  onClose: () => void;
  api: InteractiveApi;
  projectId: string;
  onCreated: (r: RuleItem) => void;
}) {
  const toast = useToast();
  const [name, setName] = useState("");
  const [priority, setPriority] = useState(100);
  const [enabled, setEnabled] = useState(true);
  const [conditionText, setConditionText] = useState('{\n  "mood": "flirty"\n}');
  const [actionText, setActionText] = useState('{\n  "bump_affinity": 0.05\n}');
  const [submitting, setSubmitting] = useState(false);
  const [serverProblems, setServerProblems] = useState<string[] | null>(null);

  const conditionErr = useMemo(() => parseJsonError(conditionText), [conditionText]);
  const actionErr = useMemo(() => parseJsonError(actionText), [actionText]);

  const reset = useCallback(() => {
    setName(""); setPriority(100); setEnabled(true);
    setConditionText('{\n  "mood": "flirty"\n}');
    setActionText('{\n  "bump_affinity": 0.05\n}');
    setServerProblems(null);
  }, []);

  const canSubmit = name.trim().length > 0 && !conditionErr && !actionErr && !submitting;

  const submit = useCallback(async () => {
    if (!canSubmit) return;
    setSubmitting(true);
    setServerProblems(null);
    try {
      const condition = JSON.parse(conditionText) as Record<string, unknown>;
      const action = JSON.parse(actionText) as Record<string, unknown>;
      const created = await api.createRule(projectId, {
        name: name.trim(), condition, action,
        priority, enabled,
      });
      reset();
      onCreated(created);
    } catch (err) {
      const e = err as InteractiveApiError;
      const problems = Array.isArray(e.data?.problems) ? (e.data.problems as string[]) : null;
      if (problems && problems.length > 0) {
        setServerProblems(problems);
      } else {
        toast.toast({
          variant: "error",
          title: "Couldn't create rule",
          message: e.message || "Check the JSON and try again.",
        });
      }
      setSubmitting(false);
    }
  }, [actionText, api, canSubmit, conditionText, enabled, name, onCreated, priority, projectId, reset, toast]);

  return (
    <Modal
      open={open}
      onClose={() => { if (!submitting) { reset(); onClose(); } }}
      title="New personalization rule"
      widthClass="max-w-2xl"
      footer={
        <>
          <SecondaryButton onClick={() => { reset(); onClose(); }} disabled={submitting}>
            Cancel
          </SecondaryButton>
          <PrimaryButton onClick={submit} disabled={!canSubmit} loading={submitting}>
            Create
          </PrimaryButton>
        </>
      }
    >
      <div className="flex flex-col gap-4">
        <Field label="Name" required>
          <input
            type="text"
            value={name}
            onChange={(e) => setName(e.target.value)}
            placeholder="e.g. Warm up when the viewer is flirty"
            maxLength={80}
            className="w-full bg-[#121212] border border-[#3f3f3f] rounded-md px-3 py-2 text-sm outline-none focus:border-[#3ea6ff]"
          />
        </Field>

        <div className="grid grid-cols-2 gap-3">
          <Field label="Priority" hint="Lower wins ties. Default 100.">
            <input
              type="number"
              min={1} max={1000}
              value={priority}
              onChange={(e) => setPriority(Math.max(1, parseInt(e.target.value, 10) || 1))}
              className="w-full bg-[#121212] border border-[#3f3f3f] rounded-md px-3 py-2 text-sm outline-none focus:border-[#3ea6ff]"
            />
          </Field>
          <Field label="Enabled">
            <label className="inline-flex items-center gap-2 mt-1">
              <input
                type="checkbox"
                checked={enabled}
                onChange={(e) => setEnabled(e.target.checked)}
                className="accent-[#3ea6ff]"
              />
              <span className="text-sm text-[#cfd8dc]">Active at runtime</span>
            </label>
          </Field>
        </div>

        <Field label="Condition (JSON)" hint={`Allowed keys: ${ALLOWED_CONDITION_KEYS.join(", ")}.`}>
          <textarea
            rows={5}
            value={conditionText}
            onChange={(e) => setConditionText(e.target.value)}
            className={[
              "w-full bg-[#121212] border rounded-md px-3 py-2 text-xs font-mono outline-none",
              conditionErr ? "border-red-500/50 focus:border-red-500" : "border-[#3f3f3f] focus:border-[#3ea6ff]",
            ].join(" ")}
            spellCheck={false}
          />
          {conditionErr && <p className="text-[11px] text-red-400 mt-1">{conditionErr}</p>}
        </Field>

        <Field label="Action (JSON)" hint={`Allowed keys: ${ALLOWED_ACTION_KEYS.join(", ")}.`}>
          <textarea
            rows={4}
            value={actionText}
            onChange={(e) => setActionText(e.target.value)}
            className={[
              "w-full bg-[#121212] border rounded-md px-3 py-2 text-xs font-mono outline-none",
              actionErr ? "border-red-500/50 focus:border-red-500" : "border-[#3f3f3f] focus:border-[#3ea6ff]",
            ].join(" ")}
            spellCheck={false}
          />
          {actionErr && <p className="text-[11px] text-red-400 mt-1">{actionErr}</p>}
        </Field>

        {serverProblems && serverProblems.length > 0 && (
          <div role="alert" className="border border-red-500/40 bg-red-500/5 rounded-md p-3 text-xs text-red-300">
            <div className="font-medium mb-1">The server rejected this rule:</div>
            <ul className="list-disc pl-5 space-y-0.5">
              {serverProblems.map((p, i) => <li key={i}>{p}</li>)}
            </ul>
          </div>
        )}
      </div>
    </Modal>
  );
}

function Field({
  label, hint, required, children,
}: {
  label: string;
  hint?: string;
  required?: boolean;
  children: React.ReactNode;
}) {
  return (
    <div className="flex flex-col gap-1">
      <label className="text-xs font-medium text-[#cfd8dc]">
        {label}
        {required && <span className="text-[#3ea6ff] ml-0.5" aria-label="required">*</span>}
      </label>
      {hint && <p className="text-[11px] text-[#777] -mt-0.5">{hint}</p>}
      {children}
    </div>
  );
}

function parseJsonError(text: string): string | null {
  try {
    const parsed = JSON.parse(text);
    if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
      return "Must be a JSON object.";
    }
    return null;
  } catch (e) {
    return `Invalid JSON: ${(e as Error).message}`;
  }
}