From c30619cd5d084bead78a9c740e9e63355abfaf52 Mon Sep 17 00:00:00 2001 From: Marius Tobiassen Bungum Date: Mon, 15 Jul 2024 10:26:40 +0200 Subject: [PATCH] :fire: Removed useSignalRMessages since it isn't a part of sam --- package-lock.json | 35 +++-- package.json | 3 +- src/api/index.ts | 2 +- src/hooks/index.ts | 2 - src/hooks/useSignalRMessages.ts | 227 -------------------------------- 5 files changed, 28 insertions(+), 241 deletions(-) delete mode 100644 src/hooks/useSignalRMessages.ts diff --git a/package-lock.json b/package-lock.json index 61e13aa..7b1389d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,16 @@ { "name": "@equinor/subsurface-app-management", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@equinor/subsurface-app-management", - "version": "1.0.4", + "version": "1.0.5", "license": "ISC", "dependencies": { "@azure/msal-browser": "3.10.0", "@azure/msal-react": "2.0.12", - "@microsoft/signalr": "^8.0.0", "jwt-decode": "^4.0.0" }, "devDependencies": { @@ -3520,6 +3519,7 @@ "version": "8.0.0", "resolved": "https://registry.npmjs.org/@microsoft/signalr/-/signalr-8.0.0.tgz", "integrity": "sha512-K/wS/VmzRWePCGqGh8MU8OWbS1Zvu7DG7LSJS62fBB8rJUXwwj4axQtqrAAwKGUZHQF6CuteuQR9xMsVpM2JNA==", + "dev": true, "dependencies": { "abort-controller": "^3.0.0", "eventsource": "^2.0.2", @@ -10304,6 +10304,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "dev": true, "dependencies": { "event-target-shim": "^5.0.0" }, @@ -13661,6 +13662,7 @@ "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==", + "dev": true, "engines": { "node": ">=6" } @@ -13675,6 +13677,7 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/eventsource/-/eventsource-2.0.2.tgz", "integrity": "sha512-IzUmBGPR3+oUG9dUeXynyNmf91/3zUSJg1lCktzKw47OXuhco54U3r9B7O4XX+Rb1Itm9OZ2b0RkTs10bICOxA==", + "dev": true, "engines": { "node": ">=12.0.0" } @@ -13839,6 +13842,7 @@ "version": "2.2.0", "resolved": "https://registry.npmjs.org/fetch-cookie/-/fetch-cookie-2.2.0.tgz", "integrity": "sha512-h9AgfjURuCgA2+2ISl8GbavpUdR+WGAM2McW/ovn4tVccegp8ZqCKWSBR8uRdM8dDNlx5WdKRWxBYUwteLDCNQ==", + "dev": true, "dependencies": { "set-cookie-parser": "^2.4.8", "tough-cookie": "^4.0.0" @@ -18258,6 +18262,7 @@ "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==", + "dev": true, "dependencies": { "whatwg-url": "^5.0.0" }, @@ -18282,17 +18287,20 @@ "node_modules/node-fetch/node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", + "dev": true }, "node_modules/node-fetch/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==" + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", + "dev": true }, "node_modules/node-fetch/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==", + "dev": true, "dependencies": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" @@ -22303,7 +22311,8 @@ "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", - "integrity": "sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==" + "integrity": "sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==", + "dev": true }, "node_modules/pump": { "version": "3.0.0", @@ -22340,6 +22349,7 @@ "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" } @@ -22371,7 +22381,8 @@ "node_modules/querystringify": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz", - "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==" + "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==", + "dev": true }, "node_modules/queue-microtask": { "version": "1.2.3", @@ -23165,7 +23176,8 @@ "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==" + "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==", + "dev": true }, "node_modules/resolve": { "version": "1.22.8", @@ -23895,7 +23907,8 @@ "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==" + "integrity": "sha512-RVnVQxTXuerk653XfuliOxBP81Sf0+qfQE73LIYKcyMYHG94AuH0kgrQpRDuTZnSmjpysHmzxJXKNfa6PjFhyQ==", + "dev": true }, "node_modules/set-function-length": { "version": "1.2.2", @@ -25136,6 +25149,7 @@ "version": "4.1.4", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.4.tgz", "integrity": "sha512-Loo5UUvLD9ScZ6jh8beX1T6sO1w2/MpCRpEP7V280GKMVUQ0Jzar2U3UJPsrdbziLEMMhu3Ujnq//rhiFuIeag==", + "dev": true, "dependencies": { "psl": "^1.1.33", "punycode": "^2.1.1", @@ -25150,6 +25164,7 @@ "version": "0.2.0", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.2.0.tgz", "integrity": "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==", + "dev": true, "engines": { "node": ">= 4.0.0" } @@ -25771,6 +25786,7 @@ "version": "1.5.10", "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz", "integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==", + "dev": true, "dependencies": { "querystringify": "^2.1.1", "requires-port": "^1.0.0" @@ -27060,6 +27076,7 @@ "version": "7.5.10", "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz", "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==", + "dev": true, "engines": { "node": ">=8.3.0" }, diff --git a/package.json b/package.json index 6b7c113..e555562 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@equinor/subsurface-app-management", - "version": "1.0.4", + "version": "1.0.5", "description": "React Typescript components/hooks to communicate with equinor/sam", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -47,7 +47,6 @@ "dependencies": { "@azure/msal-browser": "3.10.0", "@azure/msal-react": "2.0.12", - "@microsoft/signalr": "^8.0.0", "jwt-decode": "^4.0.0" }, "devDependencies": { diff --git a/src/api/index.ts b/src/api/index.ts index f87f3df..bd229e3 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,7 +3,7 @@ /* eslint-disable */ export { ApiError } from './core/ApiError'; export { CancelablePromise, CancelError } from './core/CancelablePromise'; -export { OpenAPI } from './core/OpenAPI'; +export { OpenAPI, getPortalToken } from './core/OpenAPI'; export { request } from './core/request'; export type { OpenAPIConfig } from './core/OpenAPI'; diff --git a/src/hooks/index.ts b/src/hooks/index.ts index f4ae0c3..b5c340a 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -3,13 +3,11 @@ import { useReleaseNotes } from '../providers/ReleaseNotesProvider'; import { useTutorialSteps } from '../providers/TutorialStepsProvider'; import { useFeatureToggling } from './useFeatureToggling'; import { useReleaseNotesQuery } from './useReleaseNotesQuery'; -import { useSignalRMessages } from './useSignalRMessages'; export { useAuth, useFeatureToggling, useReleaseNotesQuery, - useSignalRMessages, useTutorialSteps, useReleaseNotes, }; diff --git a/src/hooks/useSignalRMessages.ts b/src/hooks/useSignalRMessages.ts deleted file mode 100644 index 1b4cced..0000000 --- a/src/hooks/useSignalRMessages.ts +++ /dev/null @@ -1,227 +0,0 @@ -import { useEffect, useMemo, useRef, useState } from 'react'; - -import { usePrevious } from '@equinor/amplify-components'; -import * as SignalR from '@microsoft/signalr'; -import { LogLevel } from '@microsoft/signalr'; -import { HubConnection } from '@microsoft/signalr/dist/esm/HubConnection'; -import { useQuery } from '@tanstack/react-query'; - -import { getPortalToken } from 'src/api/core/OpenAPI'; -import { EnvironmentType } from 'src/types/Environment'; -import { environment } from 'src/utils/auth_environment'; - -export function useSignalRMessages< - T extends { - SequenceNumber?: number | null; - Read?: boolean | null; - Subject?: string | null; - }, ->(topic: string) { - const connectionRef = useRef(undefined); - const [messages, setMessages] = useState([]); - const [deletedMessageSequenceNumber, setDeletedMessageSequenceNumber] = - useState(); - const [updateMessage, setUpdateMessage] = useState(); - - const { data: amplifyPortalToken } = useQuery({ - queryKey: ['get-amplify-portal-token'], - queryFn: () => getPortalToken(), - }); - - const host = useMemo(() => { - const environmentName = environment.getEnvironmentName( - import.meta.env.VITE_ENVIRONMENT_NAME - ); - - const environmentNameForUrl = - environmentName === EnvironmentType.LOCALHOST - ? EnvironmentType.DEVELOP - : environmentName; - - return `wss://api-amplify-portal-${environmentNameForUrl}.radix.equinor.com`; - }, []); - - const previousTopic = usePrevious(topic); - const previousAmplifyPortalToken = usePrevious(amplifyPortalToken); - useEffect(() => { - async function setupConnection() { - if ( - amplifyPortalToken === undefined || - (previousTopic === topic && - previousAmplifyPortalToken === amplifyPortalToken) - ) { - return; - } - - if ( - connectionRef.current !== undefined && - previousTopic && - previousAmplifyPortalToken && - (previousTopic !== topic || - previousAmplifyPortalToken !== amplifyPortalToken) - ) { - await connectionRef.current.stop(); - } - - const connection = new SignalR.HubConnectionBuilder() - .configureLogging(LogLevel.Error) - .withUrl(`${host}/hubs/notifications`, { - accessTokenFactory: () => amplifyPortalToken, - withCredentials: false, - skipNegotiation: true, - transport: SignalR.HttpTransportType.WebSockets, - }) - .withAutomaticReconnect() - .build(); - - try { - await connection.start(); - - /* receive messages */ - connection.on( - 'ActiveMessages', - (subject: string, activeMessages: T[]) => { - setMessages( - activeMessages.map((d) => ({ ...d, Subject: subject })).reverse() - ); - } - ); - - /* connect to a topic */ - connection.onreconnected(() => connection.invoke('Subscribe', topic)); - - /* receive one new message from the servicebus */ - connection.on('NewMessage', (subject: string, message: T) => { - setMessages((ct) => [{ ...message, Subject: subject }, ...ct]); - }); - - /* update a message */ - connection.on('UpdateMessage', (subject: string, message: T) => { - /** - * you cant get the value of state variables inside connection.on() - * but you can still set state. - * this uses another useEffect by setting a temp state that updates the message state. - */ - setUpdateMessage({ ...message, Subject: subject }); - }); - - /* delete a message from servicebus */ - connection.on( - 'Delete', - (subject: string, messageSequenceNumber: number) => { - /** - * you cant get the value of state variables inside connection.on() - * but you can still set state. - * this uses another useEffect by setting a temp state that updates the messages state. - */ - setDeletedMessageSequenceNumber(messageSequenceNumber); - } - ); - - /* print error to console */ - connection.on('Error', (msg: string) => { - throw new Error('Connection error: ' + msg); - }); - - /* connect to a topic */ - await connection.invoke('Subscribe', topic); - - /* Get all active messages */ - await connection.invoke('PeekMessages'); - } catch (error) { - console.error('Connection failed', error); - } - connectionRef.current = connection; - } - - setupConnection().catch((error) => { - console.error('Error setting up connection', error); - }); - }, [ - host, - topic, - amplifyPortalToken, - previousTopic, - previousAmplifyPortalToken, - ]); - - // Update notification useeffect - useEffect(() => { - if (updateMessage) { - const updateMessageIndex = messages.findIndex( - (x) => x.SequenceNumber === updateMessage.SequenceNumber - ); - const tempNotifications = [...messages]; - if (updateMessageIndex > -1) { - tempNotifications[updateMessageIndex] = updateMessage; - setMessages(tempNotifications); - setUpdateMessage(undefined); - } - } - }, [updateMessage, messages]); - - // Delete notification useEffect - useEffect(() => { - /* remove notification from state */ - if (deletedMessageSequenceNumber) { - const removeNotificationIndex = messages.findIndex( - (x) => x.SequenceNumber === deletedMessageSequenceNumber - ); - const tempNotifications = [...messages]; - if (removeNotificationIndex > -1) { - tempNotifications.splice(removeNotificationIndex, 1); - setMessages(tempNotifications); - setDeletedMessageSequenceNumber(undefined); - } - } - }, [deletedMessageSequenceNumber, messages]); - - async function deleteMessage(message: T) { - if (connectionRef.current && message.SequenceNumber !== null) { - await connectionRef.current.invoke( - 'DeleteMessage', - message.SequenceNumber - ); - setDeletedMessageSequenceNumber(message.SequenceNumber); - } - } - - const setMessageAsRead = (message: T) => { - if (connectionRef.current) { - message.Read = !message.Read; - connectionRef.current - .invoke('PatchMessage', message.SequenceNumber, message) - .catch((error) => { - console.error('Error setting message as read', error); - }); - } - }; - - const setAllMessagesAsRead = () => { - messages.forEach((no) => { - if (!no.Read) { - setMessageAsRead(no); - } - }); - }; - - const hasUnreadMessages = useMemo(() => { - return messages.some((no) => no.Read === false); - }, [messages]); - - const closeConnection = async () => { - if (connectionRef.current) { - return await connectionRef.current.stop(); - } - return; - }; - - return { - messages, - hasUnreadMessages, - setMessageAsRead, - setAllMessagesAsRead, - deleteMessage, - closeConnection, - }; -}