SprocketTab-C6SjlIoC.js raw

   1  import{u as F,a as L,r as l,b as c,t as a,j as s,B as d,c as S}from"./index-DfKg850Q.js";function U(){const{pubkey:N}=F(),{isOwner:p,userRole:v}=L(),[g,u]=l.useState(""),[t,k]=l.useState(null),[y,w]=l.useState([]),[i,r]=l.useState(!1),[x,b]=l.useState(null),f=l.useRef(null),m=l.useCallback(async()=>{try{const e=await c.loadSprocketStatus();k(e)}catch(e){console.error("Failed to load sprocket status:",e)}},[]),h=l.useCallback(async()=>{r(!0);try{const e=await c.loadSprocketScript();u(e),a.success("Script loaded")}catch(e){a.error(`Failed to load script: ${e instanceof Error?e.message:String(e)}`)}finally{r(!1)}},[]),o=l.useCallback(async()=>{try{const e=await c.loadSprocketVersions();w(e)}catch(e){a.error(`Failed to load versions: ${e instanceof Error?e.message:String(e)}`)}},[]);l.useEffect(()=>{p&&(m(),h(),o())},[p,m,h,o]);const C=async()=>{r(!0);try{await c.saveSprocketScript(g),a.success("Script saved and updated"),await m(),await o()}catch(e){a.error(`Save failed: ${e instanceof Error?e.message:String(e)}`)}finally{r(!1)}},E=async()=>{r(!0);try{await c.restartSprocket(),a.success("Sprocket restarted"),await m()}catch(e){a.error(`Restart failed: ${e instanceof Error?e.message:String(e)}`)}finally{r(!1)}},R=async()=>{if(confirm("Delete the sprocket script? This cannot be undone.")){r(!0);try{await c.deleteSprocket(),u(""),a.success("Script deleted"),await m(),await o()}catch(e){a.error(`Delete failed: ${e instanceof Error?e.message:String(e)}`)}finally{r(!1)}}},$=e=>{var j;const n=((j=e.target.files)==null?void 0:j[0])||null;b(n)},V=async()=>{if(x){r(!0);try{const e=await x.text();u(e),await c.saveSprocketScript(e),a.success("Script uploaded and updated"),b(null),f.current&&(f.current.value=""),await m(),await o()}catch(e){a.error(`Upload failed: ${e instanceof Error?e.message:String(e)}`)}finally{r(!1)}}},z=async e=>{r(!0);try{const n=await c.loadSprocketVersion(e.name);u(n),a.success(`Loaded version: ${e.name}`)}catch(n){a.error(`Failed to load version: ${n instanceof Error?n.message:String(n)}`)}finally{r(!1)}},D=async e=>{if(confirm(`Delete version "${e}"?`)){r(!0);try{await c.deleteSprocketVersion(e),a.success(`Version "${e}" deleted`),await o()}catch(n){a.error(`Failed to delete version: ${n instanceof Error?n.message:String(n)}`)}finally{r(!1)}}};return N?p?s.jsxs("div",{className:"p-4 space-y-4 w-full max-w-2xl",children:[s.jsx("h3",{className:"text-lg font-semibold",children:"Sprocket Script Management"}),s.jsxs("div",{className:"rounded-lg border bg-card p-4 space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("h4",{className:"font-semibold",children:"Script Editor"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(d,{variant:"outline",size:"sm",onClick:E,disabled:i,children:"Restart"}),s.jsx(d,{variant:"destructive",size:"sm",onClick:R,disabled:i||!(t!=null&&t.script_exists),children:"Delete Script"})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("label",{className:"text-sm font-medium",children:"Upload Script"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{ref:f,type:"file",accept:".sh,.bash",onChange:$,disabled:i,className:"flex-1 text-sm file:mr-2 file:rounded-md file:border-0 file:bg-primary file:px-3 file:py-1.5 file:text-xs file:font-medium file:text-primary-foreground hover:file:bg-primary-hover"}),s.jsx(d,{size:"sm",onClick:V,disabled:i||!x,children:"Upload"})]})]}),s.jsxs("div",{className:"rounded-md border bg-background p-3 space-y-1 text-sm",children:[s.jsxs("div",{className:"flex justify-between",children:[s.jsx("span",{className:"font-medium",children:"Status"}),s.jsx("span",{className:S(t!=null&&t.is_running?"text-green-500":"text-red-500"),children:t!=null&&t.is_running?"Running":"Stopped"})]}),(t==null?void 0:t.pid)!=null&&s.jsxs("div",{className:"flex justify-between",children:[s.jsx("span",{className:"font-medium",children:"PID"}),s.jsx("span",{children:t.pid})]}),s.jsxs("div",{className:"flex justify-between",children:[s.jsx("span",{className:"font-medium",children:"Script"}),s.jsx("span",{children:t!=null&&t.script_exists?"Exists":"Not found"})]})]}),s.jsx("textarea",{value:g,onChange:e=>u(e.target.value),placeholder:`#!/bin/bash
   2  # Enter your sprocket script here...`,disabled:i,className:"w-full h-72 rounded-md border bg-background p-3 font-mono text-sm resize-y focus:outline-none focus:ring-2 focus:ring-ring disabled:opacity-50 disabled:cursor-not-allowed"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(d,{size:"sm",onClick:C,disabled:i,children:"Save & Update"}),s.jsx(d,{variant:"outline",size:"sm",onClick:h,disabled:i,children:"Load Current"})]})]}),s.jsxs("div",{className:"rounded-lg border bg-card p-4 space-y-3",children:[s.jsx("h4",{className:"font-semibold",children:"Script Versions"}),s.jsx("div",{className:"space-y-2",children:y.length===0?s.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:"No versions found."}):y.map(e=>s.jsxs("div",{className:S("flex items-center justify-between rounded-md border p-3",e.is_current?"border-primary bg-primary/5":"bg-background"),children:[s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsx("div",{className:"font-medium text-sm truncate",children:e.name}),s.jsxs("div",{className:"text-xs text-muted-foreground flex items-center gap-2",children:[new Date(e.modified).toLocaleString(),e.is_current&&s.jsx("span",{className:"rounded bg-primary px-1.5 py-0.5 text-[10px] font-semibold text-primary-foreground",children:"Current"})]})]}),s.jsxs("div",{className:"flex gap-1 ml-2 shrink-0",children:[s.jsx(d,{variant:"outline",size:"sm",onClick:()=>z(e),disabled:i,children:"Load"}),!e.is_current&&s.jsx(d,{variant:"destructive",size:"sm",onClick:()=>D(e.name),disabled:i,children:"Delete"})]})]},e.name))}),s.jsx(d,{variant:"outline",size:"sm",onClick:o,disabled:i,children:"Refresh Versions"})]})]}):s.jsxs("div",{className:"p-8 text-center space-y-2",children:[s.jsx("p",{className:"text-muted-foreground",children:"Owner permission required for sprocket management."}),s.jsxs("p",{className:"text-sm text-muted-foreground",children:["Set the ",s.jsx("code",{className:"rounded bg-muted px-1.5 py-0.5 font-mono text-xs",children:"ORLY_OWNERS"})," environment variable with your npub when starting the relay."]}),s.jsxs("p",{className:"text-sm text-muted-foreground",children:["Current role: ",s.jsx("span",{className:"font-semibold",children:v||"none"})]})]}):s.jsx("div",{className:"p-8 text-center",children:s.jsx("p",{className:"text-muted-foreground mb-4",children:"Please log in to access sprocket management."})})}export{U as default};
   3