UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

2 lines (1 loc) • 8.94 kB
import{j as e,aU as O,i as I,u as N,r as C,cg as ee,ao as P,lr as ae,bf as te,cT as ne,aX as U,w as se,s as M,cj as re,aN as oe,ls as ie,f as de,aQ as ue,aT as le,aR as ce,K as k,h as q}from"./index-Cikp5fMR.js";const ge=({isOpen:a,onConfirm:t,onClose:n,safeguardData:s,mode:r,safeguardType:i})=>{const{metricName:g}=s.impactMetric,f=i==="featureEnvironment"?"disables the environment":"pauses automation";return e.jsx(O,{title:"Request changes",open:a,secondaryButtonText:"Cancel",onClose:n,customButton:e.jsx(I,{color:"primary",variant:"contained",onClick:t,autoFocus:!0,children:"Add suggestion to draft"}),children:e.jsxs("p",{children:[r==="create"?"Add":"Update"," safeguard that an"," ",f," based on ",e.jsx("strong",{children:g})]})})},fe=()=>{const{makeRequest:a,createRequest:t,errors:n,loading:s}=N({propagateErrors:!0});return{createOrUpdateReleasePlanSafeguard:async({projectId:u,featureName:h,environment:d,planId:p,body:l})=>{const c="createOrUpdateReleasePlanSafeguard",m=`api/admin/projects/${u}/features/${h}/environments/${d}/release-plans/${p}/safeguards`,x=t(m,{method:"PUT",body:JSON.stringify(l)},c);await a(x.caller,x.id)},createOrUpdateFeatureEnvironmentSafeguard:async({projectId:u,featureName:h,environment:d,body:p})=>{const l="createOrUpdateFeatureEnvironmentSafeguard",c=`api/admin/projects/${u}/features/${h}/environments/${d}/safeguards`,m=t(c,{method:"PUT",body:JSON.stringify(p)},l);await a(m.caller,m.id)},deleteReleasePlanSafeguard:async({projectId:u,featureName:h,environment:d,planId:p,safeguardId:l})=>{const c="deleteReleasePlanSafeguard",m=`api/admin/projects/${u}/features/${h}/environments/${d}/release-plans/${p}/safeguards/${l}`,x=t(m,{method:"DELETE"},c);await a(x.caller,x.id)},deleteFeatureEnvironmentSafeguard:async({projectId:u,featureName:h,environment:d,safeguardId:p})=>{const l="deleteFeatureEnvironmentSafeguard",c=`api/admin/projects/${u}/features/${h}/environments/${d}/safeguards/${p}`,m=t(c,{method:"DELETE"},l);await a(m.caller,m.id)},errors:n,loading:s}},pe=({open:a,onClose:t,onConfirm:n,isDeleting:s=!1})=>e.jsxs(O,{title:"Remove safeguard?",open:a,primaryButtonText:s?"Removing...":"Remove safeguard",secondaryButtonText:"Cancel",onClick:n,onClose:t,disabledPrimaryButton:s,children:[e.jsx("p",{children:"You are about to remove the safeguard that pauses automation when conditions are met."}),e.jsx("br",{}),e.jsx("p",{children:"This action cannot be undone."})]}),R=M("div")(({theme:a})=>({display:"flex",padding:a.spacing(.25,.25),backgroundColor:re(a),borderTop:`1px solid ${a.palette.divider}`,borderBottom:`1px solid ${a.palette.divider}`,"& > form":{borderRadius:0,border:"none"}})),he=M("div")(({theme:a})=>({display:"flex",alignItems:"center",gap:a.spacing(1),justifyContent:"space-between",width:"100%",paddingRight:a.spacing(2)})),me=({onSelect:a,releasePlan:t})=>{const[n,s]=C.useState(null),r=ee("featureEnvSafeguards"),{trackEvent:i}=P();return!r&&!t?null:e.jsxs(R,{children:[e.jsx(he,{children:e.jsx(ae,{onClick:g=>{r?(i("safeguards",{props:{eventType:"choose safeguard opened"}}),s(g.currentTarget)):(i("safeguards",{props:{eventType:"form opened",safeguardType:"releasePlan"}}),a("releasePlan"))},color:"primary",startIcon:e.jsx(te,{}),sx:{m:2},children:"Add safeguard"})}),e.jsxs(ne,{anchorEl:n,open:!!n,onClose:()=>s(null),children:[e.jsx(U,{onClick:()=>{i("safeguards",{props:{eventType:"form opened",safeguardType:"featureEnvironment"}}),a("featureEnvironment"),s(null)},children:"Disable environment"}),e.jsx(se,{title:t?"":"Add a release plan to use this safeguard",placement:"right",arrow:!0,children:e.jsx("span",{children:e.jsx(U,{disabled:!t,onClick:()=>{i("safeguards",{props:{eventType:"form opened",safeguardType:"releasePlan"}}),a("releasePlan"),s(null)},children:"Pause automation"})})})]})]})},xe=({projectId:a,featureId:t,environmentName:n,safeguardType:s,releasePlan:r,safeguard:i,onSafeguardChange:g})=>{const{setToastData:f,setToastApiError:u}=de(),{trackEvent:h}=P(),{addChange:d}=ue(),{isChangeRequestConfigured:p}=le(a),{data:l,refetch:c}=ce(a),{createOrUpdateReleasePlanSafeguard:m,deleteReleasePlanSafeguard:x,createOrUpdateFeatureEnvironmentSafeguard:T,deleteFeatureEnvironmentSafeguard:L}=fe(),[J,A]=C.useState(!1),[K,F]=C.useState(!1),[Q,w]=C.useState(!1),[E,$]=C.useState(null),[y,j]=C.useState(!1),B=p(n),X=s==="releasePlan"&&r?o=>m({projectId:a,featureName:t,environment:n,planId:r.id,body:o}):o=>T({projectId:a,featureName:t,environment:n,body:o}),Y=s==="releasePlan"&&r?o=>x({projectId:a,featureName:t,environment:n,planId:r.id,safeguardId:o}):o=>L({projectId:a,featureName:t,environment:n,safeguardId:o}),_=async o=>{if(h("safeguards",{props:{eventType:"safeguard submitted",safeguardType:s}}),B){$(o),w(!0);return}try{await X(o),f({type:"success",text:"Safeguard added successfully"}),g()}catch(S){u(q(S))}},z=async()=>{if(E)try{if(s==="featureEnvironment")await d(a,n,{feature:t,action:"changeFeatureEnvSafeguard",payload:{safeguard:E}});else{if(!r)return;await d(a,n,{feature:t,action:"changeReleasePlanSafeguard",payload:{planId:r.id,safeguard:E}})}await c(),f({type:"success",text:"Added to draft"}),g()}catch(o){u(q(o))}finally{w(!1),$(null)}},G=()=>{i&&(B?F(!0):A(!0))},H=async()=>{if(!(!i||y)){j(!0);try{await Y(i.id),f({type:"success",text:"Safeguard deleted successfully"}),g()}catch(o){u(q(o))}finally{j(!1),A(!1)}}},V=async()=>{if(!(!i||y)){j(!0);try{if(s==="featureEnvironment")await d(a,n,{feature:t,action:"deleteFeatureEnvSafeguard",payload:{safeguardId:i.id}});else{if(!r)return;await d(a,n,{feature:t,action:"deleteReleasePlanSafeguard",payload:{planId:r.id,safeguardId:i.id}})}await c(),f({type:"success",text:"Added to draft"}),g()}catch(o){u(q(o))}finally{j(!1),F(!1)}}},W=o=>{var b;const S=(o.action==="changeReleasePlanSafeguard"||o.action==="deleteReleasePlanSafeguard")&&!!r&&((b=o.payload)==null?void 0:b.planId)===r.id,v=o.action==="changeFeatureEnvSafeguard"||o.action==="deleteFeatureEnvSafeguard";return S||v},Z=C.useMemo(()=>{if(!l)return null;for(const o of l){if(o.environment!==n)continue;const S=o.features.find(b=>b.name===t);if(!S)continue;const v=S.changes.find(W);if(v)return v.action}return null},[l,n,t,r==null?void 0:r.id]);return{handleSubmit:_,handleDeleteRequest:G,pendingSafeguardAction:Z,deleteDialog:{open:J,isDeleting:y,onConfirm:H,onClose:()=>{y||A(!1)}},deleteChangeRequestDialog:{open:K,isDeleting:y,onConfirm:V,onClose:()=>F(!1)},submitChangeRequestDialog:{open:Q,data:E,onConfirm:z,onClose:()=>{w(!1),$(null)}}}},D=({safeguardType:a,releasePlan:t,safeguard:n,environmentName:s,featureId:r,onSafeguardChange:i,onCancel:g})=>{const f=oe("projectId"),{handleSubmit:u,handleDeleteRequest:h,pendingSafeguardAction:d,deleteDialog:p,deleteChangeRequestDialog:l,submitChangeRequestDialog:c}=xe({projectId:f,featureId:r,environmentName:s,safeguardType:a,releasePlan:t,safeguard:n,onSafeguardChange:i}),T=d==="deleteReleasePlanSafeguard"||d==="deleteFeatureEnvSafeguard"?e.jsx(k,{color:"error",children:"Deleted in draft"}):d==="changeReleasePlanSafeguard"||d==="changeFeatureEnvSafeguard"?e.jsx(k,{color:"warning",children:"Modified in draft"}):void 0;return e.jsxs(e.Fragment,{children:[e.jsx(ie,{safeguard:n,onSubmit:u,onCancel:g,onDelete:n?h:void 0,environment:s,featureId:r,badge:T,safeguardType:a}),c.data&&e.jsx(ge,{isOpen:c.open,onConfirm:c.onConfirm,onClose:()=>{c.onClose(),g()},safeguardData:c.data,environment:s,mode:n?"edit":"create",safeguardType:a}),n&&e.jsx(pe,{open:p.open,onClose:p.onClose,onConfirm:p.onConfirm,isDeleting:p.isDeleting}),n&&e.jsx(O,{title:"Request changes",open:l.open,secondaryButtonText:"Cancel",onClose:l.onClose,primaryButtonText:"Add suggestion to draft",onClick:l.onConfirm,disabledPrimaryButton:l.isDeleting,children:e.jsxs("p",{children:[e.jsx("strong",{children:"Remove"})," safeguard",t&&e.jsxs(e.Fragment,{children:[" ","from release plan"," ",e.jsx("strong",{children:t.name})]})," ","for ",e.jsx("strong",{children:r})," in"," ",e.jsx("strong",{children:s})]})})]})},Se=({featureEnvSafeguard:a,releasePlan:t,environmentName:n,featureId:s,onSafeguardChange:r})=>{var d;const i=(d=t==null?void 0:t.safeguards)==null?void 0:d[0],[g,f]=C.useState(null),u=()=>{f(null),r()};return a||i?e.jsxs(e.Fragment,{children:[a&&e.jsx(R,{children:e.jsx(D,{safeguardType:"featureEnvironment",safeguard:a,environmentName:n,featureId:s,onSafeguardChange:u,onCancel:()=>{}})}),i&&e.jsx(R,{children:e.jsx(D,{safeguardType:"releasePlan",releasePlan:t,safeguard:i,environmentName:n,featureId:s,onSafeguardChange:u,onCancel:()=>{}})})]}):g==="featureEnvironment"?e.jsx(R,{children:e.jsx(D,{safeguardType:"featureEnvironment",environmentName:n,featureId:s,onSafeguardChange:r,onCancel:()=>f(null)})}):g==="releasePlan"&&t?e.jsx(R,{children:e.jsx(D,{safeguardType:"releasePlan",releasePlan:t,environmentName:n,featureId:s,onSafeguardChange:r,onCancel:()=>f(null)})}):e.jsx(me,{onSelect:f,releasePlan:t})};export{me as AddSafeguard,Se as Safeguard,Se as default};