Files
Tom Moor bcf282a2f6 feat: Auto-save settings and tidy settings layout (#14569)
* feat: Auto-save General and Profile settings, add settings sections

- Save General and Profile fields on change and remove the Save button
- Add SettingsSection with consistent spacing and a tinted Danger block
- Right-align setting controls and center switches in their row
- Let descriptions use the full row width beside switches

* fix: Avoid settings autosave overwrites and IME Enter saves

- Send only the changed team preference so concurrent saves do not revert each other
- Save separate editing as a single preference instead of the whole team
- Treat a cleared color input as empty so the default is restored
- Ignore Enter during IME composition

* feat: Add change and remove icon buttons below image inputs

- Replace the Remove button with edit and trash icon buttons with tooltips
- Show an upload icon on the image hover overlay
- Re-render ImageInput when the image changes so the remove action appears immediately

* feat: Use footer buttons in the change profile picture dialog

- Add Replace and Delete buttons beside Done in the admin avatar dialog
- Add showActions and an open handle to ImageInput so dialogs can supply their own controls

* fix: Style the danger section with an outer shadow instead of tinted text
2026-11-13 04:01:55 +00:00

297 lines
8.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { observer } from "mobx-react";
import * as React from "react";
import { Trans, useTranslation } from "react-i18next";
import { toast } from "sonner";
import { errToString } from "@shared/utils/error";
import { UserRole } from "@shared/types";
import { UserValidation } from "@shared/validations";
import type User from "~/models/User";
import Button from "~/components/Button";
import ConfirmationDialog from "~/components/ConfirmationDialog";
import Flex from "~/components/Flex";
import Input from "~/components/Input";
import useCurrentUser from "~/hooks/useCurrentUser";
import useStores from "~/hooks/useStores";
import ImageInput from "~/scenes/Settings/components/ImageInput";
import type { ImageUploadHandle } from "~/scenes/Settings/components/ImageUpload";
import { performBatch } from "~/actions/definitions/common";
import { client } from "~/utils/ApiClient";
import Text from "./Text";
type Props = {
user: User;
onSubmit: () => void;
};
type BulkProps = {
users: User[];
onSubmit: () => void;
};
export function UserChangeRoleDialog({
users,
role,
onSubmit,
}: BulkProps & {
role: UserRole;
}) {
const { t } = useTranslation();
const { users: usersStore } = useStores();
const handleSubmit = async () => {
await performBatch(users, (user) => usersStore.updateRole(user, role));
onSubmit();
};
let accessNote;
switch (role) {
case UserRole.Admin:
accessNote = t("Admins can manage the workspace and access billing.");
break;
case UserRole.Member:
accessNote = t("Editors can create, edit, and comment on documents.");
break;
case UserRole.Viewer:
accessNote = t("Viewers can only view and comment on documents.");
break;
}
return (
<ConfirmationDialog onSubmit={handleSubmit} savingText={`${t("Saving")}…`}>
{users.length === 1
? t(
"Are you sure you want to change the role of {{ userName }} to {{ role }}?",
{
role,
userName: users[0].name,
}
)
: t(
"Are you sure you want to change the role of {{ count }} user to {{ role }}?",
{
role,
count: users.length,
}
)}{" "}
{accessNote}
</ConfirmationDialog>
);
}
export function UserDeleteDialog({ users, onSubmit }: BulkProps) {
const { t } = useTranslation();
const handleSubmit = async () => {
await performBatch(users, (user) => user.delete());
onSubmit();
};
return (
<ConfirmationDialog
onSubmit={handleSubmit}
submitText={t("I understand, delete")}
savingText={`${t("Deleting")}…`}
danger
>
{users.length === 1
? t(
"Are you sure you want to permanently delete {{ userName }}? This operation is unrecoverable. Any API keys, webhooks, and integrations they created will stop working — consider suspending the user instead.",
{
userName: users[0].name,
}
)
: t(
"Are you sure you want to permanently delete {{ count }} user? This operation is unrecoverable. Any API keys, webhooks, and integrations they created will stop working — consider suspending them instead.",
{
count: users.length,
}
)}
</ConfirmationDialog>
);
}
export function UserSuspendDialog({ users, onSubmit }: BulkProps) {
const { t } = useTranslation();
const { users: usersStore } = useStores();
const handleSubmit = async () => {
await performBatch(users, (user) => usersStore.suspend(user));
onSubmit();
};
return (
<ConfirmationDialog
onSubmit={handleSubmit}
savingText={`${t("Saving")}…`}
danger
>
{users.length === 1
? t(
"Are you sure you want to suspend {{ userName }}? Suspended users will be prevented from logging in.",
{
userName: users[0].name,
}
)
: t(
"Are you sure you want to suspend {{ count }} user? Suspended users will be prevented from logging in.",
{
count: users.length,
}
)}
</ConfirmationDialog>
);
}
export function UserChangeNameDialog({ user, onSubmit }: Props) {
const { t } = useTranslation();
const [name, setName] = React.useState<string>(user.name);
const handleSubmit = async () => {
await user.save({ name });
onSubmit();
};
const handleChange = (ev: React.ChangeEvent<HTMLInputElement>) => {
setName(ev.target.value);
};
return (
<ConfirmationDialog
onSubmit={handleSubmit}
submitText={t("Save")}
savingText={`${t("Saving")}…`}
disabled={!name}
>
<Input
type="text"
name="name"
label={t("New name")}
onChange={handleChange}
error={!name ? t("Name can’t be empty") : undefined}
value={name}
maxLength={UserValidation.maxNameLength}
showCharacterCount
autoSelect
required
flex
/>
</ConfirmationDialog>
);
}
export const UserChangeAvatarDialog = observer(function UserChangeAvatarDialog({
user,
onSubmit,
}: Props) {
const { t } = useTranslation();
const imageRef = React.useRef<ImageUploadHandle>(null);
const handleReplace = () => {
imageRef.current?.open();
};
const handleAvatarChange = async (avatarUrl: string | null) => {
try {
await user.save({ avatarUrl });
toast.success(t("Profile picture updated"));
} catch (err) {
toast.error(errToString(err));
}
};
const handleAvatarError = (error: string | null | undefined) => {
toast.error(error || t("Unable to upload new profile picture"));
};
return (
<Flex column gap={16}>
<Flex justify="center">
<ImageInput
alt={t("Profile picture")}
onSuccess={handleAvatarChange}
onError={handleAvatarError}
model={user}
showActions={false}
ref={imageRef}
/>
</Flex>
<Flex justify="space-between" gap={8}>
<Flex gap={8}>
<Button onClick={handleReplace} neutral>
{user.avatarUrl ? t("Replace") : t("Upload")}…
</Button>
{user.avatarUrl && (
<Button onClick={() => handleAvatarChange(null)} neutral>
{t("Delete")}
</Button>
)}
</Flex>
<Button onClick={onSubmit}>{t("Done")}</Button>
</Flex>
</Flex>
);
});
export function UserChangeEmailDialog({ user, onSubmit }: Props) {
const { t } = useTranslation();
const actor = useCurrentUser();
const [email, setEmail] = React.useState<string>(user.email);
const [error, setError] = React.useState<string | undefined>();
const handleSubmit = async () => {
try {
await client.post(`/users.updateEmail`, { id: user.id, email });
onSubmit();
toast.info(
actor.id === user.id
? t("Check your email to verify the new address.")
: t("The email will be changed once verified.")
);
return true;
} catch (err) {
setError(errToString(err));
return false;
}
};
const handleChange = (ev: React.ChangeEvent<HTMLInputElement>) => {
setEmail(ev.target.value);
};
return (
<ConfirmationDialog
onSubmit={handleSubmit}
submitText={t("Save")}
savingText={`${t("Saving")}…`}
disabled={!email || email === user.email}
>
<Text as="p">
{actor.id === user.id ? (
<Trans>
You will receive an email to verify your new address. It must be
unique in the workspace.
</Trans>
) : (
<Trans>
A confirmation email will be sent to the new address before it is
changed.
</Trans>
)}
</Text>
<Input
type="email"
name="email"
label={t("New email")}
onChange={handleChange}
error={!email ? t("Email can’t be empty") : error}
value={email}
maxLength={UserValidation.maxEmailLength}
showCharacterCount
autoSelect
required
flex
/>
</ConfirmationDialog>
);
}