fix: remove unused hooks

This commit is contained in:
shadcn 2023-06-16 21:54:32 +04:00
parent cb941daca9
commit 93a7ad99ad
5 changed files with 11 additions and 135 deletions

View file

@ -1,43 +0,0 @@
import { useEffect, useRef } from 'react'
const scrollToEnd = () => {
window.scrollTo({
top: document.body.scrollHeight,
left: 0
})
}
export function useFollowScroll(shouldFollow: boolean) {
const shouldFollowRef = useRef(shouldFollow)
const scrollRef = useRef(false)
const triggeredBySelfRef = useRef(false)
useEffect(() => {
const handleScroll = () => {
if (triggeredBySelfRef.current) {
triggeredBySelfRef.current = false
return
}
scrollRef.current = true
}
window.addEventListener('scroll', handleScroll)
return () => window.removeEventListener('scroll', handleScroll)
}, [])
useEffect(() => {
if (!scrollRef.current && shouldFollowRef.current) {
setTimeout(() => {
triggeredBySelfRef.current = true
scrollToEnd()
})
}
})
useEffect(() => {
shouldFollowRef.current = shouldFollow
if (!shouldFollow) {
// Reset scrollRef
scrollRef.current = false
}
}, [shouldFollow])
}

View file

@ -1,26 +0,0 @@
import { useEffect, useState } from 'react'
const useLocalStorage = <T>(
key: string,
initialValue: T
): [T, (value: T) => void] => {
const [storedValue, setStoredValue] = useState(initialValue)
useEffect(() => {
// Retrieve from localStorage
const item = window.localStorage.getItem(key)
if (item) {
setStoredValue(JSON.parse(item))
}
}, [key])
const setValue = (value: T) => {
// Save state
setStoredValue(value)
// Save to localStorage
window.localStorage.setItem(key, JSON.stringify(value))
}
return [storedValue, setValue]
}
export default useLocalStorage

View file

@ -1,16 +0,0 @@
import { useCallback, useEffect, useState } from 'react'
export default function useScroll(threshold: number) {
const [scrolled, setScrolled] = useState(false)
const onScroll = useCallback(() => {
setScrolled(window.pageYOffset > threshold)
}, [threshold])
useEffect(() => {
window.addEventListener('scroll', onScroll)
return () => window.removeEventListener('scroll', onScroll)
}, [onScroll])
return scrolled
}

View file

@ -1,39 +0,0 @@
'use client'
import { useEffect, useState } from 'react'
export default function useWindowSize() {
const [windowSize, setWindowSize] = useState<{
width: number | undefined
height: number | undefined
}>({
width: undefined,
height: undefined
})
useEffect(() => {
// Handler to call on window resize
function handleResize() {
// Set window width/height to state
setWindowSize({
width: window.innerWidth,
height: window.innerHeight
})
}
// Add event listener
window.addEventListener('resize', handleResize)
// Call handler right away so state gets updated with initial window size
handleResize()
// Remove event listener on cleanup
return () => window.removeEventListener('resize', handleResize)
}, []) // Empty array ensures that effect is only run on mount
return {
windowSize,
isMobile: typeof windowSize?.width === 'number' && windowSize?.width < 768,
isDesktop: typeof windowSize?.width === 'number' && windowSize?.width >= 768
}
}