PluginProbe
AI / trunk
AI vtrunk
1.3.0 1.2.0 1.1.0 1.0.2 1.0.1 1.0.0 0.9.0 trunk 0.1.1 0.2.0 0.2.1 0.3.0 0.3.1 0.4.0 0.4.1 0.5.0 0.6.0 0.7.0 0.8.0
ai / build-scripts / experiments / connector-approval.js

connector-approval.js in AI trunk, at build-scripts/experiments/connector-approval.js

2 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (()=>{"use strict";const e={n:n=>{const r=n&&n.__esModule?()=>n.default:()=>n;return e.d(r,{a:r}),r},d:(n,r)=>{if(Array.isArray(r))for(var a=0;a<r.length;){var s=r[a++],t=r[a++];e.o(n,s)?0===t&&a++:0===t?Object.defineProperty(n,s,{enumerable:!0,value:r[a++]}):Object.defineProperty(n,s,{enumerable:!0,get:t})}else for(var s in r)e.o(r,s)&&!e.o(n,s)&&Object.defineProperty(n,s,{enumerable:!0,get:r[s]})},o:(e,n)=>Object.hasOwn(e,n)},n=window.wp.domReady;var r=e.n(n);const a=window.wp.element,s=window.wp.components,t=window.wp.i18n,i=window.wp.apiFetch;var l=e.n(i);const c=window.wp.url,o="/ai/v1/connector-approvals";let d=!1;function p(){d||(l().use(l().createNonceMiddleware(window.aiConnectorApproval?.nonce??"")),d=!0)}function u(e,n){return e instanceof Error?e.message:n}function h(e,n){return e.find(e=>e.id===n)?.name??n}const m=window.ReactJSXRuntime,x=({connectors:e,plugins:n,themes:r,approvals:i,isSaving:l,onToggle:c})=>{const o=function(e,n,r){const a=[...e.map(e=>({...e,type:"plugin"})),...n.map(e=>({...e,type:"theme"}))],s=new Set([...a.map(e=>e.basename),...Object.keys(r)]);return Array.from(s).map(e=>{const n=a.find(n=>n.basename===e);return{basename:e,type:n?.type??"unknown",name:n?.name??e}}).sort((e,n)=>e.name.localeCompare(n.name))}(n,r,i),d=(0,a.useRef)(null),p=(0,a.useRef)(!1),u=(0,a.useRef)(null);return(0,a.useEffect)(()=>{p.current&&!l&&(d.current?.focus(),d.current=null),p.current=l},[l]),(0,m.jsxs)(s.Card,{className:"ai-connector-approval__matrix",children:[(0,m.jsx)(s.CardHeader,{children:(0,m.jsx)("h2",{children:(0,t.__)("Approval matrix","ai")})}),(0,m.jsx)(s.CardBody,{children:0===e.length?(0,m.jsx)("p",{children:(0,t.__)("No AI connectors are currently registered. Configure a connector first.","ai")}):(0,m.jsxs)("table",{className:"widefat striped",ref:u,children:[(0,m.jsx)("thead",{children:(0,m.jsxs)("tr",{children:[(0,m.jsx)("th",{children:(0,t.__)("Caller","ai")}),e.map(e=>(0,m.jsx)("th",{children:e.name},e.id))]})}),(0,m.jsx)("tbody",{children:o.map(n=>(0,m.jsxs)("tr",{children:[(0,m.jsxs)("td",{children:[(0,m.jsx)("strong",{children:n.name}),(0,m.jsx)("br",{}),(0,m.jsx)("em",{children:"theme"===n.type?(0,t.__)("Theme","ai"):(0,t.__)("Plugin","ai")}),(0,m.jsx)("br",{}),(0,m.jsx)("code",{children:n.basename})]}),e.map(e=>{const r=Boolean(i[n.basename]?.[e.id]);return(0,m.jsx)("td",{children:(0,m.jsx)(s.ToggleControl,{checked:r,disabled:l,"aria-label":(0,t.sprintf)(/* translators: %1$s: caller name (plugin or theme), %2$s: connector name */ /* translators: %1$s: caller name (plugin or theme), %2$s: connector name */
2 (0,t.__)("Allow %1$s to use %2$s","ai"),n.name,e.name),label:"",onChange:r=>{d.current=u.current?.ownerDocument.activeElement,c(n.basename,e.id,r)}})},e.id)})]},n.basename))})]})})]})},j=({connectors:e,pending:n,isSaving:r,onApprove:a,onDismiss:i})=>(0,m.jsxs)(s.Card,{children:[(0,m.jsx)(s.CardHeader,{children:(0,m.jsx)("h2",{children:(0,t.__)("Pending requests","ai")})}),(0,m.jsx)(s.CardBody,{children:0===n.length?(0,m.jsx)("p",{children:(0,t.__)("No plugins or themes are currently waiting for AI access.","ai")}):(0,m.jsxs)("table",{className:"widefat striped",children:[(0,m.jsx)("thead",{children:(0,m.jsxs)("tr",{children:[(0,m.jsx)("th",{children:(0,t.__)("Caller","ai")}),(0,m.jsx)("th",{children:(0,t.__)("Connector","ai")}),(0,m.jsx)("th",{children:(0,t.__)("Attempts","ai")}),(0,m.jsx)("th",{children:(0,t.__)("Last seen","ai")}),(0,m.jsx)("th",{children:(0,t.__)("Actions","ai")})]})}),(0,m.jsx)("tbody",{children:n.map(n=>{return(0,m.jsxs)("tr",{children:[(0,m.jsxs)("td",{children:[(0,m.jsx)("strong",{children:n.caller_name}),(0,m.jsx)("br",{}),(0,m.jsx)("code",{children:n.caller_basename}),(0,m.jsx)("br",{}),(0,m.jsx)("em",{children:n.caller_type})]}),(0,m.jsx)("td",{children:h(e,n.connector_id)}),(0,m.jsx)("td",{children:n.attempts}),(0,m.jsx)("td",{children:(l=n.last_seen,l?new Date(1e3*l).toLocaleString():"")}),(0,m.jsx)("td",{children:(0,m.jsxs)(s.Flex,{justify:"flex-start",gap:2,children:[(0,m.jsx)(s.FlexItem,{children:(0,m.jsx)(s.Button,{variant:"primary",isBusy:r,disabled:r,onClick:()=>a(n.caller_basename,n.connector_id),__next40pxDefaultSize:!0,children:(0,t.__)("Approve","ai")})}),(0,m.jsx)(s.FlexItem,{children:(0,m.jsx)(s.Button,{variant:"secondary",isDestructive:!0,disabled:r,onClick:()=>i(n.key),__next40pxDefaultSize:!0,children:(0,t.__)("Dismiss","ai")})})]})})]},n.key);var l})})]})})]}),_=()=>{const{state:e,error:n,isSaving:r,clearError:i,setApproval:d,dismissPending:h}=function(){const[e,n]=(0,a.useState)(null),[r,s]=(0,a.useState)(null),[i,d]=(0,a.useState)(!1),h=(0,a.useCallback)(async()=>{s(null);try{n(await(p(),l()({path:o})))}catch(e){s(u(e,(0,t.__)("Failed to load approval data.","ai")))}},[]);(0,a.useEffect)(()=>{h()},[h]);const m=(0,a.useCallback)(async(e,r,a)=>{d(!0),s(null);try{n(await function(e,n,r){return p(),l()({path:o,method:"POST",data:{plugin_basename:e,connector_id:n,approved:r}})}(e,r,a))}catch(e){s(u(e,(0,t.__)("Failed to update approval.","ai")))}finally{d(!1)}},[]),x=(0,a.useCallback)(async e=>{d(!0),s(null);try{n(await function(e){return p(),l()({path:(0,c.addQueryArgs)(`${o}/pending`,{key:e}),method:"DELETE"})}(e))}catch(e){s(u(e,(0,t.__)("Failed to dismiss request.","ai")))}finally{d(!1)}},[]);return{state:e,error:r,isSaving:i,clearError:(0,a.useCallback)(()=>s(null),[]),setApproval:m,dismissPending:x}}();return null===e?(0,m.jsx)("div",{className:"ai-connector-approval",children:n?(0,m.jsx)(s.Notice,{status:"error",isDismissible:!1,children:n}):(0,m.jsx)(s.Spinner,{})}):(0,m.jsxs)("div",{className:"ai-connector-approval",children:[n?(0,m.jsx)(s.Notice,{status:"error",onDismiss:i,children:n}):null,(0,m.jsx)(j,{connectors:e.connectors,pending:e.pending,isSaving:r,onApprove:(e,n)=>d(e,n,!0),onDismiss:h}),(0,m.jsx)(x,{connectors:e.connectors,plugins:e.plugins,themes:e.themes,approvals:e.approvals,isSaving:r,onToggle:d})]})};r()(()=>{const e=document.getElementById("ai-connector-approval-root");e&&(0,a.createRoot)(e).render((0,m.jsx)(_,{}))})})();