Files

2 lines
18 KiB
JavaScript

import{a as c,d as U,j as r}from"./react-vendor-Bf9ELcll.js";import{o as ae}from"./vendor-BE-_xUHH.js";import{F as se,X as D,T as re,C as ne,S as ie,P as oe}from"./icons-vendor-eB2mgMyz.js";let z=!1;class $ extends Error{constructor(t,a){super(de(a,t)),this.name="ApiError",this.status=t,this.code=M(a)?a.code:`http_${t}`,this.requestId=M(a)?a.request_id:void 0,this.request_id=this.requestId,this.details=M(a)?a.details:void 0,this.payload=a}}const le=()=>{z||typeof window>"u"||(z=!0,window.dispatchEvent(new Event("auth:logout")),queueMicrotask(()=>{z=!1}))},ce=(e,t)=>{const a=new AbortController,s=()=>a.abort(e==null?void 0:e.reason);e!=null&&e.aborted?s():e==null||e.addEventListener("abort",s,{once:!0});const i=globalThis.setTimeout(()=>a.abort(new DOMException("timeout","TimeoutError")),Math.max(1,t));return{signal:a.signal,cleanup:()=>{globalThis.clearTimeout(i),e==null||e.removeEventListener("abort",s)}}},M=e=>{if(typeof e!="object"||e===null)return!1;const t=e;return typeof t.code=="string"&&typeof t.message=="string"},de=(e,t)=>typeof e=="string"?e:M(e)?e.message:`请求失败: ${t}`,ue=3e4,me=(e,t)=>fetch(e,{...t,credentials:"include"}),be=typeof location>"u"?"http://localhost":location.origin,p=ae({baseUrl:be,fetch:me});function ve(e){return e}async function h(e,t={}){var s;const a=ce(t.signal,t.timeoutMs??ue);try{const i=await e(a.signal);if(i.response.status===401&&!t.skipAuthLogout&&le(),i.error!==void 0)throw new $(i.response.status,i.error);if(i.data===void 0)throw new $(i.response.status,void 0);return i.data}catch(i){throw a.signal.aborted?new Error((s=t.signal)!=null&&s.aborted?"请求已取消":"请求超时,请稍后重试"):i}finally{a.cleanup()}}const je=async e=>(await h(a=>p.GET("/api/v1/accounts/details",{signal:a}),e)).map(a=>({id:a.id,enabled:a.enabled,auto_confirm:a.auto_confirm,remark:a.remark,pause_duration:a.pause_duration,paused_until:a.paused_until,paused:a.paused,username:a.username,show_browser:a.show_browser,nickname:a.nickname,avatar_url:a.avatar_url,profile_error:a.profile_error})),Ne=async e=>h(t=>p.GET("/api/v1/accounts/runtime-status",{signal:t}),e),Ce=async(e,t,a,s=!1)=>await h(d=>p.GET("/api/v1/chat/sessions",{params:{query:{account_id:e,cursor:t,refresh:s?1:void 0}},signal:d}),{timeoutMs:s?6e4:a==null?void 0:a.timeoutMs,signal:a==null?void 0:a.signal}),Re=async(e,t,a,s,i)=>await h(f=>p.GET("/api/v1/chat/messages",{params:{query:{account_id:e,chat_id:t,cursor:a,before_id:s}},signal:f}),i),pe=async(e,t)=>(await h(s=>p.GET("/api/v1/chat/quick-replies",{params:{query:{account_id:e}},signal:s}),t)).quick_replies.map(s=>({id:s.id,account_id:s.account_id,content:s.content,created_at:s.created_at})),he=async(e,t,a)=>{const s=await h(i=>p.POST("/api/v1/chat/quick-replies",{body:{account_id:e,content:t},signal:i}),a);return{id:s.id,account_id:s.account_id,content:s.content,created_at:s.created_at}},fe=async(e,t,a)=>h(s=>p.DELETE("/api/v1/chat/quick-replies/{quick_reply_id}",{params:{path:{quick_reply_id:t},query:{account_id:e}},signal:s}),a),ge=async(e,t,a)=>{const s=await h(i=>p.GET("/api/v1/chat/buyer-notes/{buyer_id}",{params:{path:{buyer_id:t},query:{account_id:e}},signal:i}),a);return{account_id:s.account_id,buyer_id:s.buyer_id,content:s.content,updated_at:s.updated_at}},ye=async(e,t,a,s)=>{const i=await h(d=>p.PUT("/api/v1/chat/buyer-notes/{buyer_id}",{params:{path:{buyer_id:t}},body:{account_id:e,content:a},signal:d}),s);return{account_id:i.account_id,buyer_id:i.buyer_id,content:i.content,updated_at:i.updated_at}},Ee=async(e,t)=>h(a=>p.POST("/api/v1/chat/messages",{body:e,signal:a}),t),qe=async(e,t)=>{const a=new FormData;a.set("account_id",e.account_id),a.set("chat_id",e.chat_id),a.set("buyer_id",e.buyer_id),a.set("image",e.image);const s=[["buyer_name",e.buyer_name],["buyer_avatar_url",e.buyer_avatar_url],["item_id",e.item_id],["item_title",e.item_title]];for(const[i,d]of s)d&&a.set(i,d);return h(i=>p.POST("/api/v1/chat/images",{body:a,signal:i}),{timeoutMs:12e4,...t})},Te=async(e,t,a,s)=>h(i=>p.POST("/api/v1/chat/read",{body:{account_id:e,chat_id:t,message_ids:a.map(d=>({messageId:d.messageId,sessionId:d.sessionId,cid:d.cid,conversationType:d.conversationType}))},signal:i}),s),De=e=>{const t=Number.isFinite(e)?Math.max(0,Math.floor(e)):0;return t>99?"99+":String(t)},Me=e=>(Number.isFinite(e)?Math.max(0,Math.floor(e)):0)<10?"inline-flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-red-500 text-[10px] font-bold leading-none text-white":"inline-flex h-5 min-w-5 shrink-0 items-center justify-center rounded-full bg-red-500 px-1.5 text-[10px] font-bold leading-none text-white",Qe=(e,t)=>e.filter(a=>a.direction==="incoming"&&a.message_type!=="system"&&!a.message_key.startsWith("in-")).map(a=>({messageId:a.message_key,sessionId:t,cid:`${t}@goofish`,conversationType:1})),Le=(e,t,a)=>{const s=t.trim().toLowerCase();return e.filter(i=>a&&i.unread_count<=0?!1:s?[i.buyer_name,i.buyer_id,i.item_title,i.last_message].some(d=>(d||"").toLowerCase().includes(s)):!0)},Be=(e,t)=>{const a=new Set(e.map(s=>s.message_key));return[...t.filter(s=>!a.has(s.message_key)),...e]},Fe=(e,t)=>{const a=e.findIndex(s=>s.message_key===t.message_key);return a<0?[...e,t]:e.map((s,i)=>i===a?t:s)},ze=(e,t)=>{if(t.direction!=="incoming"||t.message_type==="system")return e;const a=t.sent_at>0?t.sent_at:Date.now();return e.map(s=>s.chat_id===t.chat_id&&s.direction==="outgoing"&&s.status==="sent"&&s.sent_at<=t.sent_at?{...s,read_status:2,read_at:a}:s)},Pe=(e,t,a)=>e===t&&!a.aborted,R=e=>e instanceof Error&&e.message==="请求已取消",Oe=e=>{if(!e)return"";const t=new Date(e<1e10?e*1e3:e),a=new Date;return t.toDateString()===a.toDateString()?t.toLocaleTimeString("zh-CN",{hour:"2-digit",minute:"2-digit",hour12:!1}):t.toLocaleDateString("zh-CN",{month:"2-digit",day:"2-digit"})},Ge=e=>new Date(e<1e10?e*1e3:e).toLocaleString("zh-CN",{month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",hour12:!1}),xe=(e,t)=>{const[a,s]=c.useState([]),[i,d]=c.useState(""),[f,n]=c.useState(!1),[y,x]=c.useState(null),[E,v]=c.useState(null),[o,j]=c.useState(null),[q,k]=c.useState(!1),[I,Q]=c.useState(!1),[K,N]=c.useState(!1),[L,w]=c.useState(""),[B,P]=c.useState(!1),[W,b]=c.useState(""),O=c.useRef(null),F=c.useRef(0),G=c.useRef(null),T=c.useRef(0),C=c.useRef(null);c.useEffect(()=>{var m;(m=O.current)==null||m.abort();const u=++F.current;if(!e){s([]),d("");return}s([]),d("");const l=new AbortController;return O.current=l,pe(e,{signal:l.signal}).then(g=>{!l.signal.aborted&&u===F.current&&s(g)}).catch(g=>{!l.signal.aborted&&u===F.current&&!R(g)&&b(g instanceof Error?g.message:"读取快捷回复失败")}),(()=>l.abort())},[e]),c.useEffect(()=>{var g;(g=G.current)==null||g.abort();const u=++T.current,l=(t==null?void 0:t.buyer_id)||"";if(!e||!l){j(null),k(!1),Q(!1),N(!1);return}const m=new AbortController;return G.current=m,k(!0),ge(e,l,{signal:m.signal}).then(_=>{!m.signal.aborted&&u===T.current&&(j(_),w(_.content))}).catch(_=>{!m.signal.aborted&&u===T.current&&!R(_)&&b(_ instanceof Error?_.message:"读取买家备注失败")}).finally(()=>{!m.signal.aborted&&u===T.current&&k(!1)}),(()=>m.abort())},[e,t==null?void 0:t.buyer_id]),c.useEffect(()=>()=>{C.current!==null&&window.clearTimeout(C.current)},[]);const V=c.useCallback(async()=>{const u=i.trim();if(!(!e||!u||f)){n(!0),b("");try{const l=await he(e,u);s(m=>[l,...m]),d("")}catch(l){R(l)||b(l instanceof Error?l.message:"添加快捷回复失败")}finally{n(!1)}}},[e,f,i]),X=c.useCallback(u=>{x(u)},[]),H=c.useCallback(()=>{x(null)},[]),J=c.useCallback(async()=>{if(!e||!y||f)return;const u=y.id;n(!0),b("");try{await fe(e,u),s(l=>l.filter(m=>m.id!==u)),x(null)}catch(l){R(l)||b(l instanceof Error?l.message:"删除快捷回复失败")}finally{n(!1)}},[e,y,f]),S=c.useCallback(async u=>{var l;if(!((l=navigator.clipboard)!=null&&l.writeText)){b("当前浏览器不支持复制到剪贴板");return}b("");try{await navigator.clipboard.writeText(u.content),v(u.id),C.current!==null&&window.clearTimeout(C.current),C.current=window.setTimeout(()=>v(null),1600)}catch(m){b(m instanceof Error?m.message:"复制快捷回复失败")}},[]),Y=c.useCallback(()=>{w((o==null?void 0:o.content)||""),N(!1),Q(!0)},[o==null?void 0:o.content]),Z=c.useCallback(()=>{w((o==null?void 0:o.content)||""),N(!1),Q(!1)},[o==null?void 0:o.content]),A=c.useCallback(()=>{w((o==null?void 0:o.content)||""),N(!0)},[o==null?void 0:o.content]),ee=c.useCallback(async()=>{const u=(t==null?void 0:t.buyer_id)||"";if(!(!e||!u||B)){P(!0),b("");try{const l=await ye(e,u,L);j(l),w(l.content),N(!1)}catch(l){R(l)||b(l instanceof Error?l.message:"保存买家备注失败")}finally{P(!1)}}},[e,L,B,t==null?void 0:t.buyer_id]),te=c.useCallback(()=>{b("")},[]);return{quickReplies:a,quickReplyDraft:i,setQuickReplyDraft:d,quickReplyBusy:f,pendingDeleteQuickReply:y,requestQuickReplyDelete:X,cancelQuickReplyDelete:H,confirmQuickReplyDelete:J,addQuickReply:V,copyQuickReply:S,copiedQuickReplyID:E,buyerNote:o,noteLoading:q,noteDialogOpen:I,noteEditing:K,noteDraft:L,setNoteDraft:w,noteSaving:B,openNoteDialog:Y,closeNoteDialog:Z,beginNoteEditing:A,saveBuyerNote:ee,metadataError:W,clearMetadataError:te}},Ue=({activeAccountID:e,selectedSession:t,quickReplyPanelOpen:a,closeQuickReplyPanel:s,sendQuickReply:i,sending:d,accountOnline:f})=>{var E,v;const n=xe(e,t),y=((E=n.buyerNote)==null?void 0:E.content.trim())||"",x=U.useRef(null);return U.useEffect(()=>{if(!a)return;const o=j=>{var k;const q=j.target;!(q instanceof Node)||(k=x.current)!=null&&k.contains(q)||s()};return document.addEventListener("pointerdown",o),(()=>document.removeEventListener("pointerdown",o))},[s,a]),r.jsxs(r.Fragment,{children:[t&&r.jsx("div",{className:"absolute left-40 right-[5.5rem] top-2 z-10 flex min-h-16 items-center justify-center",children:r.jsxs("button",{type:"button",onClick:n.openNoteDialog,className:"inline-flex w-fit max-w-full items-center gap-2 rounded-lg border border-transparent px-2.5 py-1.5 text-left text-xs font-medium leading-5 text-slate-500 transition hover:border-sky-100 hover:bg-sky-50 hover:text-sky-700",title:y||"添加用户备注",children:[r.jsx(se,{className:"h-4 w-4 shrink-0 self-center text-slate-400"}),r.jsx("span",{className:"min-w-0 break-words line-clamp-2",children:n.noteLoading?"正在读取备注…":y||"添加用户备注"})]})}),a&&e&&r.jsxs("aside",{ref:x,"aria-label":"账号快捷回复",className:"absolute inset-y-0 right-0 z-20 flex w-[304px] max-w-[88%] flex-col border-l border-slate-200 bg-white shadow-2xl",children:[r.jsxs("div",{className:"flex items-center justify-between border-b border-slate-100 px-4 py-3",children:[r.jsxs("div",{children:[r.jsx("div",{className:"text-sm font-black text-slate-900",children:"快捷回复"}),r.jsxs("div",{className:"mt-0.5 text-[11px] font-medium text-slate-400",children:["当前账号 · ",n.quickReplies.length,"/50"]})]}),r.jsx("button",{type:"button",onClick:s,className:"rounded-lg p-1.5 text-slate-400 transition hover:bg-slate-100 hover:text-slate-700","aria-label":"收起快捷回复",children:r.jsx(D,{className:"h-4 w-4"})})]}),r.jsxs("div",{className:"min-h-0 flex-1 space-y-2 overflow-y-auto p-3",children:[n.quickReplies.map(o=>r.jsxs("article",{className:"rounded-xl border border-slate-200 bg-slate-50/70 p-3 transition hover:border-sky-200 hover:bg-sky-50/40",children:[r.jsx("p",{className:"whitespace-pre-wrap break-words text-xs leading-5 text-slate-700",children:o.content}),r.jsxs("div",{className:"mt-2 flex items-center justify-end gap-1",children:[r.jsx("button",{type:"button",onClick:(()=>n.requestQuickReplyDelete(o)),disabled:n.quickReplyBusy,className:"rounded-lg p-1 text-slate-400 transition hover:bg-red-50 hover:text-red-600 disabled:opacity-40","aria-label":"删除快捷回复",children:r.jsx(re,{className:"h-3.5 w-3.5"})}),r.jsxs("button",{type:"button",onClick:(()=>{s(),n.copyQuickReply(o)}),className:"rounded-lg px-2 py-1 text-[11px] font-bold text-slate-500 transition hover:bg-white hover:text-sky-700",title:"复制快捷回复",children:[r.jsx(ne,{className:"mr-1 inline h-3 w-3"}),n.copiedQuickReplyID===o.id?"已复制":"复制"]}),r.jsxs("button",{type:"button",onClick:(()=>{s(),i(o.content)}),disabled:!t||d||!f,className:"rounded-lg bg-sky-500 px-2 py-1 text-[11px] font-bold text-white transition hover:bg-sky-600 disabled:cursor-not-allowed disabled:bg-slate-300",title:"发送快捷回复",children:[r.jsx(ie,{className:"mr-1 inline h-3 w-3"}),"发送"]})]})]},o.id)),n.quickReplies.length===0&&r.jsx("div",{className:"px-5 py-12 text-center text-xs leading-5 text-slate-400",children:"还没有快捷回复。把常用话术添加在下方,当前账号的所有聊天都可以使用。"})]}),r.jsxs("div",{className:"border-t border-slate-200 bg-slate-50/80 p-3",children:[n.metadataError&&r.jsxs("div",{className:"mb-2 flex items-start justify-between gap-2 rounded-lg bg-red-50 px-2.5 py-2 text-[11px] leading-4 text-red-700",children:[r.jsx("span",{children:n.metadataError}),r.jsx("button",{type:"button",onClick:n.clearMetadataError,className:"shrink-0","aria-label":"关闭错误提示",children:r.jsx(D,{className:"h-3.5 w-3.5"})})]}),r.jsx("textarea",{value:n.quickReplyDraft,onChange:(o=>n.setQuickReplyDraft(o.target.value)),onKeyDown:(o=>{o.key==="Enter"&&!o.shiftKey&&(o.preventDefault(),n.addQuickReply())}),rows:2,maxLength:2e3,placeholder:"添加常用快捷回复…",className:"min-h-14 w-full resize-none rounded-xl border border-slate-200 bg-white px-3 py-2 text-xs leading-5 outline-none transition focus:border-sky-400 focus:ring-2 focus:ring-sky-100"}),r.jsxs("button",{type:"button",onClick:(()=>void n.addQuickReply()),disabled:!n.quickReplyDraft.trim()||n.quickReplyBusy||n.quickReplies.length>=50,className:"mt-2 flex w-full items-center justify-center gap-1.5 rounded-xl bg-slate-900 py-2 text-xs font-bold text-white transition hover:bg-slate-700 disabled:cursor-not-allowed disabled:bg-slate-300",children:[r.jsx(oe,{className:"h-3.5 w-3.5"}),"添加快捷回复"]})]})]}),n.noteDialogOpen&&r.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-slate-950/50 p-4",onClick:n.closeNoteDialog,children:r.jsxs("div",{role:"dialog","aria-modal":"true","aria-labelledby":"buyer-note-title",className:"flex max-h-[82vh] w-full max-w-lg flex-col overflow-hidden rounded-2xl bg-white shadow-2xl",onClick:(o=>o.stopPropagation()),children:[r.jsxs("div",{className:"flex items-center justify-between border-b border-slate-200 px-5 py-4",children:[r.jsxs("div",{className:"min-w-0",children:[r.jsx("div",{id:"buyer-note-title",className:"text-sm font-black text-slate-900",children:"用户备注"}),r.jsxs("div",{className:"mt-0.5 truncate text-xs text-slate-400",children:[(t==null?void 0:t.buyer_name)||(t==null?void 0:t.buyer_id)||"当前用户"," · ID ",(t==null?void 0:t.buyer_id)||"-"]})]}),r.jsx("button",{type:"button",onClick:n.closeNoteDialog,className:"rounded-lg p-1.5 text-slate-400 transition hover:bg-slate-100 hover:text-slate-700","aria-label":"关闭备注",children:r.jsx(D,{className:"h-5 w-5"})})]}),r.jsxs("div",{className:"min-h-0 flex-1 overflow-y-auto p-5",children:[n.metadataError&&r.jsxs("div",{className:"mb-4 flex items-start justify-between gap-2 rounded-xl bg-red-50 px-3 py-2.5 text-xs leading-5 text-red-700",children:[r.jsx("span",{children:n.metadataError}),r.jsx("button",{type:"button",onClick:n.clearMetadataError,"aria-label":"关闭错误提示",children:r.jsx(D,{className:"h-4 w-4"})})]}),n.noteEditing?r.jsx("textarea",{autoFocus:!0,value:n.noteDraft,onChange:(o=>n.setNoteDraft(o.target.value)),maxLength:2e3,rows:10,placeholder:"记录该用户的沟通偏好、需求或重要事项…",className:"min-h-52 w-full resize-y rounded-xl border border-slate-200 bg-slate-50 px-3 py-3 text-sm leading-6 outline-none transition focus:border-sky-400 focus:bg-white focus:ring-2 focus:ring-sky-100"}):r.jsx("div",{className:"min-h-40 whitespace-pre-wrap break-words rounded-xl border border-slate-100 bg-slate-50/80 px-4 py-3 text-sm leading-6 text-slate-700",children:((v=n.buyerNote)==null?void 0:v.content)||"尚未添加备注。点击“编辑”即可记录该用户的信息。"})]}),r.jsx("div",{className:"flex items-center justify-end gap-2 border-t border-slate-200 px-5 py-3",children:n.noteEditing?r.jsxs(r.Fragment,{children:[r.jsx("button",{type:"button",onClick:n.closeNoteDialog,disabled:n.noteSaving,className:"rounded-xl px-4 py-2 text-sm font-bold text-slate-600 transition hover:bg-slate-100 disabled:opacity-50",children:"取消"}),r.jsx("button",{type:"button",onClick:(()=>void n.saveBuyerNote()),disabled:n.noteSaving,className:"rounded-xl bg-sky-500 px-4 py-2 text-sm font-bold text-white shadow-sm transition hover:bg-sky-600 disabled:cursor-not-allowed disabled:bg-slate-300",children:n.noteSaving?"保存中…":"保存"})]}):r.jsx("button",{type:"button",onClick:n.beginNoteEditing,className:"rounded-xl bg-sky-500 px-4 py-2 text-sm font-bold text-white shadow-sm transition hover:bg-sky-600",children:"编辑"})})]})}),n.pendingDeleteQuickReply&&r.jsx("div",{className:"fixed inset-0 z-[60] flex items-center justify-center bg-slate-950/50 p-4",onClick:n.cancelQuickReplyDelete,children:r.jsxs("div",{role:"alertdialog","aria-modal":"true","aria-labelledby":"quick-reply-delete-title",className:"w-full max-w-sm rounded-2xl bg-white p-5 shadow-2xl",onClick:(o=>o.stopPropagation()),children:[r.jsx("div",{id:"quick-reply-delete-title",className:"text-base font-black text-slate-900",children:"删除快捷回复?"}),r.jsxs("p",{className:"mt-2 whitespace-pre-wrap break-words text-sm leading-6 text-slate-500",children:["“",n.pendingDeleteQuickReply.content,"” 将从当前账号的快捷回复中移除。"]}),r.jsxs("div",{className:"mt-5 flex justify-end gap-2",children:[r.jsx("button",{type:"button",onClick:n.cancelQuickReplyDelete,disabled:n.quickReplyBusy,className:"rounded-xl px-4 py-2 text-sm font-bold text-slate-600 transition hover:bg-slate-100 disabled:opacity-50",children:"取消"}),r.jsx("button",{type:"button",onClick:(()=>void n.confirmQuickReplyDelete()),disabled:n.quickReplyBusy,className:"rounded-xl bg-red-500 px-4 py-2 text-sm font-bold text-white transition hover:bg-red-600 disabled:cursor-not-allowed disabled:bg-red-300",children:n.quickReplyBusy?"删除中…":"删除"})]})]})})]})};export{$ as A,Ue as C,Ce as a,ve as b,p as c,R as d,Re as e,Qe as f,je as g,Be as h,Pe as i,ze as j,Fe as k,Le as l,Te as m,qe as n,Ge as o,Oe as p,Ne as q,h as r,Ee as s,De as t,Me as u};