@bitcoin-os/bridge
Version: 
Reusable Bitcoin OS components for seamless app integration
56 lines (55 loc) • 2.07 kB
JavaScript
// Shared hooks for Bitcoin OS applications
import { useState, useEffect } from 'react';
export function useIsMobile(breakpoint = 768) {
    const [isMobile, setIsMobile] = useState(false);
    useEffect(() => {
        const checkIfMobile = () => {
            setIsMobile(window.innerWidth < breakpoint);
        };
        checkIfMobile();
        window.addEventListener('resize', checkIfMobile);
        return () => window.removeEventListener('resize', checkIfMobile);
    }, [breakpoint]);
    return isMobile;
}
export function useLocalStorage(key, initialValue) {
    const [storedValue, setStoredValue] = useState(() => {
        try {
            if (typeof window === 'undefined')
                return initialValue;
            const item = window.localStorage.getItem(key);
            return item ? JSON.parse(item) : initialValue;
        }
        catch (error) {
            console.warn(`Error reading localStorage key "${key}":`, error);
            return initialValue;
        }
    });
    const setValue = (value) => {
        try {
            const valueToStore = value instanceof Function ? value(storedValue) : value;
            setStoredValue(valueToStore);
            if (typeof window !== 'undefined') {
                window.localStorage.setItem(key, JSON.stringify(valueToStore));
            }
        }
        catch (error) {
            console.warn(`Error setting localStorage key "${key}":`, error);
        }
    };
    return [storedValue, setValue];
}
export function useDevSidebar() {
    const [showDevSidebar, setShowDevSidebar] = useLocalStorage('showDevSidebar', true);
    useEffect(() => {
        const handleKeyPress = (e) => {
            if (e.metaKey && e.key === 'd') {
                e.preventDefault();
                setShowDevSidebar(!showDevSidebar);
            }
        };
        window.addEventListener('keydown', handleKeyPress);
        return () => window.removeEventListener('keydown', handleKeyPress);
    }, [showDevSidebar, setShowDevSidebar]);
    return [showDevSidebar, setShowDevSidebar];
}