import { useEffect, useState } from "react"; import type { DdlParams } from "../../../lib/objectCrud"; import * as cmd from "../../../lib/commands"; import type { SchemaGraph } from "../../../lib/types"; import { ColumnPicker } from "./ColumnPicker"; import { FormRow, inputClass, controlClass } from "./formRow"; interface Props { connectionId: string; params: DdlParams; schemas?: string[]; onChange: (p: DdlParams) => void; } const METHODS = ["", "btree", "hash", "gist", "gin", "brin"]; function patchAction(params: DdlParams, patch: Record): DdlParams { const action = (params.action ?? {}) as Record; return { ...params, action: { ...action, ...patch } }; } export function IndexForm({ connectionId, params, schemas, onChange }: Props) { const p = params as Record; const action = (p.action ?? {}) as Record; const [cols, setCols] = useState([]); useEffect(() => { cmd .getSchemaGraph(connectionId, (p.schema as string) || undefined) .then((g: SchemaGraph) => { const t = g.tables.find( (t) => t.name === (p.table as string) && t.schema === (p.schema as string), ); setCols(t ? t.columns.map((c) => c.name) : []); }) .catch(() => setCols([])); }, [connectionId, p.schema, p.table]); const selected: string[] = (action.columns as string[]) ?? []; const setAction = (patch: Record) => onChange(patchAction(params, patch)); return (
{schemas && schemas.length > 0 ? ( ) : ( onChange({ ...p, schema: e.target.value })} className={inputClass} /> )} onChange({ ...p, table: e.target.value })} className={inputClass} /> onChange({ ...p, name: e.target.value })} className={inputClass} />
setAction({ columns: selected.includes(c) ? selected.filter((x) => x !== c) : [...selected, c], }) } />
setAction({ predicate: e.target.value || null })} className={inputClass} />
); }