-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.ts
100 lines (93 loc) · 2.77 KB
/
search.ts
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
type SearchResource = {
id: {
kind: string;
videoId: string;
channelId: string;
playlistId?: string;
},
snippet?: Snippet;
statistics?: Statistics;
}
type Snippet = {
publishedAt: Date;
channelId: string;
title: string;
description: string;
thumbnails: {
default?: {
url: string;
width: number;
height: number;
},
medium?: {
url: string;
width: number;
height: number;
},
high?: {
url: string;
width: number;
height: number;
},
};
channelTitle: string;
}
type Statistics = {
viewCount: number;
likeCount: number;
favoriteCount: number;
commentCount: number;
}
type SearchResponse = {
nextPageToken: string;
prevPageToken: string;
regionCode: string;
items: SearchResource[]
}
export default function Search(results: SearchResponse) {
return (`
<ul>
${results.items.reduce((prev, item) => prev + (`
<li key="${item?.id?.videoId}">
<h2>${item?.snippet?.title}</h2>
<p>${item?.snippet?.description}</p>
</li>
`), "")}
</ul>
<script type="text/javascript">
const items = ${JSON.stringify(results.items)};
let counter = 0;
window.MyTube = {
...window.MyTube,
results: {
data: items,
result: function(prop) {
if (!prop) return {
videoId: items[counter]?.id?.videoId,
select: function() {
let location = "/?v=" + items[counter]?.id?.videoId;
window.location.href = location;
}
}
return items[counter]?.snippet[prop];
},
nextResult: function() {
counter++;
if (counter >= items.length) counter = 0;
// TODO: Fetch next page instead of cycling to the beginning
return items[counter]?.snippet?.title;
},
prevResult: function() {
counter--;
if (counter < 0) {
counter = 0;
throw new Error("Already at the first item.");
}
return items[counter]?.snippet?.title;
}
},
}
console.log('See .results for operations on search results');
</script>
`);
}