PageSourceSearch

https://touch-task.netlify.app/assets/KnowledgeBasesPage-DEoDT07k.js

js touch-task.netlify.app collected 2026-10-03 11:31:31 UTC 12,219 bytes, 1 lines download raw bytes

1import{u as V,r,s as w,j as e}from"./site-C5SUB8ov.js";import{u as I,y as J,a0 as N,a1 as W,q as F,t as Y,a2 as P,X as K,d as Z,i as ee,T as $,x as te,c as z,L as se}from"./main-_fPdXjGG.js";import{u as ae}from"./useCanEdit-ChYkI6QT.js";import{K as ne,a as re,b as oe}from"./KbCardSkeleton-BR1kvIT1.js";const H=50,T=200;function ge(){const{t:s}=I(),g=V(),{currentCompany:o}=J(),n=ae("knowledge"),[x,p]=r.useState([]),[y,h]=r.useState(!0),[l,S]=r.useState(""),[u,v]=r.useState(!1),[C,B]=r.useState(null),[_,M]=r.useState(""),[E,q]=r.useState(""),[L,D]=r.useState(null),[A,O]=r.useState(null),c=o==null?void 0:o.id,R=t=>n&&!!(A&&(A.isOwner||t.created_by===A.id));r.useEffect(()=>{if(!c)return;let t=!1;return(async()=>{const{data:{session:a}}=await w.auth.getSession();if(!a||t)return;const{data:i}=await w.from("company_members").select("role").eq("company_id",c).eq("user_id",a.user.id).maybeSingle();t||O({id:a.user.id,isOwner:(i==null?void 0:i.role)==="owner"})})(),()=>{t=!0}},[c]);const k=r.useCallback(async()=>{if(c){h(!0);try{const{data:t,error:a}=await w.from("knowledge_bases").select("id, name, description, created_at, created_by").eq("company_id",c).order("created_at",{ascending:!1});if(a)throw a;const i=t??[],{data:b}=await w.from("knowledge_items").select("knowledge_base_id, updated_at, created_at").in("knowledge_base_id",i.map(m=>m.id)),d=new Map;(b??[]).forEach(m=>{const f=d.get(m.knowledge_base_id)??{count:0,lastUpdatedAt:null};f.count+=1;const j=m.updated_at||m.created_at;j&&(!f.lastUpdatedAt||j>f.lastUpdatedAt)&&(f.lastUpdatedAt=j),d.set(m.knowledge_base_id,f)}),p(i.map(m=>{var f,j;return{...m,itemCount:((f=d.get(m.id))==null?void 0:f.count)??0,lastUpdatedAt:((j=d.get(m.id))==null?void 0:j.lastUpdatedAt)??null}}))}catch(t){console.error("Failed to fetch knowledge bases:",t),N.error(s("knowledge.base.errors.loadFailed"))}finally{h(!1)}}},[c,s]);r.useEffect(()=>{k()},[k]);const X=async(t,a)=>{if(!c||!n)return null;const{data:{session:i}}=await w.auth.getSession();if(!i)return null;const{data:b,error:d}=await w.from("knowledge_bases").insert({name:t.trim(),description:a.trim()||null,company_id:c,created_by:i.user.id}).select("id").single();return d?(N.error(s("knowledge.base.errors.updateFailed")),null):(v(!1),g(`/${c}/knowledge/${b.id}`),b.id)},G=async()=>{if(!C||!_.trim()||!n)return;const t=_.trim(),a=E.trim()||null;p(b=>b.map(d=>d.id===C?{...d,name:t,description:a}:d)),B(null);const{error:i}=await w.from("knowledge_bases").update({name:t,description:a}).eq("id",C);i&&(N.error(s("knowledge.base.errors.updateFailed")),k())},Q=async t=>{if(!n)return;D(null),p(b=>b.filter(d=>d.id!==t.id));const{data:a,error:i}=await w.from("knowledge_bases").delete().eq("id",t.id).select("id");if(i){N.error(s("knowledge.bases.deleteFailed")),k();return}if(!(a!=null&&a.length)){N.error(s("knowledge.bases.deleteForbidden")),k();return}N.success(s("knowledge.bases.deleted"))},U=r.useMemo(()=>{const t=l.trim().toLowerCase();return t?x.filter(a=>a.name.toLowerCase().includes(t)||(a.description??"").toLowerCase().includes(t)):x},[x,l]);return e.jsxs("div",{className:"mx-auto max-w-6xl px-4 py-8",children:[e.jsx(ne,{icon:W,title:s("knowledge.base.title"),subtitle:s("knowledge.bases.subtitle"),actions:n?e.jsxs("button",{onClick:()=>v(!0),className:"inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-3.5 py-2 text-sm font-medium text-white hover:bg-indigo-700 transition-colors",children:[e.jsx(F,{className:"w-4 h-4"}),s("knowledge.base.createNew")]}):null}),e.jsx("div",{className:"mt-6",children:e.jsx(Y,{className:"w-full max-w-md",value:l,onChange:S,placeholder:s("knowledge.bases.searchBases")})}),e.jsx("div",{className:"mt-6",children:y?e.jsx("div",{className:"grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3",children:Array.from({length:6},(t,a)=>e.jsx(re,{},a))}):U.length===0?e.jsx(oe,{icon:P,title:s(l?"knowledge.bases.noSearchResults":"knowledge.bases.noBases"),description:s(l?"knowledge.bases.tryAnotherSearch":"knowledge.bases.createFirstBase"),action:!l&&n?e.jsxs("button",{onClick:()=>v(!0),className:"inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-3.5 py-2 text-sm font-medium text-white hover:bg-indigo-700",children:[e.jsx(F,{className:"w-4 h-4"}),s("knowledge.bases.createBase")]}):null}):e.jsx("div",{className:"grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3",children:U.map(t=>C===t.id?e.jsxs("div",{className:"rounded-2xl border border-indigo-200 bg-white p-5",children:[e.jsx("input",{type:"text",value:_,autoFocus:!0,maxLength:H,onChange:a=>M(a.target.value),className:"w-full border-0 p-0 text-base font-semibold text-gray-900 focus:outline-none focus:ring-0",placeholder:s("knowledge.bases.namePlaceholder")}),e.jsx("textarea",{value:E,rows:3,maxLength:T,onChange:a=>q(a.target.value),className:"mt-2 w-full resize-none border-0 p-0 text-sm text-gray-600 focus:outline-none focus:ring-0",placeholder:s("knowledge.bases.descriptionPlaceholder")}),e.jsxs("div",{className:"mt-3 flex items-center justify-between",children:[e.jsxs("span",{className:"text-xs text-gray-400 tabular-nums",children:[E.length,"/",T]}),e.jsxs("div",{className:"flex gap-1",children:[e.jsx("button",{onClick:()=>B(null),className:"rounded-lg p-1.5 text-gray-400 hover:bg-gray-100 hover:text-gray-700",children:e.jsx(K,{className:"w-4 h-4"})}),e.jsx("button",{onClick:()=>void G(),disabled:!_.trim(),className:"rounded-lg p-1.5 text-emerald-600 hover:bg-emerald-50 disabled:text-gray-300",children:e.jsx(Z,{className:"w-4 h-4"})})]})]})]},t.id):e.jsxs("div",{role:"button",tabIndex:0,onClick:()=>g(`/${c}/knowledge/${t.id}`),onKeyDown:a=>{a.key==="Enter"&&g(`/${c}/knowledge/${t.id}`)},className:"group flex cursor-pointer flex-col rounded-2xl border border-gray-200 bg-white p-5 text-left transition-all hover:border-gray-300 hover:-translate-y-0.5",children:[e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"flex h-9 w-9 flex-shrink-0 items-center justify-center rounded-xl bg-slate-100",children:e.jsx(P,{className:"h-[18px] w-[18px] text-slate-600"})}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("h3",{className:"truncate text-base font-semibold text-gray-900 group-hover:text-indigo-600 transition-colors",children:t.name}),t.description&&e.jsx("p",{className:"mt-1 line-clamp-2 text-sm text-gray-500",children:t.description})]}),e.jsxs("div",{className:"flex items-center gap-0.5 opacity-0 transition-opacity focus-within:opacity-100 group-hover:opacity-100",children:[n&&e.jsx("button",{type:"button",onClick:a=>
1{a.stopPropagation(),B(t.id),M(t.name),q(t.description??"")},className:"rounded-lg p-1.5 text-gray-300 transition-colors hover:bg-gray-100 hover:text-gray-600",title:s("common.edit"),children:e.jsx(ee,{className:"h-4 w-4"})}),R(t)&&e.jsx("button",{type:"button",onClick:a=>{a.stopPropagation(),D(t)},className:"rounded-lg p-1.5 text-gray-300 transition-colors hover:bg-red-50 hover:text-red-600",title:s("knowledge.bases.delete"),children:e.jsx($,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{className:"mt-auto flex items-center gap-3 pt-5 text-xs text-gray-400",children:[e.jsx("span",{className:"tabular-nums",children:s("knowledge.bases.itemCount",{count:t.itemCount})}),t.lastUpdatedAt&&e.jsxs("span",{className:"tabular-nums",children:[s("knowledge.view.updated"),": ",new Date(t.lastUpdatedAt).toLocaleDateString()]})]})]},t.id))})}),u&&n&&e.jsx(ie,{onClose:()=>v(!1),onCreateBase:X}),L&&n&&e.jsx(le,{base:L,onClose:()=>D(null),onConfirm:()=>void Q(L)})]})}function le({base:s,onClose:g,onConfirm:o}){const{t:n}=I(),[x,p]=r.useState(""),y=s.itemCount>0,h=!y||x.trim()===s.name;return e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4",onClick:g,children:e.jsxs("div",{className:"w-full max-w-md rounded-2xl border border-gray-200 bg-white p-6 shadow-xl",onClick:l=>l.stopPropagation(),children:[e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"flex h-9 w-9 flex-shrink-0 items-center justify-center rounded-xl bg-red-50",children:e.jsx(te,{className:"h-[18px] w-[18px] text-red-600"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("h3",{className:"text-base font-semibold text-gray-900",children:n("knowledge.bases.deleteConfirm.title")}),e.jsx("p",{className:"mt-1 text-sm text-gray-500",children:y?n("knowledge.bases.deleteConfirm.withItems",{name:s.name,count:s.itemCount}):n("knowledge.bases.deleteConfirm.empty",{name:s.name})})]})]}),y&&e.jsxs("div",{className:"mt-4",children:[e.jsx("label",{htmlFor:"confirm-base-name",className:"mb-1.5 block text-xs font-medium text-gray-700",children:n("knowledge.bases.deleteConfirm.typeName")}),e.jsx("input",{id:"confirm-base-name",type:"text",value:x,autoFocus:!0,autoComplete:"off",onChange:l=>p(l.target.value),placeholder:s.name,className:"w-full rounded-lg border border-gray-200 px-3 py-2 text-sm focus:border-red-400 focus:outline-none focus:ring-0"})]}),e.jsxs("div",{className:"mt-6 flex justify-end gap-2",children:[e.jsx("button",{type:"button",onClick:g,className:"rounded-lg border border-gray-200 px-3.5 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50",children:n("common.cancel")}),e.jsxs("button",{type:"button",onClick:o,disabled:!h,className:z("inline-flex items-center gap-2 rounded-lg bg-red-600 px-3.5 py-2 text-sm font-medium text-white","hover:bg-red-700 disabled:cursor-not-allowed disabled:opacity-50"),children:[e.jsx($,{className:"h-4 w-4"}),n("common.delete")]})]})]})})}function ie({onClose:s,onCreateBase:g}){const{t:o}=I(),[n,x]=r.useState(""),[p,y]=r.useState(""),[h,l]=r.useState(!1),S=async u=>{if(u.preventDefault(),!!n.trim()){l(!0);try{await g(n,p)}finally{l(!1)}}};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4",onClick:s,children:e.jsxs("form",{onSubmit:S,onClick:u=>u.stopPropagation(),className:"w-full max-w-md rounded-2xl border border-gray-200 bg-white shadow-xl",children:[e.jsxs("div",{className:"flex items-center justify-between border-b border-gray-100 px-5 py-4",children:[e.jsxs("div",{className:"flex items-center gap-2.5",children:[e.jsx("div",{className:"flex h-8 w-8 items-center justify-center rounded-xl bg-indigo-50",children:e.jsx(P,{className:"h-4 w-4 text-indigo-600"})}),e.jsx("h2",{className:"text-base font-semibold text-gray-900",children:o("knowledge.bases.createNewBase")})]}),e.jsx("button",{type:"button",onClick:s,className:"rounded-lg p-1.5 text-gray-400 hover:bg-gray-100 hover:text-gray-600",children:e.jsx(K,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:"space-y-4 px-5 py-5",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"base-name",className:"mb-1.5 block text-xs font-medium text-gray-700",children:o("knowledge.bases.baseName")}),e.jsx("input",{id:"base-name",type:"text",value:n,required:!0,maxLength:H,onChange:u=>x(u.target.value),placeholder:o("knowledge.bases.baseNamePlaceholder"),className:"w-full rounded-lg border border-gray-200 px-3 py-2 text-sm focus:border-indigo-400 focus:outline-none focus:ring-0"})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"base-description",className:"mb-1.5 block text-xs font-medium text-gray-700",children:o("knowledge.bases.baseDescription")}),e.jsx("textarea",{id:"base-description",value:p,rows:3,maxLength:T,onChange:u=>y(u.target.value),placeholder:o("knowledge.bases.baseDescriptionPlaceholder"),className:"w-full resize-none rounded-lg border border-gray-200 px-3 py-2 text-sm focus:border-indigo-400 focus:outline-none focus:ring-0"})]})]}),e.jsxs("div",{className:"flex justify-end gap-2 border-t border-gray-100 px-5 py-4",children:[e.jsx("button",{type:"button",onClick:s,disabled:h,className:"rounded-lg border border-gray-200 px-3.5 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50",children:o("common.cancel")}),e.jsxs("button",{type:"submit",disabled:h||!n.trim(),className:z("inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-3.5 py-2 text-sm font-medium text-white","hover:bg-indigo-700 disabled:cursor-not-allowed disabled:opacity-50"),children:[h?e.jsx(se,{className:"h-4 w-4 animate-spin"}):e.jsx(F,{className:"h-4 w-4"}),o("knowledge.bases.createBase")]})]})]})})}export{ge as KnowledgeBasesPage,ge as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.