From 75a715d002f005e327442015dc322b278f59bc8e Mon Sep 17 00:00:00 2001 From: Stixx Date: Tue, 10 Sep 2024 16:00:48 +0200 Subject: [PATCH 1/8] feat: Add Sigit ID as a path param --- src/components/DisplaySigit/index.tsx | 5 +-- src/pages/home/index.tsx | 1 + src/pages/sign/index.tsx | 51 +++++++++++++++++++++++---- src/routes/index.tsx | 5 +++ 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/components/DisplaySigit/index.tsx b/src/components/DisplaySigit/index.tsx index 473a942..00716a3 100644 --- a/src/components/DisplaySigit/index.tsx +++ b/src/components/DisplaySigit/index.tsx @@ -24,11 +24,12 @@ import { useSigitMeta } from '../../hooks/useSigitMeta' import { extractFileExtensions } from '../../utils/file' type SigitProps = { + sigitKey: string meta: Meta parsedMeta: SigitCardDisplayInfo } -export const DisplaySigit = ({ meta, parsedMeta }: SigitProps) => { +export const DisplaySigit = ({ meta, parsedMeta, sigitKey }: SigitProps) => { const { title, createdAt, submittedBy, signers, signedStatus, isValid } = parsedMeta @@ -47,7 +48,7 @@ export const DisplaySigit = ({ meta, parsedMeta }: SigitProps) => { to={ signedStatus === SigitStatus.Complete ? appPublicRoutes.verify - : appPrivateRoutes.sign + : `${appPrivateRoutes.sign}/${sigitKey}` } state={{ meta }} className={styles.insetLink} diff --git a/src/pages/home/index.tsx b/src/pages/home/index.tsx index c93c9f8..3e333d3 100644 --- a/src/pages/home/index.tsx +++ b/src/pages/home/index.tsx @@ -257,6 +257,7 @@ export const HomePage = () => { .map((key) => ( diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index 8720954..70d9784 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -7,7 +7,7 @@ import { MuiFileInput } from 'mui-file-input' import { Event, verifyEvent } from 'nostr-tools' import { useCallback, useEffect, useState } from 'react' import { useSelector } from 'react-redux' -import { useLocation, useNavigate } from 'react-router-dom' +import { useLocation, useNavigate, useParams } from 'react-router-dom' import { toast } from 'react-toastify' import { LoadingSpinner } from '../../components/LoadingSpinner' import { NostrController } from '../../controllers' @@ -53,6 +53,7 @@ import { SigitFile } from '../../utils/file.ts' import { ARRAY_BUFFER, DEFLATE } from '../../utils/const.ts' +import { useAppSelector } from '../../hooks/store.ts' enum SignedStatus { Fully_Signed, User_Is_Next_Signer, @@ -62,17 +63,20 @@ enum SignedStatus { export const SignPage = () => { const navigate = useNavigate() const location = useLocation() + const params = useParams() /** + * Received from `location.state` + * * uploadedZip will be received from home page when a user uploads a sigit zip wrapper that contains keys.json * arrayBuffer will be received in navigation from create page in offline mode * meta will be received in navigation from create & home page in online mode */ - const { - meta: metaInNavState, - arrayBuffer: decryptedArrayBuffer, - uploadedZip - } = location.state || {} + const [metaInNavState, setMetaInNavState] = useState() + const [decryptedArrayBuffer, setDecryptedArrayBuffer] = useState< + ArrayBuffer | undefined + >() + const [uploadedZip, setUploadedZip] = useState() const [displayInput, setDisplayInput] = useState(false) @@ -115,6 +119,41 @@ export const SignPage = () => { const [isMarksCompleted, setIsMarksCompleted] = useState(false) const [otherUserMarks, setOtherUserMarks] = useState([]) + const usersAppData = useAppSelector((state) => state.userAppData) + + /** + * When location state changes, update the states in the component + * If sigit id is found in the URL PARAM we will set metaInNavState manually + * after we do the fetching based on the ID + */ + useEffect(() => { + if (location.state) { + const { meta, arrayBuffer, uploadedZip } = location.state + + setMetaInNavState(meta) + setDecryptedArrayBuffer(arrayBuffer) + setUploadedZip(uploadedZip) + } + }, [location.state]) + + useEffect(() => { + // If meta in nav state is populated we will not try to fetch sigit details + // from the redux store + if (metaInNavState) return + + if (usersAppData) { + const sigitId = params.id + + if (sigitId) { + const sigit = usersAppData.sigits[sigitId] + + if (sigit) { + setMetaInNavState(sigit) + } + } + } + }, [usersAppData, metaInNavState, params.id]) + useEffect(() => { if (signers.length > 0) { // check if all signers have signed then its fully signed diff --git a/src/routes/index.tsx b/src/routes/index.tsx index 2d36db1..643017f 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -19,6 +19,7 @@ export const appPrivateRoutes = { homePage: '/', create: '/create', sign: '/sign', + signId: '/sign/:id', settings: '/settings', profileSettings: '/settings/profile/:npub', cacheSettings: '/settings/cache', @@ -132,6 +133,10 @@ export const privateRoutes = [ path: appPrivateRoutes.sign, element: }, + { + path: appPrivateRoutes.signId, + element: + }, { path: appPrivateRoutes.settings, element: -- 2.34.1 From 8e71592d8815471bde6fb74230ba9742308daad8 Mon Sep 17 00:00:00 2001 From: Stixx Date: Wed, 11 Sep 2024 11:59:12 +0200 Subject: [PATCH 2/8] fix: routing, removed useEffect --- src/components/DisplaySigit/index.tsx | 1 - src/pages/sign/index.tsx | 63 ++++++++++----------------- src/routes/index.tsx | 7 +-- 3 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/components/DisplaySigit/index.tsx b/src/components/DisplaySigit/index.tsx index 00716a3..641e9d6 100644 --- a/src/components/DisplaySigit/index.tsx +++ b/src/components/DisplaySigit/index.tsx @@ -50,7 +50,6 @@ export const DisplaySigit = ({ meta, parsedMeta, sigitKey }: SigitProps) => { ? appPublicRoutes.verify : `${appPrivateRoutes.sign}/${sigitKey}` } - state={{ meta }} className={styles.insetLink} >

{title}

diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index 70d9784..fa11fb7 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -65,6 +65,8 @@ export const SignPage = () => { const location = useLocation() const params = useParams() + const usersAppData = useAppSelector((state) => state.userAppData) + /** * Received from `location.state` * @@ -72,11 +74,27 @@ export const SignPage = () => { * arrayBuffer will be received in navigation from create page in offline mode * meta will be received in navigation from create & home page in online mode */ - const [metaInNavState, setMetaInNavState] = useState() - const [decryptedArrayBuffer, setDecryptedArrayBuffer] = useState< - ArrayBuffer | undefined - >() - const [uploadedZip, setUploadedZip] = useState() + const { + meta: metaInNavState, + arrayBuffer: decryptedArrayBuffer, + uploadedZip + } = location.state || { + meta: undefined, + arrayBuffer: undefined, + uploadedZip: undefined + } + + if (usersAppData) { + const sigitId = params.id + + if (sigitId) { + const sigit = usersAppData.sigits[sigitId] + + if (sigit) { + metaInNavState = sigit + } + } + } const [displayInput, setDisplayInput] = useState(false) @@ -119,41 +137,6 @@ export const SignPage = () => { const [isMarksCompleted, setIsMarksCompleted] = useState(false) const [otherUserMarks, setOtherUserMarks] = useState([]) - const usersAppData = useAppSelector((state) => state.userAppData) - - /** - * When location state changes, update the states in the component - * If sigit id is found in the URL PARAM we will set metaInNavState manually - * after we do the fetching based on the ID - */ - useEffect(() => { - if (location.state) { - const { meta, arrayBuffer, uploadedZip } = location.state - - setMetaInNavState(meta) - setDecryptedArrayBuffer(arrayBuffer) - setUploadedZip(uploadedZip) - } - }, [location.state]) - - useEffect(() => { - // If meta in nav state is populated we will not try to fetch sigit details - // from the redux store - if (metaInNavState) return - - if (usersAppData) { - const sigitId = params.id - - if (sigitId) { - const sigit = usersAppData.sigits[sigitId] - - if (sigit) { - setMetaInNavState(sigit) - } - } - } - }, [usersAppData, metaInNavState, params.id]) - useEffect(() => { if (signers.length > 0) { // check if all signers have signed then its fully signed diff --git a/src/routes/index.tsx b/src/routes/index.tsx index 643017f..3e42e78 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -19,7 +19,6 @@ export const appPrivateRoutes = { homePage: '/', create: '/create', sign: '/sign', - signId: '/sign/:id', settings: '/settings', profileSettings: '/settings/profile/:npub', cacheSettings: '/settings/cache', @@ -130,11 +129,7 @@ export const privateRoutes = [ element: }, { - path: appPrivateRoutes.sign, - element: - }, - { - path: appPrivateRoutes.signId, + path: `${appPrivateRoutes.sign}/:id?`, element: }, { -- 2.34.1 From 7c027825cdce6e36ba23b726200d36eb7aa50d70 Mon Sep 17 00:00:00 2001 From: Stixx Date: Wed, 11 Sep 2024 12:03:23 +0200 Subject: [PATCH 3/8] style: lint fix --- src/pages/sign/index.tsx | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index fa11fb7..ef22eb8 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -74,11 +74,9 @@ export const SignPage = () => { * arrayBuffer will be received in navigation from create page in offline mode * meta will be received in navigation from create & home page in online mode */ - const { - meta: metaInNavState, - arrayBuffer: decryptedArrayBuffer, - uploadedZip - } = location.state || { + let metaInNavState = location.state.meta + + const { arrayBuffer: decryptedArrayBuffer, uploadedZip } = location.state || { meta: undefined, arrayBuffer: undefined, uploadedZip: undefined -- 2.34.1 From 86a16c13ced0b2ce1dcd8470db17c83aebd57042 Mon Sep 17 00:00:00 2001 From: Stixx Date: Wed, 11 Sep 2024 12:29:38 +0200 Subject: [PATCH 4/8] chore: comments and lint (typing) --- src/pages/sign/index.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index ef22eb8..74194de 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -71,18 +71,22 @@ export const SignPage = () => { * Received from `location.state` * * uploadedZip will be received from home page when a user uploads a sigit zip wrapper that contains keys.json - * arrayBuffer will be received in navigation from create page in offline mode - * meta will be received in navigation from create & home page in online mode + * arrayBuffer (decryptedArrayBuffer) will be received in navigation from create page in offline mode + * meta (metaInNavState) will be received in navigation from create & home page in online mode */ - let metaInNavState = location.state.meta - + let metaInNavState = location?.state?.meta || undefined const { arrayBuffer: decryptedArrayBuffer, uploadedZip } = location.state || { - meta: undefined, - arrayBuffer: undefined, + decryptedArrayBuffer: undefined, uploadedZip: undefined } + /** + * If userAppData (redux) is available, and we have route param (sigit id) + * we will fetch a sigit based on the provided route ID and set it + * to the metaInNavState + */ if (usersAppData) { + // Sigit id is a createEventId const sigitId = params.id if (sigitId) { -- 2.34.1 From 5dc8d53503e72bd92a04f4034b6be68929cbc4f5 Mon Sep 17 00:00:00 2001 From: Stixx Date: Wed, 11 Sep 2024 12:33:40 +0200 Subject: [PATCH 5/8] chore: sigitCreateId naming --- src/pages/sign/index.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index 74194de..47fa7a7 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -86,11 +86,10 @@ export const SignPage = () => { * to the metaInNavState */ if (usersAppData) { - // Sigit id is a createEventId - const sigitId = params.id + const sigitCreateId = params.id - if (sigitId) { - const sigit = usersAppData.sigits[sigitId] + if (sigitCreateId) { + const sigit = usersAppData.sigits[sigitCreateId] if (sigit) { metaInNavState = sigit -- 2.34.1 From 64e8ebba85313169a451bbfd2258f7ed3b2b14c6 Mon Sep 17 00:00:00 2001 From: Stixx Date: Wed, 11 Sep 2024 13:30:39 +0200 Subject: [PATCH 6/8] chore: renamed sigitKey to sigitCreateId --- src/components/DisplaySigit/index.tsx | 10 +++++++--- src/pages/home/index.tsx | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/DisplaySigit/index.tsx b/src/components/DisplaySigit/index.tsx index 641e9d6..0b02383 100644 --- a/src/components/DisplaySigit/index.tsx +++ b/src/components/DisplaySigit/index.tsx @@ -24,12 +24,16 @@ import { useSigitMeta } from '../../hooks/useSigitMeta' import { extractFileExtensions } from '../../utils/file' type SigitProps = { - sigitKey: string + sigitCreateId: string meta: Meta parsedMeta: SigitCardDisplayInfo } -export const DisplaySigit = ({ meta, parsedMeta, sigitKey }: SigitProps) => { +export const DisplaySigit = ({ + meta, + parsedMeta, + sigitCreateId: sigitCreateId +}: SigitProps) => { const { title, createdAt, submittedBy, signers, signedStatus, isValid } = parsedMeta @@ -48,7 +52,7 @@ export const DisplaySigit = ({ meta, parsedMeta, sigitKey }: SigitProps) => { to={ signedStatus === SigitStatus.Complete ? appPublicRoutes.verify - : `${appPrivateRoutes.sign}/${sigitKey}` + : `${appPrivateRoutes.sign}/${sigitCreateId}` } className={styles.insetLink} > diff --git a/src/pages/home/index.tsx b/src/pages/home/index.tsx index 3e333d3..1a29021 100644 --- a/src/pages/home/index.tsx +++ b/src/pages/home/index.tsx @@ -257,7 +257,7 @@ export const HomePage = () => { .map((key) => ( -- 2.34.1 From 79e14d45a12076ada7f1884c8885d751c9bdb013 Mon Sep 17 00:00:00 2001 From: Stixx Date: Wed, 11 Sep 2024 15:41:42 +0200 Subject: [PATCH 7/8] chore: comment fix --- src/pages/sign/index.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index 47fa7a7..f6e2ef4 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -81,9 +81,9 @@ export const SignPage = () => { } /** - * If userAppData (redux) is available, and we have route param (sigit id) - * we will fetch a sigit based on the provided route ID and set it - * to the metaInNavState + * If userAppData (redux) is available, and we have the route param (sigit id) + * which is actually a `createEventId`, we will fetch a `sigit` + * based on the provided route ID and set fetched `sigit` to the `metaInNavState` */ if (usersAppData) { const sigitCreateId = params.id -- 2.34.1 From e48a3969904c1ec9759a60e9b21f998569ce6b13 Mon Sep 17 00:00:00 2001 From: Stixx Date: Wed, 11 Sep 2024 17:29:47 +0200 Subject: [PATCH 8/8] fix: verify/sign link --- src/components/DisplaySigit/index.tsx | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/components/DisplaySigit/index.tsx b/src/components/DisplaySigit/index.tsx index 0b02383..7d9cd3b 100644 --- a/src/components/DisplaySigit/index.tsx +++ b/src/components/DisplaySigit/index.tsx @@ -48,14 +48,19 @@ export const DisplaySigit = ({ return (
- + {signedStatus === SigitStatus.Complete && ( + + )} + {signedStatus !== SigitStatus.Complete && ( + + )}

{title}

{submittedBy && -- 2.34.1