Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add navigation debounce component to prevent duplicate navigation when link is tapped multiple times (#1797)
Leendert de Borstc7774b3e2c83
diff --git a/apps/mobile-app/app/(tabs)/items/[id].tsx b/apps/mobile-app/app/(tabs)/items/[id].tsxindex e6a61099c..3e8df77a4 100644--- a/apps/mobile-app/app/(tabs)/items/[id].tsx+++ b/apps/mobile-app/app/(tabs)/items/[id].tsx@@ -12,6 +12,7 @@ import { FieldTypes, getFieldValue, FieldKey } from '@/utils/dist/core/models/vaimport emitter from '@/utils/EventEmitter';import { useColors } from '@/hooks/useColorScheme';+import { useNavigationDebounce } from '@/hooks/useNavigationDebounce';import { AliasDetails } from '@/components/items/details/AliasDetails';import { AttachmentSection } from '@/components/items/details/AttachmentSection';@@ -45,14 +46,15 @@ export default function ItemDetailsScreen() : React.ReactNode {const navigation = useNavigation();const colors = useColors();const router = useRouter();+ const navigate = useNavigationDebounce();const { t } = useTranslation();/*** Handle the edit button press.*/const handleEdit = useCallback(() : void => {- router.push(`/(tabs)/items/add-edit?id=${id}`);- }, [id, router]);+ navigate(() => router.push(`/(tabs)/items/add-edit?id=${id}`));+ }, [id, router, navigate]);// Set header buttonsuseEffect(() => {diff --git a/apps/mobile-app/app/(tabs)/items/folder/[id].tsx b/apps/mobile-app/app/(tabs)/items/folder/[id].tsxindex b32b048d4..5fb3657fd 100644--- a/apps/mobile-app/app/(tabs)/items/folder/[id].tsx+++ b/apps/mobile-app/app/(tabs)/items/folder/[id].tsx@@ -18,6 +18,7 @@ import { VaultAuthenticationError } from '@/utils/types/errors/VaultAuthenticatiimport { useColors } from '@/hooks/useColorScheme';import { useItemSort, useSortedItems } from '@/hooks/useItemSort';import { useMinDurationLoading } from '@/hooks/useMinDurationLoading';+import { useNavigationDebounce } from '@/hooks/useNavigationDebounce';import { useVaultMutate } from '@/hooks/useVaultMutate';import { useVaultSync } from '@/hooks/useVaultSync';@@ -76,6 +77,7 @@ export default function FolderViewScreen(): React.ReactNode {const { t } = useTranslation();const navigation = useNavigation();const router = useRouter();+ const navigate = useNavigationDebounce();const insets = useSafeAreaInsets();const flatListRef = useRef<FlatList<Item | null>>(null);@@ -400,9 +402,11 @@ export default function FolderViewScreen(): React.ReactNode {* Handle FAB press - navigate to add item screen with folder pre-selected.*/const handleAddItem = useCallback(() => {- router.push(`/(tabs)/items/add-edit?folderId=${folderId}` as '/(tabs)/items/add-edit');- Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);- }, [folderId, router]);+ navigate(() => {+ router.push(`/(tabs)/items/add-edit?folderId=${folderId}` as '/(tabs)/items/add-edit');+ Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);+ });+ }, [folderId, router, navigate]);// Header styles (stable, not dependent on colors) - prefixed with _ as styles are inlined in useEffectconst _headerStyles = StyleSheet.create({diff --git a/apps/mobile-app/app/(tabs)/items/index.tsx b/apps/mobile-app/app/(tabs)/items/index.tsxindex 7feeeebb3..dc724fe5f 100644--- a/apps/mobile-app/app/(tabs)/items/index.tsx+++ b/apps/mobile-app/app/(tabs)/items/index.tsx@@ -17,6 +17,7 @@ import { VaultAuthenticationError } from '@/utils/types/errors/VaultAuthenticatiimport { useColors } from '@/hooks/useColorScheme';import { useMinDurationLoading } from '@/hooks/useMinDurationLoading';+import { useNavigationDebounce } from '@/hooks/useNavigationDebounce';import { useVaultMutate } from '@/hooks/useVaultMutate';import { useVaultSync } from '@/hooks/useVaultSync';@@ -75,6 +76,7 @@ export default function ItemsScreen(): React.ReactNode {const { syncVault } = useVaultSync();const { t } = useTranslation();const colors = useColors();+ const navigate = useNavigationDebounce();const scrollY = useRef(new Animated.Value(0)).current;const navigation = useNavigation();const insets = useSafeAreaInsets();@@ -517,8 +519,8 @@ export default function ItemsScreen(): React.ReactNode {* Navigate to a folder.*/const handleFolderClick = useCallback((folderId: string) => {- router.push(`/(tabs)/items/folder/${folderId}`);- }, [router]);+ navigate(() => router.push(`/(tabs)/items/folder/${folderId}`));+ }, [router, navigate]);/*** Create a new folder.@@ -535,19 +537,21 @@ export default function ItemsScreen(): React.ReactNode {* If there's a search query, pass it as itemUrl (if URL) or itemName (if not).*/const handleAddItem = useCallback(() => {- const trimmedQuery = searchQuery.trim();- if (trimmedQuery) {- const isUrl = /^https?:\/\//i.test(trimmedQuery);- if (isUrl) {- router.push(`/(tabs)/items/add-edit?itemUrl=${encodeURIComponent(trimmedQuery)}`);+ navigate(() => {+ const trimmedQuery = searchQuery.trim();+ if (trimmedQuery) {+ const isUrl = /^https?:\/\//i.test(trimmedQuery);+ if (isUrl) {+ router.push(`/(tabs)/items/add-edit?itemUrl=${encodeURIComponent(trimmedQuery)}`);+ } else {+ router.push(`/(tabs)/items/add-edit?itemName=${encodeURIComponent(trimmedQuery)}`);+ }} else {- router.push(`/(tabs)/items/add-edit?itemName=${encodeURIComponent(trimmedQuery)}`);+ router.push('/(tabs)/items/add-edit');}- } else {- router.push('/(tabs)/items/add-edit');- }- Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);- }, [router, searchQuery]);+ Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);+ });+ }, [router, searchQuery, navigate]);const styles = StyleSheet.create({container: {diff --git a/apps/mobile-app/app/(tabs)/settings/index.tsx b/apps/mobile-app/app/(tabs)/settings/index.tsxindex 7ae5765ef..56e4023c9 100644--- a/apps/mobile-app/app/(tabs)/settings/index.tsx+++ b/apps/mobile-app/app/(tabs)/settings/index.tsx@@ -10,6 +10,7 @@ import { AppInfo } from '@/utils/AppInfo';import { useColors } from '@/hooks/useColorScheme';import { useLogout } from '@/hooks/useLogout';import { useMinDurationLoading } from '@/hooks/useMinDurationLoading';+import { useNavigationDebounce } from '@/hooks/useNavigationDebounce';import { useTranslation } from '@/hooks/useTranslation';import { ThemedContainer } from '@/components/themed/ThemedContainer';@@ -34,6 +35,7 @@ export default function SettingsScreen() : React.ReactNode {const { getAutoLockTimeout } = useApp();const { logoutUserInitiated } = useLogout();const { loadApiUrl, getDisplayUrl } = useApiUrl();+ const navigate = useNavigationDebounce();const scrollY = useRef(new Animated.Value(0)).current;const scrollViewRef = useRef<ScrollView>(null);const [autoLockDisplay, setAutoLockDisplay] = useState<string>('');@@ -115,49 +117,49 @@ export default function SettingsScreen() : React.ReactNode {* Handle the vault unlock press.*/const handleVaultUnlockPress = () : void => {- router.push('/(tabs)/settings/vault-unlock');+ navigate(() => router.push('/(tabs)/settings/vault-unlock'));};/*** Handle the auto-lock press.*/const handleAutoLockPress = () : void => {- router.push('/(tabs)/settings/auto-lock');+ navigate(() => router.push('/(tabs)/settings/auto-lock'));};/*** Handle the iOS autofill press.*/const handleIosAutofillPress = () : void => {- router.push('/(tabs)/settings/ios-autofill');+ navigate(() => router.push('/(tabs)/settings/ios-autofill'));};/*** Handle the Android autofill press.*/const handleAndroidAutofillPress = () : void => {- router.push('/(tabs)/settings/android-autofill');+ navigate(() => router.push('/(tabs)/settings/android-autofill'));};/*** Handle the identity generator settings press.*/const handleIdentityGeneratorPress = () : void => {- router.push('/(tabs)/settings/identity-generator');+ navigate(() => router.push('/(tabs)/settings/identity-generator'));};/*** Handle the password generator settings press.*/const handlePasswordGeneratorPress = () : void => {- router.push('/(tabs)/settings/password-generator');+ navigate(() => router.push('/(tabs)/settings/password-generator'));};/*** Handle the clipboard clear settings press.*/const handleClipboardClearPress = () : void => {- router.push('/(tabs)/settings/clipboard-clear');+ navigate(() => router.push('/(tabs)/settings/clipboard-clear'));};/**diff --git a/apps/mobile-app/components/EmailCard.tsx b/apps/mobile-app/components/EmailCard.tsxindex 578958f83..2b42bc744 100644--- a/apps/mobile-app/components/EmailCard.tsx+++ b/apps/mobile-app/components/EmailCard.tsx@@ -6,6 +6,7 @@ import type { Item } from '@/utils/dist/core/models/vault';import type { MailboxEmail } from '@/utils/dist/core/models/webapi';import { useColors } from '@/hooks/useColorScheme';+import { useNavigationDebounce } from '@/hooks/useNavigationDebounce';import { useTranslation } from '@/hooks/useTranslation';import { ThemedText } from '@/components/themed/ThemedText';@@ -24,6 +25,7 @@ type EmailCardProps = {export function EmailCard({ email }: EmailCardProps) : React.ReactNode {const colors = useColors();const { t } = useTranslation();+ const navigate = useNavigationDebounce();const dbContext = useDb();const [associatedItem, setAssociatedItem] = useState<Item | null>(null);@@ -136,7 +138,7 @@ export function EmailCard({ email }: EmailCardProps) : React.ReactNode {return (<RobustPressablestyle={styles.emailCard}- onPress={() => router.push(`/(tabs)/emails/${email.id}`)}+ onPress={() => navigate(() => router.push(`/(tabs)/emails/${email.id}`))}><View style={styles.emailHeader}><ThemedText style={styles.emailSubject} numberOfLines={1}>diff --git a/apps/mobile-app/components/items/ItemCard.tsx b/apps/mobile-app/components/items/ItemCard.tsxindex ea877b95b..af962a52a 100644--- a/apps/mobile-app/components/items/ItemCard.tsx+++ b/apps/mobile-app/components/items/ItemCard.tsx@@ -12,6 +12,7 @@ import { ItemIcon } from '@/components/items/ItemIcon';import { useDialog } from '@/context/DialogContext';import { LocalPreferencesService } from '@/services/LocalPreferencesService';import { useColors } from '@/hooks/useColorScheme';+import { useNavigationDebounce } from '@/hooks/useNavigationDebounce';import { copyToClipboardWithExpiration } from '@/utils/ClipboardUtility';import type { Item } from '@/utils/dist/core/models/vault';import { getFieldValue, FieldKey } from '@/utils/dist/core/models/vault';@@ -29,6 +30,7 @@ export function ItemCard({ item, onItemDelete, showFolderPath = false }: ItemCarconst colors = useColors();const { t } = useTranslation();const { showConfirm } = useDialog();+ const navigate = useNavigationDebounce();/*** Get the display text for an item, showing username by default,@@ -86,10 +88,12 @@ export function ItemCard({ item, onItemDelete, showFolderPath = false }: ItemCarswitch (name) {case t('items.contextMenu.edit'):- Keyboard.dismiss();- router.push({- pathname: '/(tabs)/items/add-edit',- params: { id: item.Id }+ navigate(() => {+ Keyboard.dismiss();+ router.push({+ pathname: '/(tabs)/items/add-edit',+ params: { id: item.Id }+ });});break;case t('items.contextMenu.delete'):@@ -276,8 +280,10 @@ export function ItemCard({ item, onItemDelete, showFolderPath = false }: ItemCar<TouchableOpacitystyle={styles.credentialCard}onPress={() => {- Keyboard.dismiss();- router.push(`/(tabs)/items/${item.Id}`);+ navigate(() => {+ Keyboard.dismiss();+ router.push(`/(tabs)/items/${item.Id}`);+ });}}onLongPress={() => {// Ignore long press to prevent context menu long press from triggering the item card press.diff --git a/apps/mobile-app/hooks/useNavigationDebounce.ts b/apps/mobile-app/hooks/useNavigationDebounce.tsnew file mode 100644index 000000000..d92892044--- /dev/null+++ b/apps/mobile-app/hooks/useNavigationDebounce.ts@@ -0,0 +1,36 @@+import { useRef, useCallback } from 'react';++/**+ * Hook to prevent rapid successive navigation calls.+ * This prevents the common issue where users accidentally tap navigation items+ * multiple times in quick succession, causing duplicate navigation.+ *+ * @param delay - Debounce delay in milliseconds (default: 300ms)+ * @returns A debounced navigation function that accepts a callback+ */+export const useNavigationDebounce = (delay: number = 300) => {+ const isNavigating = useRef(false);++ const navigate = useCallback(+ (navigationFn: () => void) => {+ // If already navigating, ignore this call+ if (isNavigating.current) {+ return;+ }++ // Set flag to prevent further calls+ isNavigating.current = true;++ // Execute the navigation function+ navigationFn();++ // Reset the flag after the delay+ setTimeout(() => {+ isNavigating.current = false;+ }, delay);+ },+ [delay]+ );++ return navigate;+};