|
@@ -0,0 +1,289 @@
|
|
|
|
|
+import { useState } from 'react'
|
|
|
|
|
+import { useQuery, useQueryClient } from '@tanstack/react-query'
|
|
|
|
|
+import { Link } from 'react-router-dom'
|
|
|
|
|
+import { Plus, Trash2, ArrowRight } from 'lucide-react'
|
|
|
|
|
+import { api } from '@/api/client'
|
|
|
|
|
+import { Modal } from '@/components/Modal'
|
|
|
|
|
+import { ConfirmDialog } from '@/components/ConfirmDialog'
|
|
|
|
|
+import { FormRow, inputCls, SaveButton } from '@/components/form'
|
|
|
|
|
+import { useAuthStore } from '@/stores/authStore'
|
|
|
|
|
+import { useToastStore } from '@/stores/toastStore'
|
|
|
|
|
+import { ApiError } from '@/types'
|
|
|
|
|
+import type { CollectionMeta, OnDelete, RelationDefinition } from '@/types'
|
|
|
|
|
+
|
|
|
|
|
+const ON_DELETE: { value: OnDelete; label: string; hint: string }[] = [
|
|
|
|
|
+ { value: 'restrict', label: 'restrict', hint: 'Block the parent delete while children reference it (default).' },
|
|
|
|
|
+ { value: 'cascade', label: 'cascade', hint: 'Delete the referencing children along with the parent.' },
|
|
|
|
|
+ { value: 'set_null', label: 'set_null', hint: 'Clear the reference field on the children.' },
|
|
|
|
|
+ { value: 'no_action', label: 'no_action', hint: 'Do nothing; the reference is left dangling.' },
|
|
|
|
|
+]
|
|
|
|
|
+
|
|
|
|
|
+const NAME_RE = /^[a-zA-Z_][a-zA-Z0-9_-]{0,62}$/
|
|
|
|
|
+
|
|
|
|
|
+// ─── create form ──────────────────────────────────────────────────────────────
|
|
|
|
|
+
|
|
|
|
|
+function NewRelationForm({
|
|
|
|
|
+ project,
|
|
|
|
|
+ collections,
|
|
|
|
|
+ onDone,
|
|
|
|
|
+ onCancel,
|
|
|
|
|
+}: {
|
|
|
|
|
+ project: string
|
|
|
|
|
+ collections: CollectionMeta[]
|
|
|
|
|
+ onDone: () => void
|
|
|
|
|
+ onCancel: () => void
|
|
|
|
|
+}) {
|
|
|
|
|
+ const push = useToastStore((s) => s.push)
|
|
|
|
|
+ const [name, setName] = useState('')
|
|
|
|
|
+ const [parent, setParent] = useState('')
|
|
|
|
|
+ const [child, setChild] = useState('')
|
|
|
|
|
+ const [childField, setChildField] = useState('')
|
|
|
|
|
+ const [onDelete, setOnDelete] = useState<OnDelete>('restrict')
|
|
|
|
|
+ const [validateOnWrite, setValidateOnWrite] = useState(true)
|
|
|
|
|
+ const [saving, setSaving] = useState(false)
|
|
|
|
|
+
|
|
|
|
|
+ async function submit(e: React.FormEvent) {
|
|
|
|
|
+ e.preventDefault()
|
|
|
|
|
+ if (!NAME_RE.test(name.trim())) {
|
|
|
|
|
+ push('Name must start with a letter or underscore (letters, digits, _ or -, max 63).', 'error')
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!parent || !child || !childField.trim()) {
|
|
|
|
|
+ push('Parent, child and child field are all required.', 'error')
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ setSaving(true)
|
|
|
|
|
+ try {
|
|
|
|
|
+ await api.createRelation(project, {
|
|
|
|
|
+ name: name.trim(),
|
|
|
|
|
+ parent,
|
|
|
|
|
+ child,
|
|
|
|
|
+ child_field: childField.trim(),
|
|
|
|
|
+ on_delete: onDelete,
|
|
|
|
|
+ validate_on_write: validateOnWrite,
|
|
|
|
|
+ })
|
|
|
|
|
+ push(`Relation ${name.trim()} created.`, 'success')
|
|
|
|
|
+ onDone()
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ push(`Could not create relation: ${err instanceof ApiError ? err.message : String(err)}`, 'error')
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ setSaving(false)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const hint = ON_DELETE.find((o) => o.value === onDelete)?.hint
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <form onSubmit={submit} className="flex flex-col gap-4">
|
|
|
|
|
+ <p className="text-sm text-slate-400">
|
|
|
|
|
+ Deleting a <span className="text-slate-200">parent</span> document affects{' '}
|
|
|
|
|
+ <span className="text-slate-200">child</span> documents whose field references it.
|
|
|
|
|
+ </p>
|
|
|
|
|
+
|
|
|
|
|
+ <FormRow label="Name" hint="Unique within the project.">
|
|
|
|
|
+ <input type="text" value={name} onChange={(e) => setName(e.target.value)}
|
|
|
|
|
+ placeholder="book_author" className={inputCls} autoFocus />
|
|
|
|
|
+ </FormRow>
|
|
|
|
|
+
|
|
|
|
|
+ <div className="grid gap-4 sm:grid-cols-2">
|
|
|
|
|
+ <FormRow label="Parent collection" hint="Holds the documents being referenced.">
|
|
|
|
|
+ <select value={parent} onChange={(e) => setParent(e.target.value)} className={inputCls}>
|
|
|
|
|
+ <option value="">Select...</option>
|
|
|
|
|
+ {collections.map((c) => <option key={c.name} value={c.name}>{c.name}</option>)}
|
|
|
|
|
+ </select>
|
|
|
|
|
+ </FormRow>
|
|
|
|
|
+ <FormRow label="Child collection" hint="Holds the reference.">
|
|
|
|
|
+ <select value={child} onChange={(e) => setChild(e.target.value)} className={inputCls}>
|
|
|
|
|
+ <option value="">Select...</option>
|
|
|
|
|
+ {collections.map((c) => <option key={c.name} value={c.name}>{c.name}</option>)}
|
|
|
|
|
+ </select>
|
|
|
|
|
+ </FormRow>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <FormRow label="Child field" hint="Field on the child document storing the parent's id.">
|
|
|
|
|
+ <input type="text" value={childField} onChange={(e) => setChildField(e.target.value)}
|
|
|
|
|
+ placeholder="author_id" className={inputCls} />
|
|
|
|
|
+ </FormRow>
|
|
|
|
|
+
|
|
|
|
|
+ <FormRow label="On delete" hint={hint}>
|
|
|
|
|
+ <select value={onDelete} onChange={(e) => setOnDelete(e.target.value as OnDelete)} className={inputCls}>
|
|
|
|
|
+ {ON_DELETE.map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}
|
|
|
|
|
+ </select>
|
|
|
|
|
+ </FormRow>
|
|
|
|
|
+
|
|
|
|
|
+ <FormRow label="Validate on write" hint="Reject a child write whose reference points at no parent.">
|
|
|
|
|
+ <label className="flex items-center gap-2 text-sm text-slate-300 cursor-pointer w-fit">
|
|
|
|
|
+ <input type="checkbox" checked={validateOnWrite}
|
|
|
|
|
+ onChange={(e) => setValidateOnWrite(e.target.checked)} className="accent-indigo-500" />
|
|
|
|
|
+ {validateOnWrite ? 'Enabled' : 'Disabled'}
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </FormRow>
|
|
|
|
|
+
|
|
|
|
|
+ {parent && child && childField && (
|
|
|
|
|
+ <div className="rounded-lg border border-slate-700 bg-slate-800/40 px-4 py-3 text-xs text-slate-400">
|
|
|
|
|
+ <span className="text-slate-300 font-medium">Reads as:</span>{' '}
|
|
|
|
|
+ <code className="text-indigo-300">{child}.{childField}</code> references{' '}
|
|
|
|
|
+ <code className="text-indigo-300">{parent}</code>, and deleting a {parent} document is{' '}
|
|
|
|
|
+ <span className="text-slate-200">{onDelete}</span>.
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
|
|
+ <div className="flex items-center gap-3">
|
|
|
|
|
+ <SaveButton saving={saving} />
|
|
|
|
|
+ <button type="button" onClick={onCancel}
|
|
|
|
|
+ className="rounded-md border border-slate-600 px-3 py-2 text-sm text-slate-300 hover:bg-slate-700">
|
|
|
|
|
+ Cancel
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </form>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ─── page ─────────────────────────────────────────────────────────────────────
|
|
|
|
|
+
|
|
|
|
|
+export default function RelationsAdmin() {
|
|
|
|
|
+ const project = useAuthStore((s) => s.currentProject)
|
|
|
|
|
+ const push = useToastStore((s) => s.push)
|
|
|
|
|
+ const queryClient = useQueryClient()
|
|
|
|
|
+ const [creating, setCreating] = useState(false)
|
|
|
|
|
+ const [pendingDelete, setPendingDelete] = useState<RelationDefinition | null>(null)
|
|
|
|
|
+
|
|
|
|
|
+ const { data, isLoading, isError, error } = useQuery({
|
|
|
|
|
+ queryKey: ['relations', project],
|
|
|
|
|
+ queryFn: () => api.listRelations(project as string),
|
|
|
|
|
+ enabled: !!project,
|
|
|
|
|
+ })
|
|
|
|
|
+ const { data: collectionsData } = useQuery({
|
|
|
|
|
+ queryKey: ['collections', project],
|
|
|
|
|
+ queryFn: () => api.listCollections(project as string),
|
|
|
|
|
+ enabled: !!project,
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ async function doDelete(rel: RelationDefinition) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await api.deleteRelation(project as string, rel.name)
|
|
|
|
|
+ await queryClient.invalidateQueries({ queryKey: ['relations', project] })
|
|
|
|
|
+ push(`Relation ${rel.name} dropped.`, 'success')
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ push(`Could not drop relation: ${err instanceof ApiError ? err.message : String(err)}`, 'error')
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!project) return <p className="text-sm text-slate-400">Select a project first.</p>
|
|
|
|
|
+
|
|
|
|
|
+ const relations = data?.relations ?? []
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <div className="flex flex-col gap-6">
|
|
|
|
|
+ <div className="flex items-start justify-between">
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <h1 className="text-2xl font-semibold text-slate-100">Relations</h1>
|
|
|
|
|
+ <p className="text-slate-400 text-sm mt-1">
|
|
|
|
|
+ How documents in <span className="text-slate-300">{project}</span> reference each other, and what
|
|
|
|
|
+ happens when a referenced document is deleted.
|
|
|
|
|
+ </p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <button
|
|
|
|
|
+ onClick={() => setCreating(true)}
|
|
|
|
|
+ className="inline-flex items-center gap-2 rounded-md bg-indigo-600 px-4 py-2 text-sm font-medium text-white hover:bg-indigo-500"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Plus size={16} /> New relation
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {isError && (
|
|
|
|
|
+ <div className="rounded-xl border border-red-500/20 bg-red-500/5 px-6 py-4 text-sm text-red-400">
|
|
|
|
|
+ Failed to load relations: {error instanceof Error ? error.message : 'Unknown error'}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
|
|
+ {isLoading && <p className="text-sm text-slate-400">Loading...</p>}
|
|
|
|
|
+
|
|
|
|
|
+ {!isLoading && relations.length === 0 && (
|
|
|
|
|
+ <div className="rounded-xl border border-slate-700 bg-slate-800/30 px-6 py-8 text-center">
|
|
|
|
|
+ <p className="text-slate-300 text-sm">No relations in this project yet.</p>
|
|
|
|
|
+ <p className="text-slate-500 text-xs mt-1">
|
|
|
|
|
+ A relation lets you jump from a document to the ones referencing it, and protects a parent from
|
|
|
|
|
+ being deleted out from under its children.
|
|
|
|
|
+ </p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
|
|
+ {relations.length > 0 && (
|
|
|
|
|
+ <div className="overflow-x-auto rounded-xl border border-slate-700">
|
|
|
|
|
+ <table className="w-full text-sm">
|
|
|
|
|
+ <thead>
|
|
|
|
|
+ <tr className="border-b border-slate-700/40 bg-slate-800/60 text-left text-xs uppercase tracking-wide text-slate-400">
|
|
|
|
|
+ <th className="px-4 py-3">Name</th>
|
|
|
|
|
+ <th className="px-4 py-3">Reference</th>
|
|
|
|
|
+ <th className="px-4 py-3">On delete</th>
|
|
|
|
|
+ <th className="px-4 py-3">Validate on write</th>
|
|
|
|
|
+ <th className="px-4 py-3"></th>
|
|
|
|
|
+ </tr>
|
|
|
|
|
+ </thead>
|
|
|
|
|
+ <tbody>
|
|
|
|
|
+ {relations.map((r) => (
|
|
|
|
|
+ <tr key={r.name} className="border-b border-slate-700/20 hover:bg-indigo-500/5">
|
|
|
|
|
+ <td className="px-4 py-3 font-mono text-slate-200">{r.name}</td>
|
|
|
|
|
+ <td className="px-4 py-3">
|
|
|
|
|
+ <span className="inline-flex items-center gap-2 text-slate-300">
|
|
|
|
|
+ <Link to={`/documents?collection=${encodeURIComponent(r.child)}`}
|
|
|
|
|
+ className="font-mono text-indigo-300 hover:underline">
|
|
|
|
|
+ {r.child}.{r.child_field}
|
|
|
|
|
+ </Link>
|
|
|
|
|
+ <ArrowRight size={14} className="text-slate-500" />
|
|
|
|
|
+ <Link to={`/documents?collection=${encodeURIComponent(r.parent)}`}
|
|
|
|
|
+ className="font-mono text-indigo-300 hover:underline">
|
|
|
|
|
+ {r.parent}
|
|
|
|
|
+ </Link>
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </td>
|
|
|
|
|
+ <td className="px-4 py-3">
|
|
|
|
|
+ <span className={`rounded px-1.5 py-0.5 text-xs font-medium ${
|
|
|
|
|
+ r.on_delete === 'restrict' ? 'bg-amber-500/15 text-amber-300'
|
|
|
|
|
+ : r.on_delete === 'cascade' ? 'bg-red-500/15 text-red-300'
|
|
|
|
|
+ : 'bg-slate-700/60 text-slate-300'}`}>
|
|
|
|
|
+ {r.on_delete}
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </td>
|
|
|
|
|
+ <td className="px-4 py-3 text-slate-400">{r.validate_on_write ? 'yes' : 'no'}</td>
|
|
|
|
|
+ <td className="px-4 py-3 text-right">
|
|
|
|
|
+ <button onClick={() => setPendingDelete(r)} title="Drop relation"
|
|
|
|
|
+ className="text-slate-400 hover:text-red-400">
|
|
|
|
|
+ <Trash2 size={16} />
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </td>
|
|
|
|
|
+ </tr>
|
|
|
|
|
+ ))}
|
|
|
|
|
+ </tbody>
|
|
|
|
|
+ </table>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
|
|
+ <Modal open={creating} title="New relation" onClose={() => setCreating(false)}>
|
|
|
|
|
+ <NewRelationForm
|
|
|
|
|
+ project={project}
|
|
|
|
|
+ collections={collectionsData?.collections ?? []}
|
|
|
|
|
+ onDone={async () => {
|
|
|
|
|
+ setCreating(false)
|
|
|
|
|
+ await queryClient.invalidateQueries({ queryKey: ['relations', project] })
|
|
|
|
|
+ }}
|
|
|
|
|
+ onCancel={() => setCreating(false)}
|
|
|
|
|
+ />
|
|
|
|
|
+ </Modal>
|
|
|
|
|
+
|
|
|
|
|
+ <ConfirmDialog
|
|
|
|
|
+ open={!!pendingDelete}
|
|
|
|
|
+ title={`Drop relation ${pendingDelete?.name ?? ''}?`}
|
|
|
|
|
+ message={
|
|
|
|
|
+ pendingDelete
|
|
|
|
|
+ ? `${pendingDelete.child}.${pendingDelete.child_field} will no longer be tied to ${pendingDelete.parent}. Documents are not changed, but deletes of ${pendingDelete.parent} documents stop being ${pendingDelete.on_delete}ed.`
|
|
|
|
|
+ : ''
|
|
|
|
|
+ }
|
|
|
|
|
+ confirmLabel="Drop"
|
|
|
|
|
+ onConfirm={() => { if (pendingDelete) void doDelete(pendingDelete) }}
|
|
|
|
|
+ onClose={() => setPendingDelete(null)}
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|