From bc1b30bc9a62136ec54715566f36b511402d3faa Mon Sep 17 00:00:00 2001 From: Hossein Rahmani Date: Sat, 3 Feb 2024 01:12:12 +0330 Subject: [PATCH] Change Key improved + Runtime Environment Variable added --- app/layout.tsx | 8 +- components/AdminKeysManagement.tsx | 260 +++++++++++++--------- components/ComplainsDetails.tsx | 11 +- components/InspectorOperations.tsx | 1 + components/NavbarPanel.tsx | 3 + components/ui/progress.tsx | 28 +++ components/ui/sonner.tsx | 2 +- hooks/useSignalR.jsx | 84 +++++++ lib/utils.ts | 1 - package-lock.json | 198 +++++++++++++++- package.json | 3 + utils/{ => ReactQuery}/Provider.jsx | 0 utils/{ => ReactQuery}/getQueryClient.jsx | 0 utils/{ => ReactQuery}/hydrate.client.jsx | 0 utils/adminApi.ts | 6 +- utils/authenticateApi.ts | 5 +- utils/commonApi.ts | 4 +- utils/infoApi.ts | 5 +- utils/inspectorApi.ts | 12 +- utils/signalrConnection.tsx | 27 +++ 20 files changed, 531 insertions(+), 127 deletions(-) create mode 100644 components/ui/progress.tsx create mode 100644 hooks/useSignalR.jsx rename utils/{ => ReactQuery}/Provider.jsx (100%) rename utils/{ => ReactQuery}/getQueryClient.jsx (100%) rename utils/{ => ReactQuery}/hydrate.client.jsx (100%) create mode 100644 utils/signalrConnection.tsx diff --git a/app/layout.tsx b/app/layout.tsx index 27706bf..9b45e98 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,9 +1,10 @@ import type { Metadata } from "next"; import "./globals.css"; import { Toaster } from "@/components/ui/toaster"; -import Provider from "../utils/Provider"; +import Provider from "../utils/ReactQuery/Provider"; import { Toaster as SonnerToaster } from "@/components/ui/sonner"; import ProgressBarProviders from "@/components/PrograssBar"; +import { EnvScript, PublicEnvScript } from "next-runtime-env"; export const metadata: Metadata = { title: "عیون", @@ -17,11 +18,14 @@ export default function RootLayout({ }) { return ( + + + {children} - + diff --git a/components/AdminKeysManagement.tsx b/components/AdminKeysManagement.tsx index 619a28a..3d31777 100644 --- a/components/AdminKeysManagement.tsx +++ b/components/AdminKeysManagement.tsx @@ -1,7 +1,12 @@ "use client"; import React, { useState, useRef, useEffect } from "react"; -import { useMutation, useQuery, RefetchOptions } from "@tanstack/react-query"; +import { + useMutation, + useQuery, + RefetchOptions, + useQueryClient, +} from "@tanstack/react-query"; import { GenerateKeyPair, GetKeys, AddKey, ChangeKey } from "@/utils/adminApi"; import { FaRegEdit } from "react-icons/fa"; import { IoMdClose } from "react-icons/io"; @@ -37,6 +42,8 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import * as z from "zod"; import { Textarea } from "./ui/textarea"; +import { Check } from "lucide-react"; +import { Progress } from "./ui/progress"; const MAX_FILE_SIZE = 5242880; // 5 MB // Edit Key Form @@ -71,16 +78,19 @@ const FormSchemaAddKey = z.object({ }), }); - export default function AdminKeysManagement() { const router = useRouter(); + const queryClient = useQueryClient(); const [keyId, setKeyId] = useState(""); + const [done, setDone] = useState(0); + const [total, setTotal] = useState(0); + const [polling, setPolling] = useState(false); const [openAdd, setOpenAdd] = React.useState(false); const [openEdit, setOpenEdit] = React.useState(false); const inputPubRef = useRef(null); const inputPrvRef = useRef(null); - + const intervalRef = useRef(null); // ..............Read .txt const [selectedFile, setSelectedFile] = useState(null); const handleFileChange = (event: any) => { @@ -89,9 +99,9 @@ export default function AdminKeysManagement() { reader.onload = (e) => { const contents = e?.target?.result; // Process the contents of the file here - setSelectedFile(contents); + setSelectedFile(contents); }; - reader?.readAsText(file); + reader?.readAsText(file); }; // Edit Form @@ -108,11 +118,27 @@ export default function AdminKeysManagement() { mutationKey: ["ChangeKey"], mutationFn: ChangeKey, onSuccess: (res) => { + if (res.total == res.done) { + localStorage.removeItem("isChangingKey"); + setPolling(false); + queryClient.invalidateQueries({ queryKey: ["KeysList"] }); + if (res.failed == 0) { + refetch1(); + toast("عملیات شما با موفقیت انجام شد. ", { + className: "!bg-green-500 !text-white", + }); + } + if (intervalRef.current !== null) { + clearInterval(intervalRef.current); + intervalRef.current = null; // Reset the ref value after clearing + } + } else { + localStorage.setItem("isChangingKey", "true"); + setPolling(true); + } setOpenEdit(false); - - toast("عملیات شما با موفقیت انجام شد. ", { - className: "!bg-green-500 !text-white", - }); + setDone(res.done); + setTotal(res.total); // router.refresh() }, @@ -123,10 +149,11 @@ export default function AdminKeysManagement() { }, }); - function onSubmitEdit(data: z.infer) { + function onSubmitEdit(data: z.infer) { ChangeKeyMutation.mutate({ privateKey: selectedFile, publicKeyId: keyId, + isPolling: false, }); } @@ -187,7 +214,7 @@ export default function AdminKeysManagement() { queryKey: ["KeysList"], queryFn: () => GetKeys(), // refetchOnWindowFocus: false, - }); + }); // GenerateKeyPair const { @@ -206,7 +233,7 @@ export default function AdminKeysManagement() { // type RefetchFunction = (options?: RefetchOptions) => Promise; const handleClickGenerate = () => { - refetch2(); + refetch2(); }; useEffect(() => { if (isSuccess2) { @@ -221,6 +248,37 @@ export default function AdminKeysManagement() { } }, [handleClickGenerate]); + // useEffect(() => { + // const isChangingKey = localStorage.getItem("isChangingKey"); + // if (isChangingKey) { + // setPolling(true); + // ChangeKeyMutation.mutate({ + // privateKey: "", + // publicKeyId: "cb7258da-64b0-4f43-b4f7-8ee11799d643", + // isPolling: true, + // }); + // } + // }, [polling]); + const refethKey = () => { + ChangeKeyMutation.mutate({ + privateKey: "", + publicKeyId: "cb7258da-64b0-4f43-b4f7-8ee11799d643", + isPolling: true, + }); + }; + useEffect(() => { + const isChangingKey = localStorage.getItem("isChangingKey"); + if (isChangingKey) { + setTimeout(() => {}, 10000); + intervalRef.current = setInterval(refethKey, 10000); + return () => { + if (intervalRef.current !== null) { + clearInterval(intervalRef.current); + intervalRef.current = null; // Reset the ref value after clearing + } + }; + } + }, [polling]); const saveTextFile = () => { const textToSave = data2?.privateKey ?? ""; const fileName = "privateKey.txt"; @@ -249,8 +307,7 @@ export default function AdminKeysManagement() { } }, [AddKeyMutation.isSuccess]); - - const resetFormAdd = () => { + const resetFormAdd = () => { formAdd.reset({ title: "", publicKey: "", @@ -260,6 +317,12 @@ export default function AdminKeysManagement() { return (
+ {polling && ( +
+

درحال تغییر کلید ...

+ +
+ )} {/* 1 */} @@ -268,7 +331,12 @@ export default function AdminKeysManagement() {

افزودن کلید جدید

- { resetFormAdd(); }}> + { + resetFormAdd(); + }} + > @@ -370,118 +438,98 @@ export default function AdminKeysManagement() {
-
+
{data1?.map((item: any) => { return (

عنوان کلید:{" "} {item.title}

-

+ {/*

آیدی بازرس:{" "} {item.inspectorId} -

+

*/} + {item?.isActive ? ( + + ) : ( + + + {/* */} + + + + تغییر کلید + +
+ +
+ ( + + + پیوست (کلید خصوصی) + + + + + + + )} + /> +
+
+ - - - {/* */} - - - - تغییر کلید - - - -
- ( - - - پیوست (کلید خصوصی) - - - - - - - )} - /> - - {/* ( - - - آی دی کلید عمومی - - - - - - - )} - /> */} -
- -
- - - {/* تغییر کلید */} - - بستن - -
- - -
-
- - {/* Continue */} - -
-
+ + بستن + +
+ + +
+
+ + {/* Continue */} + +
+
+ )}
); })} diff --git a/components/ComplainsDetails.tsx b/components/ComplainsDetails.tsx index f1c7bbc..b47c983 100644 --- a/components/ComplainsDetails.tsx +++ b/components/ComplainsDetails.tsx @@ -166,7 +166,6 @@ export default function ComplainsDetails({ const checkKey2 = (item: any) => { const dcKey = Decrypt(item.cipherKeyInspector, selectedFile); - console.log(dcKey); if (dcKey != false && dcKey != null) { if (check) { localStorage.setItem("privateKey", selectedFile); @@ -425,7 +424,7 @@ export default function ComplainsDetails({ روند شکایت

{data.contents?.map((item: any) => { @@ -447,11 +446,9 @@ export default function ComplainsDetails({

-
- - {item.text ? item.text : "-"} - -
+

+ {item.text ? item.text : "-"} +

{item.media.length !== 0 && ( )} diff --git a/components/InspectorOperations.tsx b/components/InspectorOperations.tsx index 9fe4432..016795b 100644 --- a/components/InspectorOperations.tsx +++ b/components/InspectorOperations.tsx @@ -157,6 +157,7 @@ export default function InspectorOperations({ const deleteFileFromAttachments = (File: any) => { const filtered = file?.filter((item) => item.name !== File); setFile(filtered); + // @ts-expect-error form.setValue("media", filtered); }; diff --git a/components/NavbarPanel.tsx b/components/NavbarPanel.tsx index 550de4b..7412c20 100644 --- a/components/NavbarPanel.tsx +++ b/components/NavbarPanel.tsx @@ -9,6 +9,8 @@ import { getProfile, revoke } from "@/utils/authenticateApi"; import { useMutation, useQuery } from "@tanstack/react-query"; import { DoorOpen, LogOut } from "lucide-react"; import { deleteCookie, getCookie } from "cookies-next"; +import useSignalR from "@/hooks/useSignalR"; + export default function NavbarPanel() { const [isOpen, setOpen] = useState(false); const ref = useRef(null); @@ -53,6 +55,7 @@ export default function NavbarPanel() { localStorage.removeItem("privatePassword"); router.push("/"); }; + const signalr = useSignalR(); return (
diff --git a/components/ui/progress.tsx b/components/ui/progress.tsx new file mode 100644 index 0000000..5c87ea4 --- /dev/null +++ b/components/ui/progress.tsx @@ -0,0 +1,28 @@ +"use client" + +import * as React from "react" +import * as ProgressPrimitive from "@radix-ui/react-progress" + +import { cn } from "@/lib/utils" + +const Progress = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, value, ...props }, ref) => ( + + + +)) +Progress.displayName = ProgressPrimitive.Root.displayName + +export { Progress } diff --git a/components/ui/sonner.tsx b/components/ui/sonner.tsx index 593c458..a736a3c 100644 --- a/components/ui/sonner.tsx +++ b/components/ui/sonner.tsx @@ -10,7 +10,7 @@ const Toaster = ({ ...props }: ToasterProps) => { return ( { + const queryClient = useQueryClient(); + const pathname = usePathname(); + const param = useParams(); + const isInComplaintDetailPage = pathname.includes( + "/UserPanel/ControllerDashboard/Complaints/" + ); + const postConnectionIdMutation = useMutation({ + mutationKey: ["connectionId"], + mutationFn: postConnectionId, + onSuccess: (res) => {}, + onError: (err) => {}, + }); + useEffect(() => { + const role = localStorage.getItem("Sootzani_Role"); + if (role != "Admin") { + const startConnection = async () => { + try { + await connection.start(); + console.log("SignalR connected"); + console.log(connection.connectionId); + + postConnectionIdMutation.mutate(connection.connectionId); + connection.on("Created", (message) => { + queryClient.invalidateQueries(["ComplaintsList"]); + toast.info("یک درخواست جدید دارید."); + }); + connection.on("Updated", (message, id) => { + toast.info(`${message}`); + if (param.id == id) { + queryClient.invalidateQueries(["Complaint", id]); + } + }); + } catch (err) { + console.error("SignalR connection failed: ", err); + } + }; + + startConnection(); + } + + // console.log(connection.connectionId); + + // return () => { + // if (connection.state === "Connected") { + // connection.stop(); + // console.log("SignalR disconnected"); + // } + // }; + }, []); + + useEffect(() => { + if (connection.connectionId) { + console.log(connection.connectionId); + } + }, [connection]); + // console.log(connection.state); + // useEffect(() => { + // console.log(11111); + // console.log(connection.state); + // // Subscribe to events and update query data accordingly + // // Example: Listen for a "newMessage" event and update a query with the new message data + + // console.log(connection.state); + + // }, [queryClient]); + + // return connection; +}; + +export default useSignalR; diff --git a/lib/utils.ts b/lib/utils.ts index 315a09d..4b20790 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -39,7 +39,6 @@ export function Decrypt(encryptedData: any, privateKey: any) { } export function formatBytes(bytes: number, decimals = 2) { - console.log(bytes); if (!+bytes) return "0 Bytes"; const k = 1024; diff --git a/package-lock.json b/package-lock.json index 0b1574e..d8e648f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,12 +9,14 @@ "version": "0.2.14", "dependencies": { "@hookform/resolvers": "^3.3.2", + "@microsoft/signalr": "^8.0.0", "@radix-ui/react-alert-dialog": "^1.0.5", "@radix-ui/react-checkbox": "^1.0.4", "@radix-ui/react-dialog": "^1.0.5", "@radix-ui/react-dropdown-menu": "^2.0.6", "@radix-ui/react-icons": "^1.3.0", "@radix-ui/react-label": "^2.0.2", + "@radix-ui/react-progress": "^1.0.3", "@radix-ui/react-radio-group": "^1.1.3", "@radix-ui/react-scroll-area": "^1.0.5", "@radix-ui/react-select": "^2.0.0", @@ -46,6 +48,7 @@ "lucide-react": "^0.292.0", "next": "14.0.3", "next-nprogress-bar": "^2.1.2", + "next-runtime-env": "^3.2.0", "next-themes": "^0.2.1", "nextjs-toploader": "^1.6.4", "npm": "^10.2.5", @@ -341,6 +344,18 @@ "resolved": "https://registry.npmjs.org/@kurkle/color/-/color-0.3.2.tgz", "integrity": "sha512-fuscdXJ9G1qb7W8VdHi+IwRqij3lBkosAm4ydQtEmbY58OzHXqQhvlxqEkoz0yssNVn38bcpRWgA9PP+OGoisw==" }, + "node_modules/@microsoft/signalr": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/@microsoft/signalr/-/signalr-8.0.0.tgz", + "integrity": "sha512-K/wS/VmzRWePCGqGh8MU8OWbS1Zvu7DG7LSJS62fBB8rJUXwwj4axQtqrAAwKGUZHQF6CuteuQR9xMsVpM2JNA==", + "dependencies": { + "abort-controller": "^3.0.0", + "eventsource": "^2.0.2", + "fetch-cookie": "^2.0.3", + "node-fetch": "^2.6.7", + "ws": "^7.4.5" + } + }, "node_modules/@next/env": { "version": "14.0.3", "resolved": "https://registry.npmjs.org/@next/env/-/env-14.0.3.tgz", @@ -1021,6 +1036,30 @@ } } }, + "node_modules/@radix-ui/react-progress": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@radix-ui/react-progress/-/react-progress-1.0.3.tgz", + "integrity": "sha512-5G6Om/tYSxjSeEdrb1VfKkfZfn/1IlPWd731h2RfPuSbIfNUgfqAwbKfJCg/PP6nuUCTrYzalwHSpSinoWoCag==", + "dependencies": { + "@babel/runtime": "^7.13.10", + "@radix-ui/react-context": "1.0.1", + "@radix-ui/react-primitive": "1.0.3" + }, + "peerDependencies": { + "@types/react": "*", + "@types/react-dom": "*", + "react": "^16.8 || ^17.0 || ^18.0", + "react-dom": "^16.8 || ^17.0 || ^18.0" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "@types/react-dom": { + "optional": true + } + } + }, "node_modules/@radix-ui/react-radio-group": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/@radix-ui/react-radio-group/-/react-radio-group-1.1.3.tgz", @@ -1759,6 +1798,17 @@ "resolved": "https://registry.npmjs.org/@xobotyi/scrollbar-width/-/scrollbar-width-1.9.5.tgz", "integrity": "sha512-N8tkAACJx2ww8vFMneJmaAgmjAG1tnVBZJRLRcx061tmsLRZHSEZSLuGWnwPtunsSLvSqXQ2wfp7Mgqg1I+2dQ==" }, + "node_modules/abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "dependencies": { + "event-target-shim": "^5.0.0" + }, + "engines": { + "node": ">=6.5" + } + }, "node_modules/acorn": { "version": "8.11.2", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.2.tgz", @@ -3326,11 +3376,27 @@ "node": ">=0.10.0" } }, + "node_modules/event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", + "engines": { + "node": ">=6" + } + }, "node_modules/eventemitter3": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-2.0.3.tgz", "integrity": "sha512-jLN68Dx5kyFHaePoXWPsCGW5qdyZQtLYHkxkg02/Mz6g0kYpDx4FyP6XfArhQdlOC4b8Mv+EMxPo/8La7Tzghg==" }, + "node_modules/eventsource": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/eventsource/-/eventsource-2.0.2.tgz", + "integrity": "sha512-IzUmBGPR3+oUG9dUeXynyNmf91/3zUSJg1lCktzKw47OXuhco54U3r9B7O4XX+Rb1Itm9OZ2b0RkTs10bICOxA==", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -3420,6 +3486,15 @@ "reusify": "^1.0.4" } }, + "node_modules/fetch-cookie": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/fetch-cookie/-/fetch-cookie-2.2.0.tgz", + "integrity": "sha512-h9AgfjURuCgA2+2ISl8GbavpUdR+WGAM2McW/ovn4tVccegp8ZqCKWSBR8uRdM8dDNlx5WdKRWxBYUwteLDCNQ==", + "dependencies": { + "set-cookie-parser": "^2.4.8", + "tough-cookie": "^4.0.0" + } + }, "node_modules/figures": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", @@ -4866,6 +4941,19 @@ "nprogress": "^0.2.0" } }, + "node_modules/next-runtime-env": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/next-runtime-env/-/next-runtime-env-3.2.0.tgz", + "integrity": "sha512-rwe3flUgSRm51hzRN4Vt5MMSYMS4aDMEPJa0r+CMONA3UyUZl8Y5O8zjHSIlaNb3yquTCttZ0ahObPyPprBj9g==", + "dependencies": { + "next": "^14", + "react": "^18" + }, + "peerDependencies": { + "next": "^14", + "react": "^18" + } + }, "node_modules/next-themes": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/next-themes/-/next-themes-0.2.1.tgz", @@ -4891,6 +4979,25 @@ "react-dom": ">= 16.0.0" } }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, "node_modules/node-releases": { "version": "2.0.13", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.13.tgz", @@ -8139,15 +8246,24 @@ "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" }, + "node_modules/psl": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", + "integrity": "sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==" + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", - "dev": true, "engines": { "node": ">=6" } }, + "node_modules/querystringify": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz", + "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==" + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -8539,6 +8655,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/requires-port": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz", + "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==" + }, "node_modules/resize-observer-polyfill": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", @@ -8750,6 +8871,11 @@ "node": ">=10" } }, + "node_modules/set-cookie-parser": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.6.0.tgz", + "integrity": "sha512-RVnVQxTXuerk653XfuliOxBP81Sf0+qfQE73LIYKcyMYHG94AuH0kgrQpRDuTZnSmjpysHmzxJXKNfa6PjFhyQ==" + }, "node_modules/set-function-length": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.1.1.tgz", @@ -9334,6 +9460,25 @@ "resolved": "https://registry.npmjs.org/toggle-selection/-/toggle-selection-1.0.6.tgz", "integrity": "sha512-BiZS+C1OS8g/q2RRbJmy59xpyghNBqrr6k5L/uKBGRsTfxmu3ffiRnd8mlGPUVayg8pvfi5urfnu8TU7DVOkLQ==" }, + "node_modules/tough-cookie": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.3.tgz", + "integrity": "sha512-aX/y5pVRkfRnfmuX+OdbSdXvPe6ieKX/G2s7e98f4poJHnqH3281gDPm/metm6E/WRamfx7WC4HUqkWHfQHprw==", + "dependencies": { + "psl": "^1.1.33", + "punycode": "^2.1.1", + "universalify": "^0.2.0", + "url-parse": "^1.5.3" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/ts-api-utils": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.0.3.tgz", @@ -9496,6 +9641,14 @@ "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", "dev": true }, + "node_modules/universalify": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.2.0.tgz", + "integrity": "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==", + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/update-browserslist-db": { "version": "1.0.13", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.13.tgz", @@ -9535,6 +9688,15 @@ "punycode": "^2.1.0" } }, + "node_modules/url-parse": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz", + "integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==", + "dependencies": { + "querystringify": "^2.1.1", + "requires-port": "^1.0.0" + } + }, "node_modules/use-callback-ref": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/use-callback-ref/-/use-callback-ref-1.3.0.tgz", @@ -9609,6 +9771,20 @@ "defaults": "^1.0.3" } }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -9718,6 +9894,26 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, + "node_modules/ws": { + "version": "7.5.9", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.9.tgz", + "integrity": "sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==", + "engines": { + "node": ">=8.3.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", diff --git a/package.json b/package.json index 66a25ab..d6923f8 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,14 @@ }, "dependencies": { "@hookform/resolvers": "^3.3.2", + "@microsoft/signalr": "^8.0.0", "@radix-ui/react-alert-dialog": "^1.0.5", "@radix-ui/react-checkbox": "^1.0.4", "@radix-ui/react-dialog": "^1.0.5", "@radix-ui/react-dropdown-menu": "^2.0.6", "@radix-ui/react-icons": "^1.3.0", "@radix-ui/react-label": "^2.0.2", + "@radix-ui/react-progress": "^1.0.3", "@radix-ui/react-radio-group": "^1.1.3", "@radix-ui/react-scroll-area": "^1.0.5", "@radix-ui/react-select": "^2.0.0", @@ -47,6 +49,7 @@ "lucide-react": "^0.292.0", "next": "14.0.3", "next-nprogress-bar": "^2.1.2", + "next-runtime-env": "^3.2.0", "next-themes": "^0.2.1", "nextjs-toploader": "^1.6.4", "npm": "^10.2.5", diff --git a/utils/Provider.jsx b/utils/ReactQuery/Provider.jsx similarity index 100% rename from utils/Provider.jsx rename to utils/ReactQuery/Provider.jsx diff --git a/utils/getQueryClient.jsx b/utils/ReactQuery/getQueryClient.jsx similarity index 100% rename from utils/getQueryClient.jsx rename to utils/ReactQuery/getQueryClient.jsx diff --git a/utils/hydrate.client.jsx b/utils/ReactQuery/hydrate.client.jsx similarity index 100% rename from utils/hydrate.client.jsx rename to utils/ReactQuery/hydrate.client.jsx diff --git a/utils/adminApi.ts b/utils/adminApi.ts index 907f215..301784c 100644 --- a/utils/adminApi.ts +++ b/utils/adminApi.ts @@ -1,7 +1,8 @@ import axios from "axios"; import { CookieValueTypes, getCookie } from "cookies-next"; - -axios.defaults.baseURL = process.env.NEXT_PUBLIC_BASE_URL_APP; +import { env } from "next-runtime-env"; +const NEXT_PUBLIC_BASE_URL_APP = env("NEXT_PUBLIC_BASE_URL_APP"); +axios.defaults.baseURL = NEXT_PUBLIC_BASE_URL_APP; axios.interceptors.request.use(function (config) { const token = getCookie("Sootzani_Admin_Token"); @@ -27,6 +28,7 @@ export async function AddKey(payload: { title: string; publicKey: string }) { export async function ChangeKey(payload: { privateKey: string; publicKeyId: string; + isPolling: boolean; }) { const data = await axios.post("api/Admin/ChangeKey", payload); return data.data; diff --git a/utils/authenticateApi.ts b/utils/authenticateApi.ts index b422d8d..d5c93a0 100644 --- a/utils/authenticateApi.ts +++ b/utils/authenticateApi.ts @@ -5,8 +5,9 @@ import { getCookie, setCookie, } from "cookies-next"; - -axios.defaults.baseURL = process.env.NEXT_PUBLIC_BASE_URL_APP; +import { env } from "next-runtime-env"; +const NEXT_PUBLIC_BASE_URL_APP = env("NEXT_PUBLIC_BASE_URL_APP"); +axios.defaults.baseURL = NEXT_PUBLIC_BASE_URL_APP; let isRefreshing = false; diff --git a/utils/commonApi.ts b/utils/commonApi.ts index 2bb73bc..d1827f2 100644 --- a/utils/commonApi.ts +++ b/utils/commonApi.ts @@ -22,4 +22,6 @@ export async function getContents() { export async function getOptions() { const data = await axios.get("/api/Common/Options"); return data.data; -} \ No newline at end of file +} + + diff --git a/utils/infoApi.ts b/utils/infoApi.ts index 48f9074..a2f09d5 100644 --- a/utils/infoApi.ts +++ b/utils/infoApi.ts @@ -1,7 +1,8 @@ import axios from "axios"; import { CookieValueTypes, getCookie } from "cookies-next"; - -axios.defaults.baseURL = process.env.NEXT_PUBLIC_BASE_URL_APP; +import { env } from "next-runtime-env"; +const NEXT_PUBLIC_BASE_URL_APP = env("NEXT_PUBLIC_BASE_URL_APP"); +axios.defaults.baseURL = NEXT_PUBLIC_BASE_URL_APP; axios.interceptors.request.use(function (config) { const token = getCookie("Sootzani_Admin_Token"); diff --git a/utils/inspectorApi.ts b/utils/inspectorApi.ts index b80eca3..90d765f 100644 --- a/utils/inspectorApi.ts +++ b/utils/inspectorApi.ts @@ -3,8 +3,9 @@ import axios from "axios"; import { CookieValueTypes, getCookie } from "cookies-next"; import { RefreshToken } from "./authenticateApi"; - -axios.defaults.baseURL = process.env.NEXT_PUBLIC_BASE_URL_APP; +import { env } from "next-runtime-env"; +const NEXT_PUBLIC_BASE_URL_APP = env("NEXT_PUBLIC_BASE_URL_APP"); +axios.defaults.baseURL = NEXT_PUBLIC_BASE_URL_APP; axios.interceptors.request.use(function (config) { const token = getCookie("Sootzani_Admin_Token"); @@ -50,3 +51,10 @@ export async function OperateByInspector(payload: FormData) { }); return data.data; } + +export async function postConnectionId(id: string) { + const data = await axios.post("/api/Common/ConnectionId", id, { + headers: { "Content-Type": "application/json" }, + }); + return data.data; +} diff --git a/utils/signalrConnection.tsx b/utils/signalrConnection.tsx new file mode 100644 index 0000000..e5ac256 --- /dev/null +++ b/utils/signalrConnection.tsx @@ -0,0 +1,27 @@ +import { + HttpTransportType, + HubConnectionBuilder, + JsonHubProtocol, + LogLevel, +} from "@microsoft/signalr"; +import { getCookie } from "cookies-next"; + +const token = getCookie("Sootzani_Admin_Token"); + +const connection = new HubConnectionBuilder() + .withUrl("https://sootzaniapi.shetabdahi.ir/notifhub", { + headers: { + Authorization: `Bearer ${token}`, + }, + }) + .withAutomaticReconnect() + .withHubProtocol(new JsonHubProtocol()) + .configureLogging(LogLevel.Debug) + .build(); + +// Optional: Configure connection options, such as access token or headers +// connection.on("your-event", (data) => { +// // Handle incoming events +// }); + +export default connection;