@baseplate-dev/project-builder-web
Version:
Web interface for constructing project builder JSON
3 lines (2 loc) • 25.9 kB
JavaScript
import{i as f}from"./_virtual___federation_fn_import-DOo8C-zl.js";import{j as e}from"./jsx-runtime-D_zvdyIk.js";import{v as g,b as ie,w as q,f as ne,S as Se,x as $e,a as Ne,y as Fe,z as te}from"./index-DiAhmW9l.js";import{u as Me}from"./use-model-form-CMNCrezb.js";import{r as Ie,c as w,N as Le,O as Te,P as Oe,Q as Ue,H as ye,L as be,A as Ce}from"./index-G5ZRloOW.js";import{u as I,f as se,e as le,i as qe}from"./index.esm-DImnoN58.js";import{i as Ee}from"./inflection-C1nKqrJS.js";import{c as Ae}from"./capitalize-gOJXDbsR.js";import{w as Pe}from"./words-Cs2RBKRm.js";import{S as Be}from"./-constants-CfroQnV_.js";function M(c){const t=Pe(c);if(t.length===0)return"";const[s,...l]=t;return`${s.toLowerCase()}${l.map(a=>Ae(a)).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(n=>n.model.fields.map(r=>r.name)),a=g(n=>n.model.primaryKeyFieldRefs.length),i=We(()=>{const n=[];a||n.push({name:"ID (uuid)",fields:[{name:"id",type:"uuid",options:{genUuid:!0},isPrimaryKey:!0}]});const r=l.includes("createdAt"),m=l.includes("updatedAt");return(!r||!m)&&n.push({name:"Timestamps",fields:[{name:"createdAt",type:"dateTime",options:{defaultToNow:!0}},{name:"updatedAt",type:"dateTime",options:{updatedAt:!0,defaultToNow:!0}}]}),n},[l,a]),o=n=>{for(const{isPrimaryKey:r,...m}of n.fields){const d=ae.generateNewId();l.includes(m.name)||t({id:d,...m}),r&&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:i.length===0,asChild:!0,children:e.jsx(re,{variant:"secondary",size:"sm",children:e.jsx(Ie,{})})}),e.jsx(Ve,{children:i.map(n=>e.jsx(ke,{onClick:()=>{o(n)},children:n.name},n.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:a=[],onChange:i}}=I({name:"model.primaryKeyFieldRefs",control:t}),{control:o,handleSubmit:n}=se({resolver:ie(Je),values:{fields:a}}),r=n(d=>{i(d.fields),s?.()}),m=Xe();return e.jsxs("form",{className:w("space-y-4",c),onSubmit:d=>(d.stopPropagation(),r(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:a}){const[i,o]=an(l,a,!1);return e.jsxs(en,{open:i,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,a]=dn(!1),i=!s||l;return e.jsx(we,{control:t,children:e.jsx(rn,{icon:Le,variant:"secondary",className:c,onMouseEnter:()=>{a(!0)},onMouseLeave:()=>{a(!1)},"aria-label":"Primary Key",title:"Primary Key",children:i&&"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$/,""),a=r=>c.models.find(m=>m.id!==t.id&&r(m))?.id,i=a(r=>r.name.toLowerCase()===l.toLowerCase());if(i)return i;const o=a(r=>r.featureRef===r.featureRef&&r.name.toLowerCase().includes(l.toLowerCase()));return o||a(r=>r.name.toLowerCase().includes(l.toLowerCase()))}function fe(c,t,s,l){const a=(()=>{if(s.name)return s.name;if(s.modelRef){const n=ce.byIdOrThrow(c,s.modelRef);return M(n.name)}})(),i=(()=>{if(!s.modelRef)return;const{model:{fields:n,primaryKeyFieldRefs:r},name:m}=ce.byIdOrThrow(c,s.modelRef),d=n.filter(x=>r.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(!i)return;if(s.foreignRelationName)return s.foreignRelationName;const n=i.every(r=>r.localRef)&&un.areScalarsUnique(t,i.map(r=>r.localRef));return M(n?t.name:Ee.pluralize(t.name))})();return{name:a??"",foreignRelationName:o??"",references:i??[]}}function Dn({className:c,control:t,onSubmitSuccess:s,relationId:l,defaultFieldName:a}){const{definition:i}=xn(),o=g(u=>u),n=o.name,{fields:r}=o.model,{field:{value:m=[],onChange:d}}=I({name:"model.relations",control:t}),p=i.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(i,o,a);return{id:pn.generateNewId(),foreignId:fn.generateNewId(),modelRef:u??"",onDelete:"Restrict",onUpdate:"Restrict",...fe(i,o,{modelRef:u},a)}},[x,a,i,o]),D=hn(mn),{control:b,handleSubmit:v,setError:H,watch:A,setValue:L}=se({resolver:ie(D),defaultValues:h}),S=A(),$=S.modelRef?i.models.find(u=>u.id===S.modelRef):void 0,T=$?.model.fields,W=r.map(u=>({label:u.name,value:u.id})),P=T?.map(u=>({label:u.name,value:u.id}))??[],j=S.references.some(u=>r.find(C=>C.id===u.localRef)?.isOptional),R=()=>{d(m.filter(u=>u.id!==l)),s?.()},Re=v(u=>{const{id:C}=u;if(r.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:n,value:n}],value:n,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(i,o,{...S,modelRef:u},a);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(n),".",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:a,relationId:i,defaultFieldName:o}){const[n,r]=In(l,a,!1);return e.jsxs(Rn,{open:n,onOpenChange:r,children:[t&&e.jsx(Mn,{asChild:s,children:t}),e.jsxs(Sn,{width:"lg",children:[e.jsxs(Nn,{children:[e.jsx(Fn,{children:i?"Edit Relation":"Create Relation"}),e.jsx($n,{children:"Set up the relation between this model and another model"})]}),e.jsx(Dn,{control:c,onSubmitSuccess:()=>{r(!1)},relationId:i,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:a}=Ln(),[i,o]=On(!1),n=!l||i;return e.jsx(V,{control:t,relationId:s.id,children:e.jsx(Tn,{icon:Te,variant:"secondary",className:w("max-w-[100px]",c),onMouseEnter:()=>{o(!0)},onMouseLeave:()=>{o(!1)},"aria-label":"Relation",title:"Relation",children:n&&`${a.nameFromId(s.modelRef)} ${s.references.length>1?`(${s.references.length})`:""}`})})}const{modelUniqueConstraintEntityType:qn}=await f("@baseplate-dev/project-builder-lib"),{Button:Q,DialogClose:En,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 a=g(h=>h.model.fields),{field:{value:i=[],onChange:o}}=I({name:"model.uniqueConstraints",control:t}),n=l===void 0?void 0:i.find(h=>h.id===l),{control:r,handleSubmit:m}=se({resolver:ie(Kn),values:n?{id:n.id,fields:n.fields.map(h=>h.fieldRef)}:{fields:[]}}),d=()=>{o(i.filter(h=>h.id!==l)),s?.()},p=m(h=>{const D={id:h.id??qn.generateNewId(),fields:h.fields.map(v=>({fieldRef:v}))},{id:b}=D;i.some(v=>v.id===b)?o(i.map(v=>v.id===b?D:v)):o([...i,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:r,name:"fields",options:a.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(En,{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:a,constraintId:i}){const[o,n]=Yn(l,a,!1);return e.jsxs(Vn,{open:o,onOpenChange:n,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:()=>{n(!1)},constraintId:i})]})]})}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[a,i]=Xn(!1),o=!l||a,n=g(r=>r.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:Oe,className:w("",c),onMouseEnter:()=>{i(!0)},onMouseLeave:()=>{i(!1)},"aria-label":"Unique Constraint",title:"Unique Constraint",children:o&&`Unique ${n>1?`(${n})`:""}`})})}function Zn({className:c,control:t,idx:s}){const l=g(d=>d.model.fields[s]),a=g(d=>d.model.primaryKeyFieldRefs.includes(l.id)),i=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))??[]}),r=(a?1:0)+i.length+o.length>2,m=[a&&e.jsx(cn,{control:t,autoCollapse:r},"primary"),...i.map(d=>e.jsx(Jn,{control:t,constraintId:d,autoCollapse:r},d)),...o.map(d=>e.jsx(Un,{control:t,relation:d,autoCollapse:r},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(),a=le({control:c,name:`model.fields.${t}.type`}),{field:{value:i,onChange:o}}=I({name:`model.fields.${t}.options`,control:c}),n=le({control:c,name:`model.fields.${t}.options.default`});if(a==="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"}),n&&e.jsx(N,{title:"Reset",onClick:()=>{s(`model.fields.${t}.options.default`,"",{shouldDirty:!0})},variant:"ghost",size:"icon",children:e.jsx(q,{})})]});if(["string","int","float"].includes(a))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"}),n&&e.jsx(N,{title:"Reset",onClick:()=>{s(`model.fields.${t}.options.default`,void 0,{shouldDirty:!0})},variant:"ghost",size:"icon",children:e.jsx(q,{})})]});if(a==="uuid")return i?.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({...i,genUuid:!1})},variant:"ghost",size:"icon",children:e.jsx(q,{})})]}):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({...i,genUuid:!0})},children:"Random UUID v4"})})})]})]});if(a==="dateTime"||a==="date"){const{defaultToNow:r,updatedAt:m}=i??{};return r??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({...i,defaultToNow:!1,updatedAt:!1})},variant:"ghost",size:"icon",children:e.jsx(q,{})})]}):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({...i,defaultToNow:!0,updatedAt:!1})},children:"Now"}),e.jsx(X,{onSelect:()=>{o({...i,defaultToNow:!0,updatedAt:!0})},children:"Last Updated At"})]})})]})]})}if(a==="enum"&&i?.enumRef){const m=ei.byIdOrThrow(l,i.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:i.defaultEnumValueRef??null,onChange:d=>{o({...i,defaultEnumValueRef:d||void 0})},options:m,className:"flex-1"}),i.defaultEnumValueRef&&e.jsx(N,{title:"Reset",onClick:()=>{o({...i,defaultEnumValueRef:""})},variant:"ghost",size:"icon",children:e.jsx(q,{})})]})}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"})),a=[...oi.filter(p=>p!=="enum").map(p=>Be[p]),...l],{field:{value:i,onChange:o}}=I({name:`model.fields.${t}.type`,control:c}),{field:{value:n,onChange:r}}=I({name:`model.fields.${t}.options`,control:c}),m=p=>{if(p?.startsWith("enum-")){const x=p.replace("enum-","");r({enumRef:x}),o("enum")}else o(p)},d=n?.enumRef;return e.jsx("div",{className:"space-y-2",children:e.jsx(ai,{value:i==="enum"&&d?`enum-${d}`:i,onChange:m,options:a,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:E}=await f("react");function ji({className:c,control:t,idx:s,setValue:l,onRemove:a}){const i=g(j=>j.model.fields[s]),o=g(j=>j.model.relations),n=g(j=>j.model.primaryKeyFieldRefs),r=g(j=>j.model.uniqueConstraints)??[],m=n.includes(i.id),d=n.length>1,p=r.filter(j=>j.fields.some(R=>R.fieldRef===i.id)),x=o?.filter(j=>j.references.some(R=>R.localRef===i.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(r.some(j=>j.fields.some(R=>R.fieldRef===i.id)))return"Unable to remove field as it is being used in in a unique constraint"})();function D(){if(h){xi.error(h);return}a(s),m&&!d&&l("model.primaryKeyFieldRefs",[],{shouldDirty:!0})}const[b,v]=E(!1),[H,A]=E(!1),[L,S]=E(),[$,T]=E(!1),[W,P]=E();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",[i.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",[...r,{id:di.generateNewId(),fields:[{fieldRef:i.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:i.name}),e.jsx(ve,{variant:"ghost",onClick:()=>{D()},size:"icon",children:e.jsx(Ue,{})})]})})]})}function gi({className:c,control:t,setValue:s}){const{fields:l,remove:a,append:i,move:o}=qe({control:t,name:"model.fields"}),n="grid grid-cols-[repeat(2,minmax(130px,1fr))_60px_minmax(130px,1fr)_minmax(100px,1fr)_80px] gap-3",r=l.map((m,d)=>({id:m.id,element:e.jsx(ji,{className:n,control:t,setValue:s,idx:d,onRemove:a},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(n,"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:r,sortItems:o})]}),e.jsx(_e,{appendField:i,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(),a=g(({model:n})=>n.relations??[]),i=g(({model:n})=>Object.fromEntries(n.fields.map(r=>[r.id,r.name])));function o(n){const r=a.find(m=>m.id===n);s({title:"Delete Relation",content:`Are you sure you want to delete the relation "${r?.name??"<invalid>"}"?`,onConfirm:()=>{t("model.relations",a.filter(m=>m.id!==n))}})}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:[a.map(n=>e.jsxs(yi,{children:[e.jsxs(Ci,{children:[e.jsx(z,{title:"Name",children:n.name}),e.jsx(z,{title:"Local Field",children:n.references.map(r=>i[r.localRef]).join(", ")}),e.jsx(z,{title:"Foreign Model",children:l.nameFromId(n.modelRef)}),e.jsx(z,{title:"On Delete",children:n.onDelete})]}),e.jsxs(bi,{children:[e.jsx(V,{relationId:n.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(n.id)},children:e.jsx(be,{})})]})]},n.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:qi,SectionListSectionHeader:Ei,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??[]),a=g(({model:o})=>Object.fromEntries(o.fields.map(n=>[n.id,n.name])));function i(o){s({title:"Delete Unique Constraint",content:"Are you sure you want to delete this unique constraint?",onConfirm:()=>{t("model.uniqueConstraints",l.filter(n=>n.id!==o))}})}return e.jsxs(Oi,{children:[e.jsxs(Ei,{children:[e.jsx(Ai,{children:"Unique Constraints"}),e.jsx(qi,{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(n=>a[n.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:()=>{i(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}");function tt(){const{key:c}=$e.useParams(),{form:t,onSubmit:s,originalModel:l}=Me({omit:["name","featureRef"],modelKey:c}),{control:a,watch:i,getValues:o,setValue:n,reset:r}=t;return ki({control:a,reset:r,onSubmit:s}),e.jsx(Ne,{children:e.jsx(Fe,{originalModel:l,getValues:o,watch:i,children:e.jsxs("form",{onSubmit:s,className:"max-w-7xl min-w-[700px] flex-1 space-y-4 px-4 pb-4",children:[e.jsx(gi,{control:a,setValue:n}),e.jsxs(Wi,{children:[e.jsx(Fi,{control:a,setValue:n}),e.jsx(Bi,{control:a,setValue:n})]}),e.jsx(Hi,{form:t})]})})})}export{tt as component};
//# sourceMappingURL=index-m5xBA8Vv.js.map