@alfatech/livechat 2024.12.3-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.
Files changed (71) hide show
  1. package/.github/workflows/release.yml +33 -0
  2. package/.husky/pre-commit +2 -0
  3. package/README.md +30 -0
  4. package/biome.json +55 -0
  5. package/index.html +20 -0
  6. package/package.json +43 -0
  7. package/postcss.config.cjs +5 -0
  8. package/public/vite.svg +1 -0
  9. package/src/api/base.api.ts +118 -0
  10. package/src/api/error.ts +109 -0
  11. package/src/assets/react.svg +1 -0
  12. package/src/components/app/WidgetProvider.tsx +80 -0
  13. package/src/components/app/button/index.tsx +47 -0
  14. package/src/components/app/button/types.ts +68 -0
  15. package/src/components/app/card/profile.tsx +33 -0
  16. package/src/components/app/icon/index.tsx +144 -0
  17. package/src/components/app/input/checkbox.tsx +115 -0
  18. package/src/components/app/input/index.tsx +133 -0
  19. package/src/components/app/input/label.tsx +33 -0
  20. package/src/components/app/input/radio.tsx +143 -0
  21. package/src/components/app/input/select.tsx +54 -0
  22. package/src/components/app/input/textarea.tsx +68 -0
  23. package/src/components/app/input/toggle.tsx +162 -0
  24. package/src/components/app/input/verification.tsx +111 -0
  25. package/src/features/chat/chat.api.ts +30 -0
  26. package/src/features/chat/chat.types.ts +50 -0
  27. package/src/features/chat/partials/ChatCreateForm.tsx +103 -0
  28. package/src/features/chat/partials/ChatMessageFormFooter.tsx +67 -0
  29. package/src/features/chat/partials/ChatMessageSection.tsx +103 -0
  30. package/src/features/widget/components/WidgetIcon.tsx +26 -0
  31. package/src/features/widget/contexts/page-tracker.tsx +74 -0
  32. package/src/features/widget/hooks/page-tracker.tsx +57 -0
  33. package/src/features/widget/widget.api.ts +14 -0
  34. package/src/features/widget/widget.store.ts +16 -0
  35. package/src/features/widget/widget.types.ts +28 -0
  36. package/src/index.css +40 -0
  37. package/src/layouts/auth.tsx +17 -0
  38. package/src/layouts/chat.tsx +18 -0
  39. package/src/lib/cdn.tsx +3 -0
  40. package/src/lib/cookie.tsx +7 -0
  41. package/src/lib/debounce.tsx +23 -0
  42. package/src/lib/hooks/cookie.tsx +22 -0
  43. package/src/lib/hooks/dayjs.tsx +10 -0
  44. package/src/lib/hooks/dom.tsx +26 -0
  45. package/src/lib/hooks/form.tsx +26 -0
  46. package/src/lib/hooks/router.tsx +31 -0
  47. package/src/lib/list.tsx +14 -0
  48. package/src/lib/obj.tsx +23 -0
  49. package/src/lib/phone.tsx +12 -0
  50. package/src/main.tsx +31 -0
  51. package/src/pages/auth/registration-view.tsx +67 -0
  52. package/src/pages/chat/chat-detail-view.tsx +200 -0
  53. package/src/router/router.tsx +17 -0
  54. package/src/router/routes/auth-routes.ts +7 -0
  55. package/src/router/routes/chat-routes.ts +7 -0
  56. package/src/router/routes/index.ts +8 -0
  57. package/src/router/routes/routes.types.ts +0 -0
  58. package/src/types/i18n.ts +11 -0
  59. package/src/vite-env.d.ts +1 -0
  60. package/src/widget.tsx +19 -0
  61. package/src/ws/events.tsx +69 -0
  62. package/src/ws/guard.tsx +16 -0
  63. package/src/ws/hooks.tsx +122 -0
  64. package/tailwind.config.js +27 -0
  65. package/tsconfig.app.json +29 -0
  66. package/tsconfig.app.tsbuildinfo +1 -0
  67. package/tsconfig.json +7 -0
  68. package/tsconfig.node.json +22 -0
  69. package/tsconfig.node.tsbuildinfo +1 -0
  70. package/version.cjs +5 -0
  71. package/vite.config.ts +14 -0
@@ -0,0 +1,33 @@
1
+ name: Release NPM Package
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ ci_cd:
9
+ name: Build and Push JS Client to CDN
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - name: Checkout Code
13
+ uses: actions/checkout@v4
14
+
15
+ - name: Chat Client Version Set
16
+ run: node ./version.cjs ${{ github.ref_name }}
17
+
18
+ - name: Publish to NPM
19
+ uses: actions/setup-node@v4
20
+ with:
21
+ node-version: '20.x'
22
+ registry-url: 'https://registry.npmjs.org'
23
+
24
+ - name: Install NPM Deps
25
+ run: npm install
26
+
27
+ - name: Build NPM Package
28
+ run: npm run build
29
+
30
+ - name: Publish NPM Package
31
+ run: npm publish --access public
32
+ env:
33
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,2 @@
1
+ npx lint-staged || exit 1
2
+ git diff --quiet || git commit -m "chore: auto-formatting files"
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # livechat widget client
2
+
3
+ ```html
4
+ <script src="https://cdn.jsdelivr.net/gh/alfatek/livechat/dist/widget.js"></script>
5
+ <div id="livechat"></div>
6
+ ```
7
+
8
+
9
+ ```js
10
+ import widget from "@alfatech/livechat";
11
+
12
+ widget({
13
+ container: document.getElementById("livechat"),
14
+ link_id: "livechat",
15
+ url: "https://livechat.alfatek.net",
16
+ token: "your-token",
17
+ user: {
18
+ id: "user-id",
19
+ name: "user-name",
20
+ email: "user-email",
21
+ avatar: "user-avatar",
22
+ },
23
+ theme: {
24
+ primary: "#4CAF50",
25
+ secondary: "#4CAF50",
26
+ },
27
+ lang: "en",
28
+ debug: true,
29
+ });
30
+ ```
package/biome.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
3
+ "files": {
4
+ "ignore": [
5
+ ".git",
6
+ "dist",
7
+ "public",
8
+ "node_modules",
9
+ ".vscode",
10
+ "tsconfig*.json",
11
+ "*.lock",
12
+ "*-lock.json",
13
+ "package-lock.json",
14
+ "plugins",
15
+ "version.js"
16
+ ]
17
+ },
18
+ "organizeImports": {
19
+ "enabled": true
20
+ },
21
+ "linter": {
22
+ "enabled": true,
23
+ "rules": {
24
+ "recommended": true,
25
+ "complexity": {
26
+ "noStaticOnlyClass": "off",
27
+ "noBannedTypes": "off"
28
+ },
29
+ "correctness": {
30
+ "useExhaustiveDependencies": "off"
31
+ },
32
+ "a11y": {
33
+ "useKeyWithClickEvents": "off",
34
+ "useButtonType": "off",
35
+ "noSvgWithoutTitle": "off",
36
+ "noLabelWithoutControl": "off"
37
+ },
38
+ "suspicious": {
39
+ "noArrayIndexKey": "off",
40
+ "noExplicitAny": "off",
41
+ "noThenProperty": "off",
42
+ "noShadowRestrictedNames": "off",
43
+ "noAssignInExpressions": "off"
44
+ },
45
+ "style": {
46
+ "noNonNullAssertion": "off",
47
+ "useSelfClosingElements": "off",
48
+ "useConst": "off"
49
+ }
50
+ }
51
+ },
52
+ "formatter": {
53
+ "enabled": true
54
+ }
55
+ }
package/index.html ADDED
@@ -0,0 +1,20 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>Vite + React + TS</title>
8
+ </head>
9
+ <body>
10
+ <div id="livechat"></div>
11
+ <script type="module">
12
+ import widget from './src/widget.tsx';
13
+
14
+ widget({
15
+ container: document.getElementById('livechat'),
16
+ link_id: "b8845163-e3df-40ea-bcbd-8d1e27f475d8", // your link id
17
+ })
18
+ </script>
19
+ </body>
20
+ </html>
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@alfatech/livechat",
3
+ "private": false,
4
+ "version": "v2024.12.31.1",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "tsc -b && vite build",
9
+ "lint": "biome lint --write",
10
+ "prepare": "husky",
11
+ "format": "biome format --write && yarn lint"
12
+ },
13
+ "dependencies": {
14
+ "@tanstack/react-query": "^5.59.20",
15
+ "classnames": "^2.5.1",
16
+ "dayjs": "^1.11.13",
17
+ "formik": "^2.4.6",
18
+ "react": "^18.3.1",
19
+ "react-dom": "^18.3.1",
20
+ "react-router-dom": "^6.26.2",
21
+ "react-toastify": "^10.0.6",
22
+ "socket.io-client": "^4.8.1",
23
+ "yup": "^1.4.0",
24
+ "zustand": "^4.5.5"
25
+ },
26
+ "devDependencies": {
27
+ "@biomejs/biome": "^1.8.3",
28
+ "@types/node": "^22.5.4",
29
+ "@types/react": "^18.3.3",
30
+ "@types/react-dom": "^18.3.0",
31
+ "@vitejs/plugin-react-swc": "^3.5.0",
32
+ "globals": "^15.9.0",
33
+ "husky": "^9.1.5",
34
+ "tailwindcss": "^3.4.10",
35
+ "typescript": "^5.5.3",
36
+ "vite": "^5.4.1"
37
+ },
38
+ "lint-staged": {
39
+ "./**/*.{js,ts,tsx,json}": [
40
+ "npm run format"
41
+ ]
42
+ }
43
+ }
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ plugins: {
3
+ tailwindcss: { config: "./tailwind.config.js" },
4
+ },
5
+ };
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
@@ -0,0 +1,118 @@
1
+ import type { ApiError, NotFoundError, TooManyRequestsError } from "./error";
2
+
3
+ const ApiUrl: string = import.meta.env.VITE_API_BASE_API_URL;
4
+
5
+ type StateHandler = (response: Response, body: object) => [ApiError, number];
6
+
7
+ const tooManyRequestsStateHandler: StateHandler = (
8
+ response: Response,
9
+ ): [TooManyRequestsError, 429] => {
10
+ const resp: TooManyRequestsError = {
11
+ limit: Number.parseInt(response.headers.get("X-Ratelimit-Limit") || "0"),
12
+ remaining: Number.parseInt(
13
+ response.headers.get("X-Ratelimit-Remaining") || "0",
14
+ ),
15
+ reset: Number.parseInt(response.headers.get("X-Ratelimit-Reset") || "0"),
16
+ retryAfter: Number.parseInt(response.headers.get("Retry-After") || "0"),
17
+ };
18
+ return [resp, 429];
19
+ };
20
+
21
+ const notFoundStateHandler: StateHandler = (_, body): [ApiError, 404] => {
22
+ return [
23
+ {
24
+ message: "message" in body ? body.message : "Not Found",
25
+ } as NotFoundError,
26
+ 404,
27
+ ];
28
+ };
29
+
30
+ const StateHandlers: Record<number, StateHandler> = {
31
+ 429: tooManyRequestsStateHandler,
32
+ 404: notFoundStateHandler,
33
+ };
34
+
35
+ export type ReqResult<T> = [T | ApiError, number];
36
+ export type ReqFuncResult<T> = Promise<ReqResult<T>>;
37
+
38
+ export async function req<T = any>(
39
+ endpoint: string,
40
+ options: RequestInit = {},
41
+ ): ReqFuncResult<T> {
42
+ const extraHeads: Record<string, string> = {};
43
+ if (!(options.body instanceof FormData) && !!options.body) {
44
+ extraHeads["Content-Type"] = "application/json";
45
+ }
46
+ const response = await fetch(`${ApiUrl}/${endpoint}`, {
47
+ ...options,
48
+ credentials: "include",
49
+
50
+ headers: {
51
+ ...options.headers,
52
+ ...extraHeads,
53
+ },
54
+ }).catch((err) => {
55
+ console.log("err::", err);
56
+ return new Response(null, { status: 500 });
57
+ });
58
+ if (!response.ok) {
59
+ let body = {};
60
+ try {
61
+ body = await response.json();
62
+ return [body, response.status];
63
+ } catch (e: any) {
64
+ console.log("response: ", response);
65
+ }
66
+ const handler = StateHandlers[response.status];
67
+ if (handler) {
68
+ return handler(response, body);
69
+ }
70
+ return [{} as ApiError, response.status];
71
+ }
72
+ const contentType = response.headers.get("Content-Type");
73
+ if (!!contentType && contentType.includes("application/json")) {
74
+ return [await response.json(), response.status];
75
+ }
76
+ return [await response.text(), response.status];
77
+ }
78
+
79
+ export const withQuery = (endpoint: string, query?: string): string => {
80
+ return query ? `${endpoint}?${query}` : endpoint;
81
+ };
82
+
83
+ export const isSuccess = (status: number): boolean => {
84
+ return [200, 201, 204].includes(status);
85
+ };
86
+
87
+ type FormDataOpts = {
88
+ arrayWithBrackets?: boolean;
89
+ arrayWithBracketAndIndex?: boolean;
90
+ };
91
+
92
+ export const jsonToFormData = (
93
+ data: { [s: string]: any },
94
+ {
95
+ arrayWithBrackets = false,
96
+ arrayWithBracketAndIndex = true,
97
+ }: FormDataOpts = {},
98
+ ): FormData => {
99
+ const formData = new FormData();
100
+ for (const [key, value] of Object.entries(data)) {
101
+ if (Array.isArray(value)) {
102
+ for (const idx in value) {
103
+ if (arrayWithBrackets) {
104
+ formData.append(`${key}[]`, value[idx]);
105
+ } else if (arrayWithBracketAndIndex) {
106
+ formData.append(`${key}[${idx}]`, value[idx]);
107
+ } else {
108
+ formData.append(key, value[idx]);
109
+ }
110
+ }
111
+ continue;
112
+ }
113
+ if (typeof value !== "undefined" && value !== null) {
114
+ formData.append(key, value);
115
+ }
116
+ }
117
+ return formData;
118
+ };
@@ -0,0 +1,109 @@
1
+ import { toast } from "react-toastify";
2
+ import { isSuccess } from "./base.api";
3
+
4
+ export type AuthRequiredError = {};
5
+
6
+ export type NotFoundError = {
7
+ message: string;
8
+ };
9
+
10
+ export type TooManyRequestsError = {
11
+ reset?: number;
12
+ remaining?: number;
13
+ limit?: number;
14
+ retryAfter?: number;
15
+ };
16
+
17
+ export type ValidationError = {
18
+ field: string;
19
+ message: string;
20
+ namespace: string;
21
+ value: string;
22
+ }[];
23
+
24
+ export type ApiError =
25
+ | AuthRequiredError
26
+ | TooManyRequestsError
27
+ | ValidationError
28
+ | NotFoundError;
29
+
30
+ export function isValidationError(error: any): error is ValidationError {
31
+ return Array.isArray(error) && error[0].field !== undefined;
32
+ }
33
+
34
+ export function isTooManyRequestsError(
35
+ error: any,
36
+ ): error is TooManyRequestsError {
37
+ return error.retryAfter !== undefined;
38
+ }
39
+ export function isApiError(error: any): error is ApiError {
40
+ return isValidationError(error) || isTooManyRequestsError(error);
41
+ }
42
+
43
+ type Form = {
44
+ setFieldError: (field: string, value: string | undefined) => void;
45
+ };
46
+
47
+ type Opts = {
48
+ form?: Form;
49
+ noToast?: boolean;
50
+ };
51
+
52
+ export const handleApiErrorResult = (res: any, opts?: Opts) => {
53
+ if (isValidationError(res)) {
54
+ for (const error of res) {
55
+ opts?.form?.setFieldError(error.field, error.message);
56
+ }
57
+ return;
58
+ }
59
+ if (!!res && res.message) {
60
+ if (!opts?.noToast) {
61
+ toast.error(res.message);
62
+ }
63
+ return;
64
+ }
65
+ if (!!res && res.error) {
66
+ if (!opts?.noToast) {
67
+ toast.error(res.error);
68
+ }
69
+ return;
70
+ }
71
+ };
72
+
73
+ const errorStates = [
74
+ {
75
+ code: 429,
76
+ text: "Çok fazla istek gönderdiniz. Lütfen biraz bekleyin",
77
+ },
78
+ {
79
+ code: 401,
80
+ text: "Bu işlemi yapabilmek için giriş yapmalısınız",
81
+ },
82
+ {
83
+ code: 403,
84
+ text: "Bu işlemi yapabilmek için yetkiniz yok",
85
+ },
86
+ {
87
+ code: 500,
88
+ text: "Sunucu hatası oluştu",
89
+ },
90
+ {
91
+ code: 422,
92
+ text: "Form doğrulaması başarısız oldu",
93
+ },
94
+ ] as const;
95
+
96
+ export const validateWithStatus = (data: any, status: number) => {
97
+ if (isSuccess(status)) return false;
98
+ if (data === null) {
99
+ toast.error("Bir hata oluştu");
100
+ return true;
101
+ }
102
+ if (typeof data === "object" && data.message) {
103
+ toast.error(data.message);
104
+ return true;
105
+ }
106
+ const state = errorStates.find((e) => e.code === status);
107
+ toast.error(state ? state.text : "Bir hata oluştu");
108
+ return true;
109
+ };
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
@@ -0,0 +1,80 @@
1
+ import { useWidgetStore } from "@/features/widget/widget.store";
2
+ import { useEffect, type PropsWithChildren } from "react";
3
+ import Button from "./button";
4
+ import Icon from "./icon";
5
+ import { useWebsocket } from "@/ws/hooks";
6
+ import { ReceiveEvents, SendEvents } from "@/ws/events";
7
+ import { useRouter } from "@/lib/hooks/router";
8
+ import { chatClear } from "@/features/chat/chat.api";
9
+
10
+ type Props = {
11
+ defaultOpen?: boolean;
12
+ };
13
+
14
+ export default function WidgetProvider({
15
+ children,
16
+ defaultOpen,
17
+ }: PropsWithChildren<Props>) {
18
+ const socket = useWebsocket();
19
+ const { open, setOpen, widget } = useWidgetStore();
20
+ const router = useRouter();
21
+
22
+ const onOpen = () => {
23
+ setOpen(true);
24
+ if (!socket) return;
25
+ socket.emit(SendEvents.WidgetOpen, {});
26
+ };
27
+
28
+ const onClose = () => {
29
+ setOpen(false);
30
+ if (!socket) return;
31
+ socket.emit(SendEvents.WidgetClose, {});
32
+ };
33
+
34
+ const onCustomerBan = () => {
35
+ setOpen(false);
36
+ chatClear(widget!.client_id);
37
+ router.push("registration");
38
+ };
39
+
40
+ const onChatClose = async () => {
41
+ setOpen(false);
42
+ await chatClear(widget!.client_id);
43
+ router.push("registration");
44
+ };
45
+
46
+ useEffect(() => {
47
+ if (defaultOpen) {
48
+ setOpen(true);
49
+ }
50
+ if (!socket || !widget) return;
51
+ socket.on(ReceiveEvents.Banned, onCustomerBan);
52
+ socket.on(ReceiveEvents.ChatUserClose, onChatClose);
53
+ return () => {
54
+ socket.off(ReceiveEvents.Banned);
55
+ socket.off(ReceiveEvents.ChatUserClose);
56
+ };
57
+ }, []);
58
+
59
+ return (
60
+ <section className="relative w-full h-full">
61
+ {open && (
62
+ <div className="w-86 h-[32rem] absolute bottom-8 right-8 z-[300]">
63
+ {children}
64
+ </div>
65
+ )}
66
+ <div className="absolute bottom-0 right-0 z-[400]">
67
+ {!open && (
68
+ <Button size="lg" rounded onClick={onOpen}>
69
+ <Icon.Chat className="size-6" />
70
+ </Button>
71
+ )}
72
+ {open && (
73
+ <Button size="lg" rounded onClick={onClose}>
74
+ <Icon.X className="size-6" />
75
+ </Button>
76
+ )}
77
+ </div>
78
+ </section>
79
+ );
80
+ }
@@ -0,0 +1,47 @@
1
+ import type { PropsWithChildren } from "react";
2
+ import {
3
+ type Kind,
4
+ type Size,
5
+ type Variant,
6
+ sizeClasses,
7
+ variantClasses,
8
+ } from "./types";
9
+ import classNames from "classnames";
10
+
11
+ type Props = {
12
+ type?: "button" | "submit" | "reset";
13
+ variant?: Variant;
14
+ kind?: Kind;
15
+ size?: Size;
16
+ rounded?: boolean;
17
+ className?: string;
18
+ } & React.ButtonHTMLAttributes<HTMLButtonElement>;
19
+ export default function Button({
20
+ children,
21
+ type = "button",
22
+ variant = "primary",
23
+ kind = "solid",
24
+ size = "md",
25
+ rounded = false,
26
+ className,
27
+ ...rest
28
+ }: PropsWithChildren<Props>) {
29
+ return (
30
+ <button
31
+ type={type}
32
+ className={classNames(
33
+ "flex items-center justify-center",
34
+ {
35
+ "rounded-full": rounded,
36
+ "rounded-md": !rounded,
37
+ },
38
+ className,
39
+ variantClasses[variant][kind],
40
+ sizeClasses[size][rounded ? "rounded" : "default"],
41
+ )}
42
+ {...rest}
43
+ >
44
+ {children}
45
+ </button>
46
+ );
47
+ }
@@ -0,0 +1,68 @@
1
+ export type Variant = "primary" | "red";
2
+ export type Kind = "solid" | "soft" | "dashed" | "outline" | "ghost";
3
+ export type Size = "xs" | "sm" | "md" | "lg" | "xl" | "2xl";
4
+
5
+ type VariantClasses = {
6
+ [key in Variant]: {
7
+ [key in Kind]: string;
8
+ };
9
+ };
10
+
11
+ type SizeClasses = {
12
+ [key in Size]: {
13
+ default: string;
14
+ rounded: string;
15
+ };
16
+ };
17
+
18
+ export const variantClasses: VariantClasses = {
19
+ primary: {
20
+ solid:
21
+ "text-white btn bg-blue-500 border-blue-500 hover:text-white hover:bg-blue-600 hover:border-blue-600 focus:text-white focus:bg-blue-600 focus:border-blue-600 focus:ring focus:ring-blue-100 active:text-white active:bg-blue-600 active:border-blue-600 active:ring active:ring-blue-100",
22
+ soft: "text-blue-500 btn bg-blue-100 hover:text-white hover:bg-blue-600 focus:text-white focus:bg-blue-600 focus:ring focus:ring-blue-100 active:text-white active:bg-blue-600 active:ring active:ring-blue-100",
23
+ dashed:
24
+ "bg-white border-dashed text-blue-500 btn border-blue-500 hover:text-blue-500 hover:bg-blue-50 hover:border-blue-600 focus:text-blue-600 focus:bg-blue-50 focus:border-blue-600 active:text-blue-600 active:bg-blue-50 active:border-blue-600",
25
+ outline:
26
+ "bg-white text-blue-500 btn border-blue-500 hover:text-white hover:bg-blue-600 hover:border-blue-600 focus:text-white focus:bg-blue-600 focus:border-blue-600 focus:ring focus:ring-blue-100 active:text-white active:bg-blue-600 active:border-blue-600 active:ring active:ring-blue-100",
27
+ ghost:
28
+ "bg-white text-blue-500 btn hover:text-blue-500 hover:bg-blue-100 focus:text-blue-500 focus:bg-blue-100 active:text-blue-500 active:bg-blue-100",
29
+ },
30
+ red: {
31
+ solid:
32
+ "text-white btn bg-red-500 border-red-500 hover:text-white hover:bg-red-600 hover:border-red-600 focus:text-white focus:bg-red-600 focus:border-red-600 focus:ring focus:ring-red-100 active:text-white active:bg-red-600 active:border-red-600 active:ring active:ring-red-100",
33
+ soft: "text-red-500 btn bg-red-100 hover:text-white hover:bg-red-600 focus:text-white focus:bg-red-600 focus:ring focus:ring-red-100 active:text-white active:bg-red-600 active:ring active:ring-red-100",
34
+ dashed:
35
+ "bg-white border-dashed text-red-500 btn border-red-500 hover:text-red-500 hover:bg-red-50 hover:border-red-600 focus:text-red-600 focus:bg-red-50 focus:border-red-600 active:text-red-600 active:bg-red-50 active:border-red-600",
36
+ outline:
37
+ "bg-white text-red-500 btn border-red-500 hover:text-white hover:bg-red-600 hover:border-red-600 focus:text-white focus:bg-red-600 focus:border-red-600 focus:ring focus:ring-red-100 active:text-white active:bg-red-600 active:border-red-600 active:ring active:ring-red-100",
38
+ ghost:
39
+ "bg-white text-red-500 btn hover:text-red-500 hover:bg-red-100 focus:text-red-500 focus:bg-red-100 active:text-red-500 active:bg-red-100",
40
+ },
41
+ };
42
+
43
+ export const sizeClasses: SizeClasses = {
44
+ xs: {
45
+ default: "py-1 text-xs px-1.5",
46
+ rounded: "p-1.5 text-xs",
47
+ },
48
+ sm: {
49
+ default: "px-2 py-1.5 text-xs",
50
+ rounded: "p-2 text-xs",
51
+ },
52
+ md: {
53
+ default: "px-2.5 py-2 text-sm",
54
+ rounded: "p-2.5 text-xs",
55
+ },
56
+ lg: {
57
+ default: "px-3 py-2.5",
58
+ rounded: "p-3",
59
+ },
60
+ xl: {
61
+ default: "px-4 py-2.5 text-15",
62
+ rounded: "p-4 text-15",
63
+ },
64
+ "2xl": {
65
+ default: "px-5 py-3 text-15",
66
+ rounded: "p-5 text-15",
67
+ },
68
+ };
@@ -0,0 +1,33 @@
1
+ import classNames from "classnames";
2
+
3
+ type Props = {
4
+ name: string;
5
+ avatar?: string;
6
+ className?: string;
7
+ };
8
+
9
+ export default function ProfileCard({
10
+ name,
11
+ avatar,
12
+ className = "text-neutral-500",
13
+ }: Props) {
14
+ return (
15
+ <div className="flex gap-2 items-center">
16
+ <div className="min-w-max">
17
+ <span>
18
+ {avatar && (
19
+ <img
20
+ className="size-10 rounded-full"
21
+ src={avatar}
22
+ aria-label={name}
23
+ />
24
+ )}
25
+ {!avatar && <div className="size-10 rounded-full bg-neutral-200" />}
26
+ </span>
27
+ </div>
28
+ <div className="flex flex-col">
29
+ <span className={classNames("line-clamp-1", className)}>{name}</span>
30
+ </div>
31
+ </div>
32
+ );
33
+ }