-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[react-hooks] Refactor useUser hook to return loadable values
- Loading branch information
Showing
3 changed files
with
72 additions
and
6 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
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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import userStore from 'part:@sanity/base/user' | ||
import {useObservable} from './utils/use-observable' | ||
import {useLoadable, LoadableState} from './utils/useLoadable' | ||
|
||
export interface User { | ||
id: string | ||
displayName?: string | ||
imageUrl?: string | ||
} | ||
|
||
export const LOADING_USER = Symbol.for('LOADING_USER') | ||
|
||
export function useUser(userId): User | null | typeof LOADING_USER { | ||
return useObservable(userStore.observable.getUser(userId)) | ||
export function useUser(userId): LoadableState<User> { | ||
return useLoadable(userStore.observable.getUser(userId)) | ||
} |
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,68 @@ | ||
import * as React from 'react' | ||
import {Observable, Subscription} from 'rxjs' | ||
|
||
export type LoadableState<T> = LoadingState<T> | LoadedState<T> | ErrorState<T> | ||
|
||
export interface LoadingState<T> { | ||
value: undefined | ||
error: undefined | ||
isLoading: true | ||
} | ||
|
||
export interface LoadedState<T> { | ||
value: T | ||
error: undefined | ||
isLoading: false | ||
} | ||
|
||
export interface ErrorState<T> { | ||
value: undefined | ||
error: Error | ||
isLoading: false | ||
} | ||
|
||
export function useLoadable<T>(observable$: Observable<T>): LoadableState<T> | ||
export function useLoadable<T>(observable$: Observable<T>, initialValue: T): LoadableState<T> | ||
export function useLoadable<T>(observable$: Observable<T>, initialValue?: T): LoadableState<T> { | ||
const subscription = React.useRef<Subscription>() | ||
const [value, setState] = React.useState<LoadableState<T>>(() => { | ||
let isSync = true | ||
let syncVal: LoadableState<T> = | ||
typeof initialValue === 'undefined' | ||
? {isLoading: true, value: undefined, error: undefined} | ||
: {isLoading: false, value: initialValue, error: undefined} | ||
|
||
subscription.current = observable$.subscribe( | ||
nextVal => { | ||
const nextState: LoadedState<T> = { | ||
isLoading: false, | ||
value: nextVal, | ||
error: undefined | ||
} | ||
|
||
if (isSync) { | ||
syncVal = nextState | ||
} else { | ||
setState(nextState) | ||
} | ||
}, | ||
error => { | ||
setState({isLoading: false, error, value: undefined}) | ||
} | ||
) | ||
|
||
isSync = false | ||
return syncVal | ||
}) | ||
|
||
React.useEffect( | ||
() => () => { | ||
if (subscription.current) { | ||
subscription.current.unsubscribe() | ||
} | ||
}, | ||
[] | ||
) | ||
|
||
return value | ||
} |