A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
refactor(app): drive setup steps from one definition table
Hampus806d2cc0f1b1
diff --git a/fluxer_app/src/features/app/components/setup/SetupWelcomeGate.tsx b/fluxer_app/src/features/app/components/setup/SetupWelcomeGate.tsxindex 28eaccc82..2d6b18fd9 100644--- a/fluxer_app/src/features/app/components/setup/SetupWelcomeGate.tsx+++ b/fluxer_app/src/features/app/components/setup/SetupWelcomeGate.tsx@@ -4,49 +4,10 @@ import styles from '@app/features/app/components/setup/SelfHostedSetupWizardGateimport {SetupWelcomeRoll} from '@app/features/app/components/setup/SetupWelcomeRoll';import {useSetupWelcomeRotation} from '@app/features/app/components/setup/useSetupWelcomeRotation';import {flxElementClassName} from '@app/lib/react';-import {msg} from '@lingui/core/macro';-import {useLingui} from '@lingui/react/macro';import {observer} from 'mobx-react-lite';-import type {ReactNode} from 'react';-const WELCOME_AUTHED_TITLE_DESCRIPTOR = msg({- message: 'Welcome to {productName}',- comment: 'Setup wizard title shown once the operator is signed in as the instance admin.',-});-const WELCOME_AUTHED_BODY_DESCRIPTOR = msg({- message:- 'You are signed in as the instance administrator. The next steps configure branding, registration, and core policy for everyone on this instance.',- comment: 'Setup wizard body shown to the signed-in instance administrator.',-});--interface SetupWelcomeGateProps {- localeCode: string;- productName: string;- isAuthenticated: boolean;-}--export const SetupWelcomeGate = observer(function SetupWelcomeGate({- localeCode,- productName,- isAuthenticated,-}: SetupWelcomeGateProps) {- const {i18n} = useLingui();+export const SetupWelcomeGate = observer(function SetupWelcomeGate({localeCode}: {localeCode: string}) {const {entry, upcoming} = useSetupWelcomeRotation(localeCode);- const renderAdministratorIntroduction = (): ReactNode => {- if (!isAuthenticated) {- return null;- }- return (- <>- <h3 className={styles.welcomeTitle} data-flx="app.self-hosted-setup-wizard-gate.welcome-title">- {i18n._(WELCOME_AUTHED_TITLE_DESCRIPTOR, {productName})}- </h3>- <p className={styles.body} data-flx="app.self-hosted-setup-wizard-gate.welcome-body">- {i18n._(WELCOME_AUTHED_BODY_DESCRIPTOR)}- </p>- </>- );- };return (<flx-app-setup-welcome-gateclassName={flxElementClassName(styles.welcomeHero)}@@ -60,7 +21,6 @@ export const SetupWelcomeGate = observer(function SetupWelcomeGate({wordClassName={styles.welcomeWord}data-flx="app.setup.setup-welcome-gate.setup-welcome-roll"/>- {renderAdministratorIntroduction()}</flx-app-setup-welcome-gate>);});diff --git a/fluxer_app/src/features/app/components/setup/SetupWizardClient.ts b/fluxer_app/src/features/app/components/setup/SetupWizardClient.tsindex fdac35c53..b3a4f41a0 100644--- a/fluxer_app/src/features/app/components/setup/SetupWizardClient.ts+++ b/fluxer_app/src/features/app/components/setup/SetupWizardClient.ts@@ -14,6 +14,7 @@ import type {import type {InstanceAccountIdentityResponse,InstanceAccountIdentityUpdateRequest,+ InstanceSetupTokenCheckRequest,} from '@fluxer/schema/src/domains/instance/InstanceSchemas';const logger = new Logger('SetupWizardClient');@@ -53,6 +54,11 @@ export async function setSetupAccountIdentity(return response.body;}+export async function checkSetupToken(setupToken: string): Promise<void> {+ const body: InstanceSetupTokenCheckRequest = {setup_token: setupToken};+ await http.post(Endpoints.INSTANCE_SETUP_TOKEN_CHECKS, {body});+}+export async function testSmtpConfig(body: InstanceEmailSmtpTestRequest): Promise<InstanceEmailSmtpTestResponse> {const response = await http.post<InstanceEmailSmtpTestResponse>(Endpoints.ADMIN_INSTANCE_CONFIG_SMTP_TESTS, {body});return response.body;diff --git a/fluxer_app/src/features/app/components/setup/SetupWizardStateMachine.ts b/fluxer_app/src/features/app/components/setup/SetupWizardStateMachine.tsindex 7c30c6c4c..3434e2bc9 100644--- a/fluxer_app/src/features/app/components/setup/SetupWizardStateMachine.ts+++ b/fluxer_app/src/features/app/components/setup/SetupWizardStateMachine.ts@@ -1,242 +1,187 @@// SPDX-License-Identifier: AGPL-3.0-or-laterimport {type AccountIdentityMode, AccountIdentityModes} from '@fluxer/constants/src/AccountIdentityConstants';-import {assign, initialTransition, type SnapshotFrom, setup, transition} from 'xstate';export type WizardStep =| 'welcome'| 'setup_token'- | 'theme'| 'sign_in_method'- | 'admin_intro'- | 'admin_account'- | 'loading'- | 'admin_recovery_kit'- | 'branding'+ | 'sign_in_locked'+ | 'shared_usernames'+ | 'email_delivery'+ | 'sign_in_to_finish'+ | 'account'+ | 'recovery_kit'+ | 'email_service'+ | 'email_sender'+ | 'name'+ | 'look'+ | 'communities'+ | 'direct_messages'| 'registration'- | 'community'- | 'media_expiry'- | 'integration_gif'- | 'integration_youtube'- | 'integration_email'- | 'integration_bluesky'- | 'push_relay_consent'- | 'services'- | 'premium'- | 'finish';--type WizardPhase = 'register' | 'loading' | 'configure';--const REGISTER_STEPS: ReadonlyArray<WizardStep> = [- 'welcome',- 'setup_token',- 'theme',- 'sign_in_method',- 'admin_intro',- 'admin_account',-];-const LOADING_STEPS: ReadonlyArray<WizardStep> = ['loading'];-const CONFIGURE_STEPS: ReadonlyArray<WizardStep> = [- 'welcome',- 'branding',- 'registration',- 'community',- 'media_expiry',- 'integration_gif',- 'integration_youtube',- 'integration_email',- 'integration_bluesky',- 'push_relay_consent',- 'services',- 'premium',- 'finish',-];--function derivePhase(isAuthenticated: boolean, hasConfig: boolean): WizardPhase {- if (!isAuthenticated) return 'register';- if (!hasConfig) return 'loading';- return 'configure';-}+ | 'push_relay'+ | 'gif_youtube'+ | 'attachments'+ | 'review'+ | 'done';-const USERNAME_CONFIGURE_STEPS: ReadonlyArray<WizardStep> = [- 'welcome',- 'admin_recovery_kit',- ...CONFIGURE_STEPS.slice(1).filter((step) => step !== 'integration_email'),-];--function phaseSteps(- phase: WizardPhase,- currentStep: WizardStep,- accountIdentity: AccountIdentityMode,- identityLocked: boolean,- setupTokenRequired: boolean,-): ReadonlyArray<WizardStep> {- switch (phase) {- case 'register':- return REGISTER_STEPS.filter(- (step) =>- step === currentStep ||- ((step !== 'sign_in_method' || !identityLocked) && (step !== 'setup_token' || setupTokenRequired)),- );- case 'loading':- return LOADING_STEPS;- case 'configure':- return accountIdentity === AccountIdentityModes.USERNAME ? USERNAME_CONFIGURE_STEPS : CONFIGURE_STEPS;- }-}+export type EmailDeliveryAnswer = 'now' | 'later';-interface SetupWizardMachineContext {- step: WizardStep;- direction: number;+export interface WizardConditions {isAuthenticated: boolean;- hasConfig: boolean;accountIdentity: AccountIdentityMode;identityLocked: boolean;+ adminExists: boolean;setupTokenRequired: boolean;setupTokenRejected: boolean;+ emailsEnabled: boolean;+ emailAnswer: EmailDeliveryAnswer;+ finished: boolean;}-export type SetupWizardMachineEvent =- | {type: 'wizard.next'}- | {type: 'wizard.back'}- | {type: 'wizard.setupTokenRejected'}- | {- type: 'wizard.sync';- isAuthenticated: boolean;- hasConfig: boolean;- accountIdentity: AccountIdentityMode;- identityLocked: boolean;- setupTokenRequired: boolean;- };--export interface SetupWizardModel {- phase: WizardPhase;- step: WizardStep;- steps: ReadonlyArray<WizardStep>;- direction: number;- stepIndex: number;-}--function contextSteps(context: SetupWizardMachineContext): ReadonlyArray<WizardStep> {- return phaseSteps(- derivePhase(context.isAuthenticated, context.hasConfig),- context.step,- context.accountIdentity,- context.identityLocked,- context.setupTokenRequired || context.setupTokenRejected,- );-}--function advance(context: SetupWizardMachineContext, delta: number): Partial<SetupWizardMachineContext> {- const steps = contextSteps(context);- const index = steps.indexOf(context.step);- const safeIndex = index < 0 ? 0 : index;- const nextIndex = Math.min(steps.length - 1, Math.max(0, safeIndex + delta));- return {step: steps[nextIndex], direction: Math.sign(delta)};-}--function targetsPhase(event: SetupWizardMachineEvent, context: SetupWizardMachineContext, phase: WizardPhase): boolean {- if (event.type !== 'wizard.sync') return false;- return (- derivePhase(event.isAuthenticated, event.hasConfig) === phase &&- derivePhase(context.isAuthenticated, context.hasConfig) !== phase- );+interface StepDefinition {+ id: WizardStep;+ shown: (conditions: WizardConditions) => boolean;+ counted: boolean;+ beforeAccount: boolean;}-const setupWizardStateMachine = setup({- types: {} as {- context: SetupWizardMachineContext;- events: SetupWizardMachineEvent;+const beforeAccount = (conditions: WizardConditions): boolean => !conditions.isAuthenticated && !conditions.finished;+const createsAccount = (conditions: WizardConditions): boolean => beforeAccount(conditions) && !conditions.adminExists;+const afterAccount = (conditions: WizardConditions): boolean => conditions.isAuthenticated && !conditions.finished;+const usesEmail = (conditions: WizardConditions): boolean => conditions.accountIdentity === AccountIdentityModes.EMAIL;+const connectsEmailNow = (conditions: WizardConditions): boolean =>+ afterAccount(conditions) && usesEmail(conditions) && !conditions.emailsEnabled && conditions.emailAnswer === 'now';++const STEP_DEFINITIONS: ReadonlyArray<StepDefinition> = [+ {id: 'welcome', shown: beforeAccount, counted: false, beforeAccount: true},+ {+ id: 'setup_token',+ shown: (c) => createsAccount(c) && (c.setupTokenRequired || c.setupTokenRejected),+ counted: true,+ beforeAccount: true,+ },+ {+ id: 'sign_in_method',+ shown: (c) => createsAccount(c) && !c.identityLocked,+ counted: true,+ beforeAccount: true,},- guards: {- shouldEnterRegister: ({context, event}) => targetsPhase(event, context, 'register'),- shouldEnterLoading: ({context, event}) => targetsPhase(event, context, 'loading'),- shouldEnterConfigure: ({context, event}) => targetsPhase(event, context, 'configure'),+ {+ id: 'sign_in_locked',+ shown: (c) => createsAccount(c) && c.identityLocked,+ counted: true,+ beforeAccount: true,},- actions: {- applyConditions: assign({- isAuthenticated: ({context, event}) =>- event.type === 'wizard.sync' ? event.isAuthenticated : context.isAuthenticated,- hasConfig: ({context, event}) => (event.type === 'wizard.sync' ? event.hasConfig : context.hasConfig),- accountIdentity: ({context, event}) =>- event.type === 'wizard.sync' ? event.accountIdentity : context.accountIdentity,- identityLocked: ({context, event}) =>- event.type === 'wizard.sync' ? event.identityLocked : context.identityLocked,- setupTokenRequired: ({context, event}) =>- event.type === 'wizard.sync' ? event.setupTokenRequired : context.setupTokenRequired,- }),- goNext: assign(({context}) => advance(context, 1)),- goBack: assign(({context}) => advance(context, -1)),- requireSetupToken: assign({setupTokenRejected: () => true, step: () => 'setup_token', direction: () => -1}),- enterRegister: assign({step: () => REGISTER_STEPS[0], direction: () => 0}),- enterLoading: assign({step: () => LOADING_STEPS[0], direction: () => 0}),- enterConfigure: assign({step: () => CONFIGURE_STEPS[0], direction: () => 0}),+ {+ id: 'shared_usernames',+ shown: (c) => createsAccount(c) && !c.identityLocked && usesEmail(c),+ counted: true,+ beforeAccount: true,},-}).createMachine({- id: 'setupWizard',- context: {- step: 'welcome',- direction: 0,- isAuthenticated: false,- hasConfig: false,- accountIdentity: AccountIdentityModes.EMAIL,- identityLocked: false,- setupTokenRequired: false,- setupTokenRejected: false,+ {+ id: 'email_delivery',+ shown: (c) => createsAccount(c) && usesEmail(c) && !c.emailsEnabled,+ counted: true,+ beforeAccount: true,},- initial: 'register',- on: {- 'wizard.sync': [- {guard: 'shouldEnterConfigure', target: '.configure', actions: 'applyConditions'},- {guard: 'shouldEnterLoading', target: '.loading', actions: 'applyConditions'},- {guard: 'shouldEnterRegister', target: '.register', actions: 'applyConditions'},- {actions: 'applyConditions'},- ],+ {+ id: 'sign_in_to_finish',+ shown: (c) => beforeAccount(c) && c.adminExists,+ counted: false,+ beforeAccount: true,},- states: {- register: {- entry: 'enterRegister',- on: {- 'wizard.next': {actions: 'goNext'},- 'wizard.back': {actions: 'goBack'},- 'wizard.setupTokenRejected': {actions: 'requireSetupToken'},- },- },- loading: {- entry: 'enterLoading',- },- configure: {- entry: 'enterConfigure',- on: {- 'wizard.next': {actions: 'goNext'},- 'wizard.back': {actions: 'goBack'},- },- },+ {id: 'account', shown: createsAccount, counted: true, beforeAccount: true},+ {+ id: 'recovery_kit',+ shown: (c) => afterAccount(c) && c.accountIdentity === AccountIdentityModes.USERNAME,+ counted: true,+ beforeAccount: false,},-});+ {id: 'email_service', shown: connectsEmailNow, counted: true, beforeAccount: false},+ {id: 'email_sender', shown: connectsEmailNow, counted: true, beforeAccount: false},+ {id: 'name', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'look', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'communities', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'direct_messages', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'registration', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'push_relay', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'gif_youtube', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'attachments', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'review', shown: afterAccount, counted: true, beforeAccount: false},+ {id: 'done', shown: (c) => c.finished, counted: false, beforeAccount: false},+];++const STEP_ORDER: ReadonlyArray<WizardStep> = STEP_DEFINITIONS.map((definition) => definition.id);-export type SetupWizardSnapshot = SnapshotFrom<typeof setupWizardStateMachine>;+export function isWizardStep(value: unknown): value is WizardStep {+ return typeof value === 'string' && (STEP_ORDER as ReadonlyArray<string>).includes(value);+}++function definitionOf(step: WizardStep): StepDefinition {+ return STEP_DEFINITIONS[STEP_ORDER.indexOf(step)];+}++export function isBeforeAccountStep(step: WizardStep): boolean {+ return definitionOf(step).beforeAccount;+}++export function visibleSteps(conditions: WizardConditions): ReadonlyArray<WizardStep> {+ return STEP_DEFINITIONS.filter((definition) => definition.shown(conditions)).map((definition) => definition.id);+}++export function resolveStep(step: WizardStep, conditions: WizardConditions): WizardStep {+ const steps = visibleSteps(conditions);+ if (steps.includes(step)) return step;+ const position = STEP_ORDER.indexOf(step);+ return steps.find((candidate) => STEP_ORDER.indexOf(candidate) > position) ?? steps[steps.length - 1] ?? 'welcome';+}++export function firstStepAfterAccount(conditions: WizardConditions): WizardStep {+ return visibleSteps(conditions).find((step) => !isBeforeAccountStep(step)) ?? 'review';+}++export function nextStep(step: WizardStep, conditions: WizardConditions): WizardStep {+ const steps = visibleSteps(conditions);+ const position = STEP_ORDER.indexOf(step);+ return steps.find((candidate) => STEP_ORDER.indexOf(candidate) > position) ?? step;+}++export function previousStep(step: WizardStep, conditions: WizardConditions): WizardStep | null {+ if (step === 'done') return null;+ const steps = visibleSteps(conditions);+ const position = STEP_ORDER.indexOf(step);+ const earlier = steps.filter((candidate) => STEP_ORDER.indexOf(candidate) < position);+ const previous = earlier[earlier.length - 1];+ if (previous === undefined) return null;+ if (isBeforeAccountStep(previous) !== isBeforeAccountStep(step)) return null;+ return previous;+}-export function createSetupWizardSnapshot(): SetupWizardSnapshot {- return initialTransition(setupWizardStateMachine)[0];+export function countedStepsBeforeAccount(conditions: WizardConditions): number {+ return visibleSteps({...conditions, isAuthenticated: false, adminExists: false, finished: false}).filter(+ (step) => definitionOf(step).counted,+ ).length;}-export function transitionSetupWizardSnapshot(- snapshot: SetupWizardSnapshot,- event: SetupWizardMachineEvent,-): SetupWizardSnapshot {- return transition(setupWizardStateMachine, snapshot, event)[0] as SetupWizardSnapshot;+export interface WizardProgress {+ current: number;+ total: number;}-export function selectSetupWizardModel(snapshot: SetupWizardSnapshot): SetupWizardModel {- const phase = derivePhase(snapshot.context.isAuthenticated, snapshot.context.hasConfig);- const steps = contextSteps(snapshot.context);- const stepIndex = steps.indexOf(snapshot.context.step);- return {- phase,- step: snapshot.context.step,- steps,- direction: snapshot.context.direction,- stepIndex: stepIndex < 0 ? 0 : stepIndex,- };+export function wizardProgress(+ step: WizardStep,+ conditions: WizardConditions,+ stepsBeforeAccount: number,+): WizardProgress | null {+ if (!definitionOf(step).counted) return null;+ const counted = visibleSteps(conditions).filter((candidate) => definitionOf(candidate).counted);+ const index = counted.indexOf(step);+ if (index < 0) return null;+ const offset = conditions.isAuthenticated ? stepsBeforeAccount : 0;+ const remainingAfterAccount = conditions.isAuthenticated+ ? 0+ : visibleSteps({...conditions, isAuthenticated: true}).filter((candidate) => definitionOf(candidate).counted)+ .length;+ return {current: offset + index + 1, total: offset + counted.length + remainingAfterAccount};}