Skip to content

Commit

Permalink
feat: preserve file query param through auth0 process (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimniels authored Apr 24, 2023
1 parent 1eb4bfb commit d711c06
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/constants/app.ts
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand Down
12 changes: 12 additions & 0 deletions src/quadratic/QuadraticAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 <QuadraticLoading></QuadraticLoading>;
}
Expand Down
12 changes: 10 additions & 2 deletions src/storage/useLocalFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d711c06

Please sign in to comment.