import { assert } from '@tldraw/utils' import { GetServerSideProps } from 'next' import { useEffect, useState } from 'react' import { getAppOctokit } from '../src/octokit' type Props = { deliveries: { id: number guid: string delivered_at: string redelivery: boolean duration: number status: string status_code: number event: string action: string | null installation_id: number | null repository_id: number | null }[] cursor: string | null } export const getServerSideProps: GetServerSideProps = async (context) => { assert(process.env.NODE_ENV !== 'production') const gh = getAppOctokit() const deliveries = await gh.octokit.rest.apps.listWebhookDeliveries({ per_page: 100, cursor: (context.query.cursor as string) ?? undefined, }) const nextLinkMatch = deliveries.headers.link?.match(/(?<=<)([\S]*)(?=>; rel="Next")/i) let cursor: string | null = null if (nextLinkMatch) { const url = new URL(nextLinkMatch[0]) cursor = url.searchParams.get('cursor') } return { props: { deliveries: deliveries.data, cursor } } } type SelectedDelivery = { id: number data?: unknown } export default function Deliveries({ deliveries, cursor }: Props) { const [selectedDelivery, setSelectedDelivery] = useState(null) const [isSimulating, setIsSimulating] = useState(false) const [isRedelivering, setIsRedelivering] = useState(false) useEffect(() => { if (!selectedDelivery || (selectedDelivery && selectedDelivery.data)) return let cancelled = false ;(async () => { const response = await fetch(`/api/dev/getDelivery?id=${selectedDelivery.id}`) const data = await response.json() if (cancelled) return setSelectedDelivery({ id: selectedDelivery.id, data }) })() return () => { cancelled = true } }) return (

Deliveries

    {deliveries.map((delivery) => (
  1. setSelectedDelivery({ id: delivery.id })} >
    {delivery.event} {delivery.action && `.${delivery.action}`}
    {formatDate(new Date(delivery.delivered_at))}
  2. ))}
  3. Load more...
{selectedDelivery && selectedDelivery.data ? (
							{JSON.stringify(selectedDelivery.data, null, '\t')}
						
) : selectedDelivery ? (
loading...
) : null}
) } function formatDate(date: Date) { const intl = new Intl.DateTimeFormat('en-GB', { dateStyle: 'short', timeStyle: 'short', }) return intl.format(date) }