@triadxyz/widgets 0.0.9 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/LiveChat.tsx +272 -105
- package/src/hooks/useChat.ts +9 -2
package/dist/index.d.mts
CHANGED
|
@@ -46,7 +46,8 @@ interface LiveChatProps {
|
|
|
46
46
|
connectWallet: string;
|
|
47
47
|
description: string;
|
|
48
48
|
};
|
|
49
|
+
signature: string;
|
|
49
50
|
}
|
|
50
|
-
declare function LiveChat({ isOpen, onClose, title, theme, classNames, renderMessage, renderHeader, authority, customerAuthority, onConnectWallet, }: LiveChatProps): react_jsx_runtime.JSX.Element | null;
|
|
51
|
+
declare function LiveChat({ isOpen, onClose, title, theme, classNames, renderMessage, renderHeader, authority, customerAuthority, onConnectWallet, signature, }: LiveChatProps): react_jsx_runtime.JSX.Element | null;
|
|
51
52
|
|
|
52
53
|
export { ButtonWidget, type ButtonWidgetProps, LiveChat, type LiveChatClassNames, type LiveChatProps, type LiveChatTheme };
|
package/dist/index.d.ts
CHANGED
|
@@ -46,7 +46,8 @@ interface LiveChatProps {
|
|
|
46
46
|
connectWallet: string;
|
|
47
47
|
description: string;
|
|
48
48
|
};
|
|
49
|
+
signature: string;
|
|
49
50
|
}
|
|
50
|
-
declare function LiveChat({ isOpen, onClose, title, theme, classNames, renderMessage, renderHeader, authority, customerAuthority, onConnectWallet, }: LiveChatProps): react_jsx_runtime.JSX.Element | null;
|
|
51
|
+
declare function LiveChat({ isOpen, onClose, title, theme, classNames, renderMessage, renderHeader, authority, customerAuthority, onConnectWallet, signature, }: LiveChatProps): react_jsx_runtime.JSX.Element | null;
|
|
51
52
|
|
|
52
53
|
export { ButtonWidget, type ButtonWidgetProps, LiveChat, type LiveChatClassNames, type LiveChatProps, type LiveChatTheme };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
'use strict';var jsxRuntime=require('react/jsx-runtime'),
|
|
1
|
+
'use strict';var jsxRuntime=require('react/jsx-runtime'),G=require('react'),lucideReact=require('lucide-react'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),V=require('socket.io-client'),dateFns=require('date-fns');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var G__default=/*#__PURE__*/_interopDefault(G);var V__default=/*#__PURE__*/_interopDefault(V);var oe=({label:t,variant:g="primary",className:d="",...p})=>jsxRuntime.jsx("button",{className:`px-4 py-2 rounded-lg font-medium transition-colors ${{primary:"bg-blue-600 text-white hover:bg-blue-700",secondary:"bg-gray-200 text-gray-800 hover:bg-gray-300"}[g]} ${d}`,...p,children:t});function y(...t){return tailwindMerge.twMerge(clsx.clsx(t))}var W=V__default.default("https://beta.triadfi.co",{path:"/socket.io",reconnection:true,reconnectionAttempts:5,reconnectionDelay:1e3,randomizationFactor:.5,timeout:5e3,transports:["websocket"]}),c=W;function E({authority:t,customerAuthority:g,signature:d}){let[p,l]=G.useState([]),[C,k]=G.useState(0),o=g?.trim();return G.useEffect(()=>{if(!o)return;let f=a=>{l(a);},h=a=>{l(m=>[...m,a]);},i=a=>{k(a.count);},b=()=>{let a=t?{authority:t,customerAuthority:o}:{customerAuthority:o};c.emit("chat:join",a,m=>{m?.error&&console.error("join error",m.error);});};return c.on("chat:history",f),c.on("chat:message",h),c.on("chat:users",i),c.on("connect",b),b(),()=>{c.off("chat:history",f),c.off("chat:message",h),c.off("chat:users",i),c.off("connect",b);}},[t,o]),{messages:p,onlineCount:C,sendMessage:f=>new Promise(h=>{if(!t||!o||!d){h({error:"INVALID_REQUEST"});return}c.emit("chat:message",{authority:t,customerAuthority:o,message:f,signature:d},i=>{i?.error&&console.error("send error",i.error),h(i??{error:"INTERNAL_ERROR"});});})}}var ee=200,te=80;function H(t){return t?[t.timestamp,t.authority??"anonymous",t.name,t.message].join(":"):null}function re(t,g=te){return t.scrollHeight-t.scrollTop-t.clientHeight<=g}function se(t){return `${t} ${t===1?"previsao":"previsoes"}`}function Me({isOpen:t,onClose:g,title:d="Live Chat",theme:p,classNames:l,renderMessage:C,renderHeader:k,authority:o,customerAuthority:L,onConnectWallet:f,signature:h}){let[i,b]=G.useState(""),a=G.useRef(null),m=G.useRef(false),M=G.useRef(true),v=G.useRef(null),P=p?.primaryColor||"#FF3D00",D=p?.background||"var(--chat-bg)",{sendMessage:B,onlineCount:_,messages:x}=E({authority:o||void 0,customerAuthority:L,signature:h}),S=()=>{let r=a.current;return {firstMessageKey:H(x[0]),lastMessageKey:H(x[x.length-1]),messageCount:x.length,scrollHeight:r?.scrollHeight??0}},K=(r="auto")=>{let e=a.current;e&&(e.scrollTo({top:e.scrollHeight,behavior:r}),M.current=true);},$=r=>{let e=a.current;if(!e)return;let s=e.scrollHeight-r.scrollHeight;s!==0&&(e.scrollTop+=s);},z=()=>{let r=a.current;r&&(M.current=re(r));};G.useLayoutEffect(()=>{if(!t){m.current=false,M.current=true,v.current=null;return}if(!a.current)return;let e=S(),s=v.current;if(!m.current){if(x.length===0){v.current=e;return}K("auto"),m.current=true,v.current=S();return}if(!s){v.current=e;return}let w=e.messageCount>s.messageCount&&s.firstMessageKey!==null&&e.firstMessageKey!==s.firstMessageKey&&e.lastMessageKey===s.lastMessageKey,N=e.messageCount>s.messageCount&&s.lastMessageKey!==null&&e.lastMessageKey!==s.lastMessageKey&&e.firstMessageKey===s.firstMessageKey,I=e.messageCount>0&&s.messageCount>0&&e.firstMessageKey!==s.firstMessageKey&&e.lastMessageKey!==s.lastMessageKey;w?$(s):N?M.current&&K("auto"):I&&M.current&&K("auto"),v.current=S();},[t,x]);let A=()=>{let r=i.trim();if(r){if(!o){f?.();return}b(""),B(r);}},F=r=>{r.key==="Enter"&&!r.shiftKey&&(r.preventDefault(),A());};return t?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx("style",{children:`
|
|
2
2
|
:root {
|
|
3
3
|
--chat-bg: #ffffff;
|
|
4
4
|
}
|
|
5
5
|
.dark {
|
|
6
6
|
--chat-bg: #15181D;
|
|
7
7
|
}
|
|
8
|
-
`}),jsxRuntime.jsxs("div",{onClick:
|
|
8
|
+
`}),jsxRuntime.jsx("div",{className:"fixed inset-0 z-[110]",onClick:g,"aria-hidden":"true",children:jsxRuntime.jsxs("div",{onClick:r=>r.stopPropagation(),role:"dialog","aria-modal":"true","aria-label":d,className:y("fixed inset-0 flex flex-col overflow-hidden bg-white dark:bg-app-dark-400 lg:inset-auto lg:right-6 lg:top-[75px] lg:max-w-[340px] lg:max-h-[calc(100vh-90px)] lg:border lg:border-black/10 lg:shadow-2xl lg:dark:border-white/10","lg:h-[800px]",l?.container),style:{background:D},children:[k?k():jsxRuntime.jsxs("div",{className:y("flex items-center justify-between border-b border-black/10 px-4 py-4 dark:border-white/10 lg:px-6",l?.header),children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-3",children:[jsxRuntime.jsx("h2",{className:"text-lg font-semibold text-triad-dark-100 dark:text-white",children:d}),jsxRuntime.jsxs("div",{className:"flex items-center gap-1.5",children:[jsxRuntime.jsx("div",{className:"size-2 rounded-full bg-green-500 animate-pulse"}),jsxRuntime.jsxs("span",{className:"text-xs text-app-gray-400 dark:text-white",children:[_," online"]})]})]}),jsxRuntime.jsx("button",{onClick:g,children:jsxRuntime.jsx(lucideReact.X,{className:"text-black dark:text-white",size:18})})]}),jsxRuntime.jsx("div",{ref:a,onScroll:z,className:y("flex-1 overflow-y-auto space-y-4 px-4 py-4 lg:px-6",l?.messages),style:{overflowAnchor:"none"},children:(()=>{let r=new Map;return x.map(e=>{let s=H(e)??"message",w=r.get(s)??0,N=`${s}:${w}`;return r.set(s,w+1),C?jsxRuntime.jsx(G__default.default.Fragment,{children:C(e)},N):jsxRuntime.jsxs("div",{className:`flex flex-col gap-1 ${e.authority===o?"items-end":"items-start"}`,children:[e.authority!==o&&jsxRuntime.jsx("span",{className:"max-w-[160px] truncate text-xs font-medium text-triad-dark-100 dark:text-white",children:e.name}),jsxRuntime.jsx("div",{className:y("max-w-[75%] rounded-2xl px-4 py-2.5",e.authority===o?"rounded-br-sm text-white":"rounded-bl-sm bg-black/5 text-triad-dark-100 dark:bg-white/5 dark:text-white"),style:e.authority===o?{backgroundColor:P}:void 0,children:jsxRuntime.jsx("p",{className:"text-sm break-words",children:e.message})}),jsxRuntime.jsxs("div",{className:"flex items-center gap-2 text-[10px] text-black opacity-50 dark:text-white",children:[jsxRuntime.jsx("span",{children:se(e.predictionsCount)}),jsxRuntime.jsx("span",{children:dateFns.format(e.timestamp,"HH:mm")})]})]},N)})})()}),jsxRuntime.jsx("div",{className:"border-t border-black/10 px-4 py-4 dark:border-white/10 lg:px-6",children:jsxRuntime.jsx("div",{className:"flex flex-col gap-2",children:jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsx("input",{value:i,onChange:r=>b(r.target.value),onKeyDown:F,maxLength:ee,placeholder:"Digite sua mensagem...",className:y("flex-1 rounded-xl bg-black/5 px-4 py-2.5 text-sm text-app-gray-400 outline-none dark:bg-white/5 dark:text-white",l?.input)}),jsxRuntime.jsx("button",{onClick:A,disabled:!i.trim()||!o&&!f,className:y("flex size-10 items-center justify-center rounded-xl transition-colors bg-app-dark-400",l?.button),style:{opacity:i.trim()?1:.5},children:jsxRuntime.jsx(lucideReact.Send,{color:p?.buttonSend.color,size:18})})]})})})]})})]}):null}exports.ButtonWidget=oe;exports.LiveChat=Me;//# sourceMappingURL=index.js.map
|
|
9
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/components/ButtonWidget.tsx","../src/utils/cn.ts","../src/utils/socket.ts","../src/hooks/useChat.ts","../src/components/LiveChat.tsx"],"names":["ButtonWidget","label","variant","className","props","jsx","cn","classes","twMerge","clsx","socket","io","socket_default","useChat","authority","customerAuthority","messages","setMessages","useState","onlineCount","setOnlineCount","normalizedCustomerAuthority","useEffect","handleHistory","msgs","handleMessage","msg","prev","handleUsers","data","joinChat","joinPayload","res","message","resolve","CHAT_MAX_MESSAGE_LENGTH","LiveChat","isOpen","onClose","title","theme","classNames","renderMessage","renderHeader","onConnectWallet","inputValue","setInputValue","messagesEndRef","useRef","primary","background","sendMessage","scrollToBottom","handleSendMessage","handleKeyDown","jsxs","Fragment","X","idx","React","format","Send"],"mappings":"sYASO,IAAMA,CAAAA,CAA4C,CAAC,CACxD,KAAA,CAAAC,CAAAA,CACA,OAAA,CAAAC,CAAAA,CAAU,SAAA,CACV,SAAA,CAAAC,EAAY,EAAA,CACZ,GAAGC,CACL,CAAA,GAQIC,cAAAA,CAAC,QAAA,CAAA,CACC,SAAA,CAAW,CAAA,mDAAA,EAPE,CACf,OAAA,CAAS,0CAAA,CACT,SAAA,CAAW,6CACb,CAAA,CAIyCH,CAAO,CAAC,CAAA,CAAA,EAAIC,CAAS,CAAA,CAAA,CACzD,GAAGC,CAAAA,CAEH,SAAAH,CAAAA,CACH,ECvBG,SAASK,CAAAA,CAAAA,GAAMC,CAAAA,CAAuB,CAC3C,OAAOC,qBAAAA,CAAQC,SAAAA,CAAKF,CAAO,CAAC,CAC9B,CCJA,IAAMG,CAAAA,CAASC,kBAAAA,CAAG,yBAAA,CAA2B,CAC3C,IAAA,CAAM,YAAA,CACN,YAAA,CAAc,IAAA,CACd,oBAAA,CAAsB,CAAA,CACtB,kBAAmB,GAAA,CACnB,mBAAA,CAAqB,EAAA,CACrB,OAAA,CAAS,GAAA,CACT,UAAA,CAAY,CAAC,WAAW,CAC1B,CAAC,CAAA,CAEMC,CAAAA,CAAQF,CAAAA,CCQR,SAASG,CAAAA,CAAQ,CACtB,SAAA,CAAAC,CAAAA,CACA,iBAAA,CAAAC,CACF,CAAA,CAGG,CACD,GAAM,CAACC,CAAAA,CAAUC,CAAW,CAAA,C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,MAAA,CAAO,KAAK,CAAA,CAC7C,SAAA,CAAWQ,CAAAA,CACX,SAAA,CAAWnB,CAAAA,CACX,WAAA,CAAY,wBAAA,CACZ,SAAA,CAAW7B,CAAAA,CACT,iHAAA,CACAmC,CAAAA,EAAY,KACd,CAAA,CACF,CAAA,CAEApC,cAAAA,CAAC,QAAA,CAAA,CACC,OAAA,CAASgD,CAAAA,CACT,QAAA,CAAU,CAACR,CAAAA,CAAW,IAAA,EAAK,EAAM,CAAC/B,GAAa,CAAC8B,CAAAA,CAChD,SAAA,CAAWtC,CAAAA,CACT,uFAAA,CACAmC,CAAAA,EAAY,MACd,CAAA,CACA,KAAA,CAAO,CACL,OAAA,CAASI,CAAAA,CAAW,IAAA,EAAK,CAAI,CAAA,CAAI,EACnC,EAEA,QAAA,CAAAxC,cAAAA,CAACwD,gBAAAA,CAAA,CAAK,KAAA,CAAOrB,CAAAA,EAAO,UAAA,CAAW,KAAA,CAAO,IAAA,CAAM,EAAA,CAAI,CAAA,CAClD,CAAA,CAAA,CACF,CAAA,CACF,CAAA,CACF,CAAA,CAAA,CACF,CAAA,CAAA,CACF,EA/HkB,IAiItB","file":"index.js","sourcesContent":["'use client';\n\nimport React from 'react';\n\nexport interface ButtonWidgetProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n label: string;\n variant?: 'primary' | 'secondary';\n}\n\nexport const ButtonWidget: React.FC<ButtonWidgetProps> = ({ \n label, \n variant = 'primary', \n className = '',\n ...props \n}) => {\n const baseStyles = 'px-4 py-2 rounded-lg font-medium transition-colors';\n const variants = {\n primary: 'bg-blue-600 text-white hover:bg-blue-700',\n secondary: 'bg-gray-200 text-gray-800 hover:bg-gray-300'\n };\n\n return (\n <button \n className={`${baseStyles} ${variants[variant]} ${className}`}\n {...props}\n >\n {label}\n </button>\n );\n};\n","import type { ClassValue } from \"clsx\";\nimport { clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...classes: ClassValue[]) {\n return twMerge(clsx(classes));\n}\n","import io from \"socket.io-client\";\n\nconst socket = io(\"https://beta.triadfi.co\", {\n path: \"/socket.io\",\n reconnection: true,\n reconnectionAttempts: 5,\n reconnectionDelay: 1000,\n randomizationFactor: 0.5,\n timeout: 5000,\n transports: [\"websocket\"],\n});\n\nexport default socket;\n","import socket from \"@/utils/socket\";\nimport { useEffect, useState } from \"react\";\n\nexport interface ChatMessage {\n authority?: string;\n name: string;\n image: string;\n predictionsCount: number;\n message: string;\n timestamp: number;\n}\n\ninterface ChatUsersPayload {\n count: number;\n}\n\ninterface ChatResponse {\n error: string | null;\n}\n\nexport function useChat({\n authority,\n customerAuthority,\n}: {\n authority?: string;\n customerAuthority: string;\n}) {\n const [messages, setMessages] = useState<ChatMessage[]>([]);\n const [onlineCount, setOnlineCount] = useState(0);\n const normalizedCustomerAuthority = customerAuthority?.trim();\n\n useEffect(() => {\n if (!normalizedCustomerAuthority) return;\n\n const handleHistory = (msgs: ChatMessage[]) => {\n setMessages(msgs);\n };\n\n const handleMessage = (msg: ChatMessage) => {\n setMessages((prev) => [...prev, msg]);\n };\n\n const handleUsers = (data: ChatUsersPayload) => {\n setOnlineCount(data.count);\n };\n\n const joinChat = () => {\n const joinPayload = authority\n ? { authority, customerAuthority: normalizedCustomerAuthority }\n : { customerAuthority: normalizedCustomerAuthority };\n\n socket.emit(\"chat:join\", joinPayload, (res: ChatResponse) => {\n if (res?.error) {\n console.error(\"join error\", res.error);\n }\n });\n };\n\n socket.on(\"chat:history\", handleHistory);\n socket.on(\"chat:message\", handleMessage);\n socket.on(\"chat:users\", handleUsers);\n socket.on(\"connect\", joinChat);\n joinChat();\n\n return () => {\n socket.off(\"chat:history\", handleHistory);\n socket.off(\"chat:message\", handleMessage);\n socket.off(\"chat:users\", handleUsers);\n socket.off(\"connect\", joinChat);\n };\n }, [authority, normalizedCustomerAuthority]);\n\n const sendMessage = (message: string): Promise<ChatResponse> =>\n new Promise((resolve) => {\n if (!authority || !normalizedCustomerAuthority) {\n resolve({ error: \"INVALID_REQUEST\" });\n return;\n }\n\n socket.emit(\n \"chat:message\",\n { authority, customerAuthority: normalizedCustomerAuthority, message },\n (res: ChatResponse) => {\n if (res?.error) {\n console.error(\"send error\", res.error);\n }\n resolve(res ?? { error: \"INTERNAL_ERROR\" });\n },\n );\n });\n\n return {\n messages,\n onlineCount,\n sendMessage,\n };\n}\n","\"use client\";\n\nimport React, { useState, useRef, useEffect } from \"react\";\nimport { X, Send } from \"lucide-react\";\nimport { cn } from \"@/utils/cn\";\nimport { useChat, ChatMessage } from \"@/hooks/useChat\";\nimport { format } from \"date-fns\";\n\nexport interface LiveChatTheme {\n primaryColor?: string;\n background?: string;\n textColor?: string;\n buttonSend: {\n color: string;\n } \n}\n\nexport interface LiveChatClassNames {\n container?: string;\n header?: string;\n messages?: string;\n input?: string;\n button?: string;\n}\n\nexport interface LiveChatProps {\n isOpen: boolean;\n onClose: () => void;\n\n // ui\n title?: string;\n\n // customization\n theme?: LiveChatTheme;\n classNames?: LiveChatClassNames;\n\n // slots\n renderMessage?: (message: ChatMessage) => React.ReactNode;\n renderHeader?: () => React.ReactNode;\n authority: string | null;\n customerAuthority: string;\n onConnectWallet?: () => void;\n fallbackMessages?: {\n connectWallet: string;\n description: string;\n };\n}\n\nconst CHAT_MAX_MESSAGE_LENGTH = 200;\n\nexport function LiveChat({\n isOpen,\n onClose,\n title = \"Live Chat\",\n theme,\n classNames,\n renderMessage,\n renderHeader,\n authority,\n customerAuthority,\n onConnectWallet,\n}: LiveChatProps) {\n const [inputValue, setInputValue] = useState(\"\");\n const messagesEndRef = useRef<HTMLDivElement>(null);\n const primary = theme?.primaryColor || \"#FF3D00\";\n const background = theme?.background || \"var(--chat-bg)\";\n\n const { sendMessage, onlineCount, messages } = useChat({\n authority: authority || undefined,\n customerAuthority: customerAuthority,\n });\n\n const scrollToBottom = () => {\n messagesEndRef.current?.scrollIntoView({ behavior: \"smooth\" });\n };\n\n useEffect(() => {\n scrollToBottom();\n }, [messages]);\n\n const handleSendMessage = () => {\n const message = inputValue.trim();\n if (!message) return;\n\n if (!authority) {\n onConnectWallet?.();\n return;\n }\n\n setInputValue(\"\");\n sendMessage(message);\n };\n\n const handleKeyDown = (e: React.KeyboardEvent) => {\n if (e.key === \"Enter\" && !e.shiftKey) {\n e.preventDefault();\n handleSendMessage();\n }\n };\n\n if (!isOpen) return null;\n\n return (\n <>\n <style>{`\n :root {\n --chat-bg: #ffffff;\n }\n .dark {\n --chat-bg: #15181D;\n }\n `}</style>\n\n <div\n onClick={(e) => e.stopPropagation()}\n className={cn(\n \"fixed inset-0 z-[110] flex flex-col overflow-hidden bg-white dark:bg-app-dark-400 lg:inset-auto lg:right-6 lg:top-[75px] lg:max-w-[340px] lg:max-h-[calc(100vh-90px)] lg:border lg:border-black/10 lg:shadow-2xl lg:dark:border-white/10\",\n \"lg:h-[800px]\",\n classNames?.container,\n )}\n style={{ background }}\n >\n {renderHeader ? (\n renderHeader()\n ) : (\n <div\n className={cn(\n \"flex items-center justify-between border-b border-black/10 px-4 py-4 dark:border-white/10 lg:px-6\",\n classNames?.header,\n )}\n >\n <div className=\"flex items-center gap-3\">\n <h2 className=\"text-lg font-semibold text-triad-dark-100 dark:text-white\">\n {title}\n </h2>\n <div className=\"flex items-center gap-1.5\">\n <div className=\"size-2 rounded-full bg-green-500 animate-pulse\" />\n <span className=\"text-xs text-app-gray-400 dark:text-white\">\n {onlineCount} online\n </span>\n </div>\n </div>\n <button onClick={onClose}>\n <X className=\"text-black dark:text-white\" size={18} />\n </button>\n </div>\n )}\n\n <div\n className={cn(\n \"flex-1 overflow-y-auto space-y-4 px-4 py-4 lg:px-6\",\n classNames?.messages,\n )}\n >\n {messages.map((message, idx) =>\n renderMessage ? (\n <React.Fragment key={idx}>{renderMessage(message)}</React.Fragment>\n ) : (\n <div\n key={idx}\n className={`flex flex-col gap-1 ${\n message.authority === authority ? \"items-end\" : \"items-start\"\n }`}\n >\n {message.authority !== authority && (\n <span className=\"max-w-[160px] truncate text-xs font-medium text-triad-dark-100 dark:text-white\">\n {message.name}\n </span>\n )}\n\n <div\n className={cn(\n \"max-w-[75%] rounded-2xl px-4 py-2.5\",\n message.authority === authority\n ? \"rounded-br-sm text-white\"\n : \"rounded-bl-sm bg-black/5 text-triad-dark-100 dark:bg-white/5 dark:text-white\",\n )}\n style={\n message.authority === authority\n ? { backgroundColor: primary }\n : undefined\n }\n >\n <p className=\"text-sm break-words\">{message.message}</p>\n </div>\n\n <span className=\"text-[10px] text-black opacity-50 dark:text-white\">\n {format(message.timestamp, \"HH:mm\")}\n </span>\n </div>\n ),\n )}\n <div ref={messagesEndRef} />\n </div>\n\n <div className=\"border-t border-black/10 px-4 py-4 dark:border-white/10 lg:px-6\">\n <div className=\"flex flex-col gap-2\">\n <div className=\"flex items-center gap-2\">\n <input\n value={inputValue}\n onChange={(e) => setInputValue(e.target.value)}\n onKeyDown={handleKeyDown}\n maxLength={CHAT_MAX_MESSAGE_LENGTH}\n placeholder=\"Digite sua mensagem...\"\n className={cn(\n \"flex-1 rounded-xl bg-black/5 px-4 py-2.5 text-sm text-app-gray-400 outline-none dark:bg-white/5 dark:text-white\",\n classNames?.input,\n )}\n />\n\n <button\n onClick={handleSendMessage}\n disabled={!inputValue.trim() || (!authority && !onConnectWallet)}\n className={cn(\n \"flex size-10 items-center justify-center rounded-xl transition-colors bg-app-dark-400\",\n classNames?.button,\n )}\n style={{\n opacity: inputValue.trim() ? 1 : 0.5,\n }}\n >\n <Send color={theme?.buttonSend.color} size={18} />\n </button>\n </div>\n </div>\n </div>\n </div>\n </>\n );\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/components/ButtonWidget.tsx","../src/utils/cn.ts","../src/utils/socket.ts","../src/hooks/useChat.ts","../src/components/LiveChat.tsx"],"names":["ButtonWidget","label","variant","className","props","jsx","cn","classes","twMerge","clsx","socket","io","socket_default","useChat","authority","customerAuthority","signature","messages","setMessages","useState","onlineCount","setOnlineCount","normalizedCustomerAuthority","useEffect","handleHistory","msgs","handleMessage","msg","prev","handleUsers","data","joinChat","joinPayload","res","message","resolve","CHAT_MAX_MESSAGE_LENGTH","AUTO_SCROLL_THRESHOLD_PX","getMessageIdentity","isNearBottom","element","threshold","formatPredictionsCount","count","LiveChat","isOpen","onClose","title","theme","classNames","renderMessage","renderHeader","onConnectWallet","inputValue","setInputValue","messagesContainerRef","useRef","hasInitialScrollRef","shouldAutoScrollRef","previousSnapshotRef","primary","background","sendMessage","createMessagesSnapshot","container","scrollToBottom","behavior","preserveScrollPositionAfterPrepend","previousSnapshot","scrollHeightDelta","handleMessagesScroll","useLayoutEffect","nextSnapshot","hasPrependedMessages","hasAppendedMessages","hasReplacedHistory","handleSendMessage","handleKeyDown","e","jsxs","Fragment","X","messageOccurrences","baseKey","occurrence","messageKey","React","format","Send"],"mappings":"sYASO,IAAMA,GAA4C,CAAC,CACxD,MAAAC,CAAAA,CACA,OAAA,CAAAC,EAAU,SAAA,CACV,SAAA,CAAAC,EAAY,EAAA,CACZ,GAAGC,CACL,CAAA,GAQIC,cAAAA,CAAC,UACC,SAAA,CAAW,CAAA,mDAAA,EAPE,CACf,OAAA,CAAS,2CACT,SAAA,CAAW,6CACb,EAIyCH,CAAO,CAAC,IAAIC,CAAS,CAAA,CAAA,CACzD,GAAGC,CAAAA,CAEH,QAAA,CAAAH,EACH,ECvBG,SAASK,KAAMC,CAAAA,CAAuB,CAC3C,OAAOC,qBAAAA,CAAQC,SAAAA,CAAKF,CAAO,CAAC,CAC9B,CCJA,IAAMG,CAAAA,CAASC,mBAAG,yBAAA,CAA2B,CAC3C,KAAM,YAAA,CACN,YAAA,CAAc,KACd,oBAAA,CAAsB,CAAA,CACtB,kBAAmB,GAAA,CACnB,mBAAA,CAAqB,GACrB,OAAA,CAAS,GAAA,CACT,UAAA,CAAY,CAAC,WAAW,CAC1B,CAAC,EAEMC,CAAAA,CAAQF,CAAAA,CCQR,SAASG,CAAAA,CAAQ,CACtB,UAAAC,CAAAA,CACA,iBAAA,CAAAC,EAC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,CAAI,CAAA,CAClD,GACF,CAAA,CACF,CAAA,CACF,GACF,CAAA,CACF,CAAA,CAAA,CACF,EA7JkB,IA+JtB","file":"index.js","sourcesContent":["'use client';\n\nimport React from 'react';\n\nexport interface ButtonWidgetProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n label: string;\n variant?: 'primary' | 'secondary';\n}\n\nexport const ButtonWidget: React.FC<ButtonWidgetProps> = ({ \n label, \n variant = 'primary', \n className = '',\n ...props \n}) => {\n const baseStyles = 'px-4 py-2 rounded-lg font-medium transition-colors';\n const variants = {\n primary: 'bg-blue-600 text-white hover:bg-blue-700',\n secondary: 'bg-gray-200 text-gray-800 hover:bg-gray-300'\n };\n\n return (\n <button \n className={`${baseStyles} ${variants[variant]} ${className}`}\n {...props}\n >\n {label}\n </button>\n );\n};\n","import type { ClassValue } from \"clsx\";\nimport { clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...classes: ClassValue[]) {\n return twMerge(clsx(classes));\n}\n","import io from \"socket.io-client\";\n\nconst socket = io(\"https://beta.triadfi.co\", {\n path: \"/socket.io\",\n reconnection: true,\n reconnectionAttempts: 5,\n reconnectionDelay: 1000,\n randomizationFactor: 0.5,\n timeout: 5000,\n transports: [\"websocket\"],\n});\n\nexport default socket;\n","import socket from \"@/utils/socket\";\nimport { useEffect, useState } from \"react\";\n\nexport interface ChatMessage {\n authority?: string;\n name: string;\n image: string;\n predictionsCount: number;\n message: string;\n timestamp: number;\n}\n\ninterface ChatUsersPayload {\n count: number;\n}\n\ninterface ChatResponse {\n error: string | null;\n}\n\nexport function useChat({\n authority,\n customerAuthority,\n signature,\n}: {\n authority?: string;\n customerAuthority: string;\n signature: string;\n}) {\n const [messages, setMessages] = useState<ChatMessage[]>([]);\n const [onlineCount, setOnlineCount] = useState(0);\n const normalizedCustomerAuthority = customerAuthority?.trim();\n\n useEffect(() => {\n if (!normalizedCustomerAuthority) return;\n\n const handleHistory = (msgs: ChatMessage[]) => {\n setMessages(msgs);\n };\n\n const handleMessage = (msg: ChatMessage) => {\n setMessages((prev) => [...prev, msg]);\n };\n\n const handleUsers = (data: ChatUsersPayload) => {\n setOnlineCount(data.count);\n };\n\n const joinChat = () => {\n const joinPayload = authority\n ? { authority, customerAuthority: normalizedCustomerAuthority }\n : { customerAuthority: normalizedCustomerAuthority };\n\n socket.emit(\"chat:join\", joinPayload, (res: ChatResponse) => {\n if (res?.error) {\n console.error(\"join error\", res.error);\n }\n });\n };\n\n socket.on(\"chat:history\", handleHistory);\n socket.on(\"chat:message\", handleMessage);\n socket.on(\"chat:users\", handleUsers);\n socket.on(\"connect\", joinChat);\n joinChat();\n\n return () => {\n socket.off(\"chat:history\", handleHistory);\n socket.off(\"chat:message\", handleMessage);\n socket.off(\"chat:users\", handleUsers);\n socket.off(\"connect\", joinChat);\n };\n }, [authority, normalizedCustomerAuthority]);\n\n const sendMessage = (message: string): Promise<ChatResponse> =>\n new Promise((resolve) => {\n if (!authority || !normalizedCustomerAuthority || !signature) {\n resolve({ error: \"INVALID_REQUEST\" });\n return;\n }\n\n socket.emit(\n \"chat:message\",\n {\n authority,\n customerAuthority: normalizedCustomerAuthority,\n message,\n signature,\n },\n (res: ChatResponse) => {\n if (res?.error) {\n console.error(\"send error\", res.error);\n }\n resolve(res ?? { error: \"INTERNAL_ERROR\" });\n },\n );\n });\n\n return {\n messages,\n onlineCount,\n sendMessage,\n };\n}\n","\"use client\";\n\nimport React, { useLayoutEffect, useRef, useState } from \"react\";\nimport { X, Send } from \"lucide-react\";\nimport { cn } from \"@/utils/cn\";\nimport { useChat, ChatMessage } from \"@/hooks/useChat\";\nimport { format } from \"date-fns\";\n\nexport interface LiveChatTheme {\n primaryColor?: string;\n background?: string;\n textColor?: string;\n buttonSend: {\n color: string;\n };\n}\n\nexport interface LiveChatClassNames {\n container?: string;\n header?: string;\n messages?: string;\n input?: string;\n button?: string;\n}\n\nexport interface LiveChatProps {\n isOpen: boolean;\n onClose: () => void;\n\n // ui\n title?: string;\n\n // customization\n theme?: LiveChatTheme;\n classNames?: LiveChatClassNames;\n\n // slots\n renderMessage?: (message: ChatMessage) => React.ReactNode;\n renderHeader?: () => React.ReactNode;\n authority: string | null;\n customerAuthority: string;\n onConnectWallet?: () => void;\n fallbackMessages?: {\n connectWallet: string;\n description: string;\n };\n signature: string;\n}\n\nconst CHAT_MAX_MESSAGE_LENGTH = 200;\nconst AUTO_SCROLL_THRESHOLD_PX = 80;\n\ninterface MessagesSnapshot {\n firstMessageKey: string | null;\n lastMessageKey: string | null;\n messageCount: number;\n scrollHeight: number;\n}\n\nfunction getMessageIdentity(message?: ChatMessage) {\n if (!message) return null;\n\n return [\n message.timestamp,\n message.authority ?? \"anonymous\",\n message.name,\n message.message,\n ].join(\":\");\n}\n\nfunction isNearBottom(\n element: HTMLDivElement,\n threshold = AUTO_SCROLL_THRESHOLD_PX,\n) {\n const distanceToBottom =\n element.scrollHeight - element.scrollTop - element.clientHeight;\n\n return distanceToBottom <= threshold;\n}\n\nfunction formatPredictionsCount(count: number) {\n return `${count} ${count === 1 ? \"previsao\" : \"previsoes\"}`;\n}\n\nexport function LiveChat({\n isOpen,\n onClose,\n title = \"Live Chat\",\n theme,\n classNames,\n renderMessage,\n renderHeader,\n authority,\n customerAuthority,\n onConnectWallet,\n signature,\n}: LiveChatProps) {\n const [inputValue, setInputValue] = useState(\"\");\n const messagesContainerRef = useRef<HTMLDivElement>(null);\n const hasInitialScrollRef = useRef(false);\n const shouldAutoScrollRef = useRef(true);\n const previousSnapshotRef = useRef<MessagesSnapshot | null>(null);\n const primary = theme?.primaryColor || \"#FF3D00\";\n const background = theme?.background || \"var(--chat-bg)\";\n\n const { sendMessage, onlineCount, messages } = useChat({\n authority: authority || undefined,\n customerAuthority: customerAuthority,\n signature,\n });\n\n const createMessagesSnapshot = (): MessagesSnapshot => {\n const container = messagesContainerRef.current;\n\n return {\n firstMessageKey: getMessageIdentity(messages[0]),\n lastMessageKey: getMessageIdentity(messages[messages.length - 1]),\n messageCount: messages.length,\n scrollHeight: container?.scrollHeight ?? 0,\n };\n };\n\n const scrollToBottom = (behavior: ScrollBehavior = \"auto\") => {\n const container = messagesContainerRef.current;\n if (!container) return;\n\n container.scrollTo({\n top: container.scrollHeight,\n behavior,\n });\n\n shouldAutoScrollRef.current = true;\n };\n\n const preserveScrollPositionAfterPrepend = (\n previousSnapshot: MessagesSnapshot,\n ) => {\n const container = messagesContainerRef.current;\n if (!container) return;\n\n const scrollHeightDelta =\n container.scrollHeight - previousSnapshot.scrollHeight;\n\n if (scrollHeightDelta !== 0) {\n container.scrollTop += scrollHeightDelta;\n }\n };\n\n const handleMessagesScroll = () => {\n const container = messagesContainerRef.current;\n if (!container) return;\n\n shouldAutoScrollRef.current = isNearBottom(container);\n };\n\n useLayoutEffect(() => {\n if (!isOpen) {\n hasInitialScrollRef.current = false;\n shouldAutoScrollRef.current = true;\n previousSnapshotRef.current = null;\n return;\n }\n\n const container = messagesContainerRef.current;\n if (!container) return;\n\n const nextSnapshot = createMessagesSnapshot();\n const previousSnapshot = previousSnapshotRef.current;\n\n if (!hasInitialScrollRef.current) {\n if (messages.length === 0) {\n previousSnapshotRef.current = nextSnapshot;\n return;\n }\n\n scrollToBottom(\"auto\");\n hasInitialScrollRef.current = true;\n previousSnapshotRef.current = createMessagesSnapshot();\n return;\n }\n\n if (!previousSnapshot) {\n previousSnapshotRef.current = nextSnapshot;\n return;\n }\n\n const hasPrependedMessages =\n nextSnapshot.messageCount > previousSnapshot.messageCount &&\n previousSnapshot.firstMessageKey !== null &&\n nextSnapshot.firstMessageKey !== previousSnapshot.firstMessageKey &&\n nextSnapshot.lastMessageKey === previousSnapshot.lastMessageKey;\n\n const hasAppendedMessages =\n nextSnapshot.messageCount > previousSnapshot.messageCount &&\n previousSnapshot.lastMessageKey !== null &&\n nextSnapshot.lastMessageKey !== previousSnapshot.lastMessageKey &&\n nextSnapshot.firstMessageKey === previousSnapshot.firstMessageKey;\n\n const hasReplacedHistory =\n nextSnapshot.messageCount > 0 &&\n previousSnapshot.messageCount > 0 &&\n nextSnapshot.firstMessageKey !== previousSnapshot.firstMessageKey &&\n nextSnapshot.lastMessageKey !== previousSnapshot.lastMessageKey;\n\n if (hasPrependedMessages) {\n preserveScrollPositionAfterPrepend(previousSnapshot);\n } else if (hasAppendedMessages) {\n if (shouldAutoScrollRef.current) {\n scrollToBottom(\"auto\");\n }\n } else if (hasReplacedHistory && shouldAutoScrollRef.current) {\n scrollToBottom(\"auto\");\n }\n\n previousSnapshotRef.current = createMessagesSnapshot();\n }, [isOpen, messages]);\n\n const handleSendMessage = () => {\n const message = inputValue.trim();\n if (!message) return;\n\n if (!authority) {\n onConnectWallet?.();\n return;\n }\n\n setInputValue(\"\");\n sendMessage(message);\n };\n\n const handleKeyDown = (e: React.KeyboardEvent) => {\n if (e.key === \"Enter\" && !e.shiftKey) {\n e.preventDefault();\n handleSendMessage();\n }\n };\n\n if (!isOpen) return null;\n\n return (\n <>\n <style>{`\n :root {\n --chat-bg: #ffffff;\n }\n .dark {\n --chat-bg: #15181D;\n }\n `}</style>\n\n <div\n className=\"fixed inset-0 z-[110]\"\n onClick={onClose}\n aria-hidden=\"true\"\n >\n <div\n onClick={(e) => e.stopPropagation()}\n role=\"dialog\"\n aria-modal=\"true\"\n aria-label={title}\n className={cn(\n \"fixed inset-0 flex flex-col overflow-hidden bg-white dark:bg-app-dark-400 lg:inset-auto lg:right-6 lg:top-[75px] lg:max-w-[340px] lg:max-h-[calc(100vh-90px)] lg:border lg:border-black/10 lg:shadow-2xl lg:dark:border-white/10\",\n \"lg:h-[800px]\",\n classNames?.container,\n )}\n style={{ background }}\n >\n {renderHeader ? (\n renderHeader()\n ) : (\n <div\n className={cn(\n \"flex items-center justify-between border-b border-black/10 px-4 py-4 dark:border-white/10 lg:px-6\",\n classNames?.header,\n )}\n >\n <div className=\"flex items-center gap-3\">\n <h2 className=\"text-lg font-semibold text-triad-dark-100 dark:text-white\">\n {title}\n </h2>\n <div className=\"flex items-center gap-1.5\">\n <div className=\"size-2 rounded-full bg-green-500 animate-pulse\" />\n <span className=\"text-xs text-app-gray-400 dark:text-white\">\n {onlineCount} online\n </span>\n </div>\n </div>\n <button onClick={onClose}>\n <X className=\"text-black dark:text-white\" size={18} />\n </button>\n </div>\n )}\n\n <div\n ref={messagesContainerRef}\n onScroll={handleMessagesScroll}\n className={cn(\n \"flex-1 overflow-y-auto space-y-4 px-4 py-4 lg:px-6\",\n classNames?.messages,\n )}\n style={{ overflowAnchor: \"none\" }}\n >\n {(() => {\n const messageOccurrences = new Map<string, number>();\n\n return messages.map((message) => {\n const baseKey = getMessageIdentity(message) ?? \"message\";\n const occurrence = messageOccurrences.get(baseKey) ?? 0;\n const messageKey = `${baseKey}:${occurrence}`;\n\n messageOccurrences.set(baseKey, occurrence + 1);\n\n return renderMessage ? (\n <React.Fragment key={messageKey}>\n {renderMessage(message)}\n </React.Fragment>\n ) : (\n <div\n key={messageKey}\n className={`flex flex-col gap-1 ${\n message.authority === authority\n ? \"items-end\"\n : \"items-start\"\n }`}\n >\n {message.authority !== authority && (\n <span className=\"max-w-[160px] truncate text-xs font-medium text-triad-dark-100 dark:text-white\">\n {message.name}\n </span>\n )}\n\n <div\n className={cn(\n \"max-w-[75%] rounded-2xl px-4 py-2.5\",\n message.authority === authority\n ? \"rounded-br-sm text-white\"\n : \"rounded-bl-sm bg-black/5 text-triad-dark-100 dark:bg-white/5 dark:text-white\",\n )}\n style={\n message.authority === authority\n ? { backgroundColor: primary }\n : undefined\n }\n >\n <p className=\"text-sm break-words\">{message.message}</p>\n </div>\n\n <div className=\"flex items-center gap-2 text-[10px] text-black opacity-50 dark:text-white\">\n <span>\n {formatPredictionsCount(message.predictionsCount)}\n </span>\n <span>{format(message.timestamp, \"HH:mm\")}</span>\n </div>\n </div>\n );\n });\n })()}\n </div>\n\n <div className=\"border-t border-black/10 px-4 py-4 dark:border-white/10 lg:px-6\">\n <div className=\"flex flex-col gap-2\">\n <div className=\"flex items-center gap-2\">\n <input\n value={inputValue}\n onChange={(e) => setInputValue(e.target.value)}\n onKeyDown={handleKeyDown}\n maxLength={CHAT_MAX_MESSAGE_LENGTH}\n placeholder=\"Digite sua mensagem...\"\n className={cn(\n \"flex-1 rounded-xl bg-black/5 px-4 py-2.5 text-sm text-app-gray-400 outline-none dark:bg-white/5 dark:text-white\",\n classNames?.input,\n )}\n />\n\n <button\n onClick={handleSendMessage}\n disabled={\n !inputValue.trim() || (!authority && !onConnectWallet)\n }\n className={cn(\n \"flex size-10 items-center justify-center rounded-xl transition-colors bg-app-dark-400\",\n classNames?.button,\n )}\n style={{\n opacity: inputValue.trim() ? 1 : 0.5,\n }}\n >\n <Send color={theme?.buttonSend.color} size={18} />\n </button>\n </div>\n </div>\n </div>\n </div>\n </div>\n </>\n );\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {jsx,jsxs,Fragment}from'react/jsx-runtime';import
|
|
1
|
+
import {jsx,jsxs,Fragment}from'react/jsx-runtime';import G,{useState,useRef,useLayoutEffect,useEffect}from'react';import {X,Send}from'lucide-react';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import V from'socket.io-client';import {format}from'date-fns';var oe=({label:t,variant:g="primary",className:d="",...p})=>jsx("button",{className:`px-4 py-2 rounded-lg font-medium transition-colors ${{primary:"bg-blue-600 text-white hover:bg-blue-700",secondary:"bg-gray-200 text-gray-800 hover:bg-gray-300"}[g]} ${d}`,...p,children:t});function y(...t){return twMerge(clsx(t))}var W=V("https://beta.triadfi.co",{path:"/socket.io",reconnection:true,reconnectionAttempts:5,reconnectionDelay:1e3,randomizationFactor:.5,timeout:5e3,transports:["websocket"]}),c=W;function E({authority:t,customerAuthority:g,signature:d}){let[p,l]=useState([]),[C,k]=useState(0),o=g?.trim();return useEffect(()=>{if(!o)return;let f=a=>{l(a);},h=a=>{l(m=>[...m,a]);},i=a=>{k(a.count);},b=()=>{let a=t?{authority:t,customerAuthority:o}:{customerAuthority:o};c.emit("chat:join",a,m=>{m?.error&&console.error("join error",m.error);});};return c.on("chat:history",f),c.on("chat:message",h),c.on("chat:users",i),c.on("connect",b),b(),()=>{c.off("chat:history",f),c.off("chat:message",h),c.off("chat:users",i),c.off("connect",b);}},[t,o]),{messages:p,onlineCount:C,sendMessage:f=>new Promise(h=>{if(!t||!o||!d){h({error:"INVALID_REQUEST"});return}c.emit("chat:message",{authority:t,customerAuthority:o,message:f,signature:d},i=>{i?.error&&console.error("send error",i.error),h(i??{error:"INTERNAL_ERROR"});});})}}var ee=200,te=80;function H(t){return t?[t.timestamp,t.authority??"anonymous",t.name,t.message].join(":"):null}function re(t,g=te){return t.scrollHeight-t.scrollTop-t.clientHeight<=g}function se(t){return `${t} ${t===1?"previsao":"previsoes"}`}function Me({isOpen:t,onClose:g,title:d="Live Chat",theme:p,classNames:l,renderMessage:C,renderHeader:k,authority:o,customerAuthority:L,onConnectWallet:f,signature:h}){let[i,b]=useState(""),a=useRef(null),m=useRef(false),M=useRef(true),v=useRef(null),P=p?.primaryColor||"#FF3D00",D=p?.background||"var(--chat-bg)",{sendMessage:B,onlineCount:_,messages:x}=E({authority:o||void 0,customerAuthority:L,signature:h}),S=()=>{let r=a.current;return {firstMessageKey:H(x[0]),lastMessageKey:H(x[x.length-1]),messageCount:x.length,scrollHeight:r?.scrollHeight??0}},K=(r="auto")=>{let e=a.current;e&&(e.scrollTo({top:e.scrollHeight,behavior:r}),M.current=true);},$=r=>{let e=a.current;if(!e)return;let s=e.scrollHeight-r.scrollHeight;s!==0&&(e.scrollTop+=s);},z=()=>{let r=a.current;r&&(M.current=re(r));};useLayoutEffect(()=>{if(!t){m.current=false,M.current=true,v.current=null;return}if(!a.current)return;let e=S(),s=v.current;if(!m.current){if(x.length===0){v.current=e;return}K("auto"),m.current=true,v.current=S();return}if(!s){v.current=e;return}let w=e.messageCount>s.messageCount&&s.firstMessageKey!==null&&e.firstMessageKey!==s.firstMessageKey&&e.lastMessageKey===s.lastMessageKey,N=e.messageCount>s.messageCount&&s.lastMessageKey!==null&&e.lastMessageKey!==s.lastMessageKey&&e.firstMessageKey===s.firstMessageKey,I=e.messageCount>0&&s.messageCount>0&&e.firstMessageKey!==s.firstMessageKey&&e.lastMessageKey!==s.lastMessageKey;w?$(s):N?M.current&&K("auto"):I&&M.current&&K("auto"),v.current=S();},[t,x]);let A=()=>{let r=i.trim();if(r){if(!o){f?.();return}b(""),B(r);}},F=r=>{r.key==="Enter"&&!r.shiftKey&&(r.preventDefault(),A());};return t?jsxs(Fragment,{children:[jsx("style",{children:`
|
|
2
2
|
:root {
|
|
3
3
|
--chat-bg: #ffffff;
|
|
4
4
|
}
|
|
5
5
|
.dark {
|
|
6
6
|
--chat-bg: #15181D;
|
|
7
7
|
}
|
|
8
|
-
`}),jsxs("div",{onClick:
|
|
8
|
+
`}),jsx("div",{className:"fixed inset-0 z-[110]",onClick:g,"aria-hidden":"true",children:jsxs("div",{onClick:r=>r.stopPropagation(),role:"dialog","aria-modal":"true","aria-label":d,className:y("fixed inset-0 flex flex-col overflow-hidden bg-white dark:bg-app-dark-400 lg:inset-auto lg:right-6 lg:top-[75px] lg:max-w-[340px] lg:max-h-[calc(100vh-90px)] lg:border lg:border-black/10 lg:shadow-2xl lg:dark:border-white/10","lg:h-[800px]",l?.container),style:{background:D},children:[k?k():jsxs("div",{className:y("flex items-center justify-between border-b border-black/10 px-4 py-4 dark:border-white/10 lg:px-6",l?.header),children:[jsxs("div",{className:"flex items-center gap-3",children:[jsx("h2",{className:"text-lg font-semibold text-triad-dark-100 dark:text-white",children:d}),jsxs("div",{className:"flex items-center gap-1.5",children:[jsx("div",{className:"size-2 rounded-full bg-green-500 animate-pulse"}),jsxs("span",{className:"text-xs text-app-gray-400 dark:text-white",children:[_," online"]})]})]}),jsx("button",{onClick:g,children:jsx(X,{className:"text-black dark:text-white",size:18})})]}),jsx("div",{ref:a,onScroll:z,className:y("flex-1 overflow-y-auto space-y-4 px-4 py-4 lg:px-6",l?.messages),style:{overflowAnchor:"none"},children:(()=>{let r=new Map;return x.map(e=>{let s=H(e)??"message",w=r.get(s)??0,N=`${s}:${w}`;return r.set(s,w+1),C?jsx(G.Fragment,{children:C(e)},N):jsxs("div",{className:`flex flex-col gap-1 ${e.authority===o?"items-end":"items-start"}`,children:[e.authority!==o&&jsx("span",{className:"max-w-[160px] truncate text-xs font-medium text-triad-dark-100 dark:text-white",children:e.name}),jsx("div",{className:y("max-w-[75%] rounded-2xl px-4 py-2.5",e.authority===o?"rounded-br-sm text-white":"rounded-bl-sm bg-black/5 text-triad-dark-100 dark:bg-white/5 dark:text-white"),style:e.authority===o?{backgroundColor:P}:void 0,children:jsx("p",{className:"text-sm break-words",children:e.message})}),jsxs("div",{className:"flex items-center gap-2 text-[10px] text-black opacity-50 dark:text-white",children:[jsx("span",{children:se(e.predictionsCount)}),jsx("span",{children:format(e.timestamp,"HH:mm")})]})]},N)})})()}),jsx("div",{className:"border-t border-black/10 px-4 py-4 dark:border-white/10 lg:px-6",children:jsx("div",{className:"flex flex-col gap-2",children:jsxs("div",{className:"flex items-center gap-2",children:[jsx("input",{value:i,onChange:r=>b(r.target.value),onKeyDown:F,maxLength:ee,placeholder:"Digite sua mensagem...",className:y("flex-1 rounded-xl bg-black/5 px-4 py-2.5 text-sm text-app-gray-400 outline-none dark:bg-white/5 dark:text-white",l?.input)}),jsx("button",{onClick:A,disabled:!i.trim()||!o&&!f,className:y("flex size-10 items-center justify-center rounded-xl transition-colors bg-app-dark-400",l?.button),style:{opacity:i.trim()?1:.5},children:jsx(Send,{color:p?.buttonSend.color,size:18})})]})})})]})})]}):null}export{oe as ButtonWidget,Me as LiveChat};//# sourceMappingURL=index.mjs.map
|
|
9
9
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/components/ButtonWidget.tsx","../src/utils/cn.ts","../src/utils/socket.ts","../src/hooks/useChat.ts","../src/components/LiveChat.tsx"],"names":["ButtonWidget","label","variant","className","props","jsx","cn","classes","twMerge","clsx","socket","io","socket_default","useChat","authority","customerAuthority","messages","setMessages","useState","onlineCount","setOnlineCount","normalizedCustomerAuthority","useEffect","handleHistory","msgs","handleMessage","msg","prev","handleUsers","data","joinChat","joinPayload","res","message","resolve","CHAT_MAX_MESSAGE_LENGTH","LiveChat","isOpen","onClose","title","theme","classNames","renderMessage","renderHeader","onConnectWallet","inputValue","setInputValue","messagesEndRef","useRef","primary","background","sendMessage","scrollToBottom","handleSendMessage","handleKeyDown","jsxs","Fragment","X","idx","React","format","Send"],"mappings":"+PASO,IAAMA,CAAAA,CAA4C,CAAC,CACxD,KAAA,CAAAC,CAAAA,CACA,OAAA,CAAAC,CAAAA,CAAU,SAAA,CACV,SAAA,CAAAC,EAAY,EAAA,CACZ,GAAGC,CACL,CAAA,GAQIC,GAAAA,CAAC,QAAA,CAAA,CACC,SAAA,CAAW,CAAA,mDAAA,EAPE,CACf,OAAA,CAAS,0CAAA,CACT,SAAA,CAAW,6CACb,CAAA,CAIyCH,CAAO,CAAC,CAAA,CAAA,EAAIC,CAAS,CAAA,CAAA,CACzD,GAAGC,CAAAA,CAEH,SAAAH,CAAAA,CACH,ECvBG,SAASK,CAAAA,CAAAA,GAAMC,CAAAA,CAAuB,CAC3C,OAAOC,OAAAA,CAAQC,IAAAA,CAAKF,CAAO,CAAC,CAC9B,CCJA,IAAMG,CAAAA,CAASC,CAAAA,CAAG,yBAAA,CAA2B,CAC3C,IAAA,CAAM,YAAA,CACN,YAAA,CAAc,IAAA,CACd,oBAAA,CAAsB,CAAA,CACtB,kBAAmB,GAAA,CACnB,mBAAA,CAAqB,EAAA,CACrB,OAAA,CAAS,GAAA,CACT,UAAA,CAAY,CAAC,WAAW,CAC1B,CAAC,CAAA,CAEMC,CAAAA,CAAQF,CAAAA,CCQR,SAASG,CAAAA,CAAQ,CACtB,SAAA,CAAAC,CAAAA,CACA,iBAAA,CAAAC,CACF,CAAA,CAGG,CACD,GAA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A,EAAMC,CAAAA,CAAc,CAAA,CAAE,MAAA,CAAO,KAAK,CAAA,CAC7C,SAAA,CAAWQ,CAAAA,CACX,SAAA,CAAWnB,CAAAA,CACX,WAAA,CAAY,wBAAA,CACZ,SAAA,CAAW7B,CAAAA,CACT,iHAAA,CACAmC,CAAAA,EAAY,KACd,CAAA,CACF,CAAA,CAEApC,GAAAA,CAAC,QAAA,CAAA,CACC,OAAA,CAASgD,CAAAA,CACT,QAAA,CAAU,CAACR,CAAAA,CAAW,IAAA,EAAK,EAAM,CAAC/B,GAAa,CAAC8B,CAAAA,CAChD,SAAA,CAAWtC,CAAAA,CACT,uFAAA,CACAmC,CAAAA,EAAY,MACd,CAAA,CACA,KAAA,CAAO,CACL,OAAA,CAASI,CAAAA,CAAW,IAAA,EAAK,CAAI,CAAA,CAAI,EACnC,EAEA,QAAA,CAAAxC,GAAAA,CAACwD,IAAAA,CAAA,CAAK,KAAA,CAAOrB,CAAAA,EAAO,UAAA,CAAW,KAAA,CAAO,IAAA,CAAM,EAAA,CAAI,CAAA,CAClD,CAAA,CAAA,CACF,CAAA,CACF,CAAA,CACF,CAAA,CAAA,CACF,CAAA,CAAA,CACF,EA/HkB,IAiItB","file":"index.mjs","sourcesContent":["'use client';\n\nimport React from 'react';\n\nexport interface ButtonWidgetProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n label: string;\n variant?: 'primary' | 'secondary';\n}\n\nexport const ButtonWidget: React.FC<ButtonWidgetProps> = ({ \n label, \n variant = 'primary', \n className = '',\n ...props \n}) => {\n const baseStyles = 'px-4 py-2 rounded-lg font-medium transition-colors';\n const variants = {\n primary: 'bg-blue-600 text-white hover:bg-blue-700',\n secondary: 'bg-gray-200 text-gray-800 hover:bg-gray-300'\n };\n\n return (\n <button \n className={`${baseStyles} ${variants[variant]} ${className}`}\n {...props}\n >\n {label}\n </button>\n );\n};\n","import type { ClassValue } from \"clsx\";\nimport { clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...classes: ClassValue[]) {\n return twMerge(clsx(classes));\n}\n","import io from \"socket.io-client\";\n\nconst socket = io(\"https://beta.triadfi.co\", {\n path: \"/socket.io\",\n reconnection: true,\n reconnectionAttempts: 5,\n reconnectionDelay: 1000,\n randomizationFactor: 0.5,\n timeout: 5000,\n transports: [\"websocket\"],\n});\n\nexport default socket;\n","import socket from \"@/utils/socket\";\nimport { useEffect, useState } from \"react\";\n\nexport interface ChatMessage {\n authority?: string;\n name: string;\n image: string;\n predictionsCount: number;\n message: string;\n timestamp: number;\n}\n\ninterface ChatUsersPayload {\n count: number;\n}\n\ninterface ChatResponse {\n error: string | null;\n}\n\nexport function useChat({\n authority,\n customerAuthority,\n}: {\n authority?: string;\n customerAuthority: string;\n}) {\n const [messages, setMessages] = useState<ChatMessage[]>([]);\n const [onlineCount, setOnlineCount] = useState(0);\n const normalizedCustomerAuthority = customerAuthority?.trim();\n\n useEffect(() => {\n if (!normalizedCustomerAuthority) return;\n\n const handleHistory = (msgs: ChatMessage[]) => {\n setMessages(msgs);\n };\n\n const handleMessage = (msg: ChatMessage) => {\n setMessages((prev) => [...prev, msg]);\n };\n\n const handleUsers = (data: ChatUsersPayload) => {\n setOnlineCount(data.count);\n };\n\n const joinChat = () => {\n const joinPayload = authority\n ? { authority, customerAuthority: normalizedCustomerAuthority }\n : { customerAuthority: normalizedCustomerAuthority };\n\n socket.emit(\"chat:join\", joinPayload, (res: ChatResponse) => {\n if (res?.error) {\n console.error(\"join error\", res.error);\n }\n });\n };\n\n socket.on(\"chat:history\", handleHistory);\n socket.on(\"chat:message\", handleMessage);\n socket.on(\"chat:users\", handleUsers);\n socket.on(\"connect\", joinChat);\n joinChat();\n\n return () => {\n socket.off(\"chat:history\", handleHistory);\n socket.off(\"chat:message\", handleMessage);\n socket.off(\"chat:users\", handleUsers);\n socket.off(\"connect\", joinChat);\n };\n }, [authority, normalizedCustomerAuthority]);\n\n const sendMessage = (message: string): Promise<ChatResponse> =>\n new Promise((resolve) => {\n if (!authority || !normalizedCustomerAuthority) {\n resolve({ error: \"INVALID_REQUEST\" });\n return;\n }\n\n socket.emit(\n \"chat:message\",\n { authority, customerAuthority: normalizedCustomerAuthority, message },\n (res: ChatResponse) => {\n if (res?.error) {\n console.error(\"send error\", res.error);\n }\n resolve(res ?? { error: \"INTERNAL_ERROR\" });\n },\n );\n });\n\n return {\n messages,\n onlineCount,\n sendMessage,\n };\n}\n","\"use client\";\n\nimport React, { useState, useRef, useEffect } from \"react\";\nimport { X, Send } from \"lucide-react\";\nimport { cn } from \"@/utils/cn\";\nimport { useChat, ChatMessage } from \"@/hooks/useChat\";\nimport { format } from \"date-fns\";\n\nexport interface LiveChatTheme {\n primaryColor?: string;\n background?: string;\n textColor?: string;\n buttonSend: {\n color: string;\n } \n}\n\nexport interface LiveChatClassNames {\n container?: string;\n header?: string;\n messages?: string;\n input?: string;\n button?: string;\n}\n\nexport interface LiveChatProps {\n isOpen: boolean;\n onClose: () => void;\n\n // ui\n title?: string;\n\n // customization\n theme?: LiveChatTheme;\n classNames?: LiveChatClassNames;\n\n // slots\n renderMessage?: (message: ChatMessage) => React.ReactNode;\n renderHeader?: () => React.ReactNode;\n authority: string | null;\n customerAuthority: string;\n onConnectWallet?: () => void;\n fallbackMessages?: {\n connectWallet: string;\n description: string;\n };\n}\n\nconst CHAT_MAX_MESSAGE_LENGTH = 200;\n\nexport function LiveChat({\n isOpen,\n onClose,\n title = \"Live Chat\",\n theme,\n classNames,\n renderMessage,\n renderHeader,\n authority,\n customerAuthority,\n onConnectWallet,\n}: LiveChatProps) {\n const [inputValue, setInputValue] = useState(\"\");\n const messagesEndRef = useRef<HTMLDivElement>(null);\n const primary = theme?.primaryColor || \"#FF3D00\";\n const background = theme?.background || \"var(--chat-bg)\";\n\n const { sendMessage, onlineCount, messages } = useChat({\n authority: authority || undefined,\n customerAuthority: customerAuthority,\n });\n\n const scrollToBottom = () => {\n messagesEndRef.current?.scrollIntoView({ behavior: \"smooth\" });\n };\n\n useEffect(() => {\n scrollToBottom();\n }, [messages]);\n\n const handleSendMessage = () => {\n const message = inputValue.trim();\n if (!message) return;\n\n if (!authority) {\n onConnectWallet?.();\n return;\n }\n\n setInputValue(\"\");\n sendMessage(message);\n };\n\n const handleKeyDown = (e: React.KeyboardEvent) => {\n if (e.key === \"Enter\" && !e.shiftKey) {\n e.preventDefault();\n handleSendMessage();\n }\n };\n\n if (!isOpen) return null;\n\n return (\n <>\n <style>{`\n :root {\n --chat-bg: #ffffff;\n }\n .dark {\n --chat-bg: #15181D;\n }\n `}</style>\n\n <div\n onClick={(e) => e.stopPropagation()}\n className={cn(\n \"fixed inset-0 z-[110] flex flex-col overflow-hidden bg-white dark:bg-app-dark-400 lg:inset-auto lg:right-6 lg:top-[75px] lg:max-w-[340px] lg:max-h-[calc(100vh-90px)] lg:border lg:border-black/10 lg:shadow-2xl lg:dark:border-white/10\",\n \"lg:h-[800px]\",\n classNames?.container,\n )}\n style={{ background }}\n >\n {renderHeader ? (\n renderHeader()\n ) : (\n <div\n className={cn(\n \"flex items-center justify-between border-b border-black/10 px-4 py-4 dark:border-white/10 lg:px-6\",\n classNames?.header,\n )}\n >\n <div className=\"flex items-center gap-3\">\n <h2 className=\"text-lg font-semibold text-triad-dark-100 dark:text-white\">\n {title}\n </h2>\n <div className=\"flex items-center gap-1.5\">\n <div className=\"size-2 rounded-full bg-green-500 animate-pulse\" />\n <span className=\"text-xs text-app-gray-400 dark:text-white\">\n {onlineCount} online\n </span>\n </div>\n </div>\n <button onClick={onClose}>\n <X className=\"text-black dark:text-white\" size={18} />\n </button>\n </div>\n )}\n\n <div\n className={cn(\n \"flex-1 overflow-y-auto space-y-4 px-4 py-4 lg:px-6\",\n classNames?.messages,\n )}\n >\n {messages.map((message, idx) =>\n renderMessage ? (\n <React.Fragment key={idx}>{renderMessage(message)}</React.Fragment>\n ) : (\n <div\n key={idx}\n className={`flex flex-col gap-1 ${\n message.authority === authority ? \"items-end\" : \"items-start\"\n }`}\n >\n {message.authority !== authority && (\n <span className=\"max-w-[160px] truncate text-xs font-medium text-triad-dark-100 dark:text-white\">\n {message.name}\n </span>\n )}\n\n <div\n className={cn(\n \"max-w-[75%] rounded-2xl px-4 py-2.5\",\n message.authority === authority\n ? \"rounded-br-sm text-white\"\n : \"rounded-bl-sm bg-black/5 text-triad-dark-100 dark:bg-white/5 dark:text-white\",\n )}\n style={\n message.authority === authority\n ? { backgroundColor: primary }\n : undefined\n }\n >\n <p className=\"text-sm break-words\">{message.message}</p>\n </div>\n\n <span className=\"text-[10px] text-black opacity-50 dark:text-white\">\n {format(message.timestamp, \"HH:mm\")}\n </span>\n </div>\n ),\n )}\n <div ref={messagesEndRef} />\n </div>\n\n <div className=\"border-t border-black/10 px-4 py-4 dark:border-white/10 lg:px-6\">\n <div className=\"flex flex-col gap-2\">\n <div className=\"flex items-center gap-2\">\n <input\n value={inputValue}\n onChange={(e) => setInputValue(e.target.value)}\n onKeyDown={handleKeyDown}\n maxLength={CHAT_MAX_MESSAGE_LENGTH}\n placeholder=\"Digite sua mensagem...\"\n className={cn(\n \"flex-1 rounded-xl bg-black/5 px-4 py-2.5 text-sm text-app-gray-400 outline-none dark:bg-white/5 dark:text-white\",\n classNames?.input,\n )}\n />\n\n <button\n onClick={handleSendMessage}\n disabled={!inputValue.trim() || (!authority && !onConnectWallet)}\n className={cn(\n \"flex size-10 items-center justify-center rounded-xl transition-colors bg-app-dark-400\",\n classNames?.button,\n )}\n style={{\n opacity: inputValue.trim() ? 1 : 0.5,\n }}\n >\n <Send color={theme?.buttonSend.color} size={18} />\n </button>\n </div>\n </div>\n </div>\n </div>\n </>\n );\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/components/ButtonWidget.tsx","../src/utils/cn.ts","../src/utils/socket.ts","../src/hooks/useChat.ts","../src/components/LiveChat.tsx"],"names":["ButtonWidget","label","variant","className","props","jsx","cn","classes","twMerge","clsx","socket","io","socket_default","useChat","authority","customerAuthority","signature","messages","setMessages","useState","onlineCount","setOnlineCount","normalizedCustomerAuthority","useEffect","handleHistory","msgs","handleMessage","msg","prev","handleUsers","data","joinChat","joinPayload","res","message","resolve","CHAT_MAX_MESSAGE_LENGTH","AUTO_SCROLL_THRESHOLD_PX","getMessageIdentity","isNearBottom","element","threshold","formatPredictionsCount","count","LiveChat","isOpen","onClose","title","theme","classNames","renderMessage","renderHeader","onConnectWallet","inputValue","setInputValue","messagesContainerRef","useRef","hasInitialScrollRef","shouldAutoScrollRef","previousSnapshotRef","primary","background","sendMessage","createMessagesSnapshot","container","scrollToBottom","behavior","preserveScrollPositionAfterPrepend","previousSnapshot","scrollHeightDelta","handleMessagesScroll","useLayoutEffect","nextSnapshot","hasPrependedMessages","hasAppendedMessages","hasReplacedHistory","handleSendMessage","handleKeyDown","e","jsxs","Fragment","X","messageOccurrences","baseKey","occurrence","messageKey","React","format","Send"],"mappings":"+QASO,IAAMA,GAA4C,CAAC,CACxD,MAAAC,CAAAA,CACA,OAAA,CAAAC,EAAU,SAAA,CACV,SAAA,CAAAC,EAAY,EAAA,CACZ,GAAGC,CACL,CAAA,GAQIC,GAAAA,CAAC,UACC,SAAA,CAAW,CAAA,mDAAA,EAPE,CACf,OAAA,CAAS,2CACT,SAAA,CAAW,6CACb,EAIyCH,CAAO,CAAC,IAAIC,CAAS,CAAA,CAAA,CACzD,GAAGC,CAAAA,CAEH,QAAA,CAAAH,EACH,ECvBG,SAASK,KAAMC,CAAAA,CAAuB,CAC3C,OAAOC,OAAAA,CAAQC,IAAAA,CAAKF,CAAO,CAAC,CAC9B,CCJA,IAAMG,CAAAA,CAASC,EAAG,yBAAA,CAA2B,CAC3C,KAAM,YAAA,CACN,YAAA,CAAc,KACd,oBAAA,CAAsB,CAAA,CACtB,kBAAmB,GAAA,CACnB,mBAAA,CAAqB,GACrB,OAAA,CAAS,GAAA,CACT,UAAA,CAAY,CAAC,WAAW,CAC1B,CAAC,EAEMC,CAAAA,CAAQF,CAAAA,CCQR,SAASG,CAAAA,CAAQ,CACtB,UAA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A,CAAO,IAAA,CAAM,EAAA,CAAI,CAAA,CAClD,GACF,CAAA,CACF,CAAA,CACF,GACF,CAAA,CACF,CAAA,CAAA,CACF,EA7JkB,IA+JtB","file":"index.mjs","sourcesContent":["'use client';\n\nimport React from 'react';\n\nexport interface ButtonWidgetProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n label: string;\n variant?: 'primary' | 'secondary';\n}\n\nexport const ButtonWidget: React.FC<ButtonWidgetProps> = ({ \n label, \n variant = 'primary', \n className = '',\n ...props \n}) => {\n const baseStyles = 'px-4 py-2 rounded-lg font-medium transition-colors';\n const variants = {\n primary: 'bg-blue-600 text-white hover:bg-blue-700',\n secondary: 'bg-gray-200 text-gray-800 hover:bg-gray-300'\n };\n\n return (\n <button \n className={`${baseStyles} ${variants[variant]} ${className}`}\n {...props}\n >\n {label}\n </button>\n );\n};\n","import type { ClassValue } from \"clsx\";\nimport { clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...classes: ClassValue[]) {\n return twMerge(clsx(classes));\n}\n","import io from \"socket.io-client\";\n\nconst socket = io(\"https://beta.triadfi.co\", {\n path: \"/socket.io\",\n reconnection: true,\n reconnectionAttempts: 5,\n reconnectionDelay: 1000,\n randomizationFactor: 0.5,\n timeout: 5000,\n transports: [\"websocket\"],\n});\n\nexport default socket;\n","import socket from \"@/utils/socket\";\nimport { useEffect, useState } from \"react\";\n\nexport interface ChatMessage {\n authority?: string;\n name: string;\n image: string;\n predictionsCount: number;\n message: string;\n timestamp: number;\n}\n\ninterface ChatUsersPayload {\n count: number;\n}\n\ninterface ChatResponse {\n error: string | null;\n}\n\nexport function useChat({\n authority,\n customerAuthority,\n signature,\n}: {\n authority?: string;\n customerAuthority: string;\n signature: string;\n}) {\n const [messages, setMessages] = useState<ChatMessage[]>([]);\n const [onlineCount, setOnlineCount] = useState(0);\n const normalizedCustomerAuthority = customerAuthority?.trim();\n\n useEffect(() => {\n if (!normalizedCustomerAuthority) return;\n\n const handleHistory = (msgs: ChatMessage[]) => {\n setMessages(msgs);\n };\n\n const handleMessage = (msg: ChatMessage) => {\n setMessages((prev) => [...prev, msg]);\n };\n\n const handleUsers = (data: ChatUsersPayload) => {\n setOnlineCount(data.count);\n };\n\n const joinChat = () => {\n const joinPayload = authority\n ? { authority, customerAuthority: normalizedCustomerAuthority }\n : { customerAuthority: normalizedCustomerAuthority };\n\n socket.emit(\"chat:join\", joinPayload, (res: ChatResponse) => {\n if (res?.error) {\n console.error(\"join error\", res.error);\n }\n });\n };\n\n socket.on(\"chat:history\", handleHistory);\n socket.on(\"chat:message\", handleMessage);\n socket.on(\"chat:users\", handleUsers);\n socket.on(\"connect\", joinChat);\n joinChat();\n\n return () => {\n socket.off(\"chat:history\", handleHistory);\n socket.off(\"chat:message\", handleMessage);\n socket.off(\"chat:users\", handleUsers);\n socket.off(\"connect\", joinChat);\n };\n }, [authority, normalizedCustomerAuthority]);\n\n const sendMessage = (message: string): Promise<ChatResponse> =>\n new Promise((resolve) => {\n if (!authority || !normalizedCustomerAuthority || !signature) {\n resolve({ error: \"INVALID_REQUEST\" });\n return;\n }\n\n socket.emit(\n \"chat:message\",\n {\n authority,\n customerAuthority: normalizedCustomerAuthority,\n message,\n signature,\n },\n (res: ChatResponse) => {\n if (res?.error) {\n console.error(\"send error\", res.error);\n }\n resolve(res ?? { error: \"INTERNAL_ERROR\" });\n },\n );\n });\n\n return {\n messages,\n onlineCount,\n sendMessage,\n };\n}\n","\"use client\";\n\nimport React, { useLayoutEffect, useRef, useState } from \"react\";\nimport { X, Send } from \"lucide-react\";\nimport { cn } from \"@/utils/cn\";\nimport { useChat, ChatMessage } from \"@/hooks/useChat\";\nimport { format } from \"date-fns\";\n\nexport interface LiveChatTheme {\n primaryColor?: string;\n background?: string;\n textColor?: string;\n buttonSend: {\n color: string;\n };\n}\n\nexport interface LiveChatClassNames {\n container?: string;\n header?: string;\n messages?: string;\n input?: string;\n button?: string;\n}\n\nexport interface LiveChatProps {\n isOpen: boolean;\n onClose: () => void;\n\n // ui\n title?: string;\n\n // customization\n theme?: LiveChatTheme;\n classNames?: LiveChatClassNames;\n\n // slots\n renderMessage?: (message: ChatMessage) => React.ReactNode;\n renderHeader?: () => React.ReactNode;\n authority: string | null;\n customerAuthority: string;\n onConnectWallet?: () => void;\n fallbackMessages?: {\n connectWallet: string;\n description: string;\n };\n signature: string;\n}\n\nconst CHAT_MAX_MESSAGE_LENGTH = 200;\nconst AUTO_SCROLL_THRESHOLD_PX = 80;\n\ninterface MessagesSnapshot {\n firstMessageKey: string | null;\n lastMessageKey: string | null;\n messageCount: number;\n scrollHeight: number;\n}\n\nfunction getMessageIdentity(message?: ChatMessage) {\n if (!message) return null;\n\n return [\n message.timestamp,\n message.authority ?? \"anonymous\",\n message.name,\n message.message,\n ].join(\":\");\n}\n\nfunction isNearBottom(\n element: HTMLDivElement,\n threshold = AUTO_SCROLL_THRESHOLD_PX,\n) {\n const distanceToBottom =\n element.scrollHeight - element.scrollTop - element.clientHeight;\n\n return distanceToBottom <= threshold;\n}\n\nfunction formatPredictionsCount(count: number) {\n return `${count} ${count === 1 ? \"previsao\" : \"previsoes\"}`;\n}\n\nexport function LiveChat({\n isOpen,\n onClose,\n title = \"Live Chat\",\n theme,\n classNames,\n renderMessage,\n renderHeader,\n authority,\n customerAuthority,\n onConnectWallet,\n signature,\n}: LiveChatProps) {\n const [inputValue, setInputValue] = useState(\"\");\n const messagesContainerRef = useRef<HTMLDivElement>(null);\n const hasInitialScrollRef = useRef(false);\n const shouldAutoScrollRef = useRef(true);\n const previousSnapshotRef = useRef<MessagesSnapshot | null>(null);\n const primary = theme?.primaryColor || \"#FF3D00\";\n const background = theme?.background || \"var(--chat-bg)\";\n\n const { sendMessage, onlineCount, messages } = useChat({\n authority: authority || undefined,\n customerAuthority: customerAuthority,\n signature,\n });\n\n const createMessagesSnapshot = (): MessagesSnapshot => {\n const container = messagesContainerRef.current;\n\n return {\n firstMessageKey: getMessageIdentity(messages[0]),\n lastMessageKey: getMessageIdentity(messages[messages.length - 1]),\n messageCount: messages.length,\n scrollHeight: container?.scrollHeight ?? 0,\n };\n };\n\n const scrollToBottom = (behavior: ScrollBehavior = \"auto\") => {\n const container = messagesContainerRef.current;\n if (!container) return;\n\n container.scrollTo({\n top: container.scrollHeight,\n behavior,\n });\n\n shouldAutoScrollRef.current = true;\n };\n\n const preserveScrollPositionAfterPrepend = (\n previousSnapshot: MessagesSnapshot,\n ) => {\n const container = messagesContainerRef.current;\n if (!container) return;\n\n const scrollHeightDelta =\n container.scrollHeight - previousSnapshot.scrollHeight;\n\n if (scrollHeightDelta !== 0) {\n container.scrollTop += scrollHeightDelta;\n }\n };\n\n const handleMessagesScroll = () => {\n const container = messagesContainerRef.current;\n if (!container) return;\n\n shouldAutoScrollRef.current = isNearBottom(container);\n };\n\n useLayoutEffect(() => {\n if (!isOpen) {\n hasInitialScrollRef.current = false;\n shouldAutoScrollRef.current = true;\n previousSnapshotRef.current = null;\n return;\n }\n\n const container = messagesContainerRef.current;\n if (!container) return;\n\n const nextSnapshot = createMessagesSnapshot();\n const previousSnapshot = previousSnapshotRef.current;\n\n if (!hasInitialScrollRef.current) {\n if (messages.length === 0) {\n previousSnapshotRef.current = nextSnapshot;\n return;\n }\n\n scrollToBottom(\"auto\");\n hasInitialScrollRef.current = true;\n previousSnapshotRef.current = createMessagesSnapshot();\n return;\n }\n\n if (!previousSnapshot) {\n previousSnapshotRef.current = nextSnapshot;\n return;\n }\n\n const hasPrependedMessages =\n nextSnapshot.messageCount > previousSnapshot.messageCount &&\n previousSnapshot.firstMessageKey !== null &&\n nextSnapshot.firstMessageKey !== previousSnapshot.firstMessageKey &&\n nextSnapshot.lastMessageKey === previousSnapshot.lastMessageKey;\n\n const hasAppendedMessages =\n nextSnapshot.messageCount > previousSnapshot.messageCount &&\n previousSnapshot.lastMessageKey !== null &&\n nextSnapshot.lastMessageKey !== previousSnapshot.lastMessageKey &&\n nextSnapshot.firstMessageKey === previousSnapshot.firstMessageKey;\n\n const hasReplacedHistory =\n nextSnapshot.messageCount > 0 &&\n previousSnapshot.messageCount > 0 &&\n nextSnapshot.firstMessageKey !== previousSnapshot.firstMessageKey &&\n nextSnapshot.lastMessageKey !== previousSnapshot.lastMessageKey;\n\n if (hasPrependedMessages) {\n preserveScrollPositionAfterPrepend(previousSnapshot);\n } else if (hasAppendedMessages) {\n if (shouldAutoScrollRef.current) {\n scrollToBottom(\"auto\");\n }\n } else if (hasReplacedHistory && shouldAutoScrollRef.current) {\n scrollToBottom(\"auto\");\n }\n\n previousSnapshotRef.current = createMessagesSnapshot();\n }, [isOpen, messages]);\n\n const handleSendMessage = () => {\n const message = inputValue.trim();\n if (!message) return;\n\n if (!authority) {\n onConnectWallet?.();\n return;\n }\n\n setInputValue(\"\");\n sendMessage(message);\n };\n\n const handleKeyDown = (e: React.KeyboardEvent) => {\n if (e.key === \"Enter\" && !e.shiftKey) {\n e.preventDefault();\n handleSendMessage();\n }\n };\n\n if (!isOpen) return null;\n\n return (\n <>\n <style>{`\n :root {\n --chat-bg: #ffffff;\n }\n .dark {\n --chat-bg: #15181D;\n }\n `}</style>\n\n <div\n className=\"fixed inset-0 z-[110]\"\n onClick={onClose}\n aria-hidden=\"true\"\n >\n <div\n onClick={(e) => e.stopPropagation()}\n role=\"dialog\"\n aria-modal=\"true\"\n aria-label={title}\n className={cn(\n \"fixed inset-0 flex flex-col overflow-hidden bg-white dark:bg-app-dark-400 lg:inset-auto lg:right-6 lg:top-[75px] lg:max-w-[340px] lg:max-h-[calc(100vh-90px)] lg:border lg:border-black/10 lg:shadow-2xl lg:dark:border-white/10\",\n \"lg:h-[800px]\",\n classNames?.container,\n )}\n style={{ background }}\n >\n {renderHeader ? (\n renderHeader()\n ) : (\n <div\n className={cn(\n \"flex items-center justify-between border-b border-black/10 px-4 py-4 dark:border-white/10 lg:px-6\",\n classNames?.header,\n )}\n >\n <div className=\"flex items-center gap-3\">\n <h2 className=\"text-lg font-semibold text-triad-dark-100 dark:text-white\">\n {title}\n </h2>\n <div className=\"flex items-center gap-1.5\">\n <div className=\"size-2 rounded-full bg-green-500 animate-pulse\" />\n <span className=\"text-xs text-app-gray-400 dark:text-white\">\n {onlineCount} online\n </span>\n </div>\n </div>\n <button onClick={onClose}>\n <X className=\"text-black dark:text-white\" size={18} />\n </button>\n </div>\n )}\n\n <div\n ref={messagesContainerRef}\n onScroll={handleMessagesScroll}\n className={cn(\n \"flex-1 overflow-y-auto space-y-4 px-4 py-4 lg:px-6\",\n classNames?.messages,\n )}\n style={{ overflowAnchor: \"none\" }}\n >\n {(() => {\n const messageOccurrences = new Map<string, number>();\n\n return messages.map((message) => {\n const baseKey = getMessageIdentity(message) ?? \"message\";\n const occurrence = messageOccurrences.get(baseKey) ?? 0;\n const messageKey = `${baseKey}:${occurrence}`;\n\n messageOccurrences.set(baseKey, occurrence + 1);\n\n return renderMessage ? (\n <React.Fragment key={messageKey}>\n {renderMessage(message)}\n </React.Fragment>\n ) : (\n <div\n key={messageKey}\n className={`flex flex-col gap-1 ${\n message.authority === authority\n ? \"items-end\"\n : \"items-start\"\n }`}\n >\n {message.authority !== authority && (\n <span className=\"max-w-[160px] truncate text-xs font-medium text-triad-dark-100 dark:text-white\">\n {message.name}\n </span>\n )}\n\n <div\n className={cn(\n \"max-w-[75%] rounded-2xl px-4 py-2.5\",\n message.authority === authority\n ? \"rounded-br-sm text-white\"\n : \"rounded-bl-sm bg-black/5 text-triad-dark-100 dark:bg-white/5 dark:text-white\",\n )}\n style={\n message.authority === authority\n ? { backgroundColor: primary }\n : undefined\n }\n >\n <p className=\"text-sm break-words\">{message.message}</p>\n </div>\n\n <div className=\"flex items-center gap-2 text-[10px] text-black opacity-50 dark:text-white\">\n <span>\n {formatPredictionsCount(message.predictionsCount)}\n </span>\n <span>{format(message.timestamp, \"HH:mm\")}</span>\n </div>\n </div>\n );\n });\n })()}\n </div>\n\n <div className=\"border-t border-black/10 px-4 py-4 dark:border-white/10 lg:px-6\">\n <div className=\"flex flex-col gap-2\">\n <div className=\"flex items-center gap-2\">\n <input\n value={inputValue}\n onChange={(e) => setInputValue(e.target.value)}\n onKeyDown={handleKeyDown}\n maxLength={CHAT_MAX_MESSAGE_LENGTH}\n placeholder=\"Digite sua mensagem...\"\n className={cn(\n \"flex-1 rounded-xl bg-black/5 px-4 py-2.5 text-sm text-app-gray-400 outline-none dark:bg-white/5 dark:text-white\",\n classNames?.input,\n )}\n />\n\n <button\n onClick={handleSendMessage}\n disabled={\n !inputValue.trim() || (!authority && !onConnectWallet)\n }\n className={cn(\n \"flex size-10 items-center justify-center rounded-xl transition-colors bg-app-dark-400\",\n classNames?.button,\n )}\n style={{\n opacity: inputValue.trim() ? 1 : 0.5,\n }}\n >\n <Send color={theme?.buttonSend.color} size={18} />\n </button>\n </div>\n </div>\n </div>\n </div>\n </div>\n </>\n );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import React, {
|
|
3
|
+
import React, { useLayoutEffect, useRef, useState } from "react";
|
|
4
4
|
import { X, Send } from "lucide-react";
|
|
5
5
|
import { cn } from "@/utils/cn";
|
|
6
6
|
import { useChat, ChatMessage } from "@/hooks/useChat";
|
|
@@ -12,7 +12,7 @@ export interface LiveChatTheme {
|
|
|
12
12
|
textColor?: string;
|
|
13
13
|
buttonSend: {
|
|
14
14
|
color: string;
|
|
15
|
-
}
|
|
15
|
+
};
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export interface LiveChatClassNames {
|
|
@@ -44,9 +44,43 @@ export interface LiveChatProps {
|
|
|
44
44
|
connectWallet: string;
|
|
45
45
|
description: string;
|
|
46
46
|
};
|
|
47
|
+
signature: string;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
const CHAT_MAX_MESSAGE_LENGTH = 200;
|
|
51
|
+
const AUTO_SCROLL_THRESHOLD_PX = 80;
|
|
52
|
+
|
|
53
|
+
interface MessagesSnapshot {
|
|
54
|
+
firstMessageKey: string | null;
|
|
55
|
+
lastMessageKey: string | null;
|
|
56
|
+
messageCount: number;
|
|
57
|
+
scrollHeight: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function getMessageIdentity(message?: ChatMessage) {
|
|
61
|
+
if (!message) return null;
|
|
62
|
+
|
|
63
|
+
return [
|
|
64
|
+
message.timestamp,
|
|
65
|
+
message.authority ?? "anonymous",
|
|
66
|
+
message.name,
|
|
67
|
+
message.message,
|
|
68
|
+
].join(":");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isNearBottom(
|
|
72
|
+
element: HTMLDivElement,
|
|
73
|
+
threshold = AUTO_SCROLL_THRESHOLD_PX,
|
|
74
|
+
) {
|
|
75
|
+
const distanceToBottom =
|
|
76
|
+
element.scrollHeight - element.scrollTop - element.clientHeight;
|
|
77
|
+
|
|
78
|
+
return distanceToBottom <= threshold;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function formatPredictionsCount(count: number) {
|
|
82
|
+
return `${count} ${count === 1 ? "previsao" : "previsoes"}`;
|
|
83
|
+
}
|
|
50
84
|
|
|
51
85
|
export function LiveChat({
|
|
52
86
|
isOpen,
|
|
@@ -59,24 +93,127 @@ export function LiveChat({
|
|
|
59
93
|
authority,
|
|
60
94
|
customerAuthority,
|
|
61
95
|
onConnectWallet,
|
|
96
|
+
signature,
|
|
62
97
|
}: LiveChatProps) {
|
|
63
98
|
const [inputValue, setInputValue] = useState("");
|
|
64
|
-
const
|
|
99
|
+
const messagesContainerRef = useRef<HTMLDivElement>(null);
|
|
100
|
+
const hasInitialScrollRef = useRef(false);
|
|
101
|
+
const shouldAutoScrollRef = useRef(true);
|
|
102
|
+
const previousSnapshotRef = useRef<MessagesSnapshot | null>(null);
|
|
65
103
|
const primary = theme?.primaryColor || "#FF3D00";
|
|
66
104
|
const background = theme?.background || "var(--chat-bg)";
|
|
67
105
|
|
|
68
106
|
const { sendMessage, onlineCount, messages } = useChat({
|
|
69
107
|
authority: authority || undefined,
|
|
70
108
|
customerAuthority: customerAuthority,
|
|
109
|
+
signature,
|
|
71
110
|
});
|
|
72
111
|
|
|
73
|
-
const
|
|
74
|
-
|
|
112
|
+
const createMessagesSnapshot = (): MessagesSnapshot => {
|
|
113
|
+
const container = messagesContainerRef.current;
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
firstMessageKey: getMessageIdentity(messages[0]),
|
|
117
|
+
lastMessageKey: getMessageIdentity(messages[messages.length - 1]),
|
|
118
|
+
messageCount: messages.length,
|
|
119
|
+
scrollHeight: container?.scrollHeight ?? 0,
|
|
120
|
+
};
|
|
75
121
|
};
|
|
76
122
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
123
|
+
const scrollToBottom = (behavior: ScrollBehavior = "auto") => {
|
|
124
|
+
const container = messagesContainerRef.current;
|
|
125
|
+
if (!container) return;
|
|
126
|
+
|
|
127
|
+
container.scrollTo({
|
|
128
|
+
top: container.scrollHeight,
|
|
129
|
+
behavior,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
shouldAutoScrollRef.current = true;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const preserveScrollPositionAfterPrepend = (
|
|
136
|
+
previousSnapshot: MessagesSnapshot,
|
|
137
|
+
) => {
|
|
138
|
+
const container = messagesContainerRef.current;
|
|
139
|
+
if (!container) return;
|
|
140
|
+
|
|
141
|
+
const scrollHeightDelta =
|
|
142
|
+
container.scrollHeight - previousSnapshot.scrollHeight;
|
|
143
|
+
|
|
144
|
+
if (scrollHeightDelta !== 0) {
|
|
145
|
+
container.scrollTop += scrollHeightDelta;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const handleMessagesScroll = () => {
|
|
150
|
+
const container = messagesContainerRef.current;
|
|
151
|
+
if (!container) return;
|
|
152
|
+
|
|
153
|
+
shouldAutoScrollRef.current = isNearBottom(container);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
useLayoutEffect(() => {
|
|
157
|
+
if (!isOpen) {
|
|
158
|
+
hasInitialScrollRef.current = false;
|
|
159
|
+
shouldAutoScrollRef.current = true;
|
|
160
|
+
previousSnapshotRef.current = null;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const container = messagesContainerRef.current;
|
|
165
|
+
if (!container) return;
|
|
166
|
+
|
|
167
|
+
const nextSnapshot = createMessagesSnapshot();
|
|
168
|
+
const previousSnapshot = previousSnapshotRef.current;
|
|
169
|
+
|
|
170
|
+
if (!hasInitialScrollRef.current) {
|
|
171
|
+
if (messages.length === 0) {
|
|
172
|
+
previousSnapshotRef.current = nextSnapshot;
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
scrollToBottom("auto");
|
|
177
|
+
hasInitialScrollRef.current = true;
|
|
178
|
+
previousSnapshotRef.current = createMessagesSnapshot();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (!previousSnapshot) {
|
|
183
|
+
previousSnapshotRef.current = nextSnapshot;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const hasPrependedMessages =
|
|
188
|
+
nextSnapshot.messageCount > previousSnapshot.messageCount &&
|
|
189
|
+
previousSnapshot.firstMessageKey !== null &&
|
|
190
|
+
nextSnapshot.firstMessageKey !== previousSnapshot.firstMessageKey &&
|
|
191
|
+
nextSnapshot.lastMessageKey === previousSnapshot.lastMessageKey;
|
|
192
|
+
|
|
193
|
+
const hasAppendedMessages =
|
|
194
|
+
nextSnapshot.messageCount > previousSnapshot.messageCount &&
|
|
195
|
+
previousSnapshot.lastMessageKey !== null &&
|
|
196
|
+
nextSnapshot.lastMessageKey !== previousSnapshot.lastMessageKey &&
|
|
197
|
+
nextSnapshot.firstMessageKey === previousSnapshot.firstMessageKey;
|
|
198
|
+
|
|
199
|
+
const hasReplacedHistory =
|
|
200
|
+
nextSnapshot.messageCount > 0 &&
|
|
201
|
+
previousSnapshot.messageCount > 0 &&
|
|
202
|
+
nextSnapshot.firstMessageKey !== previousSnapshot.firstMessageKey &&
|
|
203
|
+
nextSnapshot.lastMessageKey !== previousSnapshot.lastMessageKey;
|
|
204
|
+
|
|
205
|
+
if (hasPrependedMessages) {
|
|
206
|
+
preserveScrollPositionAfterPrepend(previousSnapshot);
|
|
207
|
+
} else if (hasAppendedMessages) {
|
|
208
|
+
if (shouldAutoScrollRef.current) {
|
|
209
|
+
scrollToBottom("auto");
|
|
210
|
+
}
|
|
211
|
+
} else if (hasReplacedHistory && shouldAutoScrollRef.current) {
|
|
212
|
+
scrollToBottom("auto");
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
previousSnapshotRef.current = createMessagesSnapshot();
|
|
216
|
+
}, [isOpen, messages]);
|
|
80
217
|
|
|
81
218
|
const handleSendMessage = () => {
|
|
82
219
|
const message = inputValue.trim();
|
|
@@ -112,115 +249,145 @@ export function LiveChat({
|
|
|
112
249
|
`}</style>
|
|
113
250
|
|
|
114
251
|
<div
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
"lg:h-[800px]",
|
|
119
|
-
classNames?.container,
|
|
120
|
-
)}
|
|
121
|
-
style={{ background }}
|
|
252
|
+
className="fixed inset-0 z-[110]"
|
|
253
|
+
onClick={onClose}
|
|
254
|
+
aria-hidden="true"
|
|
122
255
|
>
|
|
123
|
-
{renderHeader ? (
|
|
124
|
-
renderHeader()
|
|
125
|
-
) : (
|
|
126
|
-
<div
|
|
127
|
-
className={cn(
|
|
128
|
-
"flex items-center justify-between border-b border-black/10 px-4 py-4 dark:border-white/10 lg:px-6",
|
|
129
|
-
classNames?.header,
|
|
130
|
-
)}
|
|
131
|
-
>
|
|
132
|
-
<div className="flex items-center gap-3">
|
|
133
|
-
<h2 className="text-lg font-semibold text-triad-dark-100 dark:text-white">
|
|
134
|
-
{title}
|
|
135
|
-
</h2>
|
|
136
|
-
<div className="flex items-center gap-1.5">
|
|
137
|
-
<div className="size-2 rounded-full bg-green-500 animate-pulse" />
|
|
138
|
-
<span className="text-xs text-app-gray-400 dark:text-white">
|
|
139
|
-
{onlineCount} online
|
|
140
|
-
</span>
|
|
141
|
-
</div>
|
|
142
|
-
</div>
|
|
143
|
-
<button onClick={onClose}>
|
|
144
|
-
<X className="text-black dark:text-white" size={18} />
|
|
145
|
-
</button>
|
|
146
|
-
</div>
|
|
147
|
-
)}
|
|
148
|
-
|
|
149
256
|
<div
|
|
257
|
+
onClick={(e) => e.stopPropagation()}
|
|
258
|
+
role="dialog"
|
|
259
|
+
aria-modal="true"
|
|
260
|
+
aria-label={title}
|
|
150
261
|
className={cn(
|
|
151
|
-
"flex-
|
|
152
|
-
|
|
262
|
+
"fixed inset-0 flex flex-col overflow-hidden bg-white dark:bg-app-dark-400 lg:inset-auto lg:right-6 lg:top-[75px] lg:max-w-[340px] lg:max-h-[calc(100vh-90px)] lg:border lg:border-black/10 lg:shadow-2xl lg:dark:border-white/10",
|
|
263
|
+
"lg:h-[800px]",
|
|
264
|
+
classNames?.container,
|
|
153
265
|
)}
|
|
266
|
+
style={{ background }}
|
|
154
267
|
>
|
|
155
|
-
{
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
>
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
268
|
+
{renderHeader ? (
|
|
269
|
+
renderHeader()
|
|
270
|
+
) : (
|
|
271
|
+
<div
|
|
272
|
+
className={cn(
|
|
273
|
+
"flex items-center justify-between border-b border-black/10 px-4 py-4 dark:border-white/10 lg:px-6",
|
|
274
|
+
classNames?.header,
|
|
275
|
+
)}
|
|
276
|
+
>
|
|
277
|
+
<div className="flex items-center gap-3">
|
|
278
|
+
<h2 className="text-lg font-semibold text-triad-dark-100 dark:text-white">
|
|
279
|
+
{title}
|
|
280
|
+
</h2>
|
|
281
|
+
<div className="flex items-center gap-1.5">
|
|
282
|
+
<div className="size-2 rounded-full bg-green-500 animate-pulse" />
|
|
283
|
+
<span className="text-xs text-app-gray-400 dark:text-white">
|
|
284
|
+
{onlineCount} online
|
|
168
285
|
</span>
|
|
169
|
-
|
|
286
|
+
</div>
|
|
287
|
+
</div>
|
|
288
|
+
<button onClick={onClose}>
|
|
289
|
+
<X className="text-black dark:text-white" size={18} />
|
|
290
|
+
</button>
|
|
291
|
+
</div>
|
|
292
|
+
)}
|
|
170
293
|
|
|
171
|
-
|
|
294
|
+
<div
|
|
295
|
+
ref={messagesContainerRef}
|
|
296
|
+
onScroll={handleMessagesScroll}
|
|
297
|
+
className={cn(
|
|
298
|
+
"flex-1 overflow-y-auto space-y-4 px-4 py-4 lg:px-6",
|
|
299
|
+
classNames?.messages,
|
|
300
|
+
)}
|
|
301
|
+
style={{ overflowAnchor: "none" }}
|
|
302
|
+
>
|
|
303
|
+
{(() => {
|
|
304
|
+
const messageOccurrences = new Map<string, number>();
|
|
305
|
+
|
|
306
|
+
return messages.map((message) => {
|
|
307
|
+
const baseKey = getMessageIdentity(message) ?? "message";
|
|
308
|
+
const occurrence = messageOccurrences.get(baseKey) ?? 0;
|
|
309
|
+
const messageKey = `${baseKey}:${occurrence}`;
|
|
310
|
+
|
|
311
|
+
messageOccurrences.set(baseKey, occurrence + 1);
|
|
312
|
+
|
|
313
|
+
return renderMessage ? (
|
|
314
|
+
<React.Fragment key={messageKey}>
|
|
315
|
+
{renderMessage(message)}
|
|
316
|
+
</React.Fragment>
|
|
317
|
+
) : (
|
|
318
|
+
<div
|
|
319
|
+
key={messageKey}
|
|
320
|
+
className={`flex flex-col gap-1 ${
|
|
321
|
+
message.authority === authority
|
|
322
|
+
? "items-end"
|
|
323
|
+
: "items-start"
|
|
324
|
+
}`}
|
|
325
|
+
>
|
|
326
|
+
{message.authority !== authority && (
|
|
327
|
+
<span className="max-w-[160px] truncate text-xs font-medium text-triad-dark-100 dark:text-white">
|
|
328
|
+
{message.name}
|
|
329
|
+
</span>
|
|
330
|
+
)}
|
|
331
|
+
|
|
332
|
+
<div
|
|
333
|
+
className={cn(
|
|
334
|
+
"max-w-[75%] rounded-2xl px-4 py-2.5",
|
|
335
|
+
message.authority === authority
|
|
336
|
+
? "rounded-br-sm text-white"
|
|
337
|
+
: "rounded-bl-sm bg-black/5 text-triad-dark-100 dark:bg-white/5 dark:text-white",
|
|
338
|
+
)}
|
|
339
|
+
style={
|
|
340
|
+
message.authority === authority
|
|
341
|
+
? { backgroundColor: primary }
|
|
342
|
+
: undefined
|
|
343
|
+
}
|
|
344
|
+
>
|
|
345
|
+
<p className="text-sm break-words">{message.message}</p>
|
|
346
|
+
</div>
|
|
347
|
+
|
|
348
|
+
<div className="flex items-center gap-2 text-[10px] text-black opacity-50 dark:text-white">
|
|
349
|
+
<span>
|
|
350
|
+
{formatPredictionsCount(message.predictionsCount)}
|
|
351
|
+
</span>
|
|
352
|
+
<span>{format(message.timestamp, "HH:mm")}</span>
|
|
353
|
+
</div>
|
|
354
|
+
</div>
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
})()}
|
|
358
|
+
</div>
|
|
359
|
+
|
|
360
|
+
<div className="border-t border-black/10 px-4 py-4 dark:border-white/10 lg:px-6">
|
|
361
|
+
<div className="flex flex-col gap-2">
|
|
362
|
+
<div className="flex items-center gap-2">
|
|
363
|
+
<input
|
|
364
|
+
value={inputValue}
|
|
365
|
+
onChange={(e) => setInputValue(e.target.value)}
|
|
366
|
+
onKeyDown={handleKeyDown}
|
|
367
|
+
maxLength={CHAT_MAX_MESSAGE_LENGTH}
|
|
368
|
+
placeholder="Digite sua mensagem..."
|
|
172
369
|
className={cn(
|
|
173
|
-
"
|
|
174
|
-
|
|
175
|
-
? "rounded-br-sm text-white"
|
|
176
|
-
: "rounded-bl-sm bg-black/5 text-triad-dark-100 dark:bg-white/5 dark:text-white",
|
|
370
|
+
"flex-1 rounded-xl bg-black/5 px-4 py-2.5 text-sm text-app-gray-400 outline-none dark:bg-white/5 dark:text-white",
|
|
371
|
+
classNames?.input,
|
|
177
372
|
)}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
373
|
+
/>
|
|
374
|
+
|
|
375
|
+
<button
|
|
376
|
+
onClick={handleSendMessage}
|
|
377
|
+
disabled={
|
|
378
|
+
!inputValue.trim() || (!authority && !onConnectWallet)
|
|
182
379
|
}
|
|
380
|
+
className={cn(
|
|
381
|
+
"flex size-10 items-center justify-center rounded-xl transition-colors bg-app-dark-400",
|
|
382
|
+
classNames?.button,
|
|
383
|
+
)}
|
|
384
|
+
style={{
|
|
385
|
+
opacity: inputValue.trim() ? 1 : 0.5,
|
|
386
|
+
}}
|
|
183
387
|
>
|
|
184
|
-
<
|
|
185
|
-
</
|
|
186
|
-
|
|
187
|
-
<span className="text-[10px] text-black opacity-50 dark:text-white">
|
|
188
|
-
{format(message.timestamp, "HH:mm")}
|
|
189
|
-
</span>
|
|
388
|
+
<Send color={theme?.buttonSend.color} size={18} />
|
|
389
|
+
</button>
|
|
190
390
|
</div>
|
|
191
|
-
),
|
|
192
|
-
)}
|
|
193
|
-
<div ref={messagesEndRef} />
|
|
194
|
-
</div>
|
|
195
|
-
|
|
196
|
-
<div className="border-t border-black/10 px-4 py-4 dark:border-white/10 lg:px-6">
|
|
197
|
-
<div className="flex flex-col gap-2">
|
|
198
|
-
<div className="flex items-center gap-2">
|
|
199
|
-
<input
|
|
200
|
-
value={inputValue}
|
|
201
|
-
onChange={(e) => setInputValue(e.target.value)}
|
|
202
|
-
onKeyDown={handleKeyDown}
|
|
203
|
-
maxLength={CHAT_MAX_MESSAGE_LENGTH}
|
|
204
|
-
placeholder="Digite sua mensagem..."
|
|
205
|
-
className={cn(
|
|
206
|
-
"flex-1 rounded-xl bg-black/5 px-4 py-2.5 text-sm text-app-gray-400 outline-none dark:bg-white/5 dark:text-white",
|
|
207
|
-
classNames?.input,
|
|
208
|
-
)}
|
|
209
|
-
/>
|
|
210
|
-
|
|
211
|
-
<button
|
|
212
|
-
onClick={handleSendMessage}
|
|
213
|
-
disabled={!inputValue.trim() || (!authority && !onConnectWallet)}
|
|
214
|
-
className={cn(
|
|
215
|
-
"flex size-10 items-center justify-center rounded-xl transition-colors bg-app-dark-400",
|
|
216
|
-
classNames?.button,
|
|
217
|
-
)}
|
|
218
|
-
style={{
|
|
219
|
-
opacity: inputValue.trim() ? 1 : 0.5,
|
|
220
|
-
}}
|
|
221
|
-
>
|
|
222
|
-
<Send color={theme?.buttonSend.color} size={18} />
|
|
223
|
-
</button>
|
|
224
391
|
</div>
|
|
225
392
|
</div>
|
|
226
393
|
</div>
|
package/src/hooks/useChat.ts
CHANGED
|
@@ -21,9 +21,11 @@ interface ChatResponse {
|
|
|
21
21
|
export function useChat({
|
|
22
22
|
authority,
|
|
23
23
|
customerAuthority,
|
|
24
|
+
signature,
|
|
24
25
|
}: {
|
|
25
26
|
authority?: string;
|
|
26
27
|
customerAuthority: string;
|
|
28
|
+
signature: string;
|
|
27
29
|
}) {
|
|
28
30
|
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
|
29
31
|
const [onlineCount, setOnlineCount] = useState(0);
|
|
@@ -72,14 +74,19 @@ export function useChat({
|
|
|
72
74
|
|
|
73
75
|
const sendMessage = (message: string): Promise<ChatResponse> =>
|
|
74
76
|
new Promise((resolve) => {
|
|
75
|
-
if (!authority || !normalizedCustomerAuthority) {
|
|
77
|
+
if (!authority || !normalizedCustomerAuthority || !signature) {
|
|
76
78
|
resolve({ error: "INVALID_REQUEST" });
|
|
77
79
|
return;
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
socket.emit(
|
|
81
83
|
"chat:message",
|
|
82
|
-
{
|
|
84
|
+
{
|
|
85
|
+
authority,
|
|
86
|
+
customerAuthority: normalizedCustomerAuthority,
|
|
87
|
+
message,
|
|
88
|
+
signature,
|
|
89
|
+
},
|
|
83
90
|
(res: ChatResponse) => {
|
|
84
91
|
if (res?.error) {
|
|
85
92
|
console.error("send error", res.error);
|