mirror of
https://github.com/RaisFast/raisfast.git
synced 2026-10-29 00:04:05 +00:00
2 lines
7.8 KiB
JavaScript
2 lines
7.8 KiB
JavaScript
import{f as h,j as e}from"./vendor-react-j_G0Dyx0.js";import{I as U}from"./input-CX4DHDvW.js";import{C as j}from"./checkbox-COY1I8--.js";import{B as R}from"./badge-BxazBb-3.js";import{ci as _,aq as E,l as z,k as L,aQ as K,c8 as Q,aW as P}from"./index-C-i99XXF.js";import{C as V}from"./check-DsFJjVO2.js";const le=["read","create","update","delete"];function N(s){const r=s.lastIndexOf(":");return r===-1?[s,"*"]:[s.slice(0,r),s.slice(r+1)]}function ce(s){const[r,n]=N(s);return{action:r==="*"&&n==="*"?"*":`${r}.${n}`,subject:r}}function X(s){return s==="*"||s==="delete"?"destructive":s==="create"||s==="update"?"default":"secondary"}function b(s){return s.group?`${s.group} / ${s.label}`:s.label}function F(s,r){const n=r.find(i=>i.name===s);if(n)return b(n);const o=s.lastIndexOf("/");if(o>0){const i=s.slice(0,o),x=s.slice(o+1).replace(/_/g," ").replace(/\b\w/g,f=>f.toUpperCase());return`${i} / ${x}`}return s.replace(/_/g," ").replace(/\b\w/g,i=>i.toUpperCase())}function G({scope:s,resources:r}){if(s==="*")return e.jsx(R,{variant:"destructive",children:"*"});const[n,o]=N(s),i=r&&r.length>0?F(n,r):n.replace(/_/g," ").replace(/\b\w/g,u=>u.toUpperCase());return e.jsxs(R,{variant:X(o),children:[i,":",o]})}function oe({resources:s,actions:r,value:n,onChange:o,fullAccess:i=!1,maxHeight:u="400px"}){const{t:x}=_(),[f,I]=h.useState(""),[w,D]=h.useState(new Set),O=h.useMemo(()=>[...new Set(s.map(t=>t.category||t.group))],[s]),T=h.useMemo(()=>{const t=new Map;for(const l of s){const c=l.category||l.group,d=l.group&&l.category?l.group:"",a=t.get(c)??[];a.includes(d)||a.push(d),t.set(c,a)}return t},[s]),p=h.useMemo(()=>{const t=new Set;for(const l of n){const[c]=N(l);t.add(c)}return[...t]},[n]);function v(t){o(J(n,t))}function W(t){o(Y(n,t,r))}function $(t){o(Z(n,t,r))}function q(t){if(p.length===0)return;const l=p.every(d=>n.has(`${d}:${t}`)),c=new Set(n);for(const d of p){const a=`${d}:${t}`;l?c.delete(a):c.add(a)}o(c)}function C(t){D(l=>{const c=new Set(l);return c.has(t)?c.delete(t):c.add(t),c})}function B(){const t=new Set;for(const l of n)l.startsWith("*:")&&t.add(l);o(t)}function S(t){v(`*:${t}`)}return e.jsx(e.Fragment,{children:i?e.jsxs("div",{className:"rounded-lg border p-4 space-y-4",children:[e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b",children:[e.jsx("th",{className:"text-left text-xs font-medium text-muted-foreground px-3 py-2",children:x("scopeMatrix.action")}),r.map(t=>e.jsx("th",{className:"w-20 px-3 py-2",children:e.jsxs("div",{className:"flex flex-col items-center gap-1",children:[e.jsx("span",{className:"text-xs font-mono text-muted-foreground",children:t}),e.jsx(j,{checked:n.has(`*:${t}`),onCheckedChange:()=>S(t)})]})},t))]})}),e.jsx("tbody",{children:e.jsxs("tr",{children:[e.jsxs("td",{className:"px-3 py-2 text-sm font-medium",children:[x("scopeMatrix.allResources")," (",e.jsx("code",{children:"*"}),")"]}),r.map(t=>e.jsx("td",{className:"w-20 px-3 py-2",children:e.jsx("div",{className:"flex justify-center",children:e.jsx(j,{checked:n.has(`*:${t}`),onCheckedChange:()=>S(t)})})},t))]})})]}),e.jsxs("div",{className:"flex flex-wrap gap-1",children:[r.filter(t=>n.has(`*:${t}`)).map(t=>e.jsx(G,{scope:`*:${t}`},t)),r.every(t=>!n.has(`*:${t}`))&&e.jsx("span",{className:"text-xs text-muted-foreground",children:x("common.noData")})]})]}):e.jsxs("div",{className:"rounded-lg border flex overflow-hidden",children:[e.jsxs("div",{className:"border-r flex flex-col shrink-0 overflow-y-auto",style:{width:200,maxHeight:u},children:[e.jsx("div",{className:"p-2 border-b sticky top-0 bg-popover z-10",children:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(E,{className:"absolute left-2 top-1/2 -translate-y-1/2 size-3.5 text-muted-foreground"}),e.jsx(U,{placeholder:x("scopeMatrix.search"),value:f,onChange:t=>I(t.target.value),className:"h-8 pl-7 text-xs"})]}),e.jsx("button",{type:"button",onClick:()=>{const t=s.filter(c=>c.label.toLowerCase().includes(f.toLowerCase())),l=new Set(n);for(const c of t)for(const d of r)l.add(`${c.name}:${d}`);o(l)},className:"text-xs text-primary hover:underline shrink-0",children:x("scopeMatrix.selectAll")})]})}),e.jsx("div",{className:"flex-1 overflow-y-auto min-h-0",children:O.map(t=>{const l=T.get(t)??[""],c=s.filter(a=>(a.category||a.group)===t&&a.label.toLowerCase().includes(f.toLowerCase()));if(c.length===0)return null;const d=w.has(t);return e.jsxs("div",{children:[e.jsxs("button",{type:"button",onClick:()=>C(t),className:"w-full flex items-center gap-1 px-2 py-1 text-xs font-medium text-muted-foreground hover:bg-muted",children:[d?e.jsx(z,{className:"size-3"}):e.jsx(L,{className:"size-3"}),t]}),!d&&l.map(a=>{const k=c.filter(m=>(m.group&&m.category?m.group:"")===a);if(k.length===0)return null;const g=a?`${t}/${a}`:t,A=w.has(g);return e.jsxs("div",{children:[a&&e.jsxs("button",{type:"button",onClick:()=>C(g),className:"w-full flex items-center gap-1 pl-4 pr-3 py-1 text-xs text-muted-foreground hover:bg-muted",children:[A?e.jsx(z,{className:"size-3"}):e.jsx(L,{className:"size-3"}),a]}),!A&&k.map(m=>{const y=p.includes(m.name),M=m.name==="*";return e.jsxs("button",{type:"button",onClick:()=>y?$(m.name):W(m.name),className:`w-full flex items-center justify-between ${a?"pl-7":"pl-5"} pr-3 py-1.5 text-sm transition-colors ${y?"bg-primary/10 text-primary font-medium":"hover:bg-muted"} ${M?"border-y":""}`,children:[e.jsxs("span",{className:"flex items-center gap-1.5",children:[M&&e.jsx("span",{className:"text-xs font-mono text-muted-foreground",children:"*"}),b(m)]}),y&&e.jsx(V,{className:"size-3.5"})]},m.name)})]},g)})]},t)})})]}),e.jsx("div",{className:"flex flex-col flex-1 min-h-0",children:p.length===0?e.jsx("div",{className:"flex items-center justify-center text-sm text-muted-foreground p-8",children:x("scopeMatrix.empty")}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex-1 overflow-y-auto",style:{maxHeight:u},children:e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{className:"sticky top-0 bg-popover z-10",children:e.jsxs("tr",{className:"border-b",children:[e.jsx("th",{className:"text-left text-xs font-medium text-muted-foreground px-3 py-2",children:x("scopeMatrix.resource")}),r.map(t=>e.jsx("th",{className:"w-20 px-3 py-2",children:e.jsxs("div",{className:"flex flex-col items-center gap-1",children:[e.jsx("span",{className:"text-xs font-mono text-muted-foreground",children:t}),e.jsx(j,{checked:p.every(l=>n.has(`${l}:${t}`)),onCheckedChange:()=>q(t)})]})},t)),e.jsx("th",{className:"w-8"})]})}),e.jsx("tbody",{children:p.map(t=>{const l=s.find(d=>d.name===t),c=l?b(l):F(t,s);return e.jsxs("tr",{className:"border-b last:border-0 hover:bg-muted/50",children:[e.jsx("td",{className:"px-3 py-2 text-sm font-medium",children:c}),r.map(d=>{const a=`${t}:${d}`;return e.jsx("td",{className:"w-20 px-3 py-2",children:e.jsx("div",{className:"flex justify-center",children:e.jsx(j,{checked:n.has(a),onCheckedChange:()=>v(a)})})},d)}),e.jsx("td",{className:"px-1 py-2",children:e.jsx("button",{type:"button",onClick:()=>$(t),className:"text-muted-foreground hover:text-destructive transition-colors p-1",children:e.jsx(K,{className:"size-3.5"})})})]},t)})})]})}),e.jsxs("div",{className:"flex items-center justify-between px-3 py-1.5 border-t shrink-0 bg-popover",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:x("scopeMatrix.selected",{count:p.length})}),e.jsx("button",{type:"button",onClick:B,className:"text-xs text-destructive hover:underline",children:x("scopeMatrix.clearAll")})]})]})})]})})}function J(s,r){const n=new Set(s);return n.has(r)?n.delete(r):n.add(r),n}function Y(s,r,n){const o=new Set(s);for(const i of n)o.add(`${r}:${i}`);return o}function Z(s,r,n){const o=new Set(s);for(const i of n)o.delete(`${r}:${i}`);return o}function ae(){return Q({queryKey:["resources"],queryFn:()=>P.admin.listResources(),staleTime:0})}export{le as A,G as S,oe as a,X as b,ce as s,ae as u};
|