suna/apps/mobile/hooks/useReactQueryFocus.ts

46 lines
1.1 KiB
TypeScript

import { useFocusEffect } from '@react-navigation/native';
import type { NotifyOnChangeProps } from '@tanstack/query-core';
import React from 'react';
// Custom hook for refreshing on screen focus
export function useRefreshOnFocus<T>(refetch: () => Promise<T>) {
const firstTimeRef = React.useRef(true);
useFocusEffect(
React.useCallback(() => {
if (firstTimeRef.current) {
firstTimeRef.current = false;
return;
}
refetch();
}, [refetch])
);
}
// Custom hook to disable re-renders on out of focus screens
export function useFocusNotifyOnChangeProps(notifyOnChangeProps?: NotifyOnChangeProps) {
const focusedRef = React.useRef(true);
useFocusEffect(
React.useCallback(() => {
focusedRef.current = true;
return () => {
focusedRef.current = false;
};
}, [])
);
return React.useCallback(() => {
if (!focusedRef.current) {
return [];
}
if (typeof notifyOnChangeProps === 'function') {
return notifyOnChangeProps();
}
return notifyOnChangeProps;
}, [notifyOnChangeProps, focusedRef.current]);
}