wildebeest/frontend/src/routes/(frontend)/explore/index.tsx

61 wiersze
2.0 KiB
TypeScript
Czysty Zwykły widok Historia

2023-02-13 12:33:24 +00:00
import { $, component$ } from '@builder.io/qwik'
import { DocumentHead, loader$ } from '@builder.io/qwik-city'
2023-01-06 10:38:28 +00:00
import * as timelines from 'wildebeest/functions/api/v1/timelines/public'
2023-02-13 12:33:24 +00:00
import { StatusesPanel } from '~/components/StatusesPanel/StatusesPanel'
2023-01-06 10:38:28 +00:00
import type { MastodonStatus } from '~/types'
import { getDocumentHead } from '~/utils/getDocumentHead'
import { getErrorHtml } from '~/utils/getErrorHtml/getErrorHtml'
2023-02-17 17:02:18 +00:00
export const statusesLoader = loader$<Promise<MastodonStatus[]>, { DATABASE: D1Database; domain: string }>(
async ({ platform, html }) => {
2023-02-03 16:11:07 +00:00
try {
// TODO: use the "trending" API endpoint here.
const response = await timelines.handleRequest(platform.domain, platform.DATABASE)
const results = await response.text()
// Manually parse the JSON to ensure that Qwik finds the resulting objects serializable.
return JSON.parse(results) as MastodonStatus[]
} catch (e: unknown) {
const error = e as { stack: string; cause: string }
console.warn(error.stack, error.cause)
throw html(500, getErrorHtml('The timeline is unavailable, please try again later'))
2023-02-03 16:11:07 +00:00
}
2023-01-06 10:38:28 +00:00
}
)
export default component$(() => {
2023-02-17 17:02:18 +00:00
const statuses = statusesLoader().value
2023-01-06 10:38:28 +00:00
return (
2023-02-13 12:33:24 +00:00
<StatusesPanel
initialStatuses={statuses}
2023-02-13 12:33:24 +00:00
fetchMoreStatuses={$(async (numOfCurrentStatuses: number) => {
let statuses: MastodonStatus[] = []
try {
const response = await fetch(`/api/v1/timelines/public?offset=${numOfCurrentStatuses}`)
if (response.ok) {
const results = await response.text()
statuses = JSON.parse(results)
}
} catch {
/* empty */
}
return statuses
})}
/>
2023-01-06 10:38:28 +00:00
)
})
export const requestLoader = loader$(async ({ request }) => {
// Manually parse the JSON to ensure that Qwik finds the resulting objects serializable.
2023-02-17 17:02:18 +00:00
return JSON.parse(JSON.stringify(request)) as Request
})
2023-02-17 17:02:18 +00:00
export const head: DocumentHead = ({ resolveValue }) => {
const { url } = resolveValue(requestLoader)
return getDocumentHead({
title: 'Explore - Wildebeest',
og: {
url,
},
})
}