import { ArrowRightSmallIcon } from '@blocksuite/icons/rc';
import clsx from 'clsx';
import { useMemo, useState } from 'react';
import type { Location } from 'react-router-dom';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { useLocation, useNavigate } from 'react-router-dom';
import useSWR from 'swr';
import { Button } from '../../ui/button';
import { Checkbox } from '../../ui/checkbox';
import { Divider } from '../../ui/divider';
import Input from '../../ui/input';
import { ScrollableContainer } from '../../ui/scrollbar';
import * as styles from './onboarding-page.css';
import type { User } from './type';
type QuestionOption = {
type: 'checkbox' | 'input';
label: string;
value: string;
};
type Question = {
id?: string;
question: string;
options?: QuestionOption[];
};
type QuestionnaireAnswer = {
form: string;
ask: string;
answer: string[];
};
function getCallbackUrl(location: Location) {
try {
const url =
location.state?.callbackURL ||
new URLSearchParams(location.search).get('redirect_uri');
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 = ({
headerItems,
children,
isMacosDesktop,
isWindowsDesktop,
}: {
isMacosDesktop?: boolean;
isWindowsDesktop?: boolean;
headerItems?: React.ReactNode;
children: React.ReactNode;
}) => {
return (
);
};
export const OnboardingPage = ({
user,
onOpenAffine,
}: {
user: User;
onOpenAffine: () => void;
}) => {
const location = useLocation();
const navigate = useNavigate();
const [questionIdx, setQuestionIdx] = useState(0);
const { data: questions } = useSWR(
'/api/worker/questionnaire',
url => fetch(url).then(r => r.json()),
{ suspense: true, revalidateOnFocus: false }
);
const [options, setOptions] = useState(new Set());
const [inputs, setInputs] = useState>({});
const callbackUrl = useMemo(() => getCallbackUrl(location), [location]);
const question = useMemo(
() => questions?.[questionIdx],
[questionIdx, questions]
);
const isMacosDesktop = environment.isDesktop && environment.isMacOs;
const isWindowsDesktop = environment.isDesktop && environment.isWindows;
if (!questions) {
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 (
setQuestionIdx(questions.length)}
>
Skip
}
isMacosDesktop={isMacosDesktop}
isWindowsDesktop={isWindowsDesktop}
>
{question.question}
{question.options &&
question.options.length > 0 &&
question.options.map((option, optionIndex) => {
if (option.type === 'checkbox') {
return (
{
setOptions(set => {
if (e.target.checked) {
set.add(option.value);
} else {
set.delete(option.value);
}
return new Set(set);
});
}}
label={option.label}
/>
);
} else if (option.type === 'input') {
return (
setInputs(prev => ({ ...prev, [option.value]: value }))
}
/>
);
}
return null;
})}
);
}
return (
Thank you!
We will continue to enhance our products based on your feedback. Thank
you once again for your supports.
);
};