import{r as f,p as ye,j as e,C as H,L as Ce,s as W,o as L,u as ce,V as Q,n as ve,q as we,$ as Se,D as Be,T as P,t as G,v as q,w as Te,x as Le,y as Ae,z as Ee,A as me,H as M,B as Ie,d as De,f as Fe,E as Pe,F as Re,i as I,I as ee,J as te,K as y,M as Ne,N as _,O as _e,P as se,Q as Ue,S as le,R as oe,U as Me,X as ne,Y as We,Z as Oe}from"./index-CCv_8omK.js";import{u as $e}from"./useLocalStorage-CbBU4OS9.js";import{E as ze}from"./ErrorOutline-CUOja7My.js";import{g as qe,H as Ve}from"./index-4-FW3N_0.js";import{y as He}from"./index-DkBdKcP-.js";import{W as Qe,B as D}from"./status-Dcx3B-gc.js";import{D as Ke,u as Xe,B as re}from"./actions-DVOuh-Fv.js";import{I as Je,B as Ye}from"./install-from-url-4VtQ9_35.js";import{u as de}from"./url-evaluation-B5iexDQo.js";import{L as Ze}from"./Launch-DITLracb.js";import{A as Ge}from"./Add-l7OvfXrD.js";import{v as F}from"./index-DcXoiIcd.js";import{s as ae}from"./schema-Dsc1QRMh.js";import{P as et}from"./permission-RtK6f5af.js";import"./FilterList-xUYmSJ-Y.js";import"./CSSTransition-Cf_KzaPo.js";import"./emotion-cache.browser.esm-CdT-VIlA.js";import"./index-hu34K3US.js";import"./redux-DfszZyTl.js";import"./typeof-QjJsDpFa.js";import"./depsEqual-BNz3Z5UN.js";import"./TableSearch-CAaRXXpO.js";import"./Search-3L2yy5pr.js";import"./Clear-Cz6Yzx0g.js";import"./MoreVert-BUlWqUnK.js";import"./get-safe-url-B_yWzv49.js";import"./ButtonGroup-CMlvIctd.js";import"./getValidReactChildren-BQH98pcn.js";import"./without-wallet-DcI5W8cf.js";import"./index-DvBW_mZc.js";import"./cloneDeep-BOd1J0Sv.js";import"./index-BCEB7zAW.js";import"./Autocomplete-Bme-ElpV.js";import"./ListSubheader-l4UAKUMT.js";import"./ListItemButton-Cs40YHcx.js";import"./Stepper-D4Zig93k.js";function V({blocklet:j,skipAccessibilityCheck:x=!1,...m}){const{t:r}=f.useContext(ye),{urls:w,loading:n,recommendedUrl:h}=de(j);if(n&&!x)return e.jsx(H,{size:12,...m});const g=x?w[0]:h;return g?e.jsx(st,{...m,children:e.jsx(tt,{children:e.jsxs(Ce,{href:g,target:"_blank",className:"blocklet-interface",underline:"hover",children:[r("common.visit"),e.jsx(Ze,{fontSize:"12",sx:{marginLeft:.5}})]})})}):r("blocklet.router.noAccessibleUrl")}const tt=W.div`
  display: flex;
  margin: 4px 0;
`,st=W.div`
  .blocklet-interface {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    white-space: nowrap;

    img {
      margin-right: 4px;
    }
  }
`;V.propTypes={blocklet:L.object.isRequired,skipAccessibilityCheck:L.bool};function ue({onCancel:j,onSuccess:x}){const{info:m}=ce(),{t:r}=Q(),{api:w}=ve(),[n,h]=we({loading:!1,title:"",titleError:"",description:"",descriptionError:"",isConnectOpen:!1}),g={title:l=>ae.installBlockletTitleSchema.messages({"*":r("blocklet.titleValidationError")}).validate(l)?.error?.message||"",description:l=>ae.installBlockletDescriptionSchema.messages({"*":r("blocklet.descriptionValidationError")}).validate(l)?.error?.message||""},C=()=>{const l=Ae(window?.env?.apiPrefix??"/","/launch-blocklet/install");window.location.href=Ee(l,{from:"empty",title:n.title,description:n.description})},S=l=>{h({loading:!1,isConnectOpen:!1}),x(l)},B=()=>{h({loading:!1,isConnectOpen:!1})},T=l=>{const{value:d,name:u}=l.target;h({[`${u}Error`]:d?g[u](d):""})},A=l=>{const{value:d,name:u}=l.target,b=`${u}Error`;n[b]?h({[u]:d,[b]:d?g[u](d):""}):h({[u]:d})};return e.jsxs(e.Fragment,{children:[e.jsx(Se,{title:r("blocklet.installFromCreate"),fullWidth:!0,maxWidth:"sm",onClose:()=>j(),showCloseButton:!0,open:!0,actions:e.jsxs(e.Fragment,{children:[e.jsx(q,{onClick:l=>{l.stopPropagation(),j()},color:"inherit",children:r("common.cancel")}),e.jsxs(q,{onClick:l=>{l.stopPropagation(),C()},color:"primary","data-cy":"install-blocklet-next-step",variant:"contained",disabled:n.loading||n.titleError||n.descriptionError||!n.title||!n.description,style:{marginLeft:8},children:[n.loading&&e.jsx(H,{size:16}),r("common.create")]})]}),children:e.jsx(Be,{component:"div",children:e.jsxs(P,{component:"div",children:[e.jsx(G,{style:{marginBottom:24},label:`Blocklet ${r("common.title")}`,autoComplete:"off",variant:"outlined",fullWidth:!0,autoFocus:!0,name:"title",value:n.title,onBlur:T,onChange:A,error:!!n.titleError,helperText:n.titleError,disabled:n.loading}),e.jsx(G,{label:`Blocklet ${r("common.description")}`,autoComplete:"off",variant:"outlined",fullWidth:!0,name:"description",value:n.description,onBlur:T,onChange:A,error:!!n.descriptionError,helperText:n.descriptionError,disabled:n.loading})]})})}),e.jsx(Te,{popup:!0,open:n.isConnectOpen,forceConnected:!1,className:"launch-from-create-auth",action:"launch-free-blocklet-by-session",checkFn:w.get,checkTimeout:5*60*1e3,webWalletUrl:Le(m),onSuccess:S,extraParams:{title:n.title,description:n.description},messages:{title:r("setup.keyPair.title"),scan:r("setup.keyPair.scan"),confirm:r("setup.keyPair.confirm"),success:r("setup.keyPair.success")},onClose:B})]})}ue.propTypes={onCancel:L.func.isRequired,onSuccess:L.func.isRequired};function pe({onCreate:j=()=>{}}){const x=me(),{t:m}=Q(),[r,w]=f.useState(""),[n,h]=f.useState(""),[g,C]=f.useState(null),[S,B]=f.useState(null),T=()=>{C(null)},A=()=>{C({onCancel:()=>{T(),C(null)},onSuccess:()=>{T()}})},l=()=>{B({onCancel:()=>{B(null)},onSuccess:(...b)=>{B(null),j(...b)}})},d=()=>{const b=new URL("http://localhost");b.searchParams.set("restoreFrom","server"),b.searchParams.set("serverUrl",window.location.href),x(`/blocklets/restore${b.search}`)},u=()=>{x("/blocklets/restore")};return e.jsxs(e.Fragment,{children:[e.jsxs(F,{onClick:()=>x("/store"),menuButtonProps:{"data-cy":"open-install-menu"},menu:[e.jsx(F.Item,{"data-cy":"open-install-form",onClick:()=>A(),children:m("blocklet.installFromUrl")},"1"),e.jsx(F.Item,{"data-cy":"open-create-form",onClick:()=>l(),children:m("blocklet.installFromCreate")},"2"),e.jsx(F.Item,{"data-cy":"open-install-from-spaces",onClick:()=>u(),children:m("blocklet.restoreFromSpaces.title")},"3"),e.jsx(F.Item,{"data-cy":"open-install-from-spaces",onClick:()=>d(),children:m("blocklet.installFromDiskBackup")},"4")].filter(Boolean),children:[e.jsx(Ge,{}),m("blocklet.installFromMarket")]}),!!r&&e.jsx(M,{variant:"error",message:r,onClose:()=>w("")}),!!n&&e.jsx(M,{variant:"success",duration:3e3,message:n,onClose:()=>h("")}),g&&e.jsx(Je,{onCancel:g.onCancel,onSuccess:g.onSuccess}),S&&e.jsx(ue,{onCancel:S.onCancel,onSuccess:S.onSuccess})]})}pe.propTypes={onCreate:L.func};function fe({blocklet:j,children:x}){const{urls:m}=de(j),r=m[0];return x(r||"")}fe.propTypes={blocklet:L.object.isRequired,children:L.func.isRequired};const U="internal",ie="external";function $t(){return e.jsx(Ke,{children:e.jsx(lt,{})})}function lt(){const j=me(),x=ce(),[m,r]=Ie(),{data:w,externalData:n,error:h,refresh:g,loading:C}=De(),{deletingBlocklets:S,addDeletingDid:B,removeDeletingDid:T,matchDeletingDid:A}=Xe(),{t:l,locale:d}=Q(),[u,b]=f.useState(m.get("searchText")),K=Fe(),xe=o=>o?Date.now()-Number(o):Date.now(),he=[{id:U,name:l("blocklet.internal")},{id:ie,name:l("blocklet.external")}],[E,be]=$e("blocklet-type-filter",U),O=f.useMemo(()=>E===U?w:n,[E,w,n]);A(O.map(o=>o.meta.did));const $=f.useCallback(({action:o,blocklet:i})=>{o==="remove"&&B(i.meta.did)},[B]),z=f.useCallback(o=>{if(o.error){M.error(o.error.message),o.action==="remove"&&T(o.blocklet.meta.did);return}o.action==="remove"&&x.refresh({silent:!0})},[x,T]),X=Pe(o=>o.breakpoints.up("md"));let R=null;const N=f.useMemo(()=>{const o=["installedAt","startedAt","stoppedAt","createdAt"];return(O||[]).map(i=>(o.forEach(s=>{if(i[s]){const t=new Date(i[s]);i[s]=t}}),i))},[O]),k=f.useMemo(()=>{let o=N||[];if(u){const i=Re.toAddress(u.trim()).toLocaleLowerCase();o=o.filter(s=>{const t=I.getDisplayName(s)||"",c=s.site?.domainAliases||[],v=s.children?.map(a=>a.meta?.did?.toLocaleLowerCase());return s.appDid?.toLocaleLowerCase().includes(i)||s.appPid?.toLocaleLowerCase().includes(i)||v.some(a=>a?.toLocaleLowerCase().includes(i))||t.toLocaleLowerCase().includes(i)||c.some(a=>a.value?.toLocaleLowerCase().includes(i))})}return o},[N,u]),[ge,ke]=f.useState([]),J=f.useCallback(()=>{const o=Array.from(new Set(k.map(s=>s.status))),i=[{label:l("common.name"),name:"meta.title",options:{filter:!1,customBodyRenderLite(s){const t=k[s];if(!t)return null;const c=I.getAppMissingConfigs(t).length>0?"components":"overview";return e.jsx(ee,{to:`/blocklets/${t.meta.did}/${c}`,className:"blocklet-name-wrapper",children:e.jsxs(P,{component:"strong",variant:"body1",className:"blocklet-name",children:[e.jsx(te,{blocklet:t,style:{marginRight:8}}),e.jsxs(y,{children:[e.jsx(y,{children:I.getDisplayName(t)}),e.jsxs(y,{children:[e.jsx(Ye,{mode:t.mode||""}),!Ne(t.controller)&&e.jsx(Qe,{type:"primary",children:l("blocklet.external")})]})]})]})},t.meta.did)}}},E===U?{label:l("common.visit"),name:"runtimeInfo.pid",align:"left",options:{sort:!1,filter:!1,customBodyRenderLite(s){const t=k[s];return t?e.jsx(V,{blocklet:t},t.meta.did):null}}}:null,{label:l("common.uptime"),name:"runtimeInfo.uptime",options:{filter:!1,customBodyRenderLite(s){const t=k[s];return t?I.isBlockletRunning(t)&&t.appRuntimeInfo?e.jsx(_,{value:xe(t.appRuntimeInfo.uptime),locale:d},t.meta.did):"":null}}},{label:l("common.installedAt"),name:"installedAt",options:{filter:!1,customBodyRenderLite(s){const t=k[s];return t?t.installedAt?e.jsx(_,{value:t.installedAt.getTime(),locale:d},t.meta.did):t.createdAt?e.jsx(_,{value:t.createdAt.getTime(),locale:d},t.meta.did):"":null}}},{label:l("common.updatedAt"),name:"updatedAt",options:{filter:!1,customBodyRenderLite(s){const t=k[s];return t?e.jsx(_,{value:t.updatedAt,locale:d},t.meta.did):null}}},{label:l("common.status"),name:"status",width:120,align:"center",options:{filter:!0,filterType:"custom",customFilterListOptions:{render:s=>s.map(t=>e.jsx(D,{"data-cy":"blocklet-status-filter-list",status:t},t)),update:(s,t,c)=>(s[c].splice(t,1),s)},filterOptions:{names:o,logic:(s,t)=>t.length?!t.includes(s):!1,display:(s,t,c,v)=>e.jsxs(se,{children:[e.jsx(Ue,{htmlFor:"select-multiple-chip",sx:{background:K.palette.background.default},children:l("common.status")}),e.jsx(le,{multiple:!0,value:s[c],renderValue:a=>a.map(p=>e.jsx(D,{"data-cy":"blocklet-status-filter-display",status:p},p)),onChange:a=>{s[c]=a.target.value,t(s[c],c,v)},sx:{minWidth:120},children:o.map(a=>e.jsxs(oe,{value:a,children:[e.jsx(Me,{color:"primary",checked:s[c].indexOf(a)>-1}),e.jsx(D,{"data-cy":"blocklet-status-filter",status:a})]},a))})]}),fullWidth:!0},customBodyRenderLite(s){const t=k[s];if(!t)return null;const c=["error","waiting","starting"],v=t.children.filter(a=>c.includes(a.status)||c.includes(a.greenStatus));return e.jsxs(y,{sx:{display:"flex",alignItems:"center"},children:[e.jsx(D,{"data-cy":"blocklet-status","status-blocklet-did":t.meta.did,status:S.includes(t.meta.did)?"deleting":t.status,source:t.source,progress:t.progress},t.meta.did),v.length>0&&e.jsx(_e,{title:l("blocklet.status.hasSomeErrors",{blocklets:v.map(a=>a.meta.title).join(", ")}),children:e.jsx(y,{sx:{display:"flex",color:"warning.main",ml:1},children:e.jsx(ze,{})})})]})}}},{label:l("common.actions"),name:"",width:90,options:{sort:!1,filter:!1,customBodyRenderLite(s){const t=k[s];return t?e.jsx(re,{blocklet:t,variant:"menu",onStart:$,onComplete:z},t.meta.did):null}}}].filter(Boolean);ke(i)},[S,d,z,$,k,l,E,K]);if(f.useEffect(()=>{J()},[J]),f.useEffect(()=>{b(m.get("searchText"))},[m]),h)R=e.jsx(ne,{severity:"error",children:e.jsxs("div",{children:[We(h),e.jsx(q,{onClick:()=>g({silent:!1}),children:e.jsx("span",{style:{color:"#44cdc6",textDecoration:"underline"},children:l("common.retry")})})]})});else if(N.length>0||C){const o=qe("blocklets-list");o.searchText&&!u&&b(o.searchText);const i={};X||(i.TableBody=function(t){if(C)return e.jsx(Oe,{relative:"parent",children:e.jsx(H,{})});const{page:c,rowsPerPage:v}=t,a=k.slice(c*v,(c+1)*v);return a.length?e.jsx("div",{children:a.map(p=>e.jsx(fe,{blocklet:p,children:Y=>e.jsxs(y,{className:"blocklet-item--mobile",sx:{display:"flex",justifyContent:"space-between",alignItems:"center"},children:[e.jsx(te,{className:"blocklet__avatar",blocklet:p,size:X?40:60}),e.jsxs(ee,{className:"blocklet__content",to:Y,onClick:Z=>{Z.stopPropagation(),Y||(Z.preventDefault(),M.warning(l("blocklet.router.noAccessibleUrl")))},target:"_blank",children:[e.jsxs(y,{sx:{display:"flex",justifyContent:"space-between",alignItems:"flex-start","& button":{width:24,height:24}},children:[e.jsx(P,{component:"strong",variant:"body1",className:"blocklet__name",children:I.getDisplayName(p,!0)}),e.jsx(re,{blocklet:p,variant:"menu",onStart:$,onComplete:z},p.meta.did)]}),e.jsxs(P,{component:"small",className:"blocklet__addons",sx:{display:"flex",alignItems:"flex-end"},children:[e.jsx(y,{sx:{display:"flex",alignItems:"center"},children:e.jsx(V,{blocklet:p,style:{fontSize:14}})}),e.jsx(D,{"status-blocklet-did":p.meta.did,status:p.status,source:p.source,progress:p.progress},p.meta.did)]})]})]})},p.meta.did))}):e.jsx(He,{children:l("common.empty")})}),R=e.jsx(ot,{children:e.jsx(Ve,{loading:C,className:"blocklet-list",title:e.jsx(y,{sx:{display:"flex",alignItems:"center"},children:l("blocklet.index.tableTitle")}),locale:d,data:k,columns:ge,durable:"blocklets-list",options:{download:!1,print:!1,searchPlaceholder:l("common.search"),searchAlwaysOpen:!1,sortOrder:{name:"uptime",direction:"desc"},filterType:"checkbox",searchDebounceTime:300,onSearchChange:s=>{b(s),r({searchText:s??""})},customSearch:()=>!0,...o,searchText:u},components:i})})}else N.length===0&&(R=e.jsx(ne,{severity:"warning",children:l("blocklet.index.noBlocklet")}));const je=n&&n.length>0;return e.jsxs(nt,{children:[e.jsxs(P,{component:"h2",variant:"h4",className:"page-header",color:"textPrimary",children:[e.jsxs(y,{sx:{display:"flex",alignItems:"center"},children:[l("common.blocklets"),(je||E===ie)&&e.jsx(se,{sx:{ml:1},children:e.jsx(le,{value:E,onChange:o=>be(o.target.value),size:"small",children:he.map(o=>e.jsx(oe,{value:o.id,children:l(o.name)},o.id))})})]}),e.jsx(et,{permission:"mutate_blocklets",children:e.jsx(pe,{onCreate:o=>{j(`/blocklets/${o.appDid}/components`)}})})]}),R]})}const ot=W.div`
  .blocklet-item--mobile {
    padding: 16px 0;
  }
`,nt=W.main`
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .blocklet-list {
    .blocklet-item--mobile {
      .blocklet__avatar {
        margin-right: -60px;
      }
      .blocklet__content {
        padding-left: 68px;
        flex: 1;
        display: flex;
        flex-direction: column;
        z-index: 1;
      }
      .blocklet__name {
        font-weight: bold;
        text-decoration: none;
        white-space: normal;
        word-break: break-all;
      }
      .blocklet__addons {
        display: flex;
        justify-content: space-between;
        margin-top: 8px;
      }
    }
    .blocklet-name-wrapper {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: flex-start;
    }
    .blocklet-name {
      font-weight: bold;
      text-decoration: underline;
      display: flex;
      align-items: center;
      white-space: normal;
      word-break: break-all;
    }

    .blocklet-interface {
      display: flex;
      align-items: center;
      justify-content: flex-start;

      img {
        margin-right: 4px;
      }
    }

    .MuiToolbar-root {
      display: flex;
    }
  }
`;export{lt as BlockletList,$t as default};
