From 90bfc53380e676d56d257f493e71ea26a100da3c Mon Sep 17 00:00:00 2001 From: jasquat <2487833+jasquat@users.noreply.github.com> Date: Fri, 7 Jul 2023 10:54:57 -0400 Subject: [PATCH] use a sha256 value as the id of the page when checking active-users w/ burnettk (#356) Co-authored-by: jasquat --- .../src/components/ActiveUsers.tsx | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/spiffworkflow-frontend/src/components/ActiveUsers.tsx b/spiffworkflow-frontend/src/components/ActiveUsers.tsx index 87b2e885..96e49373 100644 --- a/spiffworkflow-frontend/src/components/ActiveUsers.tsx +++ b/spiffworkflow-frontend/src/components/ActiveUsers.tsx @@ -1,36 +1,51 @@ import { useEffect, useState } from 'react'; import HttpService from '../services/HttpService'; -import { - encodeBase64, - refreshAtInterval, - REFRESH_TIMEOUT_SECONDS, -} from '../helpers'; import { User } from '../interfaces'; +import { refreshAtInterval, REFRESH_TIMEOUT_SECONDS } from '../helpers'; + +async function sha256(message: string) { + // encode as UTF-8 + const msgBuffer = new TextEncoder().encode(message); + + // hash the message + const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer); + + // convert ArrayBuffer to Array + const hashArray = Array.from(new Uint8Array(hashBuffer)); + + // convert bytes to hex string + return hashArray.map((b) => b.toString(16).padStart(2, '0')).join(''); +} export default function ActiveUsers() { // Handles getting and displaying active users. const [activeUsers, setActiveUsers] = useState([]); - const lastVisitedIdentifier = encodeBase64(window.location.pathname); useEffect(() => { const updateActiveUsers = () => { - HttpService.makeCallToBackend({ - path: `/active-users/updates/${lastVisitedIdentifier}`, - successCallback: setActiveUsers, - httpMethod: 'POST', - }); - }; - - const unregisterUser = () => { - HttpService.makeCallToBackend({ - path: `/active-users/unregister/${lastVisitedIdentifier}`, - successCallback: setActiveUsers, - httpMethod: 'POST', - }); + const makeCall = (lastVisitedIdentifier: any) => { + HttpService.makeCallToBackend({ + path: `/active-users/updates/${lastVisitedIdentifier}`, + successCallback: setActiveUsers, + httpMethod: 'POST', + }); + }; + sha256(window.location.pathname).then(makeCall); }; updateActiveUsers(); + const unregisterUser = () => { + const makeCall = (lastVisitedIdentifier: any) => { + HttpService.makeCallToBackend({ + path: `/active-users/unregister/${lastVisitedIdentifier}`, + successCallback: setActiveUsers, + httpMethod: 'POST', + }); + }; + sha256(window.location.pathname).then(makeCall); + }; + return refreshAtInterval( 15, REFRESH_TIMEOUT_SECONDS,