Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Simplify clipboard countdown timer to be more robust (#1967)

Leendert de Borstcad43c55429d
diff --git a/apps/mobile-app/components/form/FormInputCopyToClipboard.tsx b/apps/mobile-app/components/form/FormInputCopyToClipboard.tsx
index a8707d8f6..a77940ece 100644
--- a/apps/mobile-app/components/form/FormInputCopyToClipboard.tsx
+++ b/apps/mobile-app/components/form/FormInputCopyToClipboard.tsx
@@ -31,67 +31,41 @@ const FormInputCopyToClipboard: React.FC<FormInputCopyToClipboardProps> = ({
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
const colors = useColors();
const { t } = useTranslation();
- const { activeFieldId, setActiveField } = useClipboardCountdown();
+ const { activeField, startCountdown, clearCountdown } = useClipboardCountdown();
const animatedWidth = useRef(new Animated.Value(0)).current;
// Create a stable unique ID based on label and value
const fieldId = useRef(`${label}-${value}-${Math.random().toString(36).substring(2, 11)}`).current;
- const isCountingDown = activeFieldId === fieldId;
+ const isCountingDown = activeField?.fieldId === fieldId;
+ // Re-trigger animation each time this field is (re)copied.
+ const trigger = isCountingDown ? activeField.trigger : 0;
+ const timeoutSeconds = isCountingDown ? activeField.timeoutSeconds : 0;
useEffect(() => {
- return (): void => {
- // Cleanup on unmount
- animatedWidth.stopAnimation();
- };
- }, [animatedWidth]);
-
- useEffect(() => {
- let animationRef: Animated.CompositeAnimation | null = null;
- let isCancelled = false;
-
- /* Handle animation based on whether this field is active */
- if (isCountingDown) {
- // This field is now active - reset and start animation
- animatedWidth.stopAnimation();
- animatedWidth.setValue(100);
-
- // Get timeout and start animation
- LocalPreferencesService.getClipboardClearTimeout().then((timeoutSeconds) => {
- if (!isCancelled && timeoutSeconds > 0 && activeFieldId === fieldId) {
- animationRef = Animated.timing(animatedWidth, {
- toValue: 0,
- duration: timeoutSeconds * 1000,
- useNativeDriver: false,
- easing: Easing.linear,
- });
-
- animationRef.start((finished) => {
- if (!isCancelled && finished && activeFieldId === fieldId) {
- // Use requestAnimationFrame to defer state update
- requestAnimationFrame(() => {
- if (!isCancelled) {
- setActiveField(null);
- }
- });
- }
- });
- }
- });
- } else {
- // This field is not active - stop animation and reset
- animatedWidth.stopAnimation();
+ if (!isCountingDown || timeoutSeconds <= 0) {
animatedWidth.setValue(0);
+ return;
}
- // Cleanup function
- return () => {
- isCancelled = true;
- if (animationRef) {
- animationRef.stop();
+ animatedWidth.setValue(100);
+
+ const animation = Animated.timing(animatedWidth, {
+ toValue: 0,
+ duration: timeoutSeconds * 1000,
+ useNativeDriver: false,
+ easing: Easing.linear,
+ });
+
+ animation.start(({ finished }) => {
+ if (finished) {
+ clearCountdown();
}
- animatedWidth.stopAnimation();
+ });
+
+ return () => {
+ animation.stop();
};
- }, [isCountingDown, activeFieldId, fieldId, animatedWidth, setActiveField]);
+ }, [isCountingDown, timeoutSeconds, trigger, animatedWidth, clearCountdown]);
/**
* Copy the value to the clipboard.
@@ -107,12 +81,7 @@ const FormInputCopyToClipboard: React.FC<FormInputCopyToClipboardProps> = ({
// Handle animation state
if (timeoutSeconds > 0) {
- // Clear any existing active field and set this one as active
- // Use functional update to avoid closure issues
- setActiveField(() => {
- // If there was a previous field, its animation will be stopped by the effect
- return fieldId;
- });
+ startCountdown(fieldId, timeoutSeconds);
}
if (Platform.OS !== 'android') {
diff --git a/apps/mobile-app/context/ClipboardCountdownContext.tsx b/apps/mobile-app/context/ClipboardCountdownContext.tsx
index 40377b033..44f5f3f20 100644
--- a/apps/mobile-app/context/ClipboardCountdownContext.tsx
+++ b/apps/mobile-app/context/ClipboardCountdownContext.tsx
@@ -1,8 +1,16 @@
import React, { createContext, useContext, useState, useCallback, useMemo } from 'react';
+export type ActiveClipboardField = {
+ fieldId: string;
+ timeoutSeconds: number;
+ /** Monotonic counter so re-copying the same field still triggers a state change. */
+ trigger: number;
+} | null;
+
type ClipboardCountdownContextType = {
- activeFieldId: string | null;
- setActiveField: (fieldId: string | null | ((prev: string | null) => string | null)) => void;
+ activeField: ActiveClipboardField;
+ startCountdown: (fieldId: string, timeoutSeconds: number) => void;
+ clearCountdown: () => void;
}
const ClipboardCountdownContext = createContext<ClipboardCountdownContextType | undefined>(undefined);
@@ -11,17 +19,24 @@ const ClipboardCountdownContext = createContext<ClipboardCountdownContextType |
* Clipboard countdown context provider.
*/
export const ClipboardCountdownProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
- const [activeFieldId, setActiveFieldId] = useState<string | null>(null);
-
- const setActiveField = useCallback((fieldId: string | null | ((prev: string | null) => string | null)) => {
- if (typeof fieldId === 'function') {
- setActiveFieldId(fieldId);
- } else {
- setActiveFieldId(fieldId);
- }
+ const [activeField, setActiveField] = useState<ActiveClipboardField>(null);
+
+ const startCountdown = useCallback((fieldId: string, timeoutSeconds: number) => {
+ setActiveField((prev) => ({
+ fieldId,
+ timeoutSeconds,
+ trigger: (prev?.trigger ?? 0) + 1,
+ }));
}, []);
- const value = useMemo(() => ({ activeFieldId, setActiveField }), [activeFieldId, setActiveField]);
+ const clearCountdown = useCallback(() => {
+ setActiveField(null);
+ }, []);
+
+ const value = useMemo(
+ () => ({ activeField, startCountdown, clearCountdown }),
+ [activeField, startCountdown, clearCountdown],
+ );
return (
<ClipboardCountdownContext.Provider value={value}>