-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
114 lines (101 loc) · 4.57 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React, { useState } from 'react';
import TwitchClient from 'twitch';
import moment from 'moment';
import ImageGallery from 'react-image-gallery';
import './App.scss';
function App() {
const clientId = '21w5wlsrs2z97lckvfraznvflm33m3';
const clientSecret = 'lzci6s1dv7uucj4hb8ji2iw35c7y7g';
const twitchClient = TwitchClient.withClientCredentials(clientId, clientSecret);
const [streamerValue, setStreamerValue] = useState("");
const [gameValue, setGameValue] = useState("");
const [streamerNameDisplay, setstreamerNameDisplay] = useState("");
const [liveStatusValue, setLiveStatusValue] = useState(false);
const [startDateValue, setStartDateValue] = useState("");
const [endDateValue, setEndDateValue] = useState("");
const [createdByValue, setCreatedByValue] = useState("");
const [listOfClips, setListOfClips] = useState([]);
const [numberOfResults, setNumberOfResults] = useState(5);
async function isStreamLive() {
const user = await twitchClient.helix.users.getUserByName(streamerValue);
if (!user) {
return false;
}
return await twitchClient.helix.streams.getStreamByUserId(user.id) !== null;
}
async function fetchClipLibrary() {
const user = await twitchClient.helix.users.getUserByName(streamerValue);
if (!user) {
return false;
}
const clips = await twitchClient.helix.clips.getClipsForBroadcaster(user.id,
{ endDate: endDateValue ? moment(endDateValue).format() : undefined,
startDate: startDateValue ? moment(startDateValue).format() : undefined,
limit: createdByValue ? 100 : numberOfResults,
});
return clips;
}
async function fetchGameId() {
const gameId = await twitchClient.helix.games.getGameByName(gameValue);
return gameId;
}
const renderVideo = clip => {
return (
<div >
<iframe
src={clip.embedUrl}
frameBorder='0'
allowFullScreen
title={clip.title}
className='iframe'
/>
</div>
)
}
return (
<div className='pageContainer'>
<form className='filterContainer'>
<input type='text' value={streamerValue} onChange={ event => {setStreamerValue(event.target.value); setstreamerNameDisplay(''); setLiveStatusValue(false) }} placeholder="Type here to search for a streamer's clips" className='streamerNameInput' />
<input type='date' onChange={ event => setStartDateValue(event.target.value) } className='startDate' />
<input type='date' onChange={ event => setEndDateValue(event.target.value) } className='endDate' />
<input type='text' value={ gameValue } onChange={ event => setGameValue(event.target.value) } placeholder="Filter by game" className='gameInput' />
<input type='text' value={ createdByValue } onChange={ event => setCreatedByValue(event.target.value) } placeholder="Created By" className='createdByInput' />
<p>Increasing this will cause some lag</p>
<input type='text' value={numberOfResults} onChange={event => setNumberOfResults(event.target.value)} />
<button
className='button'
type='submit'
onClick={ (e) => {
e.preventDefault();
console.log('coming soon chat, relax');
isStreamLive().then(data => setLiveStatusValue(data));
fetchGameId().then(gameId => {
fetchClipLibrary().then(data => {
const filteredClips = data?.data?.filter(clip => {
if (createdByValue && gameId) {
return clip.gameId.toString() === gameId.id.toString() && clip.createdBy === createdByValue;
} else if (!createdByValue && gameId) {
return clip.gameId.toString() === gameId.id.toString();
} else if (!gameId && createdByValue) {
return clip.creatorDisplayName === createdByValue;
} else {
return clip;
}
});
console.log(filteredClips)
const mungedClips = filteredClips.map(clip => ({thumbnail: clip.thumbnailUrl, original: clip.url, embedUrl: clip.embedUrl, description: clip.title , renderItem: () => renderVideo(clip)}))
return setListOfClips(mungedClips)
})
});
} }
>
search
</button>
</form>
<section className='galleryContainer'>
<ImageGallery showFullscreenButton={false} showPlayButton={false} items={listOfClips} />
</section>
</div>
);
}
export default App;