kopia lustrzana https://github.com/cloudflare/wildebeest
66 wiersze
2.0 KiB
TypeScript
66 wiersze
2.0 KiB
TypeScript
import { $, component$, useStyles$ } from '@builder.io/qwik'
|
|
import { getDatabase } from 'wildebeest/backend/src/database'
|
|
import { loader$ } from '@builder.io/qwik-city'
|
|
import styles from '../../../../utils/innerHtmlContent.scss?inline'
|
|
import { getErrorHtml } from '~/utils/getErrorHtml/getErrorHtml'
|
|
import type { MastodonStatus } from '~/types'
|
|
import { StatusesPanel } from '~/components/StatusesPanel/StatusesPanel'
|
|
import { getLocalStatuses } from 'wildebeest/functions/api/v1/accounts/[id]/statuses'
|
|
import { parseHandle } from 'wildebeest/backend/src/utils/parse'
|
|
|
|
export const statusesLoader = loader$<
|
|
Promise<{
|
|
accountId: string
|
|
statuses: MastodonStatus[]
|
|
}>,
|
|
{ DATABASE: D1Database }
|
|
>(async ({ platform, request, html }) => {
|
|
let statuses: MastodonStatus[] = []
|
|
let accountId = ''
|
|
try {
|
|
const url = new URL(request.url)
|
|
accountId = url.pathname.split('/')[1]
|
|
|
|
const handle = parseHandle(accountId)
|
|
accountId = handle.localPart
|
|
const response = await getLocalStatuses(request as Request, await getDatabase(platform as any), handle, 0, true)
|
|
statuses = await response.json<Array<MastodonStatus>>()
|
|
} catch {
|
|
throw html(
|
|
500,
|
|
getErrorHtml(`An error happened when trying to retrieve the account's statuses, please try again later`)
|
|
)
|
|
}
|
|
|
|
return { accountId, statuses: JSON.parse(JSON.stringify(statuses)) }
|
|
})
|
|
|
|
export default component$(() => {
|
|
useStyles$(styles)
|
|
|
|
const { accountId, statuses } = statusesLoader().value
|
|
|
|
return (
|
|
<div data-testid="account-posts-and-replies">
|
|
<StatusesPanel
|
|
initialStatuses={statuses}
|
|
fetchMoreStatuses={$(async (numOfCurrentStatuses: number) => {
|
|
let statuses: MastodonStatus[] = []
|
|
try {
|
|
const response = await fetch(
|
|
`/api/v1/accounts/${accountId}/statuses?offset=${numOfCurrentStatuses}&with-replies=true`
|
|
)
|
|
if (response.ok) {
|
|
const results = await response.text()
|
|
statuses = JSON.parse(results)
|
|
}
|
|
} catch {
|
|
/* empty */
|
|
}
|
|
return statuses
|
|
})}
|
|
/>
|
|
</div>
|
|
)
|
|
})
|