Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

2.1 KB
Raw Blame
import { useTranslation } from 'react-i18next'
import type { LoaderFunctionArgs, MetaFunction } from 'react-router'
import { useLoaderData } from 'react-router'
import type { SitemapFunction } from 'remix-sitemap'

import { serverFetch } from '~/api/api.server'
import { getOgImageUrl } from '~/lib/constants'
import StatusPage from '~/ui/StatusPage'
import routes from '~/utils/routes'
import { getDescription, getPreviewImage, getTitle } from '~/utils/seo'

export const meta: MetaFunction = () => {
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const { t } = useTranslation('common')

  return [
    ...getTitle(t('titles.invitation')),
    ...getDescription(t('description.default')),
    ...getPreviewImage(
      getOgImageUrl(t('titles.invitation'), t('description.default')),
    ),
  ]
}

export const sitemap: SitemapFunction = () => ({
  exclude: true,
})

export interface TransferCancelLoaderData {
  success: boolean
  error?: string
}

export async function loader({
  request,
}: LoaderFunctionArgs): Promise<TransferCancelLoaderData> {
  const url = new URL(request.url)
  const token = url.searchParams.get('token')

  if (!token) {
    return { success: false, error: 'Invalid or expired token' }
  }

  const result = await serverFetch(request, `project/transfer?token=${token}`, {
    method: 'DELETE',
  })

  if (result.error) {
    const errorMessage =
      typeof result.error === 'string'
        ? result.error
        : 'Invalid or expired token'
    return { success: false, error: errorMessage }
  }

  return { success: true }
}

export default function TransferCancelRoute() {
  const data = useLoaderData<TransferCancelLoaderData>()

  if (data.error) {
    return (
      <StatusPage
        type='error'
        title={data.error}
        actions={[
          { label: 'Dashboard', to: routes.dashboard, primary: true },
          { label: 'Support', to: routes.contact },
        ]}
      />
    )
  }

  return (
    <StatusPage
      type='info'
      title='Project transfer rejected'
      actions={[{ label: 'Dashboard', to: routes.dashboard, primary: true }]}
    />
  )
}