From e7bc029146a198b74d5eba113710df9641179717 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Fri, 10 Nov 2023 11:23:14 +0100 Subject: [PATCH 1/8] chore(clerk-js,shared,clerk-react): Migrate useCoreSession, useCoreUser -> useSession, useUser --- .changeset/blue-grapes-marry.md | 6 +++++ .../clerk-js/src/ui.retheme/common/Gate.tsx | 6 ++--- .../ImpersonationFab/ImpersonationFab.tsx | 10 ++++--- .../OrganizationList/UserMembershipList.tsx | 9 +++++-- .../ActionConfirmationPage.tsx | 8 +++--- .../OrganizationProfile/ActiveMembersList.tsx | 7 ++--- .../OrganizationSwitcherPopover.tsx | 8 ++++-- .../OrganizationSwitcherTrigger.tsx | 10 +++++-- .../OtherOrganizationActions.tsx | 6 ++--- .../UserMembershipList.tsx | 8 ++++-- .../components/UserButton/UserButton.tsx | 9 +++++-- .../UserButton/UserButtonPopover.tsx | 7 ++--- .../UserButton/UserButtonTrigger.tsx | 4 +-- .../UserButton/useMultisessionActions.tsx | 9 ++++--- .../UserProfile/ActiveDevicesSection.tsx | 16 ++++++------ .../UserProfile/AddAuthenticatorApp.tsx | 6 ++--- .../UserProfile/ConnectedAccountsPage.tsx | 13 +++++----- .../UserProfile/ConnectedAccountsSection.tsx | 14 +++++++--- .../components/UserProfile/DeletePage.tsx | 10 +++++-- .../components/UserProfile/EmailPage.tsx | 11 ++++---- .../components/UserProfile/EmailSection.tsx | 12 ++++----- .../UserProfile/EnterpriseAccountsSection.tsx | 8 +++--- .../UserProfile/MfaBackupCodeCreatePage.tsx | 6 ++--- .../UserProfile/MfaBackupCodeList.tsx | 11 ++++++-- .../components/UserProfile/MfaPage.tsx | 10 +++++-- .../UserProfile/MfaPhoneCodePage.tsx | 9 +++++-- .../components/UserProfile/MfaSection.tsx | 10 +++++-- .../components/UserProfile/PasswordPage.tsx | 14 +++++++--- .../UserProfile/PasswordSection.tsx | 11 ++++++-- .../components/UserProfile/PhonePage.tsx | 12 ++++----- .../components/UserProfile/PhoneSection.tsx | 13 +++++++--- .../components/UserProfile/ProfilePage.tsx | 10 +++++-- .../UserProfile/RemoveResourcePage.tsx | 26 +++++++++---------- .../components/UserProfile/RootPage.tsx | 10 ++++--- .../UserProfile/UserProfileSection.tsx | 11 ++++++-- .../components/UserProfile/UsernamePage.tsx | 11 ++++++-- .../UserProfile/UsernameSection.tsx | 9 +++++-- .../components/UserProfile/VerifyTOTP.tsx | 6 ++--- .../components/UserProfile/Web3Page.tsx | 17 ++++++++---- .../components/UserProfile/Web3Section.tsx | 6 ++--- .../contexts/CoreClerkContextWrapper.tsx | 6 ++--- .../ui.retheme/contexts/CoreClientContext.tsx | 8 +----- .../contexts/CoreSessionContext.tsx | 19 +++----------- .../ui.retheme/contexts/CoreUserContext.tsx | 12 +-------- packages/react/src/hooks/index.ts | 4 +-- packages/shared/src/react/hooks/index.ts | 3 +++ .../src/react}/hooks/useSession.ts | 2 +- .../src/react}/hooks/useSessionList.ts | 5 ++-- .../src => shared/src/react}/hooks/useUser.ts | 2 +- 49 files changed, 280 insertions(+), 180 deletions(-) create mode 100644 .changeset/blue-grapes-marry.md rename packages/{react/src => shared/src/react}/hooks/useSession.ts (95%) rename packages/{react/src => shared/src/react}/hooks/useSessionList.ts (76%) rename packages/{react/src => shared/src/react}/hooks/useUser.ts (95%) diff --git a/.changeset/blue-grapes-marry.md b/.changeset/blue-grapes-marry.md new file mode 100644 index 00000000000..e815dbac055 --- /dev/null +++ b/.changeset/blue-grapes-marry.md @@ -0,0 +1,6 @@ +--- +'@clerk/clerk-js': minor +'@clerk/shared': minor +--- + +Move usage of internal useCoreX hooks to useX hooks diff --git a/packages/clerk-js/src/ui.retheme/common/Gate.tsx b/packages/clerk-js/src/ui.retheme/common/Gate.tsx index 0c1a016742e..1644b6c7b05 100644 --- a/packages/clerk-js/src/ui.retheme/common/Gate.tsx +++ b/packages/clerk-js/src/ui.retheme/common/Gate.tsx @@ -1,8 +1,8 @@ +import { useSession } from '@clerk/shared/react'; import type { CheckAuthorization } from '@clerk/types'; import type { ComponentType, PropsWithChildren, ReactNode } from 'react'; import React, { useEffect } from 'react'; -import { useCoreSession } from '../contexts'; import { useRouter } from '../router'; type GateParams = Parameters[0]; @@ -14,10 +14,10 @@ type GateProps = PropsWithChildren< >; export const useGate = (params: GateParams) => { - const { experimental__checkAuthorization } = useCoreSession(); + const { session } = useSession(); return { - isAuthorizedUser: experimental__checkAuthorization(params), + isAuthorizedUser: session?.experimental__checkAuthorization(params), }; }; diff --git a/packages/clerk-js/src/ui.retheme/components/ImpersonationFab/ImpersonationFab.tsx b/packages/clerk-js/src/ui.retheme/components/ImpersonationFab/ImpersonationFab.tsx index 1478bb9a747..0e69e688f57 100644 --- a/packages/clerk-js/src/ui.retheme/components/ImpersonationFab/ImpersonationFab.tsx +++ b/packages/clerk-js/src/ui.retheme/components/ImpersonationFab/ImpersonationFab.tsx @@ -1,8 +1,9 @@ +import { useSession } from '@clerk/shared/react'; import type { PointerEventHandler } from 'react'; import React, { useEffect, useRef } from 'react'; import { getFullName, getIdentifier } from '../../../utils/user'; -import { useCoreClerk, useCoreSession, withCoreUserGuard } from '../../contexts'; +import { useCoreClerk, withCoreUserGuard } from '../../contexts'; import type { LocalizationKey } from '../../customizables'; import { Col, @@ -57,7 +58,7 @@ const EyeCircle = ({ width, height, ...props }: EyeCircleProps) => { type FabContentProps = { title: LocalizationKey; signOutText: LocalizationKey }; const FabContent = ({ title, signOutText }: FabContentProps) => { - const session = useCoreSession(); + const { session } = useSession(); const { signOut } = useCoreClerk(); return ( @@ -88,7 +89,8 @@ const FabContent = ({ title, signOutText }: FabContentProps) => { })} localizationKey={signOutText} onClick={async () => { - await signOut({ sessionId: session.id }); + // clerk-js has been loaded at this point so we can safely access session + await signOut({ sessionId: session!.id }); }} /> @@ -96,7 +98,7 @@ const FabContent = ({ title, signOutText }: FabContentProps) => { }; const _ImpersonationFab = () => { - const session = useCoreSession(); + const { session } = useSession(); const { t } = useLocalizations(); const { parsedInternalTheme } = useAppearance(); const containerRef = useRef(null); diff --git a/packages/clerk-js/src/ui.retheme/components/OrganizationList/UserMembershipList.tsx b/packages/clerk-js/src/ui.retheme/components/OrganizationList/UserMembershipList.tsx index 612838c7db0..9f3666050ab 100644 --- a/packages/clerk-js/src/ui.retheme/components/OrganizationList/UserMembershipList.tsx +++ b/packages/clerk-js/src/ui.retheme/components/OrganizationList/UserMembershipList.tsx @@ -1,6 +1,7 @@ +import { useUser } from '@clerk/shared/react'; import type { OrganizationResource } from '@clerk/types'; -import { useCoreOrganizationList, useCoreUser, useOrganizationListContext } from '../../contexts'; +import { useCoreOrganizationList, useOrganizationListContext } from '../../contexts'; import { OrganizationPreview, PersonalWorkspacePreview, useCardState, withCardStateProvider } from '../../elements'; import { localizationKeys } from '../../localization'; import { OrganizationListPreviewButton, sharedMainIdentifierSx } from './shared'; @@ -36,7 +37,11 @@ export const PersonalAccountPreview = withCardStateProvider(() => { const card = useCardState(); const { hidePersonal, navigateAfterSelectPersonal } = useOrganizationListContext(); const { isLoaded, setActive } = useCoreOrganizationList(); - const user = useCoreUser(); + const { user } = useUser(); + + if (!user) { + return null; + } const { username, primaryEmailAddress, primaryPhoneNumber, ...userWithoutIdentifiers } = user; diff --git a/packages/clerk-js/src/ui.retheme/components/OrganizationProfile/ActionConfirmationPage.tsx b/packages/clerk-js/src/ui.retheme/components/OrganizationProfile/ActionConfirmationPage.tsx index c176b6ef5c3..357c1675092 100644 --- a/packages/clerk-js/src/ui.retheme/components/OrganizationProfile/ActionConfirmationPage.tsx +++ b/packages/clerk-js/src/ui.retheme/components/OrganizationProfile/ActionConfirmationPage.tsx @@ -1,5 +1,7 @@ +import { useUser } from '@clerk/shared/react'; + import { useWizard, Wizard } from '../../common'; -import { useCoreOrganization, useCoreUser, useOrganizationProfileContext } from '../../contexts'; +import { useCoreOrganization, useOrganizationProfileContext } from '../../contexts'; import type { LocalizationKey } from '../../customizables'; import { localizationKeys, Text } from '../../customizables'; import { @@ -18,9 +20,9 @@ export const LeaveOrganizationPage = () => { const card = useCardState(); const { navigateAfterLeaveOrganization } = useOrganizationProfileContext(); const { organization } = useCoreOrganization(); - const user = useCoreUser(); + const { user } = useUser(); - if (!organization) { + if (!organization || !user) { return null; } diff --git a/packages/clerk-js/src/ui.retheme/components/OrganizationProfile/ActiveMembersList.tsx b/packages/clerk-js/src/ui.retheme/components/OrganizationProfile/ActiveMembersList.tsx index bae66d80d9b..09a59d42ea2 100644 --- a/packages/clerk-js/src/ui.retheme/components/OrganizationProfile/ActiveMembersList.tsx +++ b/packages/clerk-js/src/ui.retheme/components/OrganizationProfile/ActiveMembersList.tsx @@ -1,7 +1,8 @@ +import { useUser } from '@clerk/shared/react'; import type { OrganizationMembershipResource } from '@clerk/types'; import { Gate } from '../../common/Gate'; -import { useCoreOrganization, useCoreUser } from '../../contexts'; +import { useCoreOrganization } from '../../contexts'; import { Badge, localizationKeys, Td, Text } from '../../customizables'; import { ThreeDotsMenu, useCardState, UserPreview } from '../../elements'; import { useFetchRoles, useLocalizeCustomRoles } from '../../hooks/useFetchRoles'; @@ -76,9 +77,9 @@ const MemberRow = (props: { const { membership, onRemove, onRoleChange, options } = props; const { localizeCustomRole } = useLocalizeCustomRoles(); const card = useCardState(); - const user = useCoreUser(); + const { user } = useUser(); - const isCurrentUser = user.id === membership.publicUserData.userId; + const isCurrentUser = user?.id === membership.publicUserData.userId; const unlocalizedRoleLabel = options?.find(a => a.value === membership.role)?.label; return ( diff --git a/packages/clerk-js/src/ui.retheme/components/OrganizationSwitcher/OrganizationSwitcherPopover.tsx b/packages/clerk-js/src/ui.retheme/components/OrganizationSwitcher/OrganizationSwitcherPopover.tsx index f584cc151bb..3fac21e57e1 100644 --- a/packages/clerk-js/src/ui.retheme/components/OrganizationSwitcher/OrganizationSwitcherPopover.tsx +++ b/packages/clerk-js/src/ui.retheme/components/OrganizationSwitcher/OrganizationSwitcherPopover.tsx @@ -1,3 +1,4 @@ +import { useUser } from '@clerk/shared/react'; import type { OrganizationResource } from '@clerk/types'; import React from 'react'; @@ -7,7 +8,6 @@ import { useCoreClerk, useCoreOrganization, useCoreOrganizationList, - useCoreUser, useEnvironment, useOrganizationSwitcherContext, } from '../../contexts'; @@ -55,7 +55,11 @@ export const OrganizationSwitcherPopover = React.forwardRef((props, ref) => { const { sx, ...rest } = props; - const { username, primaryEmailAddress, primaryPhoneNumber, ...userWithoutIdentifiers } = useCoreUser(); + const { user } = useUser(); const { organization } = useCoreOrganization(); const { hidePersonal } = useOrganizationSwitcherContext(); + if (!user) { + return null; + } + + const { username, primaryEmailAddress, primaryPhoneNumber, ...userWithoutIdentifiers } = user; + return (