-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathStreamGrid.vue
159 lines (141 loc) · 4.32 KB
/
StreamGrid.vue
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<template>
<div class="px-0">
<transition-group
tag="div"
class="row flex-wrap"
name="fade-transition"
>
<!-- Loading placeholder -->
<v-col v-if="loading">
<v-card>
<v-card-text>
<v-row align-center>
<v-col shrink>
<v-progress-circular
indeterminate
color="primary"
/>
</v-col>
<v-flex>Loading...</v-flex>
</v-row>
</v-card-text>
</v-card>
</v-col>
<!-- Cards -->
<v-col
v-if="!loading"
v-for="stream in streamerList"
:key="stream.name"
:cols="cols"
:sm="sm"
:md="md"
:lg="lg"
:xl="xl"
>
<stream-card
:to="stream.name"
:image="stream.thumbnail"
:nsfw="stream.nsfw"
:title="stream.title"
:name="stream.name"
:viewers="stream.viewCount"
:blur="blurNsfw"
live
/>
</v-col>
</transition-group>
</div>
</template>
<script>
import StreamCard from '@/components/StreamCard';
import { db } from '@/plugins/firebase';
import { mapGetters } from 'vuex';
import { VStore } from '@/store';
export default {
name: 'StreamGrid',
serverCacheKey: () => Math.trunc( Date.now() / ( 1000 * 10 ) ),
props: {
streamers: {},
blurNsfw: { type: Boolean, default: true },
cols: { type: Number, default: 12 },
sm: { type: Number, default: 6 },
md: { type: Number, default: 4 },
lg: { type: Number, default: 3 },
xl: { type: Number, default: 2 },
},
components: {
StreamCard,
},
data () {
return {
streamDataListener: null,
thumbnailInterval: null,
streams: [],
thumbnails: [],
loading: true,
imageIndex: 0,
}
},
methods: {
getData () {
const streamRef = db
.collection( 'streams' )
.where( 'live', '==', true )
.limit( 16 );
this.streamDataListener = streamRef.onSnapshot( async res => await this.dataChanged( res.docs ), error => console.warn( error ) );
},
async dataChanged( docs ) {
this.streams = docs.map( doc => {
const stream = doc.data();
const thumbnail = ( stream.live ? stream.thumbnail : stream.cover ) || stream.cover;
return {
title : stream.title,
live : stream.live,
nsfw : stream.nsfw,
owner : stream.owner,
thumbnail : thumbnail,
name : stream.user.name,
}
});
this.thumbnails = this.streams.map( stream => stream.thumbnail );
this.loading = false;
},
updateThumbnails () {
if ( this.imageIndex < this.streams.length ) this.imageIndex++;
if ( this.imageIndex === this.streams.length ) this.imageIndex = 0;
const coeff = 1000 * 60; // ms * sec
const timestamp = Math.round( Date.now() / coeff ) * coeff;
const _thumbnail = this.thumbnails[this.imageIndex] || 'https://cdn.bitwave.tv/static/img/Bitwave_Banner.jpg';
this.streams[this.imageIndex].thumbnail = `${_thumbnail}?${timestamp}`;
},
},
computed: {
...mapGetters({
getChannelViews: VStore.$getters.getChannelViews,
}),
streamerList () {
const streams = this.streams.map( stream => {
return { ...stream, viewCount: this.getChannelViews( stream.name ) || 0 };
});
return streams.sort( (a, b) => b.viewCount - a.viewCount );
},
},
created () {
this.loading = !this.streamers;
this.streams = this.streamers;
if ( this.streams ) this.thumbnails = this.streams.map( stream => stream.thumbnail );
},
mounted () {
// Wait 30 seconds before attaching DB listener
if ( this.streams && this.streams.length > 0 )
setTimeout( () => this.getData(), 30 * 1000 );
else
this.getData();
this.thumbnailInterval = setInterval( () => this.updateThumbnails(), 10 * 1000 );
},
beforeDestroy() {
if ( this.streamDataListener ) this.streamDataListener();
if ( this.thumbnailInterval ) clearInterval( this.thumbnailInterval );
},
}
</script>