From 6bcad79475f88d1071325c0a0921d51681bbee8f Mon Sep 17 00:00:00 2001 From: Mekan1206 Date: Sat, 15 Aug 2026 17:51:20 +0500 Subject: [PATCH] some changes --- .env.example | 4 + package-lock.json | 248 ++++++++++++++- package.json | 6 + src/App.jsx | 4 +- src/app/api/chatApi.js | 55 ++++ src/app/store.js | 52 +++ .../ChatWidget/ChatWidget.module.scss | 167 ++++++++++ src/components/ChatWidget/index.jsx | 296 ++++++++++++++++++ src/i18n/locales/en.js | 8 + src/i18n/locales/ru.js | 8 + src/i18n/locales/tm.js | 8 + src/utils/echo.js | 80 +++++ 12 files changed, 933 insertions(+), 3 deletions(-) create mode 100644 src/app/api/chatApi.js create mode 100644 src/components/ChatWidget/ChatWidget.module.scss create mode 100644 src/components/ChatWidget/index.jsx create mode 100644 src/utils/echo.js diff --git a/.env.example b/.env.example index 58fd94e..b25ecb4 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,6 @@ VITE_API_URL=https://mm.com.tm/api/v1/ VITE_API_TOKEN=hello-mf-s +VITE_REVERB_APP_KEY= +VITE_REVERB_HOST=127.0.0.1 +VITE_REVERB_PORT=8080 +VITE_REVERB_SCHEME=http diff --git a/package-lock.json b/package-lock.json index 56d0ba5..ac8aa2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,14 +10,20 @@ "dependencies": { "@reduxjs/toolkit": "^2.5.0", "antd": "^5.22.7", +<<<<<<< Updated upstream "express": "^5.2.1", +======= + "axios": "^1.15.2", +>>>>>>> Stashed changes "framer-motion": "^12.5.0", "i18next": "^24.2.1", "i18next-browser-languagedetector": "^8.0.2", "imask": "^7.6.1", + "laravel-echo": "^2.3.4", "lodash": "^4.17.21", "lucide-react": "^0.469.0", "nprogress": "^0.2.0", + "pusher-js": "^8.5.0", "react": "^18.3.1", "react-dom": "^18.3.1", "react-i18next": "^15.4.0", @@ -1506,6 +1512,12 @@ "win32" ] }, + "node_modules/@socket.io/component-emitter": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.1.2.tgz", + "integrity": "sha512-9BCxFwvbGg/RsZK9tjXd8s4UcwR0MWeFQ1XEKIQVVvAGJyINdrqKMcTRyLoK8Rse1GjzLV9cwjWV1olXRWEXVA==", + "peer": true + }, "node_modules/@types/babel__core": { "version": "7.20.5", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz", @@ -1879,6 +1891,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/available-typed-arrays": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz", @@ -1894,6 +1911,16 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/axios": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.15.2.tgz", + "integrity": "sha512-wLrXxPtcrPTsNlJmKjkPnNPK2Ihe0hn0wGSaTEiHRPxwjvJwT3hKmXF4dpqxmPO9SoNb2FsYXj/xEo0gHN+D5A==", + "dependencies": { + "follow-redirects": "^1.15.11", + "form-data": "^4.0.5", + "proxy-from-env": "^2.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -2101,6 +2128,28 @@ "integrity": "sha512-twmVoizEW7ylZSN32OgKdXRmo1qg+wT5/6C3xu5b9QsWzSFAhHLn2xd8ro0diCsKfCj1RdaTP/nrcW+vAoQPIw==", "dev": true }, +<<<<<<< Updated upstream +======= + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true, + "optional": true, + "peer": true + }, +>>>>>>> Stashed changes "node_modules/compute-scroll-into-view": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/compute-scroll-into-view/-/compute-scroll-into-view-3.1.0.tgz", @@ -2255,7 +2304,10 @@ "version": "4.4.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", +<<<<<<< Updated upstream "license": "MIT", +======= +>>>>>>> Stashed changes "dependencies": { "ms": "^2.1.3" }, @@ -2308,6 +2360,7 @@ "url": "https://github.com/sponsors/ljharb" } }, +<<<<<<< Updated upstream "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -2315,6 +2368,14 @@ "license": "MIT", "engines": { "node": ">= 0.8" +======= + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" +>>>>>>> Stashed changes } }, "node_modules/doctrine": { @@ -2354,6 +2415,7 @@ "integrity": "sha512-CjVQyG7n7Sr+eBXE86HIulnL5N8xZY1sgmOPGuq/F0Rr0FJq63lg0kEtOIDfZBk44FnDLf6FUJ+dsJcuiUDdDQ==", "dev": true }, +<<<<<<< Updated upstream "node_modules/encodeurl": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", @@ -2361,6 +2423,28 @@ "license": "MIT", "engines": { "node": ">= 0.8" +======= + "node_modules/engine.io-client": { + "version": "6.6.4", + "resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.6.4.tgz", + "integrity": "sha512-+kjUJnZGwzewFDw951CDWcwj35vMNf2fcj7xQWOctq1F2i1jkDdVvdFG9kM/BEChymCH36KgjnW0NsL58JYRxw==", + "peer": true, + "dependencies": { + "@socket.io/component-emitter": "~3.1.0", + "debug": "~4.4.1", + "engine.io-parser": "~5.2.1", + "ws": "~8.18.3", + "xmlhttprequest-ssl": "~2.1.1" + } + }, + "node_modules/engine.io-parser": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.2.3.tgz", + "integrity": "sha512-HqD3yTBfnBxIrbnM1DoD6Pcq8NECnh8d4As1Qgh0z5Gg3jRRIqijury0CL3ghu/edArpUYiYqQiDUQBIs4np3Q==", + "peer": true, + "engines": { + "node": ">=10.0.0" +>>>>>>> Stashed changes } }, "node_modules/es-abstract": { @@ -2486,7 +2570,6 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", - "dev": true, "dependencies": { "es-errors": "^1.3.0", "get-intrinsic": "^1.2.6", @@ -2937,6 +3020,25 @@ "integrity": "sha512-AiwGJM8YcNOaobumgtng+6NHuOqC3A7MixFeDafM3X9cIUM+xUXoS5Vfgf+OihAYe20fxqNM9yPBXJzRtZ/4eA==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.16.0", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.16.0.tgz", + "integrity": "sha512-y5rN/uOsadFT/JfYwhxRS5R7Qce+g3zG97+JrtFZlC9klX/W5hD7iiLzScI4nZqUS7DNUdhPgw4xI8W2LuXlUw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/for-each": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", @@ -2946,6 +3048,7 @@ "is-callable": "^1.1.3" } }, +<<<<<<< Updated upstream "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -2953,6 +3056,21 @@ "license": "MIT", "engines": { "node": ">= 0.6" +======= + "node_modules/form-data": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.5.tgz", + "integrity": "sha512-8RipRLol37bNs2bhoV67fiTEvdTrbMUYcFTiy3+wuuOnUog2QBHCZWXDRijWQfAkhBj2Uf5UnVaiWwA5vdd82w==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "hasown": "^2.0.2", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" +>>>>>>> Stashed changes } }, "node_modules/framer-motion": { @@ -3216,7 +3334,6 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", - "dev": true, "dependencies": { "has-symbols": "^1.0.3" }, @@ -3889,6 +4006,18 @@ "json-buffer": "3.0.1" } }, + "node_modules/laravel-echo": { + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/laravel-echo/-/laravel-echo-2.3.4.tgz", + "integrity": "sha512-rpALCIK1uw2SrttcK9P5JzItt5I85RcfXQKUNnkcorzhtKeXi5GS0PVFFBH8ppNo8wnbdBKuD1EtIHgTbXo9FQ==", + "engines": { + "node": ">=20" + }, + "peerDependencies": { + "pusher-js": "*", + "socket.io-client": "*" + } + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -3964,6 +4093,7 @@ "node": ">= 0.4" } }, +<<<<<<< Updated upstream "node_modules/media-typer": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.0.tgz", @@ -3990,11 +4120,18 @@ "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz", "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==", "license": "MIT", +======= + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", +>>>>>>> Stashed changes "engines": { "node": ">= 0.6" } }, "node_modules/mime-types": { +<<<<<<< Updated upstream "version": "3.0.2", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.2.tgz", "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==", @@ -4008,6 +4145,16 @@ "funding": { "type": "opencollective", "url": "https://opencollective.com/express" +======= + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" +>>>>>>> Stashed changes } }, "node_modules/minimatch": { @@ -7044,6 +7191,7 @@ "react-is": "^16.13.1" } }, +<<<<<<< Updated upstream "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", @@ -7055,6 +7203,14 @@ }, "engines": { "node": ">= 0.10" +======= + "node_modules/proxy-from-env": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-2.1.0.tgz", + "integrity": "sha512-cJ+oHTW1VAEa8cJslgmUZrc+sjRKgAKl3Zyse6+PV38hZe/V6Z14TbCuXcan9F9ghlz4QrFr2c92TNF82UkYHA==", + "engines": { + "node": ">=10" +>>>>>>> Stashed changes } }, "node_modules/punycode": { @@ -7066,6 +7222,7 @@ "node": ">=6" } }, +<<<<<<< Updated upstream "node_modules/qs": { "version": "6.15.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.15.0.tgz", @@ -7103,6 +7260,14 @@ }, "engines": { "node": ">= 0.10" +======= + "node_modules/pusher-js": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/pusher-js/-/pusher-js-8.5.0.tgz", + "integrity": "sha512-V7uzGi9bqOOOyM/6IkJdpFyjGZj7llz1v0oWnYkZKcYLvbz6VcHVLmzKqkvegjuMumpfIEKGLmWHwFb39XFCpw==", + "dependencies": { + "tweetnacl": "^1.0.3" +>>>>>>> Stashed changes } }, "node_modules/rc-cascader": { @@ -8645,6 +8810,48 @@ "url": "https://github.com/sponsors/ljharb" } }, +<<<<<<< Updated upstream +======= + "node_modules/socket.io-client": { + "version": "4.8.3", + "resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.8.3.tgz", + "integrity": "sha512-uP0bpjWrjQmUt5DTHq9RuoCBdFJF10cdX9X+a368j/Ft0wmaVgxlrjvK3kjvgCODOMMOz9lcaRzxmso0bTWZ/g==", + "peer": true, + "dependencies": { + "@socket.io/component-emitter": "~3.1.0", + "debug": "~4.4.1", + "engine.io-client": "~6.6.1", + "socket.io-parser": "~4.2.4" + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/socket.io-parser": { + "version": "4.2.6", + "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.6.tgz", + "integrity": "sha512-asJqbVBDsBCJx0pTqw3WfesSY0iRX+2xzWEWzrpcH7L6fLzrhyF8WPI8UaeM4YCuDfpwA/cgsdugMsmtz8EJeg==", + "peer": true, + "dependencies": { + "@socket.io/component-emitter": "~3.1.0", + "debug": "~4.4.1" + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "optional": true, + "peer": true, + "engines": { + "node": ">=0.10.0" + } + }, +>>>>>>> Stashed changes "node_modules/source-map-js": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", @@ -8873,6 +9080,11 @@ "resolved": "https://registry.npmjs.org/turbo-stream/-/turbo-stream-2.4.0.tgz", "integrity": "sha512-FHncC10WpBd2eOmGwpmQsWLDoK4cqsA/UT/GqNoaKOQnT8uzhtCbg3EoUDMvqpOSAI0S26mr0rkjzbOO6S3v1g==" }, + "node_modules/tweetnacl": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" + }, "node_modules/type-check": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", @@ -9250,11 +9462,43 @@ "node": ">=0.10.0" } }, +<<<<<<< Updated upstream "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "license": "ISC" +======= + "node_modules/ws": { + "version": "8.18.3", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.3.tgz", + "integrity": "sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==", + "peer": true, + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/xmlhttprequest-ssl": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-2.1.2.tgz", + "integrity": "sha512-TEU+nJVUUnA4CYJFLvK5X9AOeH4KvDvhIfm0vV1GaQRtchnG0hgK5p8hw/xjv8cunWYCsiPCSDzObPyhEwq3KQ==", + "peer": true, + "engines": { + "node": ">=0.4.0" + } +>>>>>>> Stashed changes }, "node_modules/yallist": { "version": "3.1.1", diff --git a/package.json b/package.json index 0b214c6..b24482b 100644 --- a/package.json +++ b/package.json @@ -12,14 +12,20 @@ "dependencies": { "@reduxjs/toolkit": "^2.5.0", "antd": "^5.22.7", +<<<<<<< Updated upstream "express": "^5.2.1", +======= + "axios": "^1.15.2", +>>>>>>> Stashed changes "framer-motion": "^12.5.0", "i18next": "^24.2.1", "i18next-browser-languagedetector": "^8.0.2", "imask": "^7.6.1", + "laravel-echo": "^2.3.4", "lodash": "^4.17.21", "lucide-react": "^0.469.0", "nprogress": "^0.2.0", + "pusher-js": "^8.5.0", "react": "^18.3.1", "react-dom": "^18.3.1", "react-i18next": "^15.4.0", diff --git a/src/App.jsx b/src/App.jsx index a140645..5748883 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -8,6 +8,7 @@ import PageLoader from "./components/Loader/pageLoader.jsx"; import ScrollToTop from "./components/ScrollToTop"; import { AuthProvider } from "./context/authContext.jsx"; +import ChatWidget from "./components/ChatWidget"; function App() { const [isLoading, setIsLoading] = useState(true); @@ -46,8 +47,9 @@ function App() { <> + {isVisible && ( - +
({ + getSupportConversation: builder.query({ + query: () => ({ + url: "/chat/support", + headers: { Accept: "application/json" }, + }), + transformResponse: (response) => response.data ?? null, + }), + startSupportConversation: builder.mutation({ + query: () => ({ + url: "/chat/support", + method: "POST", + headers: { Accept: "application/json" }, + }), + transformResponse: (response) => response.data, + }), + getChatMessages: builder.query({ + query: ({ conversationId, page = 1, perPage = 50 }) => ({ + url: `/chat/conversations/${conversationId}/messages?perPage=${perPage}&page=${page}`, + headers: { Accept: "application/json" }, + }), + transformResponse: (response) => ({ + items: response.data || [], + pagination: response.pagination || {}, + }), + }), + sendChatMessage: builder.mutation({ + query: ({ conversationId, body }) => ({ + url: `/chat/conversations/${conversationId}/messages`, + method: "POST", + body: { body }, + headers: { Accept: "application/json" }, + }), + transformResponse: (response) => response.data, + }), + readChat: builder.mutation({ + query: (conversationId) => ({ + url: `/chat/conversations/${conversationId}/read`, + method: "POST", + headers: { Accept: "application/json" }, + }), + }), + }), +}); + +export const { + useGetSupportConversationQuery, + useStartSupportConversationMutation, + useLazyGetChatMessagesQuery, + useSendChatMessageMutation, + useReadChatMutation, +} = chatApi; diff --git a/src/app/store.js b/src/app/store.js index 1afa2ec..6d7f417 100644 --- a/src/app/store.js +++ b/src/app/store.js @@ -1,12 +1,64 @@ import { configureStore } from "@reduxjs/toolkit"; import { baseApi } from "./api/baseApi"; +<<<<<<< Updated upstream +======= +import { categoriesApi } from "./api/categories"; +import { searchApi } from "./api/searchApi"; +import { cartApi } from "./api/cartApi"; +import { brandsApi } from "./api/brandsApi"; +import { collectionsApi } from "./api/collectionsApi"; +import { favoritesApi } from "./api/favoritesApi"; +import { legalPagesApi } from "./api/legalPagesApi"; +import { locationApi } from "./api/locationApi"; +import { orderApi } from "./api/orderApi"; +import { mediaApi } from "./api/bannersApi"; +import { reviewsApi } from "./api/reviewApi"; +import { profileApi } from "./api/myProfileApi"; +import { contactApi } from "./api/contactUs"; +import { chatApi } from "./api/chatApi"; +>>>>>>> Stashed changes const store = configureStore({ reducer: { [baseApi.reducerPath]: baseApi.reducer, +<<<<<<< Updated upstream }, middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(baseApi.middleware), +======= + [categoriesApi.reducerPath]: categoriesApi.reducer, + [searchApi.reducerPath]: searchApi.reducer, + [cartApi.reducerPath]: cartApi.reducer, + [brandsApi.reducerPath]: brandsApi.reducer, + [collectionsApi.reducerPath]: collectionsApi.reducer, + [favoritesApi.reducerPath]: favoritesApi.reducer, + [legalPagesApi.reducerPath]: legalPagesApi.reducer, + [locationApi.reducerPath]: locationApi.reducer, + [orderApi.reducerPath]: orderApi.reducer, + [mediaApi.reducerPath]: mediaApi.reducer, + [reviewsApi.reducerPath]: reviewsApi.reducer, + [profileApi.reducerPath]: profileApi.reducer, + [contactApi.reducerPath]: contactApi.reducer, + [chatApi.reducerPath]: chatApi.reducer, + }, + middleware: (getDefaultMiddleware) => + getDefaultMiddleware().concat( + baseApi.middleware, + categoriesApi.middleware, + searchApi.middleware, + brandsApi.middleware, + collectionsApi.middleware, + favoritesApi.middleware, + legalPagesApi.middleware, + locationApi.middleware, + orderApi.middleware, + reviewsApi.middleware, + mediaApi.middleware, + profileApi.middleware, + contactApi.middleware, + chatApi.middleware, + ), +>>>>>>> Stashed changes }); export default store; diff --git a/src/components/ChatWidget/ChatWidget.module.scss b/src/components/ChatWidget/ChatWidget.module.scss new file mode 100644 index 0000000..dff3d4f --- /dev/null +++ b/src/components/ChatWidget/ChatWidget.module.scss @@ -0,0 +1,167 @@ +.wrap { + position: fixed; + right: 24px; + bottom: 24px; + z-index: 1100; + display: flex; + flex-direction: column; + align-items: flex-end; + gap: 12px; +} + +.panel { + width: 380px; + max-width: calc(100vw - 32px); + height: 520px; + max-height: calc(100vh - 120px); + background: #fff; + border-radius: 16px; + box-shadow: 0 12px 40px rgba(15, 23, 42, 0.2); + display: flex; + flex-direction: column; + overflow: hidden; +} + +.header { + display: flex; + align-items: center; + justify-content: space-between; + padding: 14px 16px; + background: #d32824; + color: #fff; + font-weight: 700; +} + +.iconBtn { + border: 0; + background: transparent; + color: #fff; + cursor: pointer; + display: flex; + padding: 4px; +} + +.messages { + flex: 1; + overflow-y: auto; + padding: 16px; + display: flex; + flex-direction: column; + gap: 8px; + background: #f8fafc; +} + +.empty { + margin: auto; + color: #94a3b8; + font-size: 13px; + text-align: center; + padding: 0 16px; +} + +.bubble { + max-width: 80%; + padding: 8px 12px; + border-radius: 14px; + font-size: 14px; + line-height: 1.4; + white-space: pre-wrap; + word-break: break-word; +} + +.bubble span { + display: block; + margin-top: 4px; + font-size: 11px; + opacity: 0.7; +} + +.in { + align-self: flex-start; + background: #fff; + color: #0f172a; +} + +.out { + align-self: flex-end; + background: #d32824; + color: #fff; +} + +.composer { + display: flex; + gap: 8px; + padding: 10px 12px; + border-top: 1px solid #e2e8f0; + background: #fff; +} + +.composer textarea { + flex: 1; + resize: none; + border: 1px solid #e2e8f0; + border-radius: 10px; + padding: 8px 10px; + font: inherit; + min-height: 40px; + max-height: 96px; +} + +.composer button { + width: 40px; + border: 0; + border-radius: 10px; + background: #d32824; + color: #fff; + cursor: pointer; + display: flex; + align-items: center; + justify-content: center; +} + +.composer button:disabled { + opacity: 0.5; + cursor: not-allowed; +} + +.fab { + position: relative; + width: 56px; + height: 56px; + border: 0; + border-radius: 50%; + background: #d32824; + color: #fff; + cursor: pointer; + box-shadow: 0 8px 20px rgba(211, 40, 36, 0.35); + display: flex; + align-items: center; + justify-content: center; +} + +.badge { + position: absolute; + top: -4px; + right: -4px; + min-width: 20px; + height: 20px; + padding: 0 6px; + border-radius: 999px; + background: #0f172a; + color: #fff; + font-size: 11px; + font-weight: 700; + line-height: 20px; +} + +@media (max-width: 480px) { + .wrap { + right: 16px; + bottom: 16px; + } + + .panel { + width: calc(100vw - 32px); + height: calc(100vh - 100px); + } +} diff --git a/src/components/ChatWidget/index.jsx b/src/components/ChatWidget/index.jsx new file mode 100644 index 0000000..6800e85 --- /dev/null +++ b/src/components/ChatWidget/index.jsx @@ -0,0 +1,296 @@ +import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react"; +import { useTranslation } from "react-i18next"; +import { MessageCircle, Send, X } from "lucide-react"; +import { useAuth } from "../../context/authContext"; +import { + useGetSupportConversationQuery, + useLazyGetChatMessagesQuery, + useReadChatMutation, + useSendChatMessageMutation, + useStartSupportConversationMutation, +} from "../../app/api/chatApi"; +import { getChatEcho, leaveChatChannel } from "../../utils/echo"; +import styles from "./ChatWidget.module.scss"; + +function formatTime(value) { + if (!value) return ""; + const date = new Date(value); + if (Number.isNaN(date.getTime())) return ""; + return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); +} + +export default function ChatWidget() { + const { t } = useTranslation(); + const { authToken, guestToken } = useAuth(); + const ready = Boolean(authToken || guestToken); + + const [open, setOpen] = useState(false); + const [hidden, setHidden] = useState(false); + const [conversation, setConversation] = useState(null); + const [messages, setMessages] = useState([]); + const [nextPage, setNextPage] = useState(null); + const [draft, setDraft] = useState(""); + const [unread, setUnread] = useState(0); + const [loadingThread, setLoadingThread] = useState(false); + + const listRef = useRef(null); + const subscribedId = useRef(null); + const openRef = useRef(open); + openRef.current = open; + const pendingPrepend = useRef(false); + + const { data: existing, error } = useGetSupportConversationQuery(undefined, { + skip: !ready || hidden, + pollingInterval: open ? 0 : 15000, + }); + const [startSupport] = useStartSupportConversationMutation(); + const [fetchMessages] = useLazyGetChatMessagesQuery(); + const [sendMessage, { isLoading: sending }] = useSendChatMessageMutation(); + const [markRead] = useReadChatMutation(); + + useEffect(() => { + const status = error?.status; + if (status === 403 || status === 422) { + setHidden(true); + } + }, [error]); + + useEffect(() => { + if (!existing) { + return; + } + setConversation(existing); + if (!open) { + setUnread(existing.unread_count || 0); + } + }, [existing, open]); + + useLayoutEffect(() => { + if (!open || loadingThread || pendingPrepend.current) { + return; + } + const el = listRef.current; + if (!el) { + return; + } + el.scrollTop = el.scrollHeight; + }, [messages, open, loadingThread]); + + const appendMessage = useCallback((message) => { + setMessages((current) => { + if (current.some((item) => item.id === message.id)) { + return current; + } + return [...current, message]; + }); + }, []); + + const loadPage = useCallback( + async (conversationId, page = 1, prepend = false) => { + const result = await fetchMessages({ conversationId, page, perPage: 50 }).unwrap(); + const items = (result.items || []).slice().reverse(); + setNextPage(result.pagination?.next_page_url ? page + 1 : null); + setMessages((current) => { + if (!prepend) { + return items; + } + const known = new Set(current.map((item) => item.id)); + return [...items.filter((item) => !known.has(item.id)), ...current]; + }); + return items; + }, + [fetchMessages] + ); + + const openThread = useCallback(async () => { + setOpen(true); + setLoadingThread(true); + try { + let current = conversation; + if (!current?.id) { + current = await startSupport().unwrap(); + setConversation(current); + } + await loadPage(current.id, 1, false); + await markRead(current.id); + setUnread(0); + } catch (err) { + if (err?.status === 403 || err?.status === 422) { + setHidden(true); + setOpen(false); + } + } finally { + setLoadingThread(false); + } + }, [conversation, loadPage, markRead, startSupport]); + + useEffect(() => { + const conversationId = conversation?.id; + if (!conversationId || hidden) { + return undefined; + } + + const echo = getChatEcho(); + if (!echo) { + return undefined; + } + + if (subscribedId.current && subscribedId.current !== conversationId) { + leaveChatChannel(subscribedId.current); + } + + subscribedId.current = conversationId; + echo.private(`chat.${conversationId}`).listen(".message.sent", (payload) => { + appendMessage(payload); + if (openRef.current) { + markRead(conversationId); + setUnread(0); + } else { + setUnread((count) => count + 1); + } + }); + + return () => { + leaveChatChannel(conversationId); + subscribedId.current = null; + }; + }, [appendMessage, conversation?.id, hidden, markRead]); + + useEffect(() => { + if (!open || !conversation?.id || getChatEcho()) { + return undefined; + } + + const timer = setInterval(async () => { + try { + const result = await fetchMessages({ + conversationId: conversation.id, + page: 1, + perPage: 50, + }).unwrap(); + const items = (result.items || []).slice().reverse(); + setMessages((current) => { + const known = new Set(current.map((item) => item.id)); + const incoming = items.filter((item) => !known.has(item.id)); + return incoming.length ? [...current, ...incoming] : current; + }); + } catch { + // polling is best-effort + } + }, 4000); + + return () => clearInterval(timer); + }, [conversation?.id, fetchMessages, open]); + + const handleScroll = async () => { + if (!listRef.current || !nextPage || !conversation?.id || loadingThread) { + return; + } + if (listRef.current.scrollTop > 40) { + return; + } + pendingPrepend.current = true; + const previousHeight = listRef.current.scrollHeight; + await loadPage(conversation.id, nextPage, true); + requestAnimationFrame(() => { + if (listRef.current) { + listRef.current.scrollTop = listRef.current.scrollHeight - previousHeight; + } + pendingPrepend.current = false; + }); + }; + + const handleSend = async (event) => { + event.preventDefault(); + const body = draft.trim(); + if (!body || sending) { + return; + } + + try { + let current = conversation; + if (!current?.id) { + current = await startSupport().unwrap(); + setConversation(current); + } + const message = await sendMessage({ + conversationId: current.id, + body, + }).unwrap(); + appendMessage(message); + setDraft(""); + } catch (err) { + if (err?.status === 403 || err?.status === 422) { + setHidden(true); + } + } + }; + + if (!ready || hidden) { + return null; + } + + const otherName = conversation?.user?.name || t("chat.title"); + const otherId = conversation?.user?.id; + + return ( +
+ {open && ( +
+
+ {otherName} + +
+
+ {loadingThread &&

{t("chat.loading")}

} + {!loadingThread && messages.length === 0 && ( +

{t("chat.empty")}

+ )} + {messages.map((message) => { + const mine = otherId ? message.user?.id !== otherId : true; + return ( +
+
{message.body}
+ {formatTime(message.created_at)} +
+ ); + })} +
+
+