|
| 1 | +<script lang="ts"> |
| 2 | + // The badge-gating POLICY BUILDER (operator dashboard). Builds a one-level |
| 3 | + // allOf / anyOf / atLeast policy over badge leaves, each with its own constraint |
| 4 | + // inputs (provider, domain, age, ...). Emits the resulting @ministryofmany/policy |
| 5 | + // PolicyNode as JSON via `bind:policyJson` (the form posts it in a hidden input) |
| 6 | + // and shows a live plain-language preview so the operator sees exactly what a |
| 7 | + // member must prove. The server re-validates with parsePolicy before any write. |
| 8 | + import Button from './Button.svelte'; |
| 9 | + import Tag from './Tag.svelte'; |
| 10 | + import { |
| 11 | + BADGE_CONSTRAINTS, |
| 12 | + buildPolicy, |
| 13 | + type BuilderLeaf, |
| 14 | + type Combinator |
| 15 | + } from '$lib/policy/builder'; |
| 16 | + import { describePolicy } from '$lib/policy/describe'; |
| 17 | +
|
| 18 | + let { |
| 19 | + badgeTypes, |
| 20 | + policyJson = $bindable('{"allOf":[]}') |
| 21 | + }: { |
| 22 | + badgeTypes: string[]; |
| 23 | + policyJson?: string; |
| 24 | + } = $props(); |
| 25 | +
|
| 26 | + let combinator = $state<Combinator>('allOf'); |
| 27 | + let atLeastN = $state(1); |
| 28 | + let leaves = $state<BuilderLeaf[]>([]); |
| 29 | +
|
| 30 | + function addLeaf() { |
| 31 | + leaves = [...leaves, { type: badgeTypes[0] ?? 'email-exact', where: {} }]; |
| 32 | + } |
| 33 | + function removeLeaf(i: number) { |
| 34 | + leaves = leaves.filter((_, idx) => idx !== i); |
| 35 | + } |
| 36 | +
|
| 37 | + const policy = $derived(buildPolicy(combinator, leaves, atLeastN)); |
| 38 | + // Keep the serialized output + the preview in sync. |
| 39 | + $effect(() => { |
| 40 | + policyJson = JSON.stringify(policy); |
| 41 | + }); |
| 42 | + const preview = $derived(describePolicy(policy)); |
| 43 | +
|
| 44 | + function fieldsFor(type: string) { |
| 45 | + return BADGE_CONSTRAINTS[type] ?? []; |
| 46 | + } |
| 47 | +</script> |
| 48 | + |
| 49 | +<div class="flex flex-col gap-3"> |
| 50 | + <div class="flex flex-wrap items-center gap-2"> |
| 51 | + <span class="text-xs font-medium text-ink-muted">Members must satisfy</span> |
| 52 | + <div |
| 53 | + class="inline-flex flex-wrap items-center gap-1 rounded-[var(--radius-pill)] bg-surface-2 p-0.5" |
| 54 | + > |
| 55 | + {#each [{ k: 'allOf', l: 'all of' }, { k: 'anyOf', l: 'any of' }, { k: 'atLeast', l: 'at least N of' }] as opt (opt.k)} |
| 56 | + <button |
| 57 | + type="button" |
| 58 | + onclick={() => (combinator = opt.k as Combinator)} |
| 59 | + aria-pressed={combinator === opt.k} |
| 60 | + class="rounded-[var(--radius-pill)] px-3 py-1 text-xs font-medium transition-colors {combinator === |
| 61 | + opt.k |
| 62 | + ? 'bg-accent-soft text-accent' |
| 63 | + : 'text-ink-muted hover:bg-surface hover:text-ink'}" |
| 64 | + > |
| 65 | + {opt.l} |
| 66 | + </button> |
| 67 | + {/each} |
| 68 | + </div> |
| 69 | + {#if combinator === 'atLeast'} |
| 70 | + <label class="flex items-center gap-1 text-xs text-ink-muted"> |
| 71 | + N = |
| 72 | + <input |
| 73 | + type="number" |
| 74 | + min="1" |
| 75 | + max={Math.max(1, leaves.length)} |
| 76 | + bind:value={atLeastN} |
| 77 | + class="w-16 rounded-[var(--radius-card)] border border-border bg-surface px-2 py-1 text-ink" |
| 78 | + /> |
| 79 | + </label> |
| 80 | + {/if} |
| 81 | + </div> |
| 82 | + |
| 83 | + {#if leaves.length === 0} |
| 84 | + <p class="text-xs text-ink-faint"> |
| 85 | + No badge requirements yet - this sub-forum will be open to anyone signed in. Add a badge to |
| 86 | + gate it. |
| 87 | + </p> |
| 88 | + {/if} |
| 89 | + |
| 90 | + <div class="flex flex-col gap-2"> |
| 91 | + {#each leaves as leaf, i (i)} |
| 92 | + <div class="rounded-[var(--radius-card)] border border-border bg-surface-2/50 p-3"> |
| 93 | + <div class="flex items-center justify-between gap-2"> |
| 94 | + <label class="flex flex-1 flex-col gap-1 text-xs"> |
| 95 | + <span class="font-medium text-ink-muted">Badge type</span> |
| 96 | + <select |
| 97 | + bind:value={leaf.type} |
| 98 | + class="rounded-[var(--radius-card)] border border-border bg-surface px-2 py-1.5 text-sm text-ink" |
| 99 | + > |
| 100 | + {#each badgeTypes as t (t)} |
| 101 | + <option value={t}>{t}</option> |
| 102 | + {/each} |
| 103 | + </select> |
| 104 | + </label> |
| 105 | + <Button variant="ghost" size="sm" onclick={() => removeLeaf(i)}>Remove</Button> |
| 106 | + </div> |
| 107 | + |
| 108 | + {#if fieldsFor(leaf.type).length > 0} |
| 109 | + <div class="mt-2 grid grid-cols-1 gap-2 sm:grid-cols-2"> |
| 110 | + {#each fieldsFor(leaf.type) as field (field.key)} |
| 111 | + <label class="flex flex-col gap-1 text-xs"> |
| 112 | + <span class="font-medium text-ink-muted">{field.label}</span> |
| 113 | + {#if field.kind === 'select'} |
| 114 | + <select |
| 115 | + bind:value={leaf.where[field.key]} |
| 116 | + class="rounded-[var(--radius-card)] border border-border bg-surface px-2 py-1.5 text-sm text-ink" |
| 117 | + > |
| 118 | + <option value="">(any)</option> |
| 119 | + {#each field.options ?? [] as opt (opt)} |
| 120 | + <option value={String(opt)}>{opt}</option> |
| 121 | + {/each} |
| 122 | + </select> |
| 123 | + {:else} |
| 124 | + <input |
| 125 | + type={field.kind === 'number' ? 'number' : 'text'} |
| 126 | + placeholder={field.placeholder} |
| 127 | + bind:value={leaf.where[field.key]} |
| 128 | + class="rounded-[var(--radius-card)] border border-border bg-surface px-2 py-1.5 text-sm text-ink placeholder:text-ink-faint" |
| 129 | + /> |
| 130 | + {/if} |
| 131 | + </label> |
| 132 | + {/each} |
| 133 | + </div> |
| 134 | + {:else} |
| 135 | + <p class="mt-1 text-xs text-ink-faint">No extra constraints for this badge type.</p> |
| 136 | + {/if} |
| 137 | + </div> |
| 138 | + {/each} |
| 139 | + </div> |
| 140 | + |
| 141 | + <div> |
| 142 | + <Button variant="secondary" size="sm" onclick={addLeaf}>+ Add badge requirement</Button> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div class="rounded-[var(--radius-card)] border border-border bg-surface-2/50 p-2 text-xs"> |
| 146 | + <span class="mr-1 font-medium text-ink-muted">Preview:</span> |
| 147 | + <Tag tone="accent">{preview}</Tag> |
| 148 | + </div> |
| 149 | +</div> |
0 commit comments