|
| 1 | +import type { |
| 2 | + OrganizationProfileModalContentProps, |
| 3 | + OrganizationProfileModalOptions, |
| 4 | +} from '@clerk/mosaic/features/organization-profile/organization-profile.modal.types'; |
| 5 | +import { OrganizationProfileView } from '@clerk/mosaic/features/organization-profile/organization-profile.view'; |
| 6 | +import type { SwitcherViewProps } from '@clerk/mosaic/features/switcher/switcher.view'; |
| 7 | +import { SwitcherView } from '@clerk/mosaic/features/switcher/switcher.view'; |
| 8 | +import type { UserButtonMode } from '@clerk/mosaic/features/user-button/user-button'; |
| 9 | +import type { |
| 10 | + UserProfileModalContentProps, |
| 11 | + UserProfileModalOptions, |
| 12 | +} from '@clerk/mosaic/features/user-profile/user-profile.modal.types'; |
| 13 | +import { UserProfileView } from '@clerk/mosaic/features/user-profile/user-profile.view'; |
| 14 | +import { defineModalSurface } from '@clerk/mosaic/modals/define-modal-surface'; |
| 15 | +import type { ModalSurfaces } from '@clerk/mosaic/modals/modal.types'; |
| 16 | +import { ModalHost } from '@clerk/mosaic/modals/modal-host'; |
| 17 | +import { useModal } from '@clerk/mosaic/modals/use-modal'; |
| 18 | +import { useState } from 'react'; |
| 19 | + |
| 20 | +import type { StoryMeta } from '@/lib/types'; |
| 21 | + |
| 22 | +import { useOrganizationProfileFixture } from './fixtures/organization-profile'; |
| 23 | +import { usePrototype } from './fixtures/switcher'; |
| 24 | +import { useUserProfileFixture } from './fixtures/user-profile'; |
| 25 | + |
| 26 | +export const meta: StoryMeta = { |
| 27 | + group: 'User Button', |
| 28 | + title: 'ProfilePermutations', |
| 29 | + label: 'Profile permutations', |
| 30 | + status: 'wip', |
| 31 | + source: 'packages/swingset/src/stories/profile-permutations.stories.tsx', |
| 32 | +}; |
| 33 | + |
| 34 | +export function CombinedButtonCombinedProfile(_args: Record<string, unknown>) { |
| 35 | + return ( |
| 36 | + <Permutation |
| 37 | + userButton='combined' |
| 38 | + profile='combined' |
| 39 | + /> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +export function CombinedButtonSeparateProfiles(_args: Record<string, unknown>) { |
| 44 | + return ( |
| 45 | + <Permutation |
| 46 | + userButton='combined' |
| 47 | + profile='separate' |
| 48 | + /> |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +export function UserButtonAccountProfile(_args: Record<string, unknown>) { |
| 53 | + return ( |
| 54 | + <Permutation |
| 55 | + userButton='user' |
| 56 | + profile='separate' |
| 57 | + /> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +export function UserButtonCombinedProfile(_args: Record<string, unknown>) { |
| 62 | + return ( |
| 63 | + <Permutation |
| 64 | + userButton='user' |
| 65 | + profile='combined' |
| 66 | + /> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +export function UserButtonAndSwitcherSeparateProfiles(_args: Record<string, unknown>) { |
| 71 | + return ( |
| 72 | + <Permutation |
| 73 | + userButton='user' |
| 74 | + organizationSwitcher |
| 75 | + profile='separate' |
| 76 | + /> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +export function UserButtonAndSwitcherCombinedProfile(_args: Record<string, unknown>) { |
| 81 | + return ( |
| 82 | + <Permutation |
| 83 | + userButton='user' |
| 84 | + organizationSwitcher |
| 85 | + profile='combined' |
| 86 | + /> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +export function SwitcherOrganizationProfile(_args: Record<string, unknown>) { |
| 91 | + return ( |
| 92 | + <Permutation |
| 93 | + organizationSwitcher |
| 94 | + profile='separate' |
| 95 | + /> |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +export function SwitcherCombinedProfile(_args: Record<string, unknown>) { |
| 100 | + return ( |
| 101 | + <Permutation |
| 102 | + organizationSwitcher |
| 103 | + profile='combined' |
| 104 | + /> |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +export function CombinedButtonAndSwitcher(_args: Record<string, unknown>) { |
| 109 | + return ( |
| 110 | + <Permutation |
| 111 | + userButton='combined' |
| 112 | + organizationSwitcher |
| 113 | + profile='combined' |
| 114 | + /> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +interface PermutationProps { |
| 119 | + userButton?: UserButtonMode; |
| 120 | + organizationSwitcher?: boolean; |
| 121 | + profile: 'combined' | 'separate'; |
| 122 | +} |
| 123 | + |
| 124 | +function Permutation(props: PermutationProps) { |
| 125 | + return ( |
| 126 | + <ModalHost surfaces={surfaces}> |
| 127 | + <Triggers {...props} /> |
| 128 | + </ModalHost> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +function Triggers({ userButton, organizationSwitcher = false, profile }: PermutationProps) { |
| 133 | + const prototype = usePrototype(); |
| 134 | + const [openSurface, setOpenSurface] = useState<'userButton' | 'organizationSwitcher'>('userButton'); |
| 135 | + const userProfile = useModal(fixtureUserProfileModal); |
| 136 | + const organizationProfile = useModal(fixtureOrganizationProfileModal); |
| 137 | + const combined = profile === 'combined' && prototype.activeOrganization ? 'combined' : 'user'; |
| 138 | + |
| 139 | + const manageAccount = () => { |
| 140 | + prototype.onManageAccount?.(); |
| 141 | + userProfile.open({ mode: combined }); |
| 142 | + }; |
| 143 | + const manageOrganization = () => { |
| 144 | + prototype.onManageOrganization?.(); |
| 145 | + if (profile === 'combined') { |
| 146 | + userProfile.open({ mode: combined, page: 'organization/general' }); |
| 147 | + } else { |
| 148 | + organizationProfile.open(); |
| 149 | + } |
| 150 | + }; |
| 151 | + const surfaceProps = (surface: typeof openSurface): Omit<SwitcherViewProps, 'mode'> => ({ |
| 152 | + ...prototype, |
| 153 | + open: prototype.open && openSurface === surface, |
| 154 | + onOpenChange: open => { |
| 155 | + setOpenSurface(surface); |
| 156 | + prototype.onOpenChange?.(open); |
| 157 | + }, |
| 158 | + onManageAccount: manageAccount, |
| 159 | + onManageOrganization: manageOrganization, |
| 160 | + }); |
| 161 | + |
| 162 | + return ( |
| 163 | + <div className='flex flex-wrap items-center gap-4'> |
| 164 | + {organizationSwitcher ? ( |
| 165 | + <SwitcherView |
| 166 | + {...surfaceProps('organizationSwitcher')} |
| 167 | + mode='organization' |
| 168 | + /> |
| 169 | + ) : null} |
| 170 | + {userButton ? ( |
| 171 | + <SwitcherView |
| 172 | + {...surfaceProps('userButton')} |
| 173 | + mode={userButton} |
| 174 | + triggerVariant={userButton === 'combined' ? 'full' : 'condensed'} |
| 175 | + onManageOrganization={userButton === 'combined' && profile === 'combined' ? undefined : manageOrganization} |
| 176 | + /> |
| 177 | + ) : null} |
| 178 | + </div> |
| 179 | + ); |
| 180 | +} |
| 181 | + |
| 182 | +const fixtureUserProfileModal = defineModalSurface<UserProfileModalOptions>({ |
| 183 | + id: 'permutationUserProfile', |
| 184 | + variant: 'profile', |
| 185 | + load: () => Promise.resolve({ default: FixtureUserProfile }), |
| 186 | +}); |
| 187 | + |
| 188 | +const fixtureOrganizationProfileModal = defineModalSurface<OrganizationProfileModalOptions>({ |
| 189 | + id: 'permutationOrganizationProfile', |
| 190 | + variant: 'profile', |
| 191 | + load: () => Promise.resolve({ default: FixtureOrganizationProfile }), |
| 192 | +}); |
| 193 | + |
| 194 | +const surfaces: ModalSurfaces = [fixtureUserProfileModal, fixtureOrganizationProfileModal]; |
| 195 | + |
| 196 | +function FixtureUserProfile({ options }: UserProfileModalContentProps) { |
| 197 | + const { pages } = useUserProfileFixture(); |
| 198 | + const organization = useOrganizationProfileFixture(); |
| 199 | + const [activePage, setActivePage] = useState(options?.page ?? 'account'); |
| 200 | + |
| 201 | + return ( |
| 202 | + <UserProfileView |
| 203 | + activePage={activePage} |
| 204 | + pages={pages} |
| 205 | + organization={options?.mode === 'combined' ? { pages: organization.pages } : undefined} |
| 206 | + onPageChange={setActivePage} |
| 207 | + /> |
| 208 | + ); |
| 209 | +} |
| 210 | + |
| 211 | +function FixtureOrganizationProfile(_props: OrganizationProfileModalContentProps) { |
| 212 | + const { activePage, setActivePage, pages } = useOrganizationProfileFixture(); |
| 213 | + |
| 214 | + return ( |
| 215 | + <OrganizationProfileView |
| 216 | + activePage={activePage} |
| 217 | + pages={pages} |
| 218 | + onPageChange={setActivePage} |
| 219 | + /> |
| 220 | + ); |
| 221 | +} |
0 commit comments