diff options
Diffstat (limited to 'src/hooks')
| -rw-r--r-- | src/hooks/useMemory.ts | 25 | ||||
| -rw-r--r-- | src/hooks/useRemainingViewPortHeight.ts | 32 | ||||
| -rw-r--r-- | src/hooks/useTextInput.ts | 6 | ||||
| -rw-r--r-- | src/hooks/useTraffic.ts | 25 | ||||
| -rw-r--r-- | src/hooks/useVersion.ts | 13 |
5 files changed, 16 insertions, 85 deletions
diff --git a/src/hooks/useMemory.ts b/src/hooks/useMemory.ts deleted file mode 100644 index b6e3b4c..0000000 --- a/src/hooks/useMemory.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { useEffect, useState } from 'react'; - -import { fetchData } from '~/api/memory'; -import { ClashAPIConfig } from '~/types'; - -export default function useMemory(apiConfig: ClashAPIConfig) { - const memory = fetchData(apiConfig); - const [data, setData] = useState({ - inuse: [...memory.inuse], - oslimit: [...memory.oslimit], - labels: [...memory.labels], - }); - - useEffect(() => { - return memory.subscribe(() => { - setData({ - inuse: [...memory.inuse], - oslimit: [...memory.oslimit], - labels: [...memory.labels], - }); - }); - }, [memory]); - - return data; -} diff --git a/src/hooks/useRemainingViewPortHeight.ts b/src/hooks/useRemainingViewPortHeight.ts deleted file mode 100644 index 2c920c2..0000000 --- a/src/hooks/useRemainingViewPortHeight.ts +++ /dev/null @@ -1,32 +0,0 @@ -import * as React from 'react'; - -const { useState, useRef, useCallback, useLayoutEffect } = React; - -/** - * cosnt [ref, remainingHeight] = useRemainingViewPortHeight(); - * - * return a reference, and the remaining height of the referenced dom node - * to the bottom of the view port - * - */ -export default function useRemainingViewPortHeight<ElementType extends HTMLDivElement>(): [ - React.MutableRefObject<ElementType>, - number -] { - const ref = useRef<ElementType>(null); - const [containerHeight, setContainerHeight] = useState(200); - const updateContainerHeight = useCallback(() => { - const { top } = ref.current.getBoundingClientRect(); - setContainerHeight(window.innerHeight - top); - }, []); - - useLayoutEffect(() => { - updateContainerHeight(); - window.addEventListener('resize', updateContainerHeight); - return () => { - window.removeEventListener('resize', updateContainerHeight); - }; - }, [updateContainerHeight]); - - return [ref, containerHeight]; -} diff --git a/src/hooks/useTextInput.ts b/src/hooks/useTextInput.ts index 8098b42..35256cd 100644 --- a/src/hooks/useTextInput.ts +++ b/src/hooks/useTextInput.ts @@ -4,8 +4,8 @@ import * as React from 'react'; const { useCallback, useState, useMemo } = React; -export function useTextInut( - x: PrimitiveAtom<string> +export function useTextInput( + x: PrimitiveAtom<string>, ): [(e: React.ChangeEvent<HTMLInputElement>) => void, string] { const [, setTextGlobal] = useAtom(x); const [text, setText] = useState(''); @@ -15,7 +15,7 @@ export function useTextInut( setText(e.target.value); setTextDebounced(e.target.value); }, - [setTextDebounced] + [setTextDebounced], ); return [onChange, text]; } diff --git a/src/hooks/useTraffic.ts b/src/hooks/useTraffic.ts deleted file mode 100644 index 66cf4bf..0000000 --- a/src/hooks/useTraffic.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { useEffect, useState } from 'react'; - -import { fetchData } from '~/api/traffic'; -import { ClashAPIConfig } from '~/types'; - -export default function useTraffic(apiConfig: ClashAPIConfig) { - const traffic = fetchData(apiConfig); - const [data, setData] = useState({ - up: [...traffic.up], - down: [...traffic.down], - labels: [...traffic.labels], - }); - - useEffect(() => { - return traffic.subscribe(() => { - setData({ - up: [...traffic.up], - down: [...traffic.down], - labels: [...traffic.labels], - }); - }); - }, [traffic]); - - return data; -} diff --git a/src/hooks/useVersion.ts b/src/hooks/useVersion.ts new file mode 100644 index 0000000..d6a4f19 --- /dev/null +++ b/src/hooks/useVersion.ts @@ -0,0 +1,13 @@ +import { useSuspenseQuery } from '@tanstack/react-query'; + +import { fetchVersion } from '~/api/version'; +import { ClashAPIConfig } from '~/types'; + +/** 内核版本信息。侧边栏和代理组都要用它区分 meta / premium,共用同一份缓存 */ +export function useVersion(apiConfig: ClashAPIConfig) { + const { data } = useSuspenseQuery({ + queryKey: ['/version', apiConfig], + queryFn: () => fetchVersion('/version', apiConfig), + }); + return data; +} |
