|
| 1 | +// Copyright 2025 New Vector Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 4 | +// Please see LICENSE in the repository root for full details. |
| 5 | + |
| 6 | +import IconEdit from "@vector-im/compound-design-tokens/assets/web/icons/edit"; |
| 7 | +import { Button, Form, IconButton, Tooltip } from "@vector-im/compound-web"; |
| 8 | +import { |
| 9 | + type ComponentPropsWithoutRef, |
| 10 | + forwardRef, |
| 11 | + useRef, |
| 12 | + useState, |
| 13 | +} from "react"; |
| 14 | +import * as Dialog from "../Dialog"; |
| 15 | +import LoadingSpinner from "../LoadingSpinner"; |
| 16 | + |
| 17 | +import type { UseMutationResult } from "@tanstack/react-query"; |
| 18 | +import { useTranslation } from "react-i18next"; |
| 19 | + |
| 20 | +// This needs to be its own component because else props and refs aren't passed properly in the trigger |
| 21 | +const EditButton = forwardRef< |
| 22 | + HTMLButtonElement, |
| 23 | + { label: string } & ComponentPropsWithoutRef<"button"> |
| 24 | +>(({ label, ...props }, ref) => ( |
| 25 | + <Tooltip label={label}> |
| 26 | + <IconButton |
| 27 | + ref={ref} |
| 28 | + type="button" |
| 29 | + size="var(--cpd-space-6x)" |
| 30 | + style={{ marginInline: "var(--cpd-space-2x)" }} |
| 31 | + {...props} |
| 32 | + > |
| 33 | + <IconEdit /> |
| 34 | + </IconButton> |
| 35 | + </Tooltip> |
| 36 | +)); |
| 37 | + |
| 38 | +type Props = { |
| 39 | + mutation: UseMutationResult<unknown, unknown, string, unknown>; |
| 40 | + deviceName: string; |
| 41 | +}; |
| 42 | + |
| 43 | +const EditSessionName: React.FC<Props> = ({ mutation, deviceName }) => { |
| 44 | + const { t } = useTranslation(); |
| 45 | + const fieldRef = useRef<HTMLInputElement>(null); |
| 46 | + const [open, setOpen] = useState(false); |
| 47 | + |
| 48 | + const onSubmit = async ( |
| 49 | + event: React.FormEvent<HTMLFormElement>, |
| 50 | + ): Promise<void> => { |
| 51 | + event.preventDefault(); |
| 52 | + |
| 53 | + const form = event.currentTarget; |
| 54 | + const formData = new FormData(form); |
| 55 | + const displayName = formData.get("name") as string; |
| 56 | + await mutation.mutateAsync(displayName); |
| 57 | + setOpen(false); |
| 58 | + }; |
| 59 | + return ( |
| 60 | + <Dialog.Dialog |
| 61 | + trigger={<EditButton label={t("action.edit")} />} |
| 62 | + open={open} |
| 63 | + onOpenChange={(open) => { |
| 64 | + // Reset the form when the dialog is opened or closed |
| 65 | + fieldRef.current?.form?.reset(); |
| 66 | + setOpen(open); |
| 67 | + }} |
| 68 | + > |
| 69 | + <Dialog.Title>{t("frontend.session.set_device_name.title")}</Dialog.Title> |
| 70 | + |
| 71 | + <Form.Root onSubmit={onSubmit}> |
| 72 | + <Form.Field name="name"> |
| 73 | + <Form.Label>{t("frontend.session.set_device_name.label")}</Form.Label> |
| 74 | + |
| 75 | + <Form.TextControl |
| 76 | + type="text" |
| 77 | + required |
| 78 | + defaultValue={deviceName} |
| 79 | + ref={fieldRef} |
| 80 | + /> |
| 81 | + |
| 82 | + <Form.HelpMessage> |
| 83 | + {t("frontend.session.set_device_name.help")} |
| 84 | + </Form.HelpMessage> |
| 85 | + </Form.Field> |
| 86 | + |
| 87 | + <Form.Submit disabled={mutation.isPending}> |
| 88 | + {mutation.isPending && <LoadingSpinner inline />} |
| 89 | + {t("action.save")} |
| 90 | + </Form.Submit> |
| 91 | + </Form.Root> |
| 92 | + |
| 93 | + <Dialog.Close asChild> |
| 94 | + <Button kind="tertiary">{t("action.cancel")}</Button> |
| 95 | + </Dialog.Close> |
| 96 | + </Dialog.Dialog> |
| 97 | + ); |
| 98 | +}; |
| 99 | + |
| 100 | +export default EditSessionName; |
0 commit comments