Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import { Avatar, Badge, Box, Center, HStack, Spinner, Stack, Switch, Table, Text } from '@chakra-ui/react';
import { deleteUser, listUsers, updateUser, type UserDTO } from '@features/identity/data/api';
import { useAuthSession } from '@features/identity/session';
import { useIdentityNotify } from '@features/identity/ui/useIdentityNotify';
import { getApiErrorMessage } from '@platform/transport/http';
import { Button, IconButton, ConfirmDialog, Tooltip } from '@platform/ui';
import { BrushCleaningIcon, PencilIcon, Trash2Icon, UserPlusIcon } from 'lucide-react';
import { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { UserFormDialog, type UserFormTarget } from './UserFormDialog';
const ROW_HOVER_STYLES = { bg: 'bg.muted' };
const SWITCH_CHECKED_STYLES = { bg: 'accent.solid' };
const DELETE_BUTTON_HOVER_STYLES = { color: 'fg.error' };
/**
* The account directory, under a page header that names it. Admin access is checked here as well as by the host.
* `onManageIntermediates` is supplied by the shell that owns the intermediates manager's home.
*/
export const UsersManagementPanel = ({
onManageIntermediates,
}: {
onManageIntermediates?: (userId: string, label: string) => void;
}) => {
const { t } = useTranslation();
const session = useAuthSession();
if (!session.multiuserEnabled || session.user?.is_admin !== true) {
return (
<Center h="full" w="full">
<Text color="fg.muted" fontSize="lg">
{t('users.adminOnly')}
</Text>
</Center>
);
}
return <UsersDirectory currentUserId={session.user.user_id} onManageIntermediates={onManageIntermediates} />;
};
const UsersDirectory = ({
currentUserId,
onManageIntermediates,
}: {
currentUserId: string;
onManageIntermediates?: (userId: string, label: string) => void;
}) => {
const { t } = useTranslation();
const notify = useIdentityNotify();
const [users, setUsers] = useState<UserDTO[] | null>(null);
const [loadError, setLoadError] = useState<string | null>(null);
const [formTarget, setFormTarget] = useState<UserFormTarget | null>(null);
const [deleteTarget, setDeleteTarget] = useState<UserDTO | null>(null);
const refresh = useCallback(async () => {
try {
setUsers(await listUsers());
setLoadError(null);
} catch (error) {
setLoadError(getApiErrorMessage(error, t('users.couldNotLoad')));
}
}, [t]);
useEffect(() => {
// eslint-disable-next-line react/set-state-in-effect
void refresh();
}, [refresh]);
const setUserActive = useCallback(
async (user: UserDTO, isActive: boolean) => {
try {
await updateUser(user.user_id, { is_active: isActive });
await refresh();
} catch (error) {
notify.error(
isActive ? t('users.couldNotActivate') : t('users.couldNotDeactivate'),
getApiErrorMessage(error, t('users.backendRejectedChange'))
);
}
},
[notify, refresh, t]
);
const confirmDelete = useCallback(async () => {
if (!deleteTarget) {
return;
}
try {
await deleteUser(deleteTarget.user_id);
notify.success(t('users.deleted'));
await refresh();
} catch (error) {
notify.error(t('users.couldNotDelete'), getApiErrorMessage(error, t('users.backendRejectedRequest')));
}
}, [deleteTarget, notify, refresh, t]);
const openCreateForm = useCallback(() => setFormTarget({ mode: 'create' }), []);
const closeForm = useCallback(() => setFormTarget(null), []);
const refreshAfterSave = useCallback(() => void refresh(), [refresh]);
const closeDeleteConfirm = useCallback(() => setDeleteTarget(null), []);
const retryLoad = useCallback(() => void refresh(), [refresh]);
return (
<>
<Stack gap="3" w="full">
<HStack justify="space-between">
<Text color="fg.muted" fontSize="md">
{users ? t('users.accountCount', { count: users.length }) : null}
</Text>
<Button variant="solid" onClick={openCreateForm}>
<UserPlusIcon />
{t('users.addUser')}
</Button>
</HStack>
<Box bg="bg.subtle" borderColor="border.subtle" borderWidth="1px" overflowX="auto" rounded="lg">
{users === null ? (
<Center minH="40">
{loadError ? (
<Stack align="center" gap="2" p="4">
<Text color="fg.error" fontSize="md" textAlign="center">
{loadError}
</Text>
<Button variant="outline" onClick={retryLoad}>
{t('common.retry')}
</Button>
</Stack>
) : (
<Spinner color="fg.muted" size="lg" />
)}
</Center>
) : (
<Table.Root minW="42rem">
<Table.Header>
<Table.Row bg="bg.muted">
<Table.ColumnHeader borderColor="border.subtle" color="fg.muted" ps="4">
{t('users.user')}
</Table.ColumnHeader>
<Table.ColumnHeader borderColor="border.subtle" color="fg.muted">
{t('users.role')}
</Table.ColumnHeader>
<Table.ColumnHeader borderColor="border.subtle" color="fg.muted">
{t('users.lastSignIn')}
</Table.ColumnHeader>
<Table.ColumnHeader borderColor="border.subtle" color="fg.muted">
{t('common.active')}
</Table.ColumnHeader>
<Table.ColumnHeader borderColor="border.subtle" pe="4" />
</Table.Row>
</Table.Header>
<Table.Body>
{users.map((user) => (
<UserRow
onManageIntermediates={onManageIntermediates}
key={user.user_id}
isSelf={user.user_id === currentUserId}
setDeleteTarget={setDeleteTarget}
setFormTarget={setFormTarget}
setUserActive={setUserActive}
user={user}
/>
))}
</Table.Body>
</Table.Root>
)}
</Box>
</Stack>
<UserFormDialog target={formTarget} onClose={closeForm} onSaved={refreshAfterSave} />
<ConfirmDialog
body={t('users.deleteBody', { name: deleteTarget ? getUserLabel(deleteTarget) : t('users.thisUser') })}
confirmLabel={t('users.deleteUser')}
isOpen={deleteTarget !== null}
title={t('users.deleteTitle')}
onClose={closeDeleteConfirm}
onConfirm={confirmDelete}
/>
</>
);
};
const getUserLabel = (user: UserDTO): string => user.display_name?.trim() || user.email;
const formatLastSignIn = (lastLoginAt: string | null, neverLabel: string): string => {
if (!lastLoginAt) {
return neverLabel;
}
const date = new Date(lastLoginAt);
return Number.isNaN(date.getTime()) ? neverLabel : date.toLocaleString();
};
const UserRow = ({
isSelf,
onManageIntermediates,
setDeleteTarget,
setFormTarget,
setUserActive,
user,
}: {
isSelf: boolean;
onManageIntermediates?: (userId: string, label: string) => void;
setDeleteTarget: (user: UserDTO) => void;
setFormTarget: (target: UserFormTarget) => void;
setUserActive: (user: UserDTO, isActive: boolean) => void;
user: UserDTO;
}) => {
const { t } = useTranslation();
const handleDelete = useCallback(() => setDeleteTarget(user), [setDeleteTarget, user]);
const handleEdit = useCallback(() => setFormTarget({ mode: 'edit', user }), [setFormTarget, user]);
const handleIntermediates = useCallback(
() => onManageIntermediates?.(user.user_id, user.display_name || user.email),
[onManageIntermediates, user.display_name, user.email, user.user_id]
);
const handleSetActive = useCallback(
(event: { checked: boolean }) => void setUserActive(user, event.checked),
[setUserActive, user]
);
return (
<Table.Row bg="transparent" _hover={ROW_HOVER_STYLES}>
<Table.Cell borderColor="border.subtle" ps="4">
<HStack gap="2.5">
<Avatar.Root bg="accent.subtle" color="fg" size="sm">
<Avatar.Fallback fontSize="xs" name={getUserLabel(user)} />
</Avatar.Root>
<Stack gap="0">
<HStack gap="1.5">
<Text fontSize="md" fontWeight="600">
{getUserLabel(user)}
</Text>
{isSelf ? (
<Badge fontSize="xs" variant="surface">
{t('users.you')}
</Badge>
) : null}
</HStack>
<Text color="fg.muted" fontSize="xs">
{user.email}
</Text>
</Stack>
</HStack>
</Table.Cell>
<Table.Cell borderColor="border.subtle">
<Badge colorPalette={user.is_admin ? 'purple' : 'gray'} fontSize="xs" variant="surface">
{user.is_admin ? t('users.admin') : t('users.user')}
</Badge>
</Table.Cell>
<Table.Cell borderColor="border.subtle">
<Text color="fg.muted" fontSize="xs">
{formatLastSignIn(user.last_login_at, t('users.never'))}
</Text>
</Table.Cell>
<Table.Cell borderColor="border.subtle">
<Tooltip content={t('users.cannotDeactivateSelf')} disabled={!isSelf} showArrow>
<Switch.Root
aria-label={t('users.activeLabel', { name: getUserLabel(user) })}
checked={user.is_active}
disabled={isSelf}
onCheckedChange={handleSetActive}
>
<Switch.HiddenInput />
<Switch.Control _checked={SWITCH_CHECKED_STYLES}>
<Switch.Thumb />
</Switch.Control>
</Switch.Root>
</Tooltip>
</Table.Cell>
<Table.Cell borderColor="border.subtle" pe="4" textAlign="end">
<HStack gap="0.5" justify="flex-end">
{onManageIntermediates ? (
<Tooltip content={t('users.manageIntermediatesNamed', { name: getUserLabel(user) })} showArrow>
<IconButton
aria-label={t('users.manageIntermediatesNamed', { name: getUserLabel(user) })}
color="fg.muted"
size="sm"
variant="ghost"
onClick={handleIntermediates}
>
<BrushCleaningIcon />
</IconButton>
</Tooltip>
) : null}
<IconButton
aria-label={t('users.editUserNamed', { name: getUserLabel(user) })}
color="fg.muted"
size="sm"
variant="ghost"
onClick={handleEdit}
>
<PencilIcon />
</IconButton>
<Tooltip content={t('users.cannotDeleteSelf')} disabled={!isSelf} showArrow>
<IconButton
aria-label={t('users.deleteUserNamed', { name: getUserLabel(user) })}
color="fg.muted"
disabled={isSelf}
size="sm"
variant="ghost"
_hover={DELETE_BUTTON_HOVER_STYLES}
onClick={handleDelete}
>
<Trash2Icon />
</IconButton>
</Tooltip>
</HStack>
</Table.Cell>
</Table.Row>
);
};