signal-desktop/ts/components/installScreen/InstallScreenChoosingDeviceNameStep.tsx

111 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-12-16 15:02:22 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactElement } from 'react';
import React, { useRef } from 'react';
import type { LocalizerType } from '../../types/Util';
2024-09-04 02:56:13 +00:00
import { MAX_DEVICE_NAME_LENGTH } from '../../types/InstallScreen';
2021-12-16 15:02:22 +00:00
import { normalizeDeviceName } from '../../util/normalizeDeviceName';
2024-03-15 14:20:33 +00:00
import { getEnvironment, Environment } from '../../environment';
2021-12-16 15:02:22 +00:00
import { Button, ButtonVariant } from '../Button';
import { TitlebarDragArea } from '../TitlebarDragArea';
import { InstallScreenSignalLogo } from './InstallScreenSignalLogo';
export type PropsType = {
2021-12-16 15:02:22 +00:00
deviceName: string;
i18n: LocalizerType;
onSubmit: () => void;
2024-03-15 14:20:33 +00:00
setBackupFile: (file: File) => void;
2021-12-16 15:02:22 +00:00
setDeviceName: (value: string) => void;
};
export function InstallScreenChoosingDeviceNameStep({
deviceName,
i18n,
onSubmit,
2024-03-15 14:20:33 +00:00
setBackupFile,
2021-12-16 15:02:22 +00:00
setDeviceName,
}: Readonly<PropsType>): ReactElement {
const hasFocusedRef = useRef<boolean>(false);
const focusRef = (el: null | HTMLElement) => {
if (el) {
el.focus();
hasFocusedRef.current = true;
}
};
const normalizedName = normalizeDeviceName(deviceName);
const canSubmit =
normalizedName.length > 0 &&
normalizedName.length <= MAX_DEVICE_NAME_LENGTH;
2024-03-15 14:20:33 +00:00
let maybeBackupInput: JSX.Element | undefined;
if (getEnvironment() !== Environment.Production) {
maybeBackupInput = (
<label className="module-InstallScreenChoosingDeviceNameStep__input">
{/* Since this is only for testing - we don't require translation */}
Backup file:
<input
type="file"
accept=".bin"
onChange={event => {
const file = event.target.files && event.target.files[0];
if (file) {
setBackupFile(file);
}
}}
/>
</label>
);
}
2021-12-16 15:02:22 +00:00
return (
<form
className="module-InstallScreenChoosingDeviceNameStep"
onSubmit={event => {
event.preventDefault();
onSubmit();
}}
>
<TitlebarDragArea />
<InstallScreenSignalLogo />
<div className="module-InstallScreenChoosingDeviceNameStep__contents">
<div className="module-InstallScreenChoosingDeviceNameStep__header">
2023-05-01 17:13:58 +00:00
<label htmlFor="deviceName">
<h1>{i18n('icu:chooseDeviceName')}</h1>
</label>
2023-03-30 00:03:25 +00:00
<h2>{i18n('icu:Install__choose-device-name__description')}</h2>
2021-12-16 15:02:22 +00:00
</div>
<div className="module-InstallScreenChoosingDeviceNameStep__inputs">
2024-03-15 14:20:33 +00:00
{maybeBackupInput}
2021-12-16 15:02:22 +00:00
<input
className="module-InstallScreenChoosingDeviceNameStep__input"
2023-05-01 17:13:58 +00:00
id="deviceName"
2021-12-16 15:02:22 +00:00
maxLength={MAX_DEVICE_NAME_LENGTH}
onChange={event => {
setDeviceName(event.target.value);
}}
2023-03-30 00:03:25 +00:00
placeholder={i18n('icu:Install__choose-device-name__placeholder')}
2021-12-16 15:02:22 +00:00
ref={focusRef}
spellCheck={false}
value={deviceName}
2023-04-20 17:03:43 +00:00
dir="auto"
2021-12-16 15:02:22 +00:00
/>
<Button
disabled={!canSubmit}
variant={ButtonVariant.Primary}
type="submit"
>
2023-03-30 00:03:25 +00:00
{i18n('icu:finishLinkingPhone')}
2021-12-16 15:02:22 +00:00
</Button>
</div>
</div>
</form>
);
}