-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace deprecated /users/follows endpoint with /channels/followed (#16)
- Loading branch information
Showing
5 changed files
with
373 additions
and
379 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
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,50 +1,49 @@ | ||
import { IApiServiceError } from '../services/api.service' | ||
import { useEffect, useState } from 'react' | ||
import twitchService, { ITwitchUserFollowsFromTo } from '../services/twitch.service' | ||
import type { ITwitchError, ITwitchLiveStream, ITwitchUser } from '../services/twitch.service' | ||
|
||
|
||
/** | ||
* A React hook to get information about streams belonging to channels that the authenticated user follows. | ||
* | ||
* @param isOfflineHidden - Specifies whether to show channels that are currently offline. | ||
* @param minViewCount - The minimum number of views to display a channel. | ||
* @returns an array of three elements: error, isLoading and an array of live stream. | ||
*/ | ||
export default ( | ||
isOfflineHidden: boolean = false, | ||
minViewCount: number = 1e4 | ||
): [ITwitchError | IApiServiceError | undefined, boolean, Array<ITwitchLiveStream>, Array<ITwitchUser>] => { | ||
const [error, setError] = useState<ITwitchError | IApiServiceError>() | ||
const [isLoading, setIsLoading] = useState<boolean>(true) | ||
const [liveStreams, setLiveStreams] = useState<Array<ITwitchLiveStream>>([]) | ||
const [offlineStreams, setOfflineStreams] = useState<Array<ITwitchUser>>([]) | ||
|
||
useEffect(() => { | ||
const getStreams = async () => { | ||
const authUser: ITwitchUser = await twitchService.getAuthUser() | ||
const liveStreams: Array<ITwitchLiveStream> = await twitchService.getLiveFollowedStreams(authUser.id) | ||
|
||
setLiveStreams(liveStreams) | ||
|
||
if (isOfflineHidden) return | ||
|
||
const followedUsers: Array<ITwitchUserFollowsFromTo> = await twitchService.getUserFollows(authUser.id) | ||
const offlineFollowedUsers: Array<ITwitchUserFollowsFromTo> = followedUsers.filter( | ||
({ to_id }) => !liveStreams.some(({ user_id }) => user_id === to_id) | ||
) | ||
const offlineStreams: Array<ITwitchUser> = await twitchService.getUsers( | ||
offlineFollowedUsers.map(({ to_id }) => to_id) | ||
) | ||
const filteredOfflineStreams: Array<ITwitchUser> = offlineStreams.filter( | ||
({ view_count }) => Number(view_count) > minViewCount | ||
) | ||
|
||
setOfflineStreams(filteredOfflineStreams) | ||
} | ||
|
||
getStreams().catch(setError).finally(() => setIsLoading(false)) | ||
}, []) | ||
|
||
return [error, isLoading, liveStreams, offlineStreams] | ||
} | ||
import { IApiServiceError } from '../services/api.service' | ||
import { useEffect, useState } from 'react' | ||
import twitchService, { ITwitchUserChannelFollow } from '../services/twitch.service' | ||
import type { ITwitchError, ITwitchLiveStream, ITwitchUser } from '../services/twitch.service' | ||
|
||
|
||
/** | ||
* A React hook to get information about streams belonging to channels that the authenticated user follows. | ||
* | ||
* @param isOfflineHidden - Specifies whether to show channels that are currently offline. | ||
* @param minViewCount - The minimum number of views to display a channel. | ||
* @returns an array of three elements: error, isLoading and an array of live stream. | ||
*/ | ||
export default ( | ||
isOfflineHidden: boolean = false | ||
): [ITwitchError | IApiServiceError | undefined, boolean, Array<ITwitchLiveStream>, Array<ITwitchUser>] => { | ||
const [error, setError] = useState<ITwitchError | IApiServiceError>() | ||
const [isLoading, setIsLoading] = useState<boolean>(true) | ||
const [liveStreams, setLiveStreams] = useState<Array<ITwitchLiveStream>>([]) | ||
const [offlineStreams, setOfflineStreams] = useState<Array<ITwitchUser>>([]) | ||
|
||
useEffect(() => { | ||
const getStreams = async () => { | ||
const authUser: ITwitchUser = await twitchService.getAuthUser() | ||
const liveStreams: Array<ITwitchLiveStream> = await twitchService.getLiveFollowedStreams(authUser.id) | ||
|
||
setLiveStreams(liveStreams) | ||
|
||
if (isOfflineHidden) return | ||
|
||
const followedUsers: Array<ITwitchUserChannelFollow> = await twitchService.getUserFollows(authUser.id) | ||
const offlineFollowedUsers: Array<ITwitchUserChannelFollow> = followedUsers.filter( | ||
({ broadcaster_id }) => !liveStreams.some(({ user_id }) => user_id === broadcaster_id) | ||
) | ||
|
||
const offlineStreams: Array<ITwitchUser> = await twitchService.getUsers( | ||
offlineFollowedUsers.map(({ broadcaster_id }) => broadcaster_id) | ||
) | ||
|
||
setOfflineStreams(offlineStreams) | ||
} | ||
|
||
getStreams() | ||
.catch(setError) | ||
.finally(() => setIsLoading(false)) | ||
}, []) | ||
|
||
return [error, isLoading, liveStreams, offlineStreams] | ||
} |
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.