diff --git a/src/constants/app.ts b/src/constants/app.ts index f72b3a7def..cfbbed2ab3 100644 --- a/src/constants/app.ts +++ b/src/constants/app.ts @@ -1,6 +1,7 @@ import { isMobile } from 'react-device-detect'; export const IS_READONLY_MODE = isMobile; +export const FILE_PARAM_KEY = 'file-param-before-login'; export const DEFAULT_FILE_NAME = 'Untitled'; export const EXAMPLE_FILES = [ { diff --git a/src/quadratic/QuadraticAuth.tsx b/src/quadratic/QuadraticAuth.tsx index bcd47d59bd..30b0da4330 100644 --- a/src/quadratic/QuadraticAuth.tsx +++ b/src/quadratic/QuadraticAuth.tsx @@ -5,6 +5,8 @@ import { captureException, setUser } from '@sentry/react'; import { QuadraticApp } from './QuadraticApp'; import apiClientSingleton from '../api-client/apiClientSingleton'; import { useEffect } from 'react'; +import { debug } from '../debugFlags'; +import { FILE_PARAM_KEY } from '../constants/app'; export const QuadraticAuth = () => { const { @@ -47,6 +49,16 @@ export const QuadraticAuth = () => { } if (!Auth0IsAuthenticated) { + // If we're not authenticated but there is a `file` query param, + // store it for later so it doesn't get lost. In `useLocalFiles` we'll + // grab it and apply it + const file = new URLSearchParams(window.location.search).get('file'); + if (file) { + sessionStorage.setItem(FILE_PARAM_KEY, file); + if (debug) + console.log('[QuadraticAuth] user is not logged in, saving `file` query param for after login: ', file); + } + loginWithRedirect({ screen_hint: 'signup' }); return ; } diff --git a/src/storage/useLocalFiles.ts b/src/storage/useLocalFiles.ts index c9663d492c..26afdd9476 100644 --- a/src/storage/useLocalFiles.ts +++ b/src/storage/useLocalFiles.ts @@ -11,7 +11,7 @@ import { downloadFile } from './downloadFile'; import { SheetController } from '../grid/controller/sheetController'; import { useSetRecoilState } from 'recoil'; import { editorInteractionStateAtom } from '../atoms/editorInteractionStateAtom'; -import { DEFAULT_FILE_NAME, EXAMPLE_FILES } from '../constants/app'; +import { DEFAULT_FILE_NAME, EXAMPLE_FILES, FILE_PARAM_KEY } from '../constants/app'; import apiClientSingleton from '../api-client/apiClientSingleton'; import mixpanel from 'mixpanel-browser'; @@ -357,8 +357,16 @@ export const useLocalFiles = (sheetController: SheetController): LocalFiles => { } // Get URL params we need at initialize time - const file = getURLParameter('file'); const local = getURLParameter('local'); + let file = getURLParameter('file'); + // We get the `file` query param from the URL, but if a user had it present + // _before_ they logged in, we lose it through the Auth0 process, so we + // store it in sessionStorage and use it (then delete it) if its present + const fileParamBeforeLogin = sessionStorage.getItem(FILE_PARAM_KEY); + if (fileParamBeforeLogin) { + file = fileParamBeforeLogin; + sessionStorage.removeItem(FILE_PARAM_KEY); + } // Migrate files from old version of the app (one-time, if necessary thing) // Note: eventually this code can be removed