BlueWallet/hooks/useIsLargeScreen.ts

42 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-04-19 00:35:50 +02:00
import { useState, useEffect, useMemo } from 'react';
import { Dimensions } from 'react-native';
import { isTablet } from 'react-native-device-info';
2024-04-24 00:53:14 +02:00
import { isDesktop } from '../blue_modules/environment';
2024-04-19 00:31:06 +02:00
// Custom hook to determine if the screen is large
export const useIsLargeScreen = () => {
const [windowWidth, setWindowWidth] = useState(Dimensions.get('window').width);
const screenWidth = useMemo(() => Dimensions.get('screen').width, []);
useEffect(() => {
const updateScreenUsage = () => {
const newWindowWidth = Dimensions.get('window').width;
if (newWindowWidth !== windowWidth) {
console.log(`Window width changed: ${newWindowWidth}`);
setWindowWidth(newWindowWidth);
}
};
// Add event listener for dimension changes
const subscription = Dimensions.addEventListener('change', updateScreenUsage);
// Cleanup function to remove the event listener
return () => {
subscription.remove();
};
}, [windowWidth]);
// Determine if the window width is at least half of the screen width
const isLargeScreen = useMemo(() => {
2024-04-24 00:53:14 +02:00
const isRunningOnTabletOrDesktop = isTablet() || isDesktop;
2024-04-19 00:31:06 +02:00
const halfScreenWidth = windowWidth >= screenWidth / 2;
2024-04-24 00:53:14 +02:00
const condition = isRunningOnTabletOrDesktop && halfScreenWidth;
2024-04-19 00:31:06 +02:00
console.log(
2024-04-24 00:53:14 +02:00
`Window width: ${windowWidth}, Screen width: ${screenWidth}, Is tablet: ${isRunningOnTabletOrDesktop}, Is large screen: ${condition}`,
2024-04-19 00:31:06 +02:00
);
return condition;
}, [windowWidth, screenWidth]);
return isLargeScreen;
};