Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 18 additions & 3 deletions apps/web/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2053,15 +2053,22 @@ type Mutation {
skipUserOnboarding: UserOnboardingProgress!

"""
Reset personal onboarding to a clean state (no steps, not skipped, not completed).
Reset personal onboarding to a clean state (no steps, not skipped, not completed, setup wizard not seen).
"""
resetUserOnboarding: UserOnboardingProgress!

"""Remember the Mingo conversation the Get Started tour lives in; a repeat overwrites it."""
"""
Remember the Mingo conversation the Get Started tour lives in; a repeat overwrites it. Also marks the setup wizard seen.
"""
setUserOnboardingTourDialog(dialogId: String!): UserOnboardingProgress!

"""
Dev/QA only. Marks the tenant Initial Setup completed (all steps) and the current user's Get Started skipped, so the user lands straight in the app. Idempotent. Not part of the schema on stage/prod.
The setup wizard showed its last screen to this user. Idempotent: the first call sets setupSeen = true and stamps setupSeenAt, later calls return the record unchanged. Independent of the tenant Initial Setup.
"""
markUserOnboardingSetupSeen: UserOnboardingProgress!

"""
Dev/QA only. Marks the tenant Initial Setup completed (all steps) and the current user's Get Started skipped with the setup wizard seen, so the user lands straight in the app. Idempotent. Not part of the schema on stage/prod.
"""
skipOnboarding: OnboardingSkipResult!

Expand Down Expand Up @@ -6364,6 +6371,14 @@ type UserOnboardingProgress {

"""The Mingo conversation the Get Started tour lives in; null until "Start with Mingo"."""
tourDialogId: String

"""
True once the user has seen the setup wizard's last screen ("You are ready to go" / "Everything is set up"). Set by markUserOnboardingSetupSeen and by setUserOnboardingTourDialog; cleared by resetUserOnboarding.
"""
setupSeen: Boolean!

"""When setupSeen was set; null while not seen."""
setupSeenAt: Instant
}

enum UserOnboardingStep {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function progress(overrides: Partial<UserOnboardingProgress> = {}): UserOnboardi
skipped: false,
skippedAt: null,
tourDialogId: TOUR_DIALOG,
setupSeen: false,
...overrides,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const FINISHED_TOUR = {
skipped: false,
skippedAt: null,
tourDialogId: null,
setupSeen: false,
};

describe('ProfileCard - Reset Onboarding', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Pins what the wizard tells the server about its last screen: "Everything is
// set up" and "You are ready to go" are recorded as seen the moment they show,
// once, and never again for an admin the server has already seen there - and
// that leaving the wizard stays the wizard's own job.

import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { useAuthStore } from '@/app/(auth)/auth/stores/auth-store';
import { TenantOnboardingStep } from '@/generated/schema-enums';
import { routes } from '@/lib/routes';
import {
type TenantOnboardingProgress,
useOnboardingStore,
type UserOnboardingProgress,
} from '@/stores/onboarding-store';
import { SetupWizard } from './setup-wizard';

const { replace, completeTenantStep, completeTenantInBackground, markSetupSeenInBackground, mode } = vi.hoisted(() => ({
replace: vi.fn(),
completeTenantStep: vi.fn(),
completeTenantInBackground: vi.fn(),
markSetupSeenInBackground: vi.fn(),
mode: { saas: false },
}));

vi.mock('next/navigation', () => ({ useRouter: () => ({ replace, push: vi.fn() }) }));
vi.mock('@/lib/app-mode', async importOriginal => ({
...(await importOriginal<object>()),
isSaasTenantMode: () => mode.saas,
}));
vi.mock('@flamingo-stack/openframe-frontend-core/components', async importOriginal => ({
...(await importOriginal<object>()),
LoadingSuspense: ({ children }: { children: React.ReactNode }) => <>{children}</>,
}));
vi.mock('@flamingo-stack/openframe-frontend-core/components/ui', async importOriginal => ({
...(await importOriginal<object>()),
LoadingWhen: ({ children }: { children: React.ReactNode }) => <>{children}</>,
useLoadingScope: () => false,
}));
vi.mock('@/app/(app)/onboarding/hooks/use-tenant-onboarding-auto-detect', () => ({
useTenantOnboardingAutoDetect: () => new Set(),
}));
vi.mock('@/graphql/onboarding/use-onboarding-mutations', () => ({
useOnboardingMutations: () => ({ completeTenantStep, completeTenantInBackground, markSetupSeenInBackground }),
}));
vi.mock('../lib/tour-state', () => ({ markTourStarted: vi.fn() }));
vi.mock('./setup-frame', () => ({
SetupFrame: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));
vi.mock('../steps/welcome-step', () => ({
WelcomeStep: ({ onContinue }: { onContinue: () => void }) => (
<button type="button" onClick={onContinue}>
Continue
</button>
),
}));
vi.mock('../steps/organization-step', () => ({ OrganizationStep: () => <div>Set Up Your Organization</div> }));
vi.mock('../steps/customer-step', () => ({ CustomerStep: () => <div>Your First Customer</div> }));
vi.mock('../steps/deploy-device-step', () => ({ DeployDeviceStep: () => <div>Deploy First Device</div> }));
vi.mock('../steps/ready-step', () => ({
ReadyStep: ({ onStart }: { onStart: () => void }) => (
<button type="button" onClick={onStart}>
You are ready to go
</button>
),
}));
vi.mock('../steps/all-set-step', () => ({
AllSetStep: ({ onStart }: { onStart: () => void }) => (
<button type="button" onClick={onStart}>
Everything is set up
</button>
),
}));

const FRESH_USER: UserOnboardingProgress = {
completedSteps: [],
completed: false,
completedAt: null,
skipped: false,
skippedAt: null,
tourDialogId: null,
setupSeen: false,
};

const SET_UP_TENANT: TenantOnboardingProgress = {
completedSteps: [
TenantOnboardingStep.MSP_SETUP,
TenantOnboardingStep.CUSTOMERS_SETUP,
TenantOnboardingStep.DEVICE_MANAGEMENT,
TenantOnboardingStep.MEET_MINGO,
],
completed: true,
completedAt: null,
};

let container: HTMLDivElement;
let root: Root;

function progress(tenant: TenantOnboardingProgress, user: UserOnboardingProgress) {
const store = useOnboardingStore.getState();
store.setTenant(tenant);
store.setUser(user);
}

function render() {
act(() => root.render(<SetupWizard />));
}

function click(label: string) {
const button = Array.from(container.querySelectorAll('button')).find(b => b.textContent === label);
if (!button) throw new Error(`no button "${label}"`);
act(() => button.dispatchEvent(new MouseEvent('click', { bubbles: true })));
}

beforeEach(() => {
replace.mockReset();
completeTenantStep.mockReset();
completeTenantInBackground.mockReset();
markSetupSeenInBackground.mockReset();
mode.saas = false;
useOnboardingStore.getState().reset();
useAuthStore.setState({ user: { id: 'u-1', email: 'ann@example.com' } } as never);
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
});

afterEach(() => {
act(() => root.unmount());
container.remove();
useAuthStore.setState({ user: null } as never);
});

describe('SetupWizard and the last screen', () => {
it('opens a set-up workspace on "Everything is set up" and records the screen as seen', () => {
progress(SET_UP_TENANT, FRESH_USER);
render();

expect(container.textContent).toContain('Everything is set up');
expect(markSetupSeenInBackground).toHaveBeenCalledTimes(1);
});

it('does not record the screen again for an admin the server has seen there', () => {
progress(SET_UP_TENANT, { ...FRESH_USER, setupSeen: true });
render();

expect(container.textContent).toContain('Everything is set up');
expect(markSetupSeenInBackground).not.toHaveBeenCalled();
});

it('records "You are ready to go" too, and none of the screens before it', () => {
progress(
{ ...SET_UP_TENANT, completedSteps: SET_UP_TENANT.completedSteps.slice(0, 3), completed: false },
FRESH_USER,
);
render();

expect(container.textContent).toContain('Continue');
expect(markSetupSeenInBackground).not.toHaveBeenCalled();

click('Continue');
expect(container.textContent).toContain('You are ready to go');
expect(markSetupSeenInBackground).toHaveBeenCalledTimes(1);
});

it('"Start with Mingo" on a set-up workspace leaves for the dashboard by itself', () => {
progress(SET_UP_TENANT, { ...FRESH_USER, setupSeen: true });
render();
click('Everything is set up');

expect(replace).toHaveBeenCalledWith(routes.dashboard);
});

it('"Start with Mingo" on a SaaS tenant leaves for Mingo\'s home, where the tour lives', () => {
mode.saas = true;
progress(SET_UP_TENANT, { ...FRESH_USER, setupSeen: true });
render();
click('Everything is set up');

expect(replace).toHaveBeenCalledWith(routes.mingo.home);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ vi.mock('@/app/(app)/onboarding/hooks/use-tenant-onboarding-auto-detect', () =>
},
}));
vi.mock('@/graphql/onboarding/use-onboarding-mutations', () => ({
useOnboardingMutations: () => ({ completeTenantStep: vi.fn(), completeTenantInBackground: vi.fn() }),
useOnboardingMutations: () => ({
completeTenantStep: vi.fn(),
completeTenantInBackground: vi.fn(),
markSetupSeenInBackground: vi.fn(),
}),
}));
vi.mock('@flamingo-stack/openframe-frontend-core/hooks', async importOriginal => ({
...(await importOriginal<Record<string, unknown>>()),
Expand Down
19 changes: 16 additions & 3 deletions apps/web/src/app/(app)/setup/components/setup-wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ function SetupLoadingFrame() {
* a workspace already set up opens on "Everything is set up"; otherwise the
* welcome screen leads to the first step not yet done, each step persists
* itself through the onboarding mutations and the walk ends on "You are ready
* to go". "Start with Mingo" marks the tenant setup complete, records that the
* tour has started and leaves for the dashboard with a fresh Mingo chat.
* to go". Either last screen, once it shows, is recorded on the server as seen
* by this admin, so no other browser or device opens on it again. "Start with
* Mingo" marks the tenant setup complete, records that the tour has started and
* leaves for the dashboard with a fresh Mingo chat.
*
* With its own boundary: in its scope the reader draws the screen the wizard opens on (`SetupLoadingFrame`). The
* boundary is the one element between the bare shell's column and the frame: it has to be a filling column too, or
Expand All @@ -83,7 +85,8 @@ function SetupWizardLive() {
const tenant = useOnboardingStore(state => state.tenant);
const userId = useAuthStore(state => state.user?.id);
const completedByData = useTenantOnboardingAutoDetect();
const { completeTenantStep, completeTenantInBackground } = useOnboardingMutations();
const setupUnseen = useOnboardingStore(state => state.user !== null && !state.user.setupSeen);
const { completeTenantStep, completeTenantInBackground, markSetupSeenInBackground } = useOnboardingMutations();

const [step, setStep] = useState<SetupStep>(() => (tenant?.completed ? 'all-set' : 'welcome'));
const [persisting, setPersisting] = useState(false);
Expand Down Expand Up @@ -150,6 +153,16 @@ function SetupWizardLive() {
return () => clearTimeout(timer);
}, [starting, leave]);

// The last screen is on: tell the server this admin has seen it, so no other
// browser or device opens on it again (`useSetupPending`). On display rather
// than on "Start with Mingo": an admin who leaves the screen another way has
// seen it too. Idempotent on the server; a mark that fails waits for the next
// visit instead of retrying in a loop.
const onLastScreen = step === 'ready' || step === 'all-set';
useEffect(() => {
if (onLastScreen && setupUnseen) markSetupSeenInBackground();
}, [onLastScreen, setupUnseen, markSetupSeenInBackground]);

const handleEnrolled = useCallback(
(enrolled: FirstDevice) => {
setDevice(enrolled);
Expand Down
Loading
Loading