2022-08-21 02:13:31 +03:00
|
|
|
import { Tabs } from 'antd';
|
|
|
|
import { useRecoilValue } from 'recoil';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { FC } from 'react';
|
2023-03-13 08:53:57 +03:00
|
|
|
import { ErrorBoundary } from 'react-error-boundary';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { IndieAuthModal } from '../IndieAuthModal/IndieAuthModal';
|
|
|
|
import { FediAuthModal } from '../FediAuthModal/FediAuthModal';
|
2022-08-21 02:13:31 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
import styles from './AuthModal.module.scss';
|
2022-08-21 02:13:31 +03:00
|
|
|
import {
|
2022-10-11 02:26:09 +03:00
|
|
|
currentUserAtom,
|
2022-08-21 02:13:31 +03:00
|
|
|
chatAuthenticatedAtom,
|
|
|
|
accessTokenAtom,
|
2022-12-30 03:26:04 +03:00
|
|
|
clientConfigStateAtom,
|
2022-08-21 02:13:31 +03:00
|
|
|
} from '../../stores/ClientConfigStore';
|
2022-12-30 03:26:04 +03:00
|
|
|
import { ClientConfig } from '../../../interfaces/client-config.model';
|
2023-03-13 08:53:57 +03:00
|
|
|
import { ComponentError } from '../../ui/ComponentError/ComponentError';
|
2022-08-21 02:13:31 +03:00
|
|
|
|
2022-12-30 03:26:04 +03:00
|
|
|
export type AuthModalProps = {
|
|
|
|
forceTabs?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AuthModal: FC<AuthModalProps> = ({ forceTabs }) => {
|
2022-10-29 08:18:51 +03:00
|
|
|
const authenticated = useRecoilValue<boolean>(chatAuthenticatedAtom);
|
|
|
|
const accessToken = useRecoilValue<string>(accessTokenAtom);
|
2022-10-11 02:26:09 +03:00
|
|
|
const currentUser = useRecoilValue(currentUserAtom);
|
2022-12-30 03:26:04 +03:00
|
|
|
const clientConfig = useRecoilValue<ClientConfig>(clientConfigStateAtom);
|
|
|
|
|
2022-10-11 02:26:09 +03:00
|
|
|
if (!currentUser) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-10-29 08:18:51 +03:00
|
|
|
const { displayName } = currentUser;
|
2022-12-30 03:26:04 +03:00
|
|
|
const { federation } = clientConfig;
|
|
|
|
const { enabled: fediverseEnabled } = federation;
|
2022-08-21 02:13:31 +03:00
|
|
|
|
2022-10-11 03:53:14 +03:00
|
|
|
const indieAuthTabTitle = (
|
|
|
|
<span className={styles.tabContent}>
|
2023-01-13 00:53:09 +03:00
|
|
|
<img className={styles.icon} src="/img/indieauth.png" alt="IndieAuth" />
|
2022-10-11 03:53:14 +03:00
|
|
|
IndieAuth
|
|
|
|
</span>
|
|
|
|
);
|
2022-10-29 08:18:51 +03:00
|
|
|
|
2022-10-11 03:53:14 +03:00
|
|
|
const indieAuthTab = (
|
|
|
|
<IndieAuthModal
|
|
|
|
authenticated={authenticated}
|
|
|
|
displayName={displayName}
|
|
|
|
accessToken={accessToken}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const fediAuthTabTitle = (
|
|
|
|
<span className={styles.tabContent}>
|
2023-01-13 00:53:09 +03:00
|
|
|
<img className={styles.icon} src="/img/fediverse-black.png" alt="Fediverse auth" />
|
2022-10-11 03:53:14 +03:00
|
|
|
FediAuth
|
|
|
|
</span>
|
|
|
|
);
|
2022-10-29 08:18:51 +03:00
|
|
|
|
2022-10-11 03:53:14 +03:00
|
|
|
const fediAuthTab = (
|
|
|
|
<FediAuthModal
|
|
|
|
authenticated={authenticated}
|
|
|
|
displayName={displayName}
|
|
|
|
accessToken={accessToken}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const items = [
|
|
|
|
{ label: indieAuthTabTitle, key: '1', children: indieAuthTab },
|
|
|
|
{ label: fediAuthTabTitle, key: '2', children: fediAuthTab },
|
|
|
|
];
|
|
|
|
|
2022-08-21 02:13:31 +03:00
|
|
|
return (
|
2023-03-13 08:53:57 +03:00
|
|
|
<ErrorBoundary
|
|
|
|
// eslint-disable-next-line react/no-unstable-nested-components
|
|
|
|
fallbackRender={({ error, resetErrorBoundary }) => (
|
|
|
|
<ComponentError
|
2023-03-13 09:05:21 +03:00
|
|
|
componentName="AuthModal"
|
2023-03-13 08:53:57 +03:00
|
|
|
message={error.message}
|
|
|
|
retryFunction={resetErrorBoundary}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<Tabs
|
|
|
|
defaultActiveKey="1"
|
|
|
|
items={items}
|
|
|
|
type="card"
|
|
|
|
size="small"
|
|
|
|
renderTabBar={fediverseEnabled || forceTabs ? null : () => null}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</ErrorBoundary>
|
2022-08-21 02:13:31 +03:00
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|