import { useMemo } from 'react'
import { SuperJSONResult } from 'superjson/dist/types'
import {
BasicLayoutSchema,
LayoutError,
MetaItemSchema,
} from '@interval/sdk/dist/classes/Layout'
import superjson from '~/utils/superjson'
import { unpackIOCall } from '~/utils/transactions'
import PageHeading from '~/components/PageHeading'
import { RenderIOCall } from '~/components/RenderIOCall'
import { LayoutProps } from '..'
import MetadataCardList from '~/components/MetadataCardList'
import useDashboardStructure from '~/utils/useDashboardStructure'
import MobilePageSubnav from '../MobileSubnav'
import { UnimplementedComponents } from '~/components/TransactionUI'
import usePageMenuItems from '~/utils/usePageMenuItems'
import { useIsFeatureEnabled } from '~/utils/useIsFeatureEnabled'
import { logger } from '~/utils/logger'
interface BasicLayoutProps extends LayoutProps {
layout: BasicLayoutSchema
}
function PageErrorItem({ error }: { error: LayoutError }) {
return (
{error.error}:
{' '}
{error.message}
)
}
export default function BasicLayout({
pageSlug,
pageKey,
layout,
onRespond,
mode,
group,
}: BasicLayoutProps) {
const { children } = layout
const ioCall = useMemo(
() => (children ? unpackIOCall(children) : undefined),
[children]
)
const pageMenuItems = usePageMenuItems(layout.menuItems ?? [])
const { hasSubnav, actionSlug, secondaryNav } = useDashboardStructure({
mode,
actionSlug: group.slug,
})
const shouldDisableTableTruncation = useIsFeatureEnabled(
'TABLE_TRUNCATION_DISABLED'
)
let pageMeta: MetaItemSchema[] | null = null
if (layout.metadata) {
try {
pageMeta = superjson.deserialize(layout.metadata as SuperJSONResult)
} catch (error) {
logger.error('Error from SuperJSON deserialization', {
error,
meta: layout.metadata.meta,
})
}
}
// don't render a page that doesn't match the current URL.
// prevents flickering when switching between pages
if (actionSlug !== pageSlug) {
return null
}
const titleError = layout.errors?.find(e => e.layoutKey === 'title')
const descriptionError = layout.errors?.find(
e => e.layoutKey === 'description'
)
const overallErrors = layout.errors?.filter(e => !e.layoutKey)
return (
{titleError.error}: {titleError.message}
)
}
description={
layout.description !== null ? layout.description : undefined
}
descriptionError={
descriptionError && (
{descriptionError.error}: {descriptionError.message}
)
}
actions={pageMenuItems}
/>
{hasSubnav && (
)}
{overallErrors && overallErrors.length > 0 && (
Errors loading page:
{overallErrors.map((error, i) => (
))}
Please check your host logs for more information.
)}
{pageMeta && (
)}
{ioCall && (
)}
)
}