Skip to content

Commit

Permalink
feat: onboarding electron redirect (toeverything#5327)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Dec 19, 2023
1 parent 8ea910a commit 33a589a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { fetchWithTraceReport } from '@affine/graphql';
import { ArrowRightSmallIcon } from '@blocksuite/icons';
import clsx from 'clsx';
import { useMemo, useState } from 'react';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { type Location, useLocation, useNavigate } from 'react-router-dom';
import useSWR from 'swr';

import { Button } from '../../ui/button';
Expand Down Expand Up @@ -30,6 +32,23 @@ type QuestionnaireAnswer = {
answer: string[];
};

function getCallbackUrl(location: Location) {
try {
const url =
location.state?.callbackURL ||
new URLSearchParams(location.search).get('callbackUrl');
if (typeof url === 'string' && url) {
if (!url.startsWith('http:') && !url.startsWith('https:')) {
return url;
}
// we will ignore host to avoid redirect hack
const parsedUrl = new URL(url);
return parsedUrl.pathname + parsedUrl.search;
}
} catch (_) {}
return null;
}

export const ScrollableLayout = ({
children,
}: {
Expand Down Expand Up @@ -69,6 +88,8 @@ export const OnboardingPage = ({
user: User;
onOpenAffine: () => void;
}) => {
const location = useLocation();
const navigate = useNavigate();
const [questionIdx, setQuestionIdx] = useState(0);
const { data: questions } = useSWR<Question[]>(
'/api/worker/questionnaire',
Expand All @@ -78,6 +99,7 @@ export const OnboardingPage = ({
const [options, setOptions] = useState(new Set<string>());
const [inputs, setInputs] = useState<Record<string, string>>({});

const callbackUrl = useMemo(() => getCallbackUrl(location), [location]);
const question = useMemo(
() => questions?.[questionIdx],
[questionIdx, questions]
Expand All @@ -87,6 +109,14 @@ export const OnboardingPage = ({
return null;
}

if (callbackUrl?.startsWith('/open-app/signin-redirect')) {
const url = new URL(callbackUrl, window.location.origin);
url.searchParams.set('next', 'onboarding');
console.log('redirect to', url.toString());
window.location.assign(url.toString());
return null;
}

if (question) {
return (
<ScrollableLayout>
Expand Down Expand Up @@ -198,7 +228,13 @@ export const OnboardingPage = ({
className={clsx(styles.button, styles.openAFFiNEButton)}
type="primary"
size="extraLarge"
onClick={onOpenAffine}
onClick={() => {
if (callbackUrl) {
navigate(callbackUrl);
} else {
onOpenAffine();
}
}}
iconPosition="end"
icon={<ArrowRightSmallIcon />}
>
Expand Down
5 changes: 4 additions & 1 deletion packages/frontend/core/src/pages/open-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,16 @@ const OpenOAuthJwt = () => {
const maybeSchema = appSchemas.safeParse(params.get('schema'));
return maybeSchema.success ? maybeSchema.data : 'affine';
}, [params]);
const next = useMemo(() => params.get('next'), [params]);
const channel = schemaToChanel[schema as Schema];

if (!currentUser || !currentUser?.token?.sessionToken) {
return null;
}

const urlToOpen = `${schema}://signin-redirect?token=${currentUser.token.sessionToken}`;
const urlToOpen = `${schema}://signin-redirect?token=${
currentUser.token.sessionToken
}&next=${next || ''}`;

return <OpenAppImpl urlToOpen={urlToOpen} channel={channel} />;
};
Expand Down
4 changes: 4 additions & 0 deletions packages/frontend/electron/src/main/deep-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ async function handleOauthJwt(url: string) {

ipcMain.once('affine:login', () => {
hiddenWindow?.destroy();
if (urlObj.searchParams.get('next') === 'onboarding') {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
mainWindow.loadURL(mainWindowOrigin + '/auth/onboarding');
}
});

// hacks to refresh auth state in the main window
Expand Down

0 comments on commit 33a589a

Please sign in to comment.