Skip to content
Open
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
2 changes: 2 additions & 0 deletions .changeset/mosaic-embedded-org-task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
4 changes: 2 additions & 2 deletions packages/mosaic/docs/views.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ When one screen replaces another on the same card or dialog, render both as step
```

- **The surface sits outside the flow, and a step renders only what goes inside it.** A screen used as a step of another feature cannot bring its own `Card.Root`: `ConfirmShell` owns the card and `Reverification` renders only its contents.
- **One flow per surface.** A step that has steps of its own adds them to the same flow, so the view renders one `Flow.Root`.
- **One flow per feature.** A feature's view renders one `Flow.Root`, so a step with steps of its own adds them to that flow. A feature mounted as a step of another keeps its own, as `Reverification` does in the add-email dialog.
- **The controller owns the step.** Going back is direction `-1`. Derive it from the step when each step has one way in, and hold it in the controller when it does not (`reverification.controller.ts`).
- **Name step kinds for what the user does,** not for what the screen looks like: `credential`, `code`, `choice`. Before adding a member to a kind, check the name still fits it (a passkey is a credential but not a secret).
- **Hold the primary action while it runs.** A step's async action is a `SubmitButton` with `isPending` and a `pendingLabel`, and it stays pending until the next step renders. Every other action on the step takes `disabled={isPending}` (`reverification.view.tsx`).
Expand All @@ -60,7 +60,7 @@ Only the part that creates the surface renders `Card.Root`, and cards do not nes
| Steps | `Flow.Root` and its steps, no card | `CreateOrganizationSteps` |
| Screen | `Card.Header`, `Card.Banner`, `Card.Content` and `Card.Footer` | A step of the flow |

A feature's skeleton is a surface too. A feature another feature can embed exports its steps, and its view is `Card.Root` around them.
A feature's own skeleton is a surface too. A feature another feature mounts exports its content without the card and a `use<Feature>Flow` hook that runs it, and renders `Card.Root` only when it is shown alone (`useCreateOrganizationFlow` with `CreateOrganizationSteps`).

Chrome that stays across steps goes under the surface, outside the flow. A step's `Card.Footer` must be a sibling of its `Card.Content`, so a step on a surface with a shared footer puts its submit button in `Card.Content` (`waitlist.view.tsx`).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ export type CreateOrganizationModel =
onComplete: (organizationId: string) => Promise<void>;
};

export function useCreateOrganizationModel(options: CreateOrganizationModelOptions = {}): CreateOrganizationModel {
export function useCreateOrganizationModel(
options: CreateOrganizationModelOptions & { enabled?: boolean } = {},
): CreateOrganizationModel {
const { isLoaded: isUserLoaded, user } = useUser();
const {
isLoaded: isListLoaded,
createOrganization,
userMemberships,
} = useOrganizationList({ userMemberships: { infinite: true } });
} = useOrganizationList(options.enabled === false ? undefined : { userMemberships: { infinite: true } });
const clerk = useClerk();
const environment = useMosaicEnvironment();
const router = useMosaicRouter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export type OrganizationListEntries = Pick<
onAcceptSuggestion: (suggestionId: string) => Promise<unknown>;
};

export function useOrganizationListEntries(): OrganizationListEntries {
const { userMemberships, userInvitations, userSuggestions, ref } = useOrganizationListInView();
export function useOrganizationListEntries({ enabled = true }: { enabled?: boolean } = {}): OrganizationListEntries {
const { userMemberships, userInvitations, userSuggestions, ref } = useOrganizationListInView({ enabled });

const membershipData = userMemberships.data ?? [];
const invitationData = userInvitations.data?.filter(Boolean) ?? [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type { TaskChooseOrganizationFlowController, TaskChooseOrganizationProps } from './task-choose-organization';
export {
isTaskChooseOrganizationReady,
TaskChooseOrganization,
TaskChooseOrganizationContent,
useTaskChooseOrganizationFlow,
} from './task-choose-organization';
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ const ABSOLUTE_URL = /^https?:\/\//;

export function useTaskChooseOrganizationModel({
redirectUrlComplete,
}: TaskChooseOrganizationModelOptions): TaskChooseOrganizationModel {
enabled = true,
}: TaskChooseOrganizationModelOptions & { enabled?: boolean }): TaskChooseOrganizationModel {
const { isLoaded: isUserLoaded, user } = useUser();
const { isLoaded: isSessionLoaded, session } = useSession();
const clerk = useClerk();
const environment = useMosaicEnvironment();
const router = useMosaicRouter();
const createOrganization = useCreateOrganizationModel({ skipInvitationScreen: true });
const { findOrganization: _findOrganization, ...entries } = useOrganizationListEntries();
const creationDefaults = useOrganizationCreationDefaults();
const createOrganization = useCreateOrganizationModel({ skipInvitationScreen: true, enabled });
const { findOrganization: _findOrganization, ...entries } = useOrganizationListEntries({ enabled });
const creationDefaults = useOrganizationCreationDefaults({ enabled });

if (!isUserLoaded || !isSessionLoaded || !environment) {
return { status: 'loading' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ import { TaskChooseOrganizationHeader, TaskChooseOrganizationSignOut } from './t

const PLACEHOLDER_ORGANIZATIONS = ['Acme Corporation', 'Globex'];

interface TaskChooseOrganizationSkeletonStepsProps {
canCreate: boolean;
identifier?: string;
}

export function TaskChooseOrganizationSkeleton({
renderBranding,
...props
}: TaskChooseOrganizationSkeletonStepsProps & { renderBranding: boolean }) {
return (
<Card.Root renderBranding={renderBranding}>
<TaskChooseOrganizationSkeletonSteps {...props} />
</Card.Root>
);
}

export function TaskChooseOrganizationSkeletonSteps({
canCreate,
identifier,
renderBranding,
}: {
canCreate: boolean;
identifier?: string;
renderBranding: boolean;
}) {
}: TaskChooseOrganizationSkeletonStepsProps) {
const m = useMessages('organizationList');
return (
<Card.Root renderBranding={renderBranding}>
<>
<TaskChooseOrganizationHeader canCreate={canCreate} />
<Card.Content>
<VisuallyHidden role='status'>{m.loading}</VisuallyHidden>
Expand All @@ -45,6 +56,6 @@ export function TaskChooseOrganizationSkeleton({
</Item.Group>
</Card.Content>
<TaskChooseOrganizationSignOut identifier={identifier} />
</Card.Root>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
import type { ReactElement, ReactNode } from 'react';
import { useEffect } from 'react';

import { Card } from '../../components/card';
import { fill, useMessages } from '../../localization';
import { useCreateOrganizationFlow } from '../create-organization/create-organization';
import type { CreateOrganizationReadyModel } from '../create-organization/create-organization.controller';
import { CreateOrganizationSteps } from '../create-organization/create-organization.view';
import type { TaskChooseOrganizationReadyController } from './task-choose-organization.controller';
import type {
TaskChooseOrganizationController,
TaskChooseOrganizationReadyController,
} from './task-choose-organization.controller';
import {
useTaskChooseOrganizationController,
useTaskChooseOrganizationScreenController,
} from './task-choose-organization.controller';
import type { TaskChooseOrganizationModelOptions } from './task-choose-organization.model';
import { useTaskChooseOrganizationModel } from './task-choose-organization.model';
import { TaskChooseOrganizationSkeleton } from './task-choose-organization.skeleton';
import { TaskChooseOrganizationSkeletonSteps } from './task-choose-organization.skeleton';
import type { TaskChooseOrganizationData } from './task-choose-organization.types';
import { TaskChooseOrganizationView } from './task-choose-organization.view';
import { TaskChooseOrganizationSteps } from './task-choose-organization.view';

/** Everything `<TaskChooseOrganization />` takes: where to go once the session has no tasks left. */
export type TaskChooseOrganizationProps = TaskChooseOrganizationModelOptions & {
Expand All @@ -40,30 +44,62 @@ export type TaskChooseOrganizationProps = TaskChooseOrganizationModelOptions & {
* ```
*/
export function TaskChooseOrganization({ fallback, ...options }: TaskChooseOrganizationProps): ReactElement | null {
const task = useTaskChooseOrganizationFlow(options);

if (task.status === 'loading') {
return <>{fallback}</>;
}

if (task.status === 'ready') {
return (
<Card.Root renderBranding={task.renderBranding}>
<TaskChooseOrganizationContent controller={task} />
</Card.Root>
);
}

return <TaskChooseOrganizationContent controller={task} />;
}

export type TaskChooseOrganizationFlowController =
| TaskChooseOrganizationController
| { status: 'complete'; onContinue: () => Promise<unknown> };

export function useTaskChooseOrganizationFlow(
options: TaskChooseOrganizationModelOptions & { enabled?: boolean },
): TaskChooseOrganizationFlowController {
const model = useTaskChooseOrganizationModel(options);
const controller = useTaskChooseOrganizationController(model);

if (controller.status !== 'ready' && model.status === 'complete') {
return { status: 'complete', onContinue: model.onContinue };
}
return controller;
}

export function isTaskChooseOrganizationReady(controller: TaskChooseOrganizationFlowController): boolean {
return controller.status !== 'loading' && !(controller.status === 'ready' && controller.organizationsLoading);
}

export function TaskChooseOrganizationContent({
controller,
}: {
controller: TaskChooseOrganizationFlowController;
}): ReactElement | null {
if (controller.status === 'ready') {
if (controller.organizationsLoading) {
return (
<TaskChooseOrganizationSkeleton
<TaskChooseOrganizationSkeletonSteps
canCreate={Boolean(controller.createOrganization)}
identifier={controller.identifier}
renderBranding={controller.renderBranding}
/>
);
}
return <ConnectedTaskChooseOrganization controller={controller} />;
return <ConnectedTaskChooseOrganizationSteps controller={controller} />;
}

if (model.status === 'complete') {
return <ContinueAfterTask onContinue={model.onContinue} />;
if (controller.status === 'complete') {
return <ContinueAfterTask onContinue={controller.onContinue} />;
}

if (controller.status === 'loading') {
return <>{fallback}</>;
}

return null;
}

Expand All @@ -76,12 +112,12 @@ function ContinueAfterTask({ onContinue }: { onContinue: () => Promise<unknown>
return null;
}

function ConnectedTaskChooseOrganization({ controller }: { controller: TaskChooseOrganizationReadyController }) {
function ConnectedTaskChooseOrganizationSteps({ controller }: { controller: TaskChooseOrganizationReadyController }) {
const screen = useTaskChooseOrganizationScreenController(controller);
const { createOrganization } = controller;

return (
<TaskChooseOrganizationView
<TaskChooseOrganizationSteps
{...controller}
step={screen.step}
createSteps={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,25 @@ type ChooseStepProps = Omit<
'step' | 'renderBranding' | 'identifier' | 'createSteps' | 'onSignOut'
>;

export function TaskChooseOrganizationView({
export function TaskChooseOrganizationView({ renderBranding = true, ...props }: TaskChooseOrganizationViewProps) {
return (
<Card.Root renderBranding={renderBranding}>
<TaskChooseOrganizationSteps {...props} />
</Card.Root>
);
}

export type TaskChooseOrganizationStepsProps = Omit<TaskChooseOrganizationViewProps, 'renderBranding'>;

export function TaskChooseOrganizationSteps({
step,
renderBranding = true,
identifier,
createSteps,
onSignOut,
...props
}: TaskChooseOrganizationViewProps) {
}: TaskChooseOrganizationStepsProps) {
return (
<Card.Root renderBranding={renderBranding}>
<>
<Flow.Root
value={step === 'create' && !createSteps ? 'choose' : step}
direction={step === 'create' ? 1 : -1}
Expand All @@ -63,7 +72,7 @@ export function TaskChooseOrganizationView({
disabled={Boolean(props.pendingKey)}
onSignOut={onSignOut}
/>
</Card.Root>
</>
);
}

Expand Down
Loading