Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(app): add email code field, resend timer and title hook

Hampus8047ac0f9011
diff --git a/fluxer_app/src/features/auth/AuthMessageDescriptors.ts b/fluxer_app/src/features/auth/AuthMessageDescriptors.ts
index 97231a0fd..1a2a60399 100644
--- a/fluxer_app/src/features/auth/AuthMessageDescriptors.ts
+++ b/fluxer_app/src/features/auth/AuthMessageDescriptors.ts
@@ -6,6 +6,10 @@ export const FORGOT_YOUR_PASSWORD_DESCRIPTOR = msg({
message: 'Forgot your password?',
comment: 'Action label that starts the password reset flow from a sign-in surface.',
});
+export const NEW_CODE_SENT_DESCRIPTOR = msg({
+ message: 'We sent a new code.',
+ comment: 'Short status line after a fresh email code was sent.',
+});
export const INSTANCE_UNAVAILABLE_DESCRIPTOR = msg({
message: 'Instance unavailable',
comment: 'Status label for a saved account whose instance cannot currently be reached.',
@@ -38,3 +42,8 @@ export const SHARED_NETWORK_DESCRIPTOR = msg({
comment:
'Paragraph on the network blocked screen for people who share a network with others. Must read the same as the error message for this block.',
});
+
+export const USE_A_DIFFERENT_EMAIL_DESCRIPTOR = msg({
+ message: 'Use a different email',
+ comment: 'Quiet text button that goes back to the email field of the sign-in screen.',
+});
diff --git a/fluxer_app/src/features/auth/flow/AuthFormField.tsx b/fluxer_app/src/features/auth/flow/AuthFormField.tsx
index c89be9db1..df506f59c 100644
--- a/fluxer_app/src/features/auth/flow/AuthFormField.tsx
+++ b/fluxer_app/src/features/auth/flow/AuthFormField.tsx
@@ -10,11 +10,21 @@ interface FormFieldProps extends Omit<React.InputHTMLAttributes<HTMLInputElement
error?: string;
placeholder?: string;
onChange: (value: string) => void;
+ inputRef?: React.Ref<HTMLInputElement>;
}
-const FormField = observer(function FormField({name, label, value, error, onChange, ...props}: FormFieldProps) {
+const FormField = observer(function FormField({
+ name,
+ label,
+ value,
+ error,
+ onChange,
+ inputRef,
+ ...props
+}: FormFieldProps) {
return (
<Input
+ ref={inputRef}
name={name}
label={typeof label === 'string' ? label : undefined}
value={value}
diff --git a/fluxer_app/src/features/auth/flow/EmailCodeField.tsx b/fluxer_app/src/features/auth/flow/EmailCodeField.tsx
new file mode 100644
index 000000000..b4fe01dd6
--- /dev/null
+++ b/fluxer_app/src/features/auth/flow/EmailCodeField.tsx
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {useMergeRefs} from '@app/features/app/hooks/useMergeRefs';
+import FormField from '@app/features/auth/flow/AuthFormField';
+import {isCompleteEmailCode, normalizeEmailCode} from '@app/features/auth/flow/identifier_first/IdentifierFirstState';
+import {VERIFICATION_CODE_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';
+import {msg} from '@lingui/core/macro';
+import {useLingui} from '@lingui/react/macro';
+import {useEffect, useId, useRef} from 'react';
+
+const SIX_DIGIT_CODE_PLACEHOLDER_DESCRIPTOR = msg({
+ message: '6-digit code',
+ comment: 'Placeholder in the field where people type the code we emailed them.',
+});
+
+interface EmailCodeFieldProps {
+ readonly value: string;
+ readonly error?: string | null;
+ readonly disabled?: boolean;
+ readonly onChange: (value: string) => void;
+ readonly onComplete?: (code: string) => void;
+ readonly inputRef?: React.Ref<HTMLInputElement>;
+}
+
+export function EmailCodeField({value, error, disabled, onChange, onComplete, inputRef}: EmailCodeFieldProps) {
+ const {i18n} = useLingui();
+ const id = useId();
+ const fieldRef = useRef<HTMLInputElement | null>(null);
+ const mergedRef = useMergeRefs([fieldRef, inputRef]);
+ const wasDisabledRef = useRef(disabled === true);
+ useEffect(() => {
+ const wasDisabled = wasDisabledRef.current;
+ wasDisabledRef.current = disabled === true;
+ const focusLost = document.activeElement === null || document.activeElement === document.body;
+ if (wasDisabled && !disabled && (error || focusLost)) {
+ fieldRef.current?.focus();
+ fieldRef.current?.select();
+ }
+ }, [disabled, error]);
+ const handleChange = (raw: string) => {
+ const next = normalizeEmailCode(raw);
+ onChange(next);
+ if (isCompleteEmailCode(next) && next !== value) {
+ onComplete?.(next);
+ }
+ };
+ return (
+ <FormField
+ inputRef={mergedRef}
+ id={id}
+ name="code"
+ type="text"
+ inputMode="numeric"
+ autoComplete="one-time-code"
+ autoCapitalize="none"
+ autoCorrect="off"
+ enterKeyHint="done"
+ spellCheck={false}
+ autoFocus
+ data-step-focus="true"
+ required
+ disabled={disabled}
+ label={i18n._(VERIFICATION_CODE_DESCRIPTOR)}
+ placeholder={i18n._(SIX_DIGIT_CODE_PLACEHOLDER_DESCRIPTOR)}
+ value={value}
+ onChange={handleChange}
+ error={error ?? undefined}
+ data-flx="auth.flow.email-code-field.form-field"
+ />
+ );
+}
diff --git a/fluxer_app/src/features/auth/flow/QuietButton.tsx b/fluxer_app/src/features/auth/flow/QuietButton.tsx
new file mode 100644
index 000000000..b4933ad0a
--- /dev/null
+++ b/fluxer_app/src/features/auth/flow/QuietButton.tsx
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import styles from '@app/features/auth/flow/identifier_first/AuthIdentifierFirstStep.module.css';
+import FocusRing from '@app/features/ui/focus_ring/FocusRing';
+import type {ReactNode} from 'react';
+
+export function QuietButton({
+ children,
+ disabled,
+ onClick,
+ 'data-flx': dataFlx,
+}: {
+ children: ReactNode;
+ disabled?: boolean;
+ onClick: () => void;
+ 'data-flx': string;
+}) {
+ return (
+ <FocusRing offset={-2} data-flx={`${dataFlx}.focus-ring`}>
+ <button type="button" className={styles.textButton} disabled={disabled} onClick={onClick} data-flx={dataFlx}>
+ {children}
+ </button>
+ </FocusRing>
+ );
+}
diff --git a/fluxer_app/src/features/auth/flow/useReportStepTitle.ts b/fluxer_app/src/features/auth/flow/useReportStepTitle.ts
new file mode 100644
index 000000000..dfef8e284
--- /dev/null
+++ b/fluxer_app/src/features/auth/flow/useReportStepTitle.ts
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {useLayoutEffect} from 'react';
+
+export type StepTitleChangeHandler = (title: string | null) => void;
+
+export function useReportStepTitle(onStepTitleChange: StepTitleChangeHandler | null, title: string | null): void {
+ useLayoutEffect(() => {
+ onStepTitleChange?.(title);
+ }, [onStepTitleChange, title]);
+}
diff --git a/fluxer_app/src/features/auth/hooks/useResendCooldown.ts b/fluxer_app/src/features/auth/hooks/useResendCooldown.ts
new file mode 100644
index 000000000..9f8199f37
--- /dev/null
+++ b/fluxer_app/src/features/auth/hooks/useResendCooldown.ts
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {secondsUntil} from '@app/features/auth/flow/identifier_first/IdentifierFirstState';
+import {useEffect, useState} from 'react';
+
+export function useResendCooldown(resendAvailableAt: number | null): number {
+ const [now, setNow] = useState(() => Date.now());
+ useEffect(() => {
+ setNow(Date.now());
+ if (resendAvailableAt === null || resendAvailableAt <= Date.now()) return;
+ const id = window.setInterval(() => {
+ const current = Date.now();
+ setNow(current);
+ if (current >= resendAvailableAt) window.clearInterval(id);
+ }, 1000);
+ return () => window.clearInterval(id);
+ }, [resendAvailableAt]);
+ return resendAvailableAt === null ? 0 : secondsUntil(resendAvailableAt, now);
+}