/* Copyright 2022 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useEffect, useState } from "react"; import { _t } from "../../../../languageHandler"; import AccessibleButton, { ButtonEvent } from "../../elements/AccessibleButton"; import Field from "../../elements/Field"; import LearnMore from "../../elements/LearnMore"; import Spinner from "../../elements/Spinner"; import { Caption } from "../../typography/Caption"; import Heading from "../../typography/Heading"; import { ExtendedDevice } from "./types"; interface Props { device: ExtendedDevice; saveDeviceName: (deviceName: string) => Promise; } const DeviceNameEditor: React.FC void }> = ({ device, saveDeviceName, stopEditing }) => { const [deviceName, setDeviceName] = useState(device.display_name || ""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { setDeviceName(device.display_name || ""); }, [device.display_name]); const onInputChange = (event: React.ChangeEvent): void => setDeviceName(event.target.value); const onSubmit = async (event: ButtonEvent): Promise => { setIsLoading(true); setError(null); event.preventDefault(); try { await saveDeviceName(deviceName); stopEditing(); } catch (error) { setError(_t("Failed to set display name")); setIsLoading(false); } }; const headingId = `device-rename-${device.device_id}`; const descriptionId = `device-rename-description-${device.device_id}`; return (

{_t("Rename session")}

{_t("Please be aware that session names are also visible to people you communicate with.")}

{_t( "Other users in direct messages and rooms that you join are able to view a full list of your sessions.", )}

{_t( "This provides them with confidence that they are really speaking to you, but it also means they can see the session name you enter here.", )}

} /> {!!error && ( {error} )}
{_t("action|save")} {_t("action|cancel")} {isLoading && }
); }; export const DeviceDetailHeading: React.FC = ({ device, saveDeviceName }) => { const [isEditing, setIsEditing] = useState(false); return isEditing ? ( setIsEditing(false)} /> ) : (
{device.display_name || device.device_id} setIsEditing(true)} className="mx_DeviceDetailHeading_renameCta" data-testid="device-heading-rename-cta" > {_t("action|rename")}
); };