-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <[email protected]>
- Loading branch information
Showing
14 changed files
with
478 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './useBizQuery' | ||
export * from './useDark' | ||
export * from './usePrevious' | ||
export * from './useRefValue' | ||
export * from './useTitle' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { useInfiniteQuery, useQuery } from '@tanstack/react-query' | ||
import type { | ||
InfiniteData, | ||
QueryKey, | ||
UseInfiniteQueryOptions, | ||
UseInfiniteQueryResult, | ||
UseQueryOptions, | ||
UseQueryResult, | ||
} from '@tanstack/react-query' | ||
import type { DefinedQuery } from '~/utils/defineQuery' | ||
import type { FetchError } from 'ofetch' | ||
|
||
// TODO split normal define query and infinite define query for better type checking | ||
export type SafeReturnType<T> = T extends (...args: any[]) => infer R | ||
? R | ||
: never | ||
|
||
export type CombinedObject<T, U> = T & U | ||
export function useAuthQuery< | ||
TQuery extends DefinedQuery<QueryKey, any>, | ||
TError = FetchError, | ||
TQueryFnData = Awaited<ReturnType<TQuery['fn']>>, | ||
TData = TQueryFnData, | ||
>( | ||
query: TQuery, | ||
options: Omit< | ||
UseQueryOptions<TQueryFnData, TError>, | ||
'queryKey' | 'queryFn' | ||
> = {}, | ||
): CombinedObject< | ||
UseQueryResult<TData, TError>, | ||
{ key: TQuery['key']; fn: TQuery['fn'] } | ||
> { | ||
// @ts-expect-error | ||
return Object.assign( | ||
{}, | ||
useQuery({ | ||
queryKey: query.key, | ||
queryFn: query.fn, | ||
enabled: options.enabled !== false, | ||
...options, | ||
}), | ||
{ | ||
key: query.key, | ||
fn: query.fn, | ||
}, | ||
) | ||
} | ||
|
||
export function useAuthInfiniteQuery< | ||
T extends DefinedQuery<any, any>, | ||
E = FetchError, | ||
FNR = Awaited<ReturnType<T['fn']>>, | ||
R = FNR, | ||
>( | ||
query: T, | ||
options: Omit<UseInfiniteQueryOptions<FNR, E>, 'queryKey' | 'queryFn'>, | ||
): CombinedObject< | ||
UseInfiniteQueryResult<InfiniteData<R>, FetchError>, | ||
{ key: T['key']; fn: T['fn'] } | ||
> { | ||
// @ts-expect-error | ||
return Object.assign( | ||
{}, | ||
// @ts-expect-error | ||
useInfiniteQuery<T, E>({ | ||
queryFn: query.fn, | ||
queryKey: query.key, | ||
enabled: options.enabled !== false, | ||
...options, | ||
}), | ||
{ | ||
key: query.key, | ||
fn: query.fn, | ||
}, | ||
) | ||
} | ||
|
||
/** | ||
* @deprecated use `useAuthQuery` instead | ||
*/ | ||
export const useBizQuery = useAuthQuery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useCallback, useRef } from 'react' | ||
import type { CompositionEventHandler } from 'react' | ||
|
||
export const useInputComposition = ( | ||
props: Pick< | ||
| React.DetailedHTMLProps< | ||
React.InputHTMLAttributes<HTMLInputElement>, | ||
HTMLInputElement | ||
> | ||
| React.DetailedHTMLProps< | ||
React.TextareaHTMLAttributes<HTMLTextAreaElement>, | ||
HTMLTextAreaElement | ||
>, | ||
'onKeyDown' | 'onCompositionEnd' | 'onCompositionStart' | ||
>, | ||
) => { | ||
const { onKeyDown, onCompositionStart, onCompositionEnd } = props | ||
|
||
const isCompositionRef = useRef(false) | ||
|
||
const handleCompositionStart: CompositionEventHandler<any> = useCallback( | ||
(e) => { | ||
isCompositionRef.current = true | ||
onCompositionStart?.(e) | ||
}, | ||
[onCompositionStart], | ||
) | ||
|
||
const handleCompositionEnd: CompositionEventHandler<any> = useCallback( | ||
(e) => { | ||
isCompositionRef.current = false | ||
onCompositionEnd?.(e) | ||
}, | ||
[onCompositionEnd], | ||
) | ||
|
||
const handleKeyDown: React.KeyboardEventHandler<any> = useCallback( | ||
(e) => { | ||
onKeyDown?.(e) | ||
|
||
if (isCompositionRef.current) { | ||
e.stopPropagation() | ||
return | ||
} | ||
|
||
if (e.key === 'Escape') { | ||
e.currentTarget.blur() | ||
e.preventDefault() | ||
e.stopPropagation() | ||
} | ||
}, | ||
[onKeyDown], | ||
) | ||
|
||
return { | ||
onCompositionEnd: handleCompositionEnd, | ||
onCompositionStart: handleCompositionStart, | ||
onKeyDown: handleKeyDown, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
export const useIsOnline = () => { | ||
const [isOnline, setIsOnline] = useState(navigator.onLine) | ||
|
||
useEffect(() => { | ||
const handleOnline = () => setIsOnline(true) | ||
const handleOffline = () => setIsOnline(false) | ||
|
||
window.addEventListener('online', handleOnline) | ||
window.addEventListener('offline', handleOffline) | ||
|
||
return () => { | ||
window.removeEventListener('online', handleOnline) | ||
window.removeEventListener('offline', handleOffline) | ||
} | ||
}, []) | ||
|
||
return isOnline | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
export const usePrevious = <T>(value: T): T | undefined => { | ||
const ref = useRef<T>() | ||
useEffect(() => { | ||
ref.current = value | ||
}) | ||
return ref.current | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useLayoutEffect, useRef } from 'react' | ||
|
||
export const useRefValue = <S>( | ||
value: S, | ||
): Readonly<{ | ||
current: Readonly<S> | ||
}> => { | ||
const ref = useRef<S>(value) | ||
|
||
useLayoutEffect(() => { | ||
ref.current = value | ||
}) | ||
return ref | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
const titleTemplate = `%s | ${APP_NAME}` | ||
export const useTitle = (title?: Nullable<string>) => { | ||
const currentTitleRef = useRef(document.title) | ||
useEffect(() => { | ||
if (!title) return | ||
|
||
document.title = titleTemplate.replace('%s', title) | ||
return () => { | ||
document.title = currentTitleRef.current | ||
} | ||
}, [title]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.