Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.9 KB
Raw Blame
'use client';

import { isDesktop } from '@lobechat/const';
import { Divider, Flexbox } from '@lobehub/ui';
import { Form } from '@lobehub/ui/form';
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';

import SkeletonBar from '@/components/Skeleton/Bar';
import ProfileSkeleton from '@/components/Skeleton/Settings/tabs/Profile';
import SettingHeader from '@/features/Settings/features/SettingHeader';
import { useServerConfigStore } from '@/store/serverConfig';
import { serverConfigSelectors } from '@/store/serverConfig/selectors';
import { useToolStore } from '@/store/tool';
import { ComposioServerStatus } from '@/store/tool/slices/composioStore';
import { useUserStore } from '@/store/user';
import { authSelectors, userProfileSelectors } from '@/store/user/selectors';

import AvatarRow from './features/AvatarRow';
import ComposioAuthorizationList from './features/ComposioAuthorizationList';
import EmailRow from './features/EmailRow';
import FullNameRow from './features/FullNameRow';
import InterestsRow from './features/InterestsRow';
import PasswordRow from './features/PasswordRow';
import ProfileRow from './features/ProfileRow';
import SSOProvidersList from './features/SSOProvidersList';
import UsernameRow from './features/UsernameRow';

interface ProfileSettingProps {
  showSettingHeader?: boolean;
}

const ProfileSetting = ({ showSettingHeader = true }: ProfileSettingProps) => {
  const isLogin = useUserStore(authSelectors.isLogin);
  const [userProfile, isUserLoaded] = useUserStore((s) => [
    userProfileSelectors.userProfile(s),
    s.isLoaded,
  ]);
  const isLoadedAuthProviders = useUserStore(authSelectors.isLoadedAuthProviders);
  const fetchAuthProviders = useUserStore((s) => s.fetchAuthProviders);
  const enableComposio = useServerConfigStore(serverConfigSelectors.enableComposio);
  const disableEmailPassword = useServerConfigStore(serverConfigSelectors.disableEmailPassword);
  const [servers, isServersInit, useFetchUserComposioConnections] = useToolStore((s) => [
    s.composioServers,
    s.isComposioServersInit,
    s.useFetchUserComposioConnections,
  ]);
  const connectedServers = servers.filter((s) => s.status === ComposioServerStatus.ACTIVE);

  // Fetch Composio servers
  useFetchUserComposioConnections(enableComposio);

  // Only the core profile rows (avatar / name / username / email) gate on the
  // user record itself. Auth-providers (SSO) and Composio are independent, slower
  // sub-sections that render their own rows when ready — folding them into one
  // composite gate let a single slow/failed dependency skeleton the whole tab.
  const isLoading = !isUserLoaded;

  useEffect(() => {
    if (isLogin) {
      fetchAuthProviders();
    }
  }, [isLogin, fetchAuthProviders]);

  const { t } = useTranslation('auth');

  // Same account group the route and chunk fallbacks draw, so the tab never
  // swaps between two differently shaped skeletons.
  if (isLoading)
    return (
      <>
        {showSettingHeader && <SettingHeader title={t('profile.title')} />}
        <ProfileSkeleton.Body />
      </>
    );

  return (
    <>
      {showSettingHeader && <SettingHeader title={t('profile.title')} />}
      <Form.Group collapsible={false} title={t('profile.account')} variant={'filled'}>
        <Flexbox>
          <AvatarRow />

          <Divider style={{ margin: 0 }} />

          <FullNameRow />

          <Divider style={{ margin: 0 }} />

          <UsernameRow />

          <Divider style={{ margin: 0 }} />

          <InterestsRow />

          {!isDesktop && isLogin && !disableEmailPassword && (
            <>
              <Divider style={{ margin: 0 }} />
              <PasswordRow />
            </>
          )}

          {isLogin && userProfile?.email && (
            <>
              <Divider style={{ margin: 0 }} />
              <EmailRow />
            </>
          )}

          {/* The row is known up front; only its provider list waits on the
              slower auth-providers fetch, so the row does not pop in. */}
          {isLogin && !isDesktop && (
            <>
              <Divider style={{ margin: 0 }} />
              <ProfileRow anchor={'profile-connected-accounts'} label={t('profile.sso.providers')}>
                {isLoadedAuthProviders ? (
                  <SSOProvidersList />
                ) : (
                  <SkeletonBar height={22} width={120} />
                )}
              </ProfileRow>
            </>
          )}

          {enableComposio && isServersInit && connectedServers.length > 0 && (
            <>
              <Divider style={{ margin: 0 }} />
              <ProfileRow
                anchor={'profile-authorizations'}
                label={t('profile.authorizations.title')}
              >
                <ComposioAuthorizationList servers={connectedServers} />
              </ProfileRow>
            </>
          )}
        </Flexbox>
      </Form.Group>
    </>
  );
};

export default ProfileSetting;