UNPKG

@birhaus/core

Version:

BIRHAUS Core Components - UndoSystem, CommandPalette, AuditTimeline

2 lines 7.73 kB
'use strict';var c=require('react'),lucideReact=require('lucide-react'),jsxRuntime=require('react/jsx-runtime');function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var c__namespace=/*#__PURE__*/_interopNamespace(c);var U=c.createContext(null);function E(){let o=c.useContext(U);if(!o)throw new Error("useUndo must be used within an UndoProvider");return o}function H({children:o,defaultTimeoutMs:v=1e4,maxActions:y=5}){let[u,i]=c.useState([]);c.useEffect(()=>{let s=setInterval(()=>{i(a=>a.filter(d=>d.expiresAt>new Date));},1e3);return ()=>clearInterval(s)},[]);let p=s=>{let a=`undo_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,d=new Date,b=new Date(d.getTime()+v),N={...s,id:a,createdAt:d,expiresAt:b};return i(m=>[N,...m].slice(0,y)),a},h=async s=>{let a=u.find(d=>d.id===s);if(!a)throw new Error("Action not found or expired");try{await a.undoFunction(),i(d=>d.filter(b=>b.id!==s));}catch(d){throw console.error("Failed to undo action:",d),d}},f=s=>{i(a=>a.filter(d=>d.id!==s));},w=()=>{i(s=>s.filter(a=>a.expiresAt>new Date));},g=s=>u.filter(a=>a.entityType===s);return jsxRuntime.jsx(U.Provider,{value:{actions:u,addAction:p,undoAction:h,dismissAction:f,clearExpired:w,getActionsByType:g},children:o})}function X(){let{addAction:o}=E();return {createUndoableAction:(y,u,i)=>o({type:i.type||"update",entityType:i.entityType,entityId:i.entityId,description:y,undoFunction:u,priority:i.priority||"normal",icon:i.icon})}}function k(...o){return o.filter(Boolean).join(" ")}var j={navigation:"Navegaci\xF3n",actions:"Acciones",search:"B\xFAsqueda",settings:"Configuraci\xF3n"};function q({items:o,placeholder:v="Buscar o ejecutar comando...",emptyMessage:y="No se encontraron comandos",recentLabel:u="Recientes",maxRecent:i=5}){let[p,h]=c__namespace.useState(false),[f,w]=c__namespace.useState(""),[g,s]=c__namespace.useState(0),[a,d]=c__namespace.useState([]),b=c__namespace.useRef(null),N=c__namespace.useRef(null),m=c__namespace.useMemo(()=>{if(!f){let n=a.map(x=>o.find(T=>T.id===x)).filter(Boolean),A=o.filter(x=>!a.includes(x.id));return {recent:n,filtered:A}}let e=f.toLowerCase(),t=o.filter(n=>{let A=n.title.toLowerCase().includes(e),x=n.description?.toLowerCase().includes(e),T=n.keywords?.some(M=>M.toLowerCase().includes(e));return A||x||T});return {recent:[],filtered:t}},[o,f,a]),I=[...m.recent,...m.filtered],_=c__namespace.useMemo(()=>{let e={};return m.filtered.forEach(t=>{let n=t.category||"actions";e[n]||(e[n]=[]),e[n].push(t);}),e},[m.filtered]);c__namespace.useEffect(()=>{let e=t=>{(t.metaKey||t.ctrlKey)&&t.key==="k"&&(t.preventDefault(),h(n=>!n)),t.key==="Escape"&&p&&h(false);};return document.addEventListener("keydown",e),()=>document.removeEventListener("keydown",e)},[p]),c__namespace.useEffect(()=>{p&&b.current&&(b.current.focus(),w(""),s(0));},[p]);let B=e=>{switch(e.key){case "ArrowDown":e.preventDefault(),s(t=>t<I.length-1?t+1:t);break;case "ArrowUp":e.preventDefault(),s(t=>t>0?t-1:0);break;case "Enter":e.preventDefault(),I[g]&&C(I[g]);break}},C=e=>{d(t=>{let n=t.filter(A=>A!==e.id);return [e.id,...n].slice(0,i)}),e.action(),h(false);};return c__namespace.useEffect(()=>{if(p&&N.current){let e=N.current.querySelector(`[data-index="${g}"]`);e&&e.scrollIntoView({block:"nearest"});}},[g,p]),p?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx("div",{className:"fixed inset-0 z-50 bg-black/20 backdrop-blur-sm",onClick:()=>h(false)}),jsxRuntime.jsx("div",{className:"fixed inset-x-0 top-20 z-50 mx-auto max-w-2xl px-4",children:jsxRuntime.jsxs("div",{className:"rounded-lg border border-gray-200 bg-white shadow-2xl",children:[jsxRuntime.jsxs("div",{className:"flex items-center border-b border-gray-200 px-4",children:[jsxRuntime.jsx(lucideReact.Search,{className:"h-5 w-5 text-gray-400"}),jsxRuntime.jsx("input",{ref:b,type:"text",value:f,onChange:e=>{w(e.target.value),s(0);},onKeyDown:B,placeholder:v,className:k("flex-1 bg-transparent px-3 py-4 text-sm text-gray-900","placeholder:text-gray-400","focus:outline-none")}),jsxRuntime.jsx("kbd",{className:"hidden sm:inline-block px-2 py-1 text-xs bg-gray-100 text-gray-500 rounded border",children:"ESC"})]}),jsxRuntime.jsx("div",{ref:N,className:"max-h-96 overflow-auto p-2",children:I.length===0?jsxRuntime.jsx("div",{className:"py-8 text-center text-sm text-gray-500",children:y}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[!f&&m.recent.length>0&&jsxRuntime.jsxs("div",{className:"mb-2",children:[jsxRuntime.jsx("div",{className:"px-2 py-1 text-xs font-medium text-gray-500 uppercase tracking-wider",children:u}),m.recent.map((e,t)=>jsxRuntime.jsx(R,{item:e,index:t,selected:g===t,onClick:()=>C(e)},e.id))]}),f?m.filtered.map((e,t)=>{let n=m.recent.length+t;return jsxRuntime.jsx(R,{item:e,index:n,selected:g===n,onClick:()=>C(e)},e.id)}):Object.entries(_).map(([e,t])=>jsxRuntime.jsxs("div",{className:"mb-2",children:[jsxRuntime.jsx("div",{className:"px-2 py-1 text-xs font-medium text-gray-500 uppercase tracking-wider",children:j[e]||e}),t.map((n,A)=>{let x=m.recent.length+m.filtered.indexOf(n);return jsxRuntime.jsx(R,{item:n,index:x,selected:g===x,onClick:()=>C(n)},n.id)})]},e))]})}),jsxRuntime.jsxs("div",{className:"border-t border-gray-200 px-4 py-2 flex items-center justify-between text-xs text-gray-500 bg-gray-50",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-4",children:[jsxRuntime.jsxs("span",{className:"flex items-center gap-1",children:[jsxRuntime.jsx("kbd",{className:"px-1.5 py-0.5 bg-white border border-gray-200 rounded text-gray-600 font-mono",children:"\u2191\u2193"}),"Navegar"]}),jsxRuntime.jsxs("span",{className:"flex items-center gap-1",children:[jsxRuntime.jsx("kbd",{className:"px-1.5 py-0.5 bg-white border border-gray-200 rounded text-gray-600 font-mono",children:"\u21B5"}),"Seleccionar"]})]}),jsxRuntime.jsxs("span",{className:"flex items-center gap-1",children:[jsxRuntime.jsx(lucideReact.Command,{className:"h-3 w-3"}),jsxRuntime.jsx("kbd",{className:"px-1.5 py-0.5 bg-white border border-gray-200 rounded text-gray-600 font-mono",children:"\u2318K"})]})]})]})})]}):null}function R({item:o,index:v,selected:y,onClick:u}){let i=o.icon;return jsxRuntime.jsxs("div",{"data-index":v,className:k("flex items-center gap-3 px-2 py-2 rounded-md cursor-pointer","transition-colors duration-150",y?"bg-primary-100 text-primary-900":"hover:bg-gray-100 text-gray-900"),onClick:u,children:[i&&jsxRuntime.jsx(i,{className:"h-4 w-4 text-gray-500 flex-shrink-0"}),jsxRuntime.jsxs("div",{className:"flex-1 min-w-0",children:[jsxRuntime.jsx("div",{className:"text-sm font-medium",children:o.title}),o.description&&jsxRuntime.jsx("div",{className:"text-xs text-gray-500 truncate",children:o.description})]}),o.shortcut&&jsxRuntime.jsx("kbd",{className:"px-2 py-1 text-xs bg-gray-200 text-gray-600 rounded border font-mono",children:o.shortcut}),jsxRuntime.jsx(lucideReact.ChevronRight,{className:"h-4 w-4 text-gray-400"})]})}var V={MAX_VISIBLE_CHOICES:7,MIN_VISIBLE_CHOICES:5,MAX_PRIMARY_NAV_ITEMS:4,MAX_CONTEXTUAL_ACTIONS:3,MAX_PRIMARY_ACTIONS:1,MAX_FIELDS_PER_STEP:5,MAX_FORM_STEPS:4,MAX_TTI:2e3,MAX_FCP:1e3,MAX_CLS:.1,DEFAULT_TABLE_COLUMNS:7,MAX_TABLE_ROWS_BEFORE_VIRTUALIZATION:100,DEFAULT_UNDO_TIMEOUT:1e4,MAX_UNDO_ACTIONS:5,MIN_TOUCH_TARGET:44,MIN_CONTRAST_RATIO:4.5,DEFAULT_TRANSITION_DURATION:150,REDUCE_MOTION_DURATION:0};exports.BIRHAUS_LIMITS=V;exports.CommandPalette=q;exports.UndoProvider=H;exports.useUndo=E;exports.useUndoAction=X;//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map