@anthonybir/birhaus-patterns
Version:
BIRHAUS Pattern Components - AuditTimeline, DataTable, EmptyState
2 lines • 6.55 kB
JavaScript
import {useState,useMemo}from'react';import {Filter,Clock,User,ChevronUp,ChevronDown}from'lucide-react';import {formatDistanceToNow}from'date-fns';import {es}from'date-fns/locale';import {jsxs,jsx,Fragment}from'react/jsx-runtime';function E(...n){return n.filter(Boolean).join(" ")}var A={"iglesia.creada":"cre\xF3 una iglesia","iglesia.actualizada":"actualiz\xF3 una iglesia","iglesia.eliminada":"elimin\xF3 una iglesia","pastor.creado":"registr\xF3 un pastor","pastor.actualizado":"actualiz\xF3 un pastor","pastor.eliminado":"elimin\xF3 un pastor","donacion.registrada":"registr\xF3 una donaci\xF3n","donacion.anulada":"anul\xF3 una donaci\xF3n","transferencia.creada":"cre\xF3 una transferencia","transferencia.completada":"complet\xF3 una transferencia","gasto.registrado":"registr\xF3 un gasto","gasto.aprobado":"aprob\xF3 un gasto","gasto.rechazado":"rechaz\xF3 un gasto","usuario.login":"inici\xF3 sesi\xF3n","usuario.logout":"cerr\xF3 sesi\xF3n","resumen.enviado":"envi\xF3 un resumen","resumen.aprobado":"aprob\xF3 un resumen","resumen.rechazado":"rechaz\xF3 un resumen",create:"cre\xF3",update:"actualiz\xF3",delete:"elimin\xF3",approve:"aprob\xF3",reject:"rechaz\xF3",login:"inici\xF3 sesi\xF3n",logout:"cerr\xF3 sesi\xF3n"},U={creada:"text-green-600",creado:"text-green-600",registrada:"text-green-600",registrado:"text-green-600",actualizada:"text-blue-600",actualizado:"text-blue-600",eliminada:"text-red-600",eliminado:"text-red-600",anulada:"text-yellow-600",aprobado:"text-green-600",rechazado:"text-red-600",completada:"text-green-600",enviado:"text-blue-600",login:"text-gray-500",logout:"text-gray-500"};function I({events:n,showFilters:l=true,maxHeight:y="600px",className:f="",emptyMessage:u="No hay eventos que mostrar",filterAllEntitiesLabel:d="Todas las entidades",filterAllActionsLabel:g="Todas las acciones",clearFiltersLabel:_="Limpiar filtros",showDetailsLabel:z="Ver detalles",hideDetailsLabel:C="Ocultar detalles"}){let[s,b]=useState("all"),[o,h]=useState("all"),[v,T]=useState(new Set),{entities:m,verbs:p}=useMemo(()=>{let e=new Set,r=new Set;return n.forEach(i=>{i.entity&&e.add(i.entity),r.add(i.verb);}),{entities:Array.from(e).sort(),verbs:Array.from(r).sort()}},[n]),x=useMemo(()=>n.filter(e=>!(s!=="all"&&e.entity!==s||o!=="all"&&e.verb!==o)),[n,s,o]),O=e=>{let r=new Set(v);r.has(e)?r.delete(e):r.add(e),T(r);},k=e=>{let r=e.split(".").pop()||"";return U[r]||"text-gray-700"},D=e=>!e||Object.keys(e).length===0?null:jsx("div",{className:"mt-2 p-3 bg-gray-50 rounded-md text-xs space-y-1 border",children:Object.entries(e).map(([r,i])=>jsxs("div",{className:"flex gap-2",children:[jsxs("span",{className:"text-gray-500 font-medium min-w-20",children:[r,":"]}),jsx("span",{className:"text-gray-700 flex-1",children:typeof i=="object"?JSON.stringify(i,null,2):String(i)})]},r))});return jsxs("div",{className:E("space-y-4",f),"data-birhaus-component":"audit-timeline",children:[l&&(m.length>0||p.length>0)&&jsxs("div",{className:"flex gap-3 items-center p-3 bg-gray-50 border border-gray-200 rounded-md",children:[jsx(Filter,{className:"h-4 w-4 text-gray-400"}),m.length>0&&jsxs("select",{value:s,onChange:e=>b(e.target.value),className:"px-3 py-1 border border-gray-300 rounded-md text-sm bg-white focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-primary-500",children:[jsx("option",{value:"all",children:d}),m.map(e=>jsx("option",{value:e,children:e.charAt(0).toUpperCase()+e.slice(1)},e))]}),p.length>0&&jsxs("select",{value:o,onChange:e=>h(e.target.value),className:"px-3 py-1 border border-gray-300 rounded-md text-sm bg-white focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-primary-500",children:[jsx("option",{value:"all",children:g}),p.map(e=>jsx("option",{value:e,children:A[e]||e},e))]}),(s!=="all"||o!=="all")&&jsx("button",{onClick:()=>{b("all"),h("all");},className:"text-sm text-primary-600 hover:text-primary-700 hover:underline focus:outline-none focus:ring-2 focus:ring-primary-500 px-2 py-1 rounded",children:_})]}),jsx("div",{className:"space-y-2 overflow-y-auto pr-2 scrollbar-thin scrollbar-thumb-gray-300 scrollbar-track-gray-100",style:{maxHeight:y},children:x.length===0?jsxs("div",{className:"text-center py-8 text-gray-500 bg-gray-50 rounded-lg border-2 border-dashed border-gray-200",children:[jsx(Clock,{className:"h-8 w-8 text-gray-400 mx-auto mb-2"}),jsx("p",{children:u})]}):x.map((e,r)=>{let i=v.has(e.id),L=e.payload&&Object.keys(e.payload).length>0;return jsxs("div",{className:"flex gap-3 p-4 bg-white border border-gray-200 rounded-lg hover:border-gray-300 hover:shadow-sm transition-all duration-150",children:[jsxs("div",{className:"flex flex-col items-center",children:[jsx("div",{className:"w-2 h-2 rounded-full bg-primary-600 mt-2 flex-shrink-0"}),r<x.length-1&&jsx("div",{className:"w-px flex-1 bg-gray-200 mt-2"})]}),jsx("div",{className:"flex-1 min-w-0",children:jsxs("div",{className:"flex items-start justify-between gap-2",children:[jsxs("div",{className:"flex-1",children:[jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[jsx(User,{className:"h-3 w-3 text-gray-400"}),jsx("span",{className:"text-sm font-medium text-gray-900",children:e.actor_name||"Usuario"}),jsx("span",{className:E("text-sm font-medium",k(e.verb)),children:A[e.verb]||e.verb}),e.entity&&jsx("span",{className:"text-xs px-2 py-1 bg-gray-200 text-gray-700 rounded-full",children:e.entity})]}),e.entity_id&&jsxs("div",{className:"text-xs text-gray-500 mt-1 font-mono",children:["ID: ",e.entity_id]}),L&&jsx("button",{onClick:()=>O(e.id),className:"flex items-center gap-1 mt-2 text-xs text-primary-600 hover:text-primary-700 hover:underline focus:outline-none focus:ring-2 focus:ring-primary-500 px-1 py-0.5 rounded",children:i?jsxs(Fragment,{children:[jsx(ChevronUp,{className:"h-3 w-3"}),C]}):jsxs(Fragment,{children:[jsx(ChevronDown,{className:"h-3 w-3"}),z]})}),i&&D(e.payload)]}),jsxs("div",{className:"flex items-center gap-1 text-xs text-gray-500 whitespace-nowrap",children:[jsx(Clock,{className:"h-3 w-3"}),jsx("span",{title:new Date(e.created_at).toLocaleString("es-ES"),children:formatDistanceToNow(new Date(e.created_at),{addSuffix:true,locale:es})})]})]})})]},e.id)})})]})}function M(){let[n,l]=useState([]);return {events:n,addEvent:u=>{let d={...u,id:`audit_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,created_at:new Date().toISOString()};return l(g=>[d,...g]),d},clearEvents:()=>{l([]);}}}export{I as AuditTimeline,M as useAuditTimeline};//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map