allow filtering for bot matches

This commit is contained in:
Ilion Beyst 2022-08-12 20:54:34 +02:00
parent 84748fd60e
commit 7ba9fcee64

View file

@ -6,11 +6,13 @@
export async function load({ url, fetch }) { export async function load({ url, fetch }) {
try { try {
const apiClient = new ApiClient(fetch); const apiClient = new ApiClient(fetch);
const botName = url.searchParams.get("bot");
let query = { let query = {
count: PAGE_SIZE, count: PAGE_SIZE,
before: url.searchParams.get("before"), before: url.searchParams.get("before"),
after: url.searchParams.get("after"), after: url.searchParams.get("after"),
bot: botName,
}; };
let matches = await apiClient.get("/api/matches", removeUndefined(query)); let matches = await apiClient.get("/api/matches", removeUndefined(query));
@ -23,6 +25,7 @@
return { return {
props: { props: {
matches, matches,
botName,
}, },
}; };
} catch (error) { } catch (error) {
@ -49,13 +52,30 @@
import MatchList from "$lib/components/matches/MatchList.svelte"; import MatchList from "$lib/components/matches/MatchList.svelte";
export let matches: object[]; export let matches: object[];
export let botName: string | null;
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}`;
}
async function loadNewer() { async function loadNewer() {
if (matches.length == 0) { if (matches.length == 0) {
return; return;
} }
const firstTimestamp = matches[0]["timestamp"]; const firstTimestamp = matches[0]["timestamp"];
goto(`?after=${firstTimestamp}`); goto(pageLink({ after: firstTimestamp }));
} }
async function loadOlder() { async function loadOlder() {
@ -63,7 +83,7 @@
return; return;
} }
const lastTimestamp = matches[matches.length - 1]["timestamp"]; const lastTimestamp = matches[matches.length - 1]["timestamp"];
goto(`?before=${lastTimestamp}`); goto(pageLink({ before: lastTimestamp }));
} }
</script> </script>