import { component$, useStyles$ } from '@builder.io/qwik' import { loader$ } from '@builder.io/qwik-city' import { MastodonAccount } from 'wildebeest/backend/src/types' import StickyHeader from '~/components/StickyHeader/StickyHeader' import { formatDateTime } from '~/utils/dateTime' import { formatRoundedNumber } from '~/utils/numbers' import styles from '../../../utils/innerHtmlContent.scss?inline' import { getAccount } from 'wildebeest/backend/src/accounts/getAccount' import { getNotFoundHtml } from '~/utils/getNotFoundHtml/getNotFoundHtml' import { getErrorHtml } from '~/utils/getErrorHtml/getErrorHtml' export const accountLoader = loader$<{ DATABASE: D1Database }, Promise>( async ({ platform, request, html }) => { let account: MastodonAccount | null = null try { const url = new URL(request.url) const domain = url.hostname const accountId = url.pathname.split('/')[1] account = await getAccount(domain, accountId, platform.DATABASE) } catch { throw html( 500, getErrorHtml(`An error happened when trying to retrieve the account's details, please try again later`) ) } if (!account) { throw html(404, getNotFoundHtml()) } return account } ) export default component$(() => { useStyles$(styles) const accountDetails = accountLoader.use().value const fields = [ { name: 'Joined', value: formatDateTime(accountDetails.created_at, false), }, ...accountDetails.fields, ] const stats = [ { name: 'Posts', value: formatRoundedNumber(accountDetails.statuses_count), }, { name: 'Following', value: formatRoundedNumber(accountDetails.following_count), }, { name: 'Followers', value: formatRoundedNumber(accountDetails.followers_count), }, ] const accountDomain = getAccountDomain(accountDetails) return (
{`Header {`Avatar

{accountDetails.display_name}

@{accountDetails.acct} {accountDomain && `@${accountDomain}`}
{fields.map(({ name, value }) => (
{name}
))}
{stats.map(({ name, value }) => (
{value} {name}
))}
) }) export function getAccountDomain(account: MastodonAccount): string | null { try { const url = new URL(account.url) return url.hostname || null } catch { return null } }