UNPKG

@baseplate-dev/project-builder-web

Version:

Web interface for constructing project builder JSON

3 lines (2 loc) 25.9 kB
import{i as f}from"./_virtual___federation_fn_import-C4el_tlG.js";import{j as e}from"./jsx-runtime-D_zvdyIk.js";import{s as ie,o as E,e as ne,S as Se,p as $e,a as Ne,r as te}from"./index-BsqYBobW.js";import{u as g,S as Fe,E as Me}from"./-constants-B0-XNY1n.js";import{u as Ie}from"./use-model-form-DBAMk4je.js";import{r as Le,c as w,N as Te,O as Oe,P as Ue,Q as Ee,H as ye,L as be,A as Ce}from"./index--XHV4ygU.js";import{u as I,b as se,e as le,f as qe}from"./index.esm-C5MSuOl4.js";import{i as Ae}from"./inflection-C1nKqrJS.js";import{c as Pe}from"./capitalize-gOJXDbsR.js";import{w as Be}from"./words-Cs2RBKRm.js";function M(c){const t=Be(c);if(t.length===0)return"";const[s,...l]=t;return`${s.toLowerCase()}${l.map(r=>Pe(r)).join("")}`}const{modelScalarFieldEntityType:ae}=await f("@baseplate-dev/project-builder-lib"),{Button:re,ButtonGroup:Ke,DropdownMenu:ze,DropdownMenuContent:Ve,DropdownMenuItem:ke,DropdownMenuTrigger:He}=await f("@baseplate-dev/ui-components"),{useMemo:We}=await f("react");function _e({className:c,appendField:t,setValue:s}){const l=g(i=>i.model.fields.map(a=>a.name)),r=g(i=>i.model.primaryKeyFieldRefs.length),n=We(()=>{const i=[];r||i.push({name:"ID (uuid)",fields:[{name:"id",type:"uuid",options:{genUuid:!0},isPrimaryKey:!0}]});const a=l.includes("createdAt"),m=l.includes("updatedAt");return(!a||!m)&&i.push({name:"Timestamps",fields:[{name:"createdAt",type:"dateTime",options:{defaultToNow:!0}},{name:"updatedAt",type:"dateTime",options:{updatedAt:!0,defaultToNow:!0}}]}),i},[l,r]),o=i=>{for(const{isPrimaryKey:a,...m}of i.fields){const d=ae.generateNewId();l.includes(m.name)||t({id:d,...m}),a&&s("model.primaryKeyFieldRefs",[d],{shouldDirty:!0})}};return e.jsxs(Ke,{className:c,children:[e.jsx(re,{variant:"secondary",onClick:()=>{t({id:ae.generateNewId(),name:"",type:"string",isOptional:!0,options:{default:""}})},size:"sm",children:"Add Field"}),e.jsxs(ze,{children:[e.jsx(He,{disabled:n.length===0,asChild:!0,children:e.jsx(re,{variant:"secondary",size:"sm",children:e.jsx(Le,{})})}),e.jsx(Ve,{children:n.map(i=>e.jsx(ke,{onClick:()=>{o(i)},children:i.name},i.name))})]})]})}const{Button:de,DialogClose:Ge,DialogFooter:Ye,MultiComboboxFieldController:Qe}=await f("@baseplate-dev/ui-components"),{useId:Xe}=await f("react"),{z:_}=await f("zod"),Je=_.object({fields:_.array(_.string()).min(1,"At least one primary key is required")});function Ze({className:c,control:t,onSubmitSuccess:s}){const l=g(d=>d.model.fields),{field:{value:r=[],onChange:n}}=I({name:"model.primaryKeyFieldRefs",control:t}),{control:o,handleSubmit:i}=se({resolver:ie(Je),values:{fields:r}}),a=i(d=>{n(d.fields),s?.()}),m=Xe();return e.jsxs("form",{className:w("space-y-4",c),onSubmit:d=>(d.stopPropagation(),a(d)),id:m,children:[e.jsx(Qe,{control:o,name:"fields",options:l.map(d=>({value:d.id,label:d.name})),placeholder:"Select fields to use as primary keys"}),e.jsxs(Ye,{children:[e.jsx(Ge,{asChild:!0,children:e.jsx(de,{variant:"secondary",children:"Cancel"})}),e.jsx(de,{type:"submit",form:m,children:"Save"})]})]})}const{Dialog:en,DialogContent:nn,DialogDescription:tn,DialogHeader:sn,DialogTitle:on,DialogTrigger:ln,useControlledState:an}=await f("@baseplate-dev/ui-components");function we({control:c,children:t,asChild:s,open:l,onOpenChange:r}){const[n,o]=an(l,r,!1);return e.jsxs(en,{open:n,onOpenChange:o,children:[t&&e.jsx(ln,{asChild:s,children:t}),e.jsxs(nn,{children:[e.jsxs(sn,{children:[e.jsx(on,{children:"Primary Keys"}),e.jsx(tn,{children:"Select the fields that will be used as the primary key for this model"})]}),e.jsx(Ze,{control:c,onSubmitSuccess:()=>{o(!1)}})]})]})}const{BadgeWithIcon:rn}=await f("@baseplate-dev/ui-components"),{useState:dn}=await f("react");function cn({className:c,control:t,autoCollapse:s}){const[l,r]=dn(!1),n=!s||l;return e.jsx(we,{control:t,children:e.jsx(rn,{icon:Te,variant:"secondary",className:c,onMouseEnter:()=>{r(!0)},onMouseLeave:()=>{r(!1)},"aria-label":"Primary Key",title:"Primary Key",children:n&&"Primary"})})}const{createModelRelationFieldSchema:mn,ModelFieldUtils:un,modelForeignRelationEntityType:fn,modelLocalRelationEntityType:pn,ModelUtils:ce}=await f("@baseplate-dev/project-builder-lib"),{useDefinitionSchema:hn,useProjectDefinition:xn}=await f("@baseplate-dev/project-builder-lib/web"),{Button:G,ComboboxField:jn,ComboboxFieldController:Y,DialogClose:gn,DialogFooter:vn,InputFieldController:me,SelectFieldController:yn,toast:ue}=await f("@baseplate-dev/ui-components"),De=await f("react"),{useId:bn,useMemo:Cn}=De;function wn(c,t,s){if(!s)return;const l=s.replace(/Id$/,""),r=a=>c.models.find(m=>m.id!==t.id&&a(m))?.id,n=r(a=>a.name.toLowerCase()===l.toLowerCase());if(n)return n;const o=r(a=>a.featureRef===a.featureRef&&a.name.toLowerCase().includes(l.toLowerCase()));return o||r(a=>a.name.toLowerCase().includes(l.toLowerCase()))}function fe(c,t,s,l){const r=(()=>{if(s.name)return s.name;if(s.modelRef){const i=ce.byIdOrThrow(c,s.modelRef);return M(i.name)}})(),n=(()=>{if(!s.modelRef)return;const{model:{fields:i,primaryKeyFieldRefs:a},name:m}=ce.byIdOrThrow(c,s.modelRef),d=i.filter(x=>a.includes(x.id)),p=s.references??[];return d.map((x,h)=>{const D=h===0&&l?l:d.length===1?`${M(m)}Id`:x.name;return{localRef:p[h]?.localRef??t.model.fields.find(v=>v.name===D)?.id,foreignRef:x.id}})})(),o=(()=>{if(!n)return;if(s.foreignRelationName)return s.foreignRelationName;const i=n.every(a=>a.localRef)&&un.areScalarsUnique(t,n.map(a=>a.localRef));return M(i?t.name:Ae.pluralize(t.name))})();return{name:r??"",foreignRelationName:o??"",references:n??[]}}function Dn({className:c,control:t,onSubmitSuccess:s,relationId:l,defaultFieldName:r}){const{definition:n}=xn(),o=g(u=>u),i=o.name,{fields:a}=o.model,{field:{value:m=[],onChange:d}}=I({name:"model.relations",control:t}),p=n.models.map(u=>({label:u.name,value:u.id})),x=l===void 0?void 0:m.find(u=>u.id===l),h=Cn(()=>{if(x)return x;const u=wn(n,o,r);return{id:pn.generateNewId(),foreignId:fn.generateNewId(),modelRef:u??"",onDelete:"Restrict",onUpdate:"Restrict",...fe(n,o,{modelRef:u},r)}},[x,r,n,o]),D=hn(mn),{control:b,handleSubmit:v,setError:H,watch:A,setValue:L}=se({resolver:ie(D),defaultValues:h}),S=A(),$=S.modelRef?n.models.find(u=>u.id===S.modelRef):void 0,T=$?.model.fields,W=a.map(u=>({label:u.name,value:u.id})),P=T?.map(u=>({label:u.name,value:u.id}))??[],j=S.references.some(u=>a.find(C=>C.id===u.localRef)?.isOptional),R=()=>{d(m.filter(u=>u.id!==l)),s?.()},Re=v(u=>{const{id:C}=u;if(a.some(y=>y.name===u.name)||m.some(y=>y.name===u.name&&y.id!==u.id)){H("name",{message:"The relation name cannot be the same as a field/relation name"});return}const U=u.references.map(y=>y.localRef);if(new Set(U).size!==U.length){ue.error("Local fields must be unique");return}const B=u.references.map(y=>y.foreignRef);if(new Set(B).size!==B.length){ue.error("Foreign fields must be unique");return}m.some(y=>y.id===C)?d(m.map(y=>y.id===C?u:y)):d([...m,u]),s?.()}),oe=bn(),O=!!$;return e.jsxs("form",{className:w("space-y-4",c),onSubmit:u=>(u.stopPropagation(),Re(u)),id:oe,children:[e.jsxs("div",{className:"grid grid-cols-2 gap-x-8 gap-y-2",children:[e.jsx(jn,{options:[{label:i,value:i}],value:i,disabled:!0,label:"Local Model"}),e.jsx(Y,{control:b,name:"modelRef",options:p,label:"Foreign Model",onChange:u=>{if(!u)return;const{foreignRelationName:C,name:U,references:B}=fe(n,o,{...S,modelRef:u},r);C&&L("foreignRelationName",C),U&&L("name",U),L("references",B)}})]}),e.jsxs("div",{className:w("grid grid-cols-2 gap-x-8 gap-y-2",!O&&"pointer-events-none opacity-50"),children:[e.jsx(me,{control:b,disabled:!O,name:"name",label:"Local Relation Name",description:e.jsxs("span",{children:["Name of the relation, e.g. ",M(i),".",e.jsx("strong",{children:$?.name?M($.name):"user"})]})}),e.jsx(me,{control:b,disabled:!O,name:"foreignRelationName",label:"Foreign Relation Name",description:e.jsxs("span",{children:["Name of the relation on the foreign model, e.g."," ",M($?.name??"post"),".",e.jsx("strong",{children:h.foreignRelationName||"user"})]})}),e.jsx("div",{className:"text-sm font-medium",children:"Local Field"}),e.jsx("div",{className:"text-sm font-medium",children:"Foreign Field"}),S.references.map((u,C)=>e.jsxs(De.Fragment,{children:[e.jsx(Y,{disabled:!O,control:b,name:`references.${C}.localRef`,options:W}),e.jsx(Y,{control:b,name:`references.${C}.foreignRef`,options:P,disabled:!0})]},C))]}),e.jsx("div",{className:"grid grid-cols-2 gap-x-8 gap-y-2 border-t pt-4",children:e.jsx(yn,{label:"On Delete",control:b,options:[{label:"Cascade (delete entire local row)",value:"Cascade"},{label:"Restrict (throw error)",value:"Restrict"},...j?[{label:"Set Null (set local field to null)",value:"SetNull"}]:[]],name:"onDelete",description:"What to do when the foreign row is deleted"})}),e.jsx(vn,{children:e.jsxs("div",{className:"flex w-full justify-between",children:[l&&e.jsx(G,{variant:"secondary",onClick:u=>{u.preventDefault(),R()},children:"Delete"}),e.jsxs("div",{className:"flex gap-4",children:[e.jsx(gn,{asChild:!0,children:e.jsx(G,{variant:"secondary",children:"Cancel"})}),e.jsx(G,{type:"submit",form:oe,disabled:!O,children:"Save"})]})]})})]})}const{Dialog:Rn,DialogContent:Sn,DialogDescription:$n,DialogHeader:Nn,DialogTitle:Fn,DialogTrigger:Mn,useControlledState:In}=await f("@baseplate-dev/ui-components");function V({control:c,children:t,asChild:s,open:l,onOpenChange:r,relationId:n,defaultFieldName:o}){const[i,a]=In(l,r,!1);return e.jsxs(Rn,{open:i,onOpenChange:a,children:[t&&e.jsx(Mn,{asChild:s,children:t}),e.jsxs(Sn,{width:"lg",children:[e.jsxs(Nn,{children:[e.jsx(Fn,{children:n?"Edit Relation":"Create Relation"}),e.jsx($n,{children:"Set up the relation between this model and another model"})]}),e.jsx(Dn,{control:c,onSubmitSuccess:()=>{a(!1)},relationId:n,defaultFieldName:o})]})]})}const{useProjectDefinition:Ln}=await f("@baseplate-dev/project-builder-lib/web"),{BadgeWithIcon:Tn}=await f("@baseplate-dev/ui-components"),{useState:On}=await f("react");function Un({className:c,control:t,relation:s,autoCollapse:l}){const{definitionContainer:r}=Ln(),[n,o]=On(!1),i=!l||n;return e.jsx(V,{control:t,relationId:s.id,children:e.jsx(Tn,{icon:Oe,variant:"secondary",className:w("max-w-[100px]",c),onMouseEnter:()=>{o(!0)},onMouseLeave:()=>{o(!1)},"aria-label":"Relation",title:"Relation",children:i&&`${r.nameFromId(s.modelRef)} ${s.references.length>1?`(${s.references.length})`:""}`})})}const{modelUniqueConstraintEntityType:En}=await f("@baseplate-dev/project-builder-lib"),{Button:Q,DialogClose:qn,DialogFooter:An,MultiComboboxFieldController:Pn}=await f("@baseplate-dev/ui-components"),{useId:Bn}=await f("react"),{z:K}=await f("zod"),Kn=K.object({id:K.string().optional(),fields:K.array(K.string()).min(1,"At least one field is required")});function zn({className:c,control:t,onSubmitSuccess:s,constraintId:l}){const r=g(h=>h.model.fields),{field:{value:n=[],onChange:o}}=I({name:"model.uniqueConstraints",control:t}),i=l===void 0?void 0:n.find(h=>h.id===l),{control:a,handleSubmit:m}=se({resolver:ie(Kn),values:i?{id:i.id,fields:i.fields.map(h=>h.fieldRef)}:{fields:[]}}),d=()=>{o(n.filter(h=>h.id!==l)),s?.()},p=m(h=>{const D={id:h.id??En.generateNewId(),fields:h.fields.map(v=>({fieldRef:v}))},{id:b}=D;n.some(v=>v.id===b)?o(n.map(v=>v.id===b?D:v)):o([...n,D]),s?.()}),x=Bn();return e.jsxs("form",{className:w("space-y-4",c),onSubmit:h=>(h.stopPropagation(),p(h)),id:x,children:[e.jsx(Pn,{control:a,name:"fields",options:r.map(h=>({value:h.id,label:h.name})),placeholder:"Select fields to use as primary keys"}),e.jsx(An,{children:e.jsxs("div",{className:"flex w-full justify-between",children:[l&&e.jsx(Q,{variant:"secondary",onClick:h=>{h.preventDefault(),d()},children:"Delete"}),e.jsxs("div",{className:"flex gap-4",children:[e.jsx(qn,{asChild:!0,children:e.jsx(Q,{variant:"secondary",children:"Cancel"})}),e.jsx(Q,{type:"submit",form:x,children:"Save"})]})]})})]})}const{Dialog:Vn,DialogContent:kn,DialogDescription:Hn,DialogHeader:Wn,DialogTitle:_n,DialogTrigger:Gn,useControlledState:Yn}=await f("@baseplate-dev/ui-components");function k({control:c,children:t,asChild:s,open:l,onOpenChange:r,constraintId:n}){const[o,i]=Yn(l,r,!1);return e.jsxs(Vn,{open:o,onOpenChange:i,children:[t&&e.jsx(Gn,{asChild:s,children:t}),e.jsxs(kn,{children:[e.jsxs(Wn,{children:[e.jsx(_n,{children:"Unique Constraint"}),e.jsx(Hn,{children:"Select the fields that will be part of this unique constraint."})]}),e.jsx(zn,{control:c,onSubmitSuccess:()=>{i(!1)},constraintId:n})]})]})}const{BadgeWithIcon:Qn}=await f("@baseplate-dev/ui-components"),{useState:Xn}=await f("react");function Jn({className:c,control:t,constraintId:s,autoCollapse:l}){const[r,n]=Xn(!1),o=!l||r,i=g(a=>a.model.uniqueConstraints?.find(m=>m.id===s)?.fields.length??0);return e.jsx(k,{control:t,constraintId:s,children:e.jsx(Qn,{variant:"secondary",icon:Ue,className:w("",c),onMouseEnter:()=>{n(!0)},onMouseLeave:()=>{n(!1)},"aria-label":"Unique Constraint",title:"Unique Constraint",children:o&&`Unique ${i>1?`(${i})`:""}`})})}function Zn({className:c,control:t,idx:s}){const l=g(d=>d.model.fields[s]),r=g(d=>d.model.primaryKeyFieldRefs.includes(l.id)),n=g(d=>d.model.uniqueConstraints?.filter(p=>p.fields.some(x=>x.fieldRef===l.id)).map(p=>p.id)??[]),o=g(({model:d})=>{const p=d.fields[s];return d.relations?.filter(x=>x.references.some(h=>h.localRef===p.id))??[]}),a=(r?1:0)+n.length+o.length>2,m=[r&&e.jsx(cn,{control:t,autoCollapse:a},"primary"),...n.map(d=>e.jsx(Jn,{control:t,constraintId:d,autoCollapse:a},d)),...o.map(d=>e.jsx(Un,{control:t,relation:d,autoCollapse:a},d.id))];return e.jsx("div",{className:w("flex gap-4",c),children:m})}const{EnumUtils:ei}=await f("@baseplate-dev/project-builder-lib"),{useProjectDefinition:ni}=await f("@baseplate-dev/project-builder-lib/web"),{Button:N,ComboboxField:ii,DropdownMenu:pe,DropdownMenuContent:he,DropdownMenuGroup:xe,DropdownMenuItem:X,DropdownMenuTrigger:je,InputField:ge,InputFieldController:J,SelectFieldController:ti}=await f("@baseplate-dev/ui-components");function si({control:c,idx:t,setValue:s}){const{definition:l}=ni(),r=le({control:c,name:`model.fields.${t}.type`}),{field:{value:n,onChange:o}}=I({name:`model.fields.${t}.options`,control:c}),i=le({control:c,name:`model.fields.${t}.options.default`});if(r==="boolean")return e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(ti,{control:c,className:"flex-1",name:`model.fields.${t}.options.default`,options:[{label:"True",value:"true"},{label:"False",value:"false"}],placeholder:"NULL"}),i&&e.jsx(N,{title:"Reset",onClick:()=>{s(`model.fields.${t}.options.default`,"",{shouldDirty:!0})},variant:"ghost",size:"icon",children:e.jsx(E,{})})]});if(["string","int","float"].includes(r))return e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(J,{control:c,placeholder:"NULL",name:`model.fields.${t}.options.default`,className:"flex-1"}),i&&e.jsx(N,{title:"Reset",onClick:()=>{s(`model.fields.${t}.options.default`,void 0,{shouldDirty:!0})},variant:"ghost",size:"icon",children:e.jsx(E,{})})]});if(r==="uuid")return n?.genUuid?e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(ge,{disabled:!0,value:"Random UUID v4",className:"flex-1"}),e.jsx(N,{title:"Reset",onClick:()=>{o({...n,genUuid:!1})},variant:"ghost",size:"icon",children:e.jsx(E,{})})]}):e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(J,{control:c,placeholder:"NULL",name:`model.fields.${t}.options.default`,className:"flex-1"}),e.jsxs(pe,{children:[e.jsx(je,{asChild:!0,children:e.jsx(N,{title:"Options",variant:"ghost",size:"icon",children:e.jsx(ne,{})})}),e.jsx(he,{children:e.jsx(xe,{children:e.jsx(X,{onSelect:()=>{o({...n,genUuid:!0})},children:"Random UUID v4"})})})]})]});if(r==="dateTime"||r==="date"){const{defaultToNow:a,updatedAt:m}=n??{};return a??m?e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(ge,{disabled:!0,value:m?"Last Updated":"Now",className:"flex-1"}),e.jsx(N,{title:"Reset",onClick:()=>{o({...n,defaultToNow:!1,updatedAt:!1})},variant:"ghost",size:"icon",children:e.jsx(E,{})})]}):e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx(J,{placeholder:"NULL",control:c,name:`model.fields.${t}.options.default`,className:"flex-1"}),e.jsxs(pe,{children:[e.jsx(je,{asChild:!0,children:e.jsx(N,{title:"Options",variant:"ghost",size:"icon",children:e.jsx(ne,{})})}),e.jsx(he,{children:e.jsxs(xe,{children:[e.jsx(X,{onSelect:()=>{o({...n,defaultToNow:!0,updatedAt:!1})},children:"Now"}),e.jsx(X,{onSelect:()=>{o({...n,defaultToNow:!0,updatedAt:!0})},children:"Last Updated At"})]})})]})]})}if(r==="enum"&&n?.enumRef){const m=ei.byIdOrThrow(l,n.enumRef).values.map(d=>({label:d.friendlyName,value:d.id}));return e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx(ii,{placeholder:"NULL",value:n.defaultEnumValueRef??null,onChange:d=>{o({...n,defaultEnumValueRef:d||void 0})},options:m,className:"flex-1"}),n.defaultEnumValueRef&&e.jsx(N,{title:"Reset",onClick:()=>{o({...n,defaultEnumValueRef:""})},variant:"ghost",size:"icon",children:e.jsx(E,{})})]})}return e.jsx("div",{})}const{SCALAR_FIELD_TYPES:oi}=await f("@baseplate-dev/project-builder-lib"),{useProjectDefinition:li}=await f("@baseplate-dev/project-builder-lib/web"),{ComboboxField:ai}=await f("@baseplate-dev/ui-components");function ri({control:c,idx:t}){const{definition:s}=li(),l=(s.enums??[]).map(p=>({label:p.name,value:`enum-${p.id}`,description:"Enum type"})),r=[...oi.filter(p=>p!=="enum").map(p=>Fe[p]),...l],{field:{value:n,onChange:o}}=I({name:`model.fields.${t}.type`,control:c}),{field:{value:i,onChange:a}}=I({name:`model.fields.${t}.options`,control:c}),m=p=>{if(p?.startsWith("enum-")){const x=p.replace("enum-","");a({enumRef:x}),o("enum")}else o(p)},d=i?.enumRef;return e.jsx("div",{className:"space-y-2",children:e.jsx(ai,{value:n==="enum"&&d?`enum-${d}`:n,onChange:m,options:r,renderItemLabel:p=>e.jsxs("div",{className:"flex flex-col",children:[e.jsx("div",{children:p.label}),e.jsx("div",{className:"text-xs text-muted-foreground",children:p.description})]})})})}const{modelUniqueConstraintEntityType:di}=await f("@baseplate-dev/project-builder-lib"),{Button:ve,DropdownMenu:ci,DropdownMenuContent:mi,DropdownMenuGroup:ui,DropdownMenuItem:F,DropdownMenuTrigger:fi,InputFieldController:pi,SwitchFieldController:hi,toast:xi}=await f("@baseplate-dev/ui-components"),{useState:q}=await f("react");function ji({className:c,control:t,idx:s,setValue:l,onRemove:r}){const n=g(j=>j.model.fields[s]),o=g(j=>j.model.relations),i=g(j=>j.model.primaryKeyFieldRefs),a=g(j=>j.model.uniqueConstraints)??[],m=i.includes(n.id),d=i.length>1,p=a.filter(j=>j.fields.some(R=>R.fieldRef===n.id)),x=o?.filter(j=>j.references.some(R=>R.localRef===n.id))??[],h=(()=>{if(x.length>0)return`Unable to remove field as it is being used in relations ${x.map(j=>j.name).join(", ")}`;if(m&&d)return"Unable to remove field as it is being used in in the primary key";if(a.some(j=>j.fields.some(R=>R.fieldRef===n.id)))return"Unable to remove field as it is being used in in a unique constraint"})();function D(){if(h){xi.error(h);return}r(s),m&&!d&&l("model.primaryKeyFieldRefs",[],{shouldDirty:!0})}const[b,v]=q(!1),[H,A]=q(!1),[L,S]=q(),[$,T]=q(!1),[W,P]=q();return e.jsxs("div",{className:w("items-center",c),children:[e.jsx("div",{children:e.jsx(pi,{control:t,name:`model.fields.${s}.name`})}),e.jsx("div",{children:e.jsx(ri,{control:t,idx:s})}),e.jsx("div",{children:e.jsx(hi,{control:t,name:`model.fields.${s}.isOptional`})}),e.jsx("div",{className:"mr-4",children:e.jsx(si,{control:t,idx:s,setValue:l})}),e.jsx("div",{children:e.jsx(Zn,{control:t,idx:s})}),e.jsx("div",{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(ci,{children:[e.jsx(fi,{asChild:!0,children:e.jsx(ve,{variant:"ghost",size:"icon",children:e.jsx(ne,{})})}),e.jsx(mi,{children:e.jsxs(ui,{children:[x.length===0&&e.jsx(F,{onSelect:()=>{T(!0),P(void 0)},children:"Add Relation"}),x.length>0&&x.map(j=>e.jsxs(F,{onSelect:()=>{T(!0),P(j.id)},children:["Edit Relation ",x.length>1&&j.name]},j.id)),!d&&!m&&e.jsx(F,{onSelect:()=>{l("model.primaryKeyFieldRefs",[n.id],{shouldDirty:!0})},children:"Set as Primary Key"}),m&&e.jsx(F,{onSelect:()=>{v(!0)},children:"Edit Primary Key"}),p.length===0&&(d||!m)&&e.jsx(F,{onSelect:()=>{l("model.uniqueConstraints",[...a,{id:di.generateNewId(),fields:[{fieldRef:n.id}]}],{shouldDirty:!0})},children:"Make Unique"}),p.length>0&&p.map((j,R)=>e.jsxs(F,{onSelect:()=>{S(j.id),A(!0)},children:["Edit Unique Constraint"," ",p.length>1&&R+1]},j.id))]})})]}),e.jsx(we,{control:t,open:b,onOpenChange:v}),e.jsx(k,{control:t,open:H,onOpenChange:A,constraintId:L}),e.jsx(V,{control:t,open:$,onOpenChange:T,relationId:W,defaultFieldName:n.name}),e.jsx(ve,{variant:"ghost",onClick:()=>{D()},size:"icon",children:e.jsx(Ee,{})})]})})]})}function gi({className:c,control:t,setValue:s}){const{fields:l,remove:r,append:n,move:o}=qe({control:t,name:"model.fields"}),i="grid grid-cols-[repeat(2,minmax(130px,1fr))_60px_minmax(130px,1fr)_minmax(100px,1fr)_80px] gap-3",a=l.map((m,d)=>({id:m.id,element:e.jsx(ji,{className:i,control:t,setValue:s,idx:d,onRemove:r},m.id)}));return e.jsxs("div",{className:w("space-y-4",c),children:[l.length===0?e.jsx("p",{className:"pt-4 text-style-muted",children:"Add some fields to get started"}):e.jsxs("div",{className:"flex w-full flex-col gap-2 bg-white",children:[e.jsxs("div",{className:w(i,"sticky -top-0 z-10 bg-white py-2 text-sm font-semibold","pl-12"),children:[e.jsx("div",{children:"Name"}),e.jsx("div",{children:"Type"}),e.jsx("div",{children:"Optional"}),e.jsx("div",{children:"Default Value"}),e.jsx("div",{className:"sr-only",children:"Badges"}),e.jsx("div",{className:"sr-only",children:"Actions"})]}),e.jsx(Se,{listItems:a,sortItems:o})]}),e.jsx(_e,{appendField:n,setValue:s})]})}const{useProjectDefinition:vi}=await f("@baseplate-dev/project-builder-lib/web"),{Button:Z,RecordView:yi,RecordViewActions:bi,RecordViewItem:z,RecordViewItemList:Ci,SectionListSection:wi,SectionListSectionContent:Di,SectionListSectionDescription:Ri,SectionListSectionHeader:Si,SectionListSectionTitle:$i,useConfirmDialog:Ni}=await f("@baseplate-dev/ui-components");function Fi({control:c,setValue:t}){const{requestConfirm:s}=Ni(),{definitionContainer:l}=vi(),r=g(({model:i})=>i.relations??[]),n=g(({model:i})=>Object.fromEntries(i.fields.map(a=>[a.id,a.name])));function o(i){const a=r.find(m=>m.id===i);s({title:"Delete Relation",content:`Are you sure you want to delete the relation "${a?.name??"<invalid>"}"?`,onConfirm:()=>{t("model.relations",r.filter(m=>m.id!==i))}})}return e.jsxs(wi,{children:[e.jsxs(Si,{children:[e.jsx($i,{children:"Relations"}),e.jsx(Ri,{children:"Define relations to enforce uniqueness on one or more fields."})]}),e.jsxs(Di,{className:"space-y-4",children:[r.map(i=>e.jsxs(yi,{children:[e.jsxs(Ci,{children:[e.jsx(z,{title:"Name",children:i.name}),e.jsx(z,{title:"Local Field",children:i.references.map(a=>n[a.localRef]).join(", ")}),e.jsx(z,{title:"Foreign Model",children:l.nameFromId(i.modelRef)}),e.jsx(z,{title:"On Delete",children:i.onDelete})]}),e.jsxs(bi,{children:[e.jsx(V,{relationId:i.id,control:c,asChild:!0,children:e.jsx(Z,{variant:"ghost",size:"icon",title:"Edit",children:e.jsx(ye,{})})}),e.jsx(Z,{variant:"ghostDestructive",size:"icon",title:"Delete",onClick:()=>{o(i.id)},children:e.jsx(be,{})})]})]},i.id)),e.jsx(V,{control:c,asChild:!0,children:e.jsxs(Z,{variant:"secondary",size:"sm",children:[e.jsx(Ce,{}),"Add Relation"]})})]})]})}const{Button:ee,RecordView:Mi,RecordViewActions:Ii,RecordViewItem:Li,RecordViewItemList:Ti,SectionListSection:Oi,SectionListSectionContent:Ui,SectionListSectionDescription:Ei,SectionListSectionHeader:qi,SectionListSectionTitle:Ai,useConfirmDialog:Pi}=await f("@baseplate-dev/ui-components");function Bi({control:c,setValue:t}){const{requestConfirm:s}=Pi(),l=g(({model:o})=>o.uniqueConstraints??[]),r=g(({model:o})=>Object.fromEntries(o.fields.map(i=>[i.id,i.name])));function n(o){s({title:"Delete Unique Constraint",content:"Are you sure you want to delete this unique constraint?",onConfirm:()=>{t("model.uniqueConstraints",l.filter(i=>i.id!==o))}})}return e.jsxs(Oi,{children:[e.jsxs(qi,{children:[e.jsx(Ai,{children:"Unique Constraints"}),e.jsx(Ei,{children:"Define unique constraints to enforce uniqueness on one or more fields."})]}),e.jsxs(Ui,{className:"space-y-4",children:[l.map(o=>e.jsxs(Mi,{children:[e.jsx(Ti,{children:e.jsx(Li,{title:"Fields",children:o.fields.map(i=>r[i.fieldRef]??"<invalid>").join(", ")})}),e.jsxs(Ii,{children:[e.jsx(k,{constraintId:o.id,control:c,asChild:!0,children:e.jsx(ee,{variant:"ghost",size:"icon",title:"Edit",children:e.jsx(ye,{})})}),e.jsx(ee,{variant:"ghostDestructive",size:"icon",title:"Delete",onClick:()=>{n(o.id)},children:e.jsx(be,{})})]})]},o.id)),e.jsx(k,{control:c,asChild:!0,children:e.jsxs(ee,{variant:"secondary",size:"sm",children:[e.jsx(Ce,{}),"Add Unique Constraint"]})})]})]})}const{modelEntityType:Ki,modelLocalRelationEntityType:zi,modelScalarFieldEntityType:Vi}=await f("@baseplate-dev/project-builder-lib"),{useBlockUnsavedChangesNavigate:ki}=await f("@baseplate-dev/project-builder-lib/web"),{FormActionBar:Hi,SectionList:Wi}=await f("@baseplate-dev/ui-components");te(Ki,"/data/models/edit/{key}");te(Vi,"/data/models/edit/{parentKey}");te(zi,"/data/models/edit/{parentKey}");const tt=function(){const{key:t}=$e.useParams(),{form:s,onSubmit:l,originalModel:r}=Ie({omit:["name","featureRef"],modelKey:t}),{control:n,watch:o,getValues:i,setValue:a,reset:m}=s;return ki({control:n,reset:m,onSubmit:l}),e.jsx(Ne,{children:e.jsx(Me,{originalModel:r,getValues:i,watch:o,children:e.jsxs("form",{onSubmit:l,className:"max-w-7xl min-w-[700px] flex-1 space-y-4 px-4 pb-4",children:[e.jsx(gi,{control:n,setValue:a}),e.jsxs(Wi,{children:[e.jsx(Fi,{control:n,setValue:a}),e.jsx(Bi,{control:n,setValue:a})]}),e.jsx(Hi,{form:s})]})})})};export{tt as component}; //# sourceMappingURL=index-Bl_u3fna.js.map