From b7b9abbc97c45ee5a4f74cc4bf2cd9b83ba91a3b Mon Sep 17 00:00:00 2001 From: Romaric Mourgues Date: Mon, 12 Jun 2023 11:00:20 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20Fix=20JWT=20loading=20for=20shar?= =?UTF-8?q?ed=20view=20(#76)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix JWT loading * Use a different local storage for shared view --- .../global/framework/local-storage-service.ts | 5 +++++ .../src/app/views/client/body/drive/index.tsx | 3 --- .../client/body/drive/modals/create/index.tsx | 12 +++++----- .../app/views/client/body/drive/shared.tsx | 22 +++++++++++++++---- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/tdrive/frontend/src/app/features/global/framework/local-storage-service.ts b/tdrive/frontend/src/app/features/global/framework/local-storage-service.ts index ddd9d36b..cbf2d19f 100755 --- a/tdrive/frontend/src/app/features/global/framework/local-storage-service.ts +++ b/tdrive/frontend/src/app/features/global/framework/local-storage-service.ts @@ -1,6 +1,11 @@ export default class LocalStorage { static prefix = 'tdrive:'; + //For shared views we will use another prefix + static setPrefix(prefix: string) { + LocalStorage.prefix = prefix; + } + static setItem(key: string, value: unknown) { window.localStorage.setItem(`${LocalStorage.prefix}${key}`, JSON.stringify(value)); } diff --git a/tdrive/frontend/src/app/views/client/body/drive/index.tsx b/tdrive/frontend/src/app/views/client/body/drive/index.tsx index 044784ae..5aee5a37 100644 --- a/tdrive/frontend/src/app/views/client/body/drive/index.tsx +++ b/tdrive/frontend/src/app/views/client/body/drive/index.tsx @@ -17,9 +17,6 @@ export default ({ context?: EmbedContext; inPublicSharing?: boolean; }) => { - //Preload applications mainly for shared view - useCompanyApplications(); - return ( <> diff --git a/tdrive/frontend/src/app/views/client/body/drive/modals/create/index.tsx b/tdrive/frontend/src/app/views/client/body/drive/modals/create/index.tsx index 9134dc89..0fc02403 100644 --- a/tdrive/frontend/src/app/views/client/body/drive/modals/create/index.tsx +++ b/tdrive/frontend/src/app/views/client/body/drive/modals/create/index.tsx @@ -1,3 +1,9 @@ +import Avatar from '@atoms/avatar'; +import A from '@atoms/link'; +import { Modal, ModalContent } from '@atoms/modal'; +import { Base } from '@atoms/text'; +import { useCompanyApplications } from '@features/applications/hooks/use-company-applications'; +import { Application } from '@features/applications/types/application'; import { Transition } from '@headlessui/react'; import { ChevronLeftIcon, @@ -6,12 +12,6 @@ import { FolderDownloadIcon, LinkIcon, } from '@heroicons/react/outline'; -import Avatar from '@atoms/avatar'; -import A from '@atoms/link'; -import { Modal, ModalContent } from '@atoms/modal'; -import { Base } from '@atoms/text'; -import { useCompanyApplications } from '@features/applications/hooks/use-company-applications'; -import { Application } from '@features/applications/types/application'; import { ReactNode } from 'react'; import { atom, useRecoilState } from 'recoil'; import { slideXTransition, slideXTransitionReverted } from 'src/utils/transitions'; diff --git a/tdrive/frontend/src/app/views/client/body/drive/shared.tsx b/tdrive/frontend/src/app/views/client/body/drive/shared.tsx index 796acfa1..9732ada8 100755 --- a/tdrive/frontend/src/app/views/client/body/drive/shared.tsx +++ b/tdrive/frontend/src/app/views/client/body/drive/shared.tsx @@ -22,12 +22,18 @@ import { } from '../../../../features/drive/api-client/api-client'; import useRouterCompany from '../../../../features/router/hooks/use-router-company'; import { CreateModalWithUploadZones } from '../../side-bar/actions'; -import { useRecoilState } from 'recoil'; -import { DriveCurrentFolderAtom } from './browser'; +import { useCompanyApplications } from 'app/features/applications/hooks/use-company-applications'; +import LocalStorage from 'app/features/global/framework/local-storage-service'; export default () => { const companyId = useRouterCompany(); + //Create a different local storage for shared view + useEffect(() => { + LocalStorage.setPrefix('tdrive-shared:'); + LocalStorage.clear(); + }, []); + const [state, setState] = useState({ group: { logo: '', name: '' } }); useEffect(() => { const routeState = RouterService.getStateFromRoute(); @@ -97,6 +103,9 @@ const AccessChecker = ({ const { details, loading, refresh } = useDriveItem(folderId); const companyId = useRouterCompany(); const [password, setPassword] = useState((token || '').split('+')[1] || ''); + const [accessGranted, setAccessGranted] = useState(false); + //Preload applications mainly for shared view + const { refresh: refreshApplications } = useCompanyApplications(); const setPublicToken = async (token: string, password?: string) => { try { @@ -114,16 +123,21 @@ const AccessChecker = ({ } JWTStorage.updateJWT(access_token); + + //Everything alright, load the drive + setAccessGranted(true); + refresh(folderId); + refreshApplications(); } catch (e) { console.error(e); ToasterService.error('Unable to access documents: ' + e); + setAccessGranted(false); } }; useEffect(() => { (async () => { await setPublicToken(token || ''); - refresh(folderId); })(); }, []); @@ -131,7 +145,7 @@ const AccessChecker = ({ return <>; } - if (!details?.item?.id && !loading) { + if ((!details?.item?.id && !loading) || !accessGranted) { return (