2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
import{i as Q,r as d,j as e}from"./router-zNjPR4CY.js";import{E as V,x,S as X,B as n,C as f,D as F,i as O,j as P,k as z,l as I,L as r,I as c,a3 as M}from"./index-CuOHsLf7.js";import{B as N}from"./badge-CDs67obV.js";import{S as g}from"./switch-Kc2EZ0Ga.js";import{T as Y,a as Z,b as A,c as h,d as ee,e as m}from"./table-DFeDhBY_.js";import{g as se,w as ae,L as ie,T as ne,C as L,a as R,u as B,v as U}from"./icons-DTcdLw9j.js";import"./misc-BwRzHX8c.js";import"./radix-C-ZuImoP.js";import"./utils-DjBw3JGv.js";function pe(){const J=Q(),{toast:l}=V(),[w,$]=d.useState([]),[H,y]=d.useState(!0),[b,C]=d.useState(null),[k,E]=d.useState(null),[G,p]=d.useState(!1),[W,j]=d.useState(!1),[a,t]=d.useState({id:"",name:"",raw_prefix:"",clone_prefix:"",enabled:!0,priority:1}),o=d.useCallback(async()=>{try{y(!0),C(null);const s=await x("/api/webui/plugins/mirrors");if(!s.ok)throw new Error("获取镜像源列表失败");const i=await s.json();$(i.mirrors||[])}catch(s){const i=s instanceof Error?s.message:"加载镜像源失败";C(i),l({title:"加载失败",description:i,variant:"destructive"})}finally{y(!1)}},[l]);d.useEffect(()=>{o()},[o]);const q=async()=>{try{const s=await x("/api/webui/plugins/mirrors",{method:"POST",body:JSON.stringify(a)});if(!s.ok){const i=await s.json();throw new Error(i.detail||"添加镜像源失败")}l({title:"添加成功",description:"镜像源已添加"}),p(!1),t({id:"",name:"",raw_prefix:"",clone_prefix:"",enabled:!0,priority:1}),o()}catch(s){l({title:"添加失败",description:s instanceof Error?s.message:"未知错误",variant:"destructive"})}},K=async()=>{if(k)try{if(!(await x(`/api/webui/plugins/mirrors/${k.id}`,{method:"PUT",body:JSON.stringify({name:a.name,raw_prefix:a.raw_prefix,clone_prefix:a.clone_prefix,enabled:a.enabled,priority:a.priority})})).ok)throw new Error("更新镜像源失败");l({title:"更新成功",description:"镜像源已更新"}),j(!1),E(null),o()}catch(s){l({title:"更新失败",description:s instanceof Error?s.message:"未知错误",variant:"destructive"})}},D=async s=>{if(confirm("确定要删除这个镜像源吗?"))try{if(!(await x(`/api/webui/plugins/mirrors/${s}`,{method:"DELETE"})).ok)throw new Error("删除镜像源失败");l({title:"删除成功",description:"镜像源已删除"}),o()}catch(i){l({title:"删除失败",description:i instanceof Error?i.message:"未知错误",variant:"destructive"})}},_=async s=>{try{if(!(await x(`/api/webui/plugins/mirrors/${s.id}`,{method:"PUT",body:JSON.stringify({enabled:!s.enabled})})).ok)throw new Error("更新状态失败");o()}catch(i){l({title:"更新失败",description:i instanceof Error?i.message:"未知错误",variant:"destructive"})}},T=s=>{E(s),t({id:s.id,name:s.name,raw_prefix:s.raw_prefix,clone_prefix:s.clone_prefix,enabled:s.enabled,priority:s.priority}),j(!0)},u=async(s,i)=>{const S=i==="up"?s.priority-1:s.priority+1;if(!(S<1))try{if(!(await x(`/api/webui/plugins/mirrors/${s.id}`,{method:"PUT",body:JSON.stringify({priority:S})})).ok)throw new Error("更新优先级失败");o()}catch(v){l({title:"更新失败",description:v instanceof Error?v.message:"未知错误",variant:"destructive"})}};return e.jsx(X,{className:"h-full",children:e.jsxs("div",{className:"space-y-6 p-4 sm:p-6",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>J({to:"/plugins"}),children:e.jsx(se,{className:"h-5 w-5"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl sm:text-3xl font-bold",children:"镜像源配置"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"管理 Git 克隆和文件下载的镜像源"})]})]}),e.jsxs(n,{onClick:()=>p(!0),children:[e.jsx(ae,{className:"h-4 w-4 mr-2"}),"添加镜像源"]})]}),H?e.jsx(f,{className:"p-6",children:e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(ie,{className:"h-8 w-8 animate-spin text-primary"})})}):b?e.jsx(f,{className:"p-6",children:e.jsxs("div",{className:"flex flex-col items-center justify-center py-8 text-center",children:[e.jsx(ne,{className:"h-12 w-12 text-destructive mb-4"}),e.jsx("h3",{className:"text-lg font-semibold mb-2",children:"加载失败"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:b}),e.jsx(n,{onClick:o,children:"重新加载"})]})}):e.jsxs(f,{children:[e.jsx("div",{className:"hidden md:block",children:e.jsxs(Y,{"aria-label":"插件镜像源列表",children:[e.jsx(Z,{children:e.jsxs(A,{children:[e.jsx(h,{children:"状态"}),e.jsx(h,{children:"名称"}),e.jsx(h,{children:"ID"}),e.jsx(h,{children:"优先级"}),e.jsx(h,{className:"text-right",children:"操作"})]})}),e.jsx(ee,{children:w.map(s=>e.jsxs(A,{children:[e.jsx(m,{children:e.jsx(g,{checked:s.enabled,onCheckedChange:()=>_(s)})}),e.jsx(m,{children:e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:s.name}),e.jsxs("div",{className:"text-xs text-muted-foreground mt-1",children:["Raw: ",s.raw_prefix]})]})}),e.jsx(m,{children:e.jsx(N,{variant:"outline",children:s.id})}),e.jsx(m,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-mono",children:s.priority}),e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx(n,{variant:"ghost",size:"icon",className:"h-5 w-5",onClick:()=>u(s,"up"),disabled:s.priority===1,children:e.jsx(L,{className:"h-3 w-3"})}),e.jsx(n,{variant:"ghost",size:"icon",className:"h-5 w-5",onClick:()=>u(s,"down"),children:e.jsx(R,{className:"h-3 w-3"})})]})]})}),e.jsx(m,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>T(s),children:e.jsx(B,{className:"h-4 w-4"})}),e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>D(s.id),children:e.jsx(U,{className:"h-4 w-4 text-destructive"})})]})})]},s.id))})]})}),e.jsx("div",{className:"md:hidden p-4 space-y-4",children:w.map(s=>e.jsx(f,{className:"p-4",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h3",{className:"font-semibold",children:s.name}),s.enabled&&e.jsx(N,{variant:"default",className:"text-xs",children:"启用"})]}),e.jsx(N,{variant:"outline",className:"mt-1 text-xs",children:s.id})]}),e.jsx(g,{checked:s.enabled,onCheckedChange:()=>_(s)})]}),e.jsxs("div",{className:"text-sm space-y-1",children:[e.jsxs("div",{className:"text-muted-foreground",children:[e.jsx("span",{className:"font-medium",children:"Raw: "}),e.jsx("span",{className:"break-all",children:s.raw_prefix})]}),e.jsxs("div",{className:"text-muted-foreground",children:[e.jsx("span",{className:"font-medium",children:"优先级: "}),e.jsx("span",{className:"font-mono",children:s.priority})]})]}),e.jsxs("div",{className:"flex items-center gap-2 pt-2 border-t",children:[e.jsxs(n,{variant:"outline",size:"sm",className:"flex-1",onClick:()=>T(s),children:[e.jsx(B,{className:"h-4 w-4 mr-1"}),"编辑"]}),e.jsx(n,{variant:"outline",size:"sm",onClick:()=>u(s,"up"),disabled:s.priority===1,children:e.jsx(L,{className:"h-4 w-4"})}),e.jsx(n,{variant:"outline",size:"sm",onClick:()=>u(s,"down"),children:e.jsx(R,{className:"h-4 w-4"})}),e.jsx(n,{variant:"destructive",size:"sm",onClick:()=>D(s.id),children:e.jsx(U,{className:"h-4 w-4"})})]})]})},s.id))})]}),e.jsx(F,{open:G,onOpenChange:p,children:e.jsxs(O,{className:"max-w-lg",children:[e.jsxs(P,{children:[e.jsx(z,{children:"添加镜像源"}),e.jsx(I,{children:"添加新的 Git 镜像源配置"})]}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"add-id",children:"镜像源 ID *"}),e.jsx(c,{id:"add-id",placeholder:"例如: my-mirror",value:a.id,onChange:s=>t({...a,id:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"add-name",children:"名称 *"}),e.jsx(c,{id:"add-name",placeholder:"例如: 我的镜像源",value:a.name,onChange:s=>t({...a,name:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"add-raw",children:"Raw 文件前缀 *"}),e.jsx(c,{id:"add-raw",placeholder:"https://example.com/raw",value:a.raw_prefix,onChange:s=>t({...a,raw_prefix:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"add-clone",children:"克隆前缀 *"}),e.jsx(c,{id:"add-clone",placeholder:"https://example.com/clone",value:a.clone_prefix,onChange:s=>t({...a,clone_prefix:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"add-priority",children:"优先级"}),e.jsx(c,{id:"add-priority",type:"number",min:"1",value:a.priority,onChange:s=>t({...a,priority:parseInt(s.target.value)||1})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"数字越小优先级越高"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(g,{id:"add-enabled",checked:a.enabled,onCheckedChange:s=>t({...a,enabled:s})}),e.jsx(r,{htmlFor:"add-enabled",children:"启用此镜像源"})]})]}),e.jsxs(M,{children:[e.jsx(n,{variant:"outline",onClick:()=>p(!1),children:"取消"}),e.jsx(n,{onClick:q,children:"添加"})]})]})}),e.jsx(F,{open:W,onOpenChange:j,children:e.jsxs(O,{className:"max-w-lg",children:[e.jsxs(P,{children:[e.jsx(z,{children:"编辑镜像源"}),e.jsx(I,{children:"修改镜像源配置"})]}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{children:"镜像源 ID"}),e.jsx(c,{value:a.id,disabled:!0})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"edit-name",children:"名称 *"}),e.jsx(c,{id:"edit-name",value:a.name,onChange:s=>t({...a,name:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"edit-raw",children:"Raw 文件前缀 *"}),e.jsx(c,{id:"edit-raw",value:a.raw_prefix,onChange:s=>t({...a,raw_prefix:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"edit-clone",children:"克隆前缀 *"}),e.jsx(c,{id:"edit-clone",value:a.clone_prefix,onChange:s=>t({...a,clone_prefix:s.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(r,{htmlFor:"edit-priority",children:"优先级"}),e.jsx(c,{id:"edit-priority",type:"number",min:"1",value:a.priority,onChange:s=>t({...a,priority:parseInt(s.target.value)||1})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"数字越小优先级越高"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(g,{id:"edit-enabled",checked:a.enabled,onCheckedChange:s=>t({...a,enabled:s})}),e.jsx(r,{htmlFor:"edit-enabled",children:"启用此镜像源"})]})]}),e.jsxs(M,{children:[e.jsx(n,{variant:"outline",onClick:()=>j(!1),children:"取消"}),e.jsx(n,{onClick:K,children:"保存"})]})]})})]})})}export{pe as PluginMirrorsPage};
|