Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: preserve file query param through auth0 process #412

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 mixpanel from 'mixpanel-browser';

const INDEX = 'file-list';
Expand Down Expand Up @@ -352,8 +352,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