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
|
<script lang="ts" context="module">
import { ApiClient } from "$lib/api_client";
const PAGE_SIZE = "50";
export async function load({ url, fetch }) {
try {
const apiClient = new ApiClient(fetch);
const botName = url.searchParams.get("bot");
let query = {
count: PAGE_SIZE,
before: url.searchParams.get("before"),
after: url.searchParams.get("after"),
bot: botName,
};
let { matches, has_next } = await apiClient.get("/api/matches", removeUndefined(query));
// TODO: should this be done client-side?
if (query["after"]) {
matches = matches.reverse();
}
return {
props: {
matches,
botName,
hasNext: has_next,
query,
},
};
} catch (error) {
return {
status: error.status,
error: new Error("failed to load matches"),
};
}
}
function removeUndefined(obj: Record<string, string>): Record<string, string> {
Object.keys(obj).forEach((key) => {
if (obj[key] === undefined || obj[key] === null) {
delete obj[key];
}
});
return obj;
}
</script>
<script lang="ts">
import MatchList from "$lib/components/matches/MatchList.svelte";
export let matches: object[];
export let botName: string | null;
// whether a next page exists in the current iteration direction (before/after)
export let hasNext: boolean;
export let query: object;
type Cursor = {
before?: string;
after?: string;
};
function pageLink(cursor: Cursor) {
let paramsObj = {
...cursor,
};
if (botName) {
paramsObj["bot"] = botName;
}
const params = new URLSearchParams(paramsObj);
return `?${params}`;
}
function olderMatchesLink(matches: object[]): string {
if (matches.length == 0 || (query["before"] && !hasNext)) {
return null;
}
const lastTimestamp = matches[matches.length - 1]["timestamp"];
return pageLink({ before: lastTimestamp });
}
function newerMatchesLink(matches: object[]): string {
if (
matches.length == 0 ||
(query["after"] && !hasNext) ||
// we are viewing the first page, so there should be no newer matches.
// alternatively, we could show a "refresh" here.
(!query["before"] && !query["after"])
) {
return null;
}
const firstTimestamp = matches[0]["timestamp"];
return pageLink({ after: firstTimestamp });
}
</script>
<div class="container">
<MatchList {matches} />
<div class="page-controls">
<div class="btn-group">
<a class="btn btn-page-prev" href={newerMatchesLink(matches)}>newer</a>
<a class="btn btn-page-next" href={olderMatchesLink(matches)}>older</a>
</div>
</div>
</div>
<style lang="scss">
.container {
width: 800px;
margin: 0 auto;
}
.page-controls {
display: flex;
justify-content: center;
margin: 24px 0;
}
</style>
|