import{r as m,p as Te,j as e,C as J,L as De,s as W,o as I,u as de,V as Y,n as Ie,q as Fe,$ as Ee,D as Ae,T as L,t as se,v as Q,w as Pe,x as Re,y as Le,z as Ne,A as ue,H as M,B as _e,d as Ue,f as $e,E as ze,i as U,F as ne,I as oe,J as y,K as Me,M as $,N as We,O as le,P as Oe,S as re,Q as ae,R as qe,U as ie,X as Ve,Y as He,Z as Ke}from"./index-D78suzy2.js";import{u as Qe}from"./useLocalStorage-CGOwOi-p.js";import{E as Xe}from"./ErrorOutline-C8or5W5q.js";import{g as Je,H as Ye}from"./index-Eiq737ML.js";import{W as Ze,B as P}from"./status-Claw5V5z.js";import{D as Ge,u as et,B as ce}from"./actions-Cx5UWmFM.js";import{I as tt,B as st}from"./install-from-url-D52UYhXy.js";import{u as pe}from"./url-evaluation-Dz0qWdnw.js";import{L as nt}from"./Launch-dZPSseNQ.js";import{A as ot}from"./Add-B60k0zLe.js";import{v as R}from"./index-j3UFl5Dq.js";import{s as me}from"./schema-DqiLBSd2.js";import{P as lt}from"./permission-CCNK9jT0.js";import"./FilterList-CLDlsWBv.js";import"./CSSTransition-CTMK0QDG.js";import"./emotion-cache.browser.esm-BSPYCi-c.js";import"./index-Deye8jzv.js";import"./redux-DfszZyTl.js";import"./typeof-QjJsDpFa.js";import"./depsEqual-CzPGJZq9.js";import"./TableSearch-fEXgtTVk.js";import"./Search-btz9KaC-.js";import"./Clear-RDv89tkq.js";import"./MoreVert-uETYsy_E.js";import"./DeleteOutline-BDLlQ3k7.js";import"./get-safe-url-BgeSRCM1.js";import"./ButtonGroup-CwgnAVvA.js";import"./getValidReactChildren-c-3WOl4y.js";import"./without-wallet-CZASlBin.js";import"./index-BU4T_Wvp.js";import"./cloneDeep-Cig9fPty.js";import"./index-uV9pYRNo.js";import"./Delete-BeglY8dh.js";import"./Autocomplete-B4QHA8fQ.js";import"./ListSubheader-fV4wdcMl.js";import"./ListItemButton-d8kfS6hh.js";import"./Stepper-Dxn7bqpJ.js";function X({blocklet:j,skipAccessibilityCheck:f=!1,...u}){const{t:i}=m.useContext(Te),{urls:v,loading:l,recommendedUrl:h}=pe(j);if(l&&!f)return e.jsx(J,{size:12,...u});const x=f?v[0]:h;return x?e.jsx(at,{...u,children:e.jsx(rt,{children:e.jsxs(De,{href:x,target:"_blank",className:"blocklet-interface",underline:"hover",children:[i("common.visit"),e.jsx(nt,{fontSize:"12",sx:{marginLeft:.5}})]})})}):i("blocklet.router.noAccessibleUrl")}const rt=W.div`
  display: flex;
  margin: 4px 0;
`,at=W.div`
  .blocklet-interface {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    white-space: nowrap;

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