import Link from 'next/link';
import { useRouter } from 'next/router';
import React from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { hasPermission, Permission } from '../../../../server/lib/permissions';
import useSettings from '../../../hooks/useSettings';
import { useUser } from '../../../hooks/useUser';
import globalMessages from '../../../i18n/globalMessages';
import Error from '../../../pages/_error';
import Alert from '../../Common/Alert';
import LoadingSpinner from '../../Common/LoadingSpinner';
import PageTitle from '../../Common/PageTitle';
import ProfileHeader from '../ProfileHeader';
const messages = defineMessages({
menuGeneralSettings: 'General',
menuChangePass: 'Password',
menuNotifications: 'Notifications',
menuPermissions: 'Permissions',
unauthorized: 'Unauthorized',
unauthorizedDescription:
"You do not have permission to modify this user's settings.",
});
interface SettingsRoute {
text: string;
route: string;
regex: RegExp;
requiredPermission?: Permission | Permission[];
permissionType?: { type: 'and' | 'or' };
hidden?: boolean;
}
const UserSettings: React.FC = ({ children }) => {
const router = useRouter();
const settings = useSettings();
const { user: currentUser } = useUser();
const { user, error } = useUser({ id: Number(router.query.userId) });
const intl = useIntl();
if (!user && !error) {
return ;
}
if (!user) {
return ;
}
const settingsRoutes: SettingsRoute[] = [
{
text: intl.formatMessage(messages.menuGeneralSettings),
route: '/settings/main',
regex: /\/settings(\/main)?$/,
},
{
text: intl.formatMessage(messages.menuChangePass),
route: '/settings/password',
regex: /\/settings\/password/,
hidden:
(!settings.currentSettings.localLogin &&
!hasPermission(
Permission.MANAGE_SETTINGS,
currentUser?.permissions ?? 0
)) ||
(currentUser?.id !== 1 &&
currentUser?.id !== user?.id &&
hasPermission(Permission.ADMIN, user?.permissions ?? 0)),
},
{
text: intl.formatMessage(messages.menuNotifications),
route: '/settings/notifications',
regex: /\/settings\/notifications/,
},
{
text: intl.formatMessage(messages.menuPermissions),
route: '/settings/permissions',
regex: /\/settings\/permissions/,
requiredPermission: Permission.MANAGE_USERS,
hidden: currentUser?.id !== 1 && currentUser?.id === user.id,
},
];
const activeLinkColor =
'border-indigo-600 text-indigo-500 focus:outline-none focus:text-indigo-500 focus:border-indigo-500';
const inactiveLinkColor =
'border-transparent text-gray-500 hover:text-gray-400 hover:border-gray-300 focus:outline-none focus:text-gray-4700 focus:border-gray-300';
const SettingsLink: React.FC<{
route: string;
regex: RegExp;
isMobile?: boolean;
}> = ({ children, route, regex, isMobile = false }) => {
const finalRoute = router.asPath.includes('/profile')
? `/profile${route}`
: `/users/${user.id}${route}`;
if (isMobile) {
return ;
}
return (
{children}
);
};
if (currentUser?.id !== 1 && user.id === 1) {
return (
<>
{intl.formatMessage(messages.unauthorizedDescription)}
>
);
}
const currentRoute = settingsRoutes.find(
(route) => !!router.pathname.match(route.regex)
)?.route;
const finalRoute = router.asPath.includes('/profile')
? `/profile${currentRoute}`
: `/users/${user.id}${currentRoute}`;
return (
<>
{children}
>
);
};
export default UserSettings;