/** @jsxRuntime classic */
/** @jsx jsx */
import { useMemo } from 'react'
import { Center, Inline, Heading, VisuallyHidden, jsx, useTheme } from '@keystone-ui/core'
import { PlusIcon } from '@keystone-ui/icons/icons/PlusIcon'
import { LoadingDots } from '@keystone-ui/loading'
import { makeDataGetter } from '../../../../admin-ui/utils'
import { PageContainer, HEADER_HEIGHT } from '../../../../admin-ui/components/PageContainer'
import { gql, useQuery } from '../../../../admin-ui/apollo'
import { useKeystone, useList } from '../../../../admin-ui/context'
import { Link, type LinkProps } from '../../../../admin-ui/router'
function ListCard ({
listKey,
count,
hideCreate
}: {
listKey: string
hideCreate: boolean
count:
| { type: 'success', count: number }
| { type: 'no-access' }
| { type: 'error', message: string }
| { type: 'loading' }
}) {
const { colors, palette, radii, spacing } = useTheme()
const list = useList(listKey)
return (
{list.label}
{list.isSingleton ? null : count.type === 'success' ? (
{count.count} item{count.count !== 1 ? 's' : ''}
) : count.type === 'error' ? (
count.message
) : count.type === 'loading' ? (
) : (
'No access'
)}
{hideCreate === false && !list.isSingleton && (
Create {list.singular}
)}
)
}
function CreateButton (props: LinkProps) {
const theme = useTheme()
return (
)
}
export function HomePage () {
const {
adminMeta: { lists },
visibleLists,
} = useKeystone()
const query = useMemo(
() => gql`
query {
keystone {
adminMeta {
lists {
key
hideCreate
}
}
}
${Object.values(lists)
.filter(list => !list.isSingleton)
.map(list => `${list.key}: ${list.gqlNames.listQueryCountName}`)
.join('\n')}
}`,
[lists]
)
const { data, error } = useQuery(query, { errorPolicy: 'all' })
const dataGetter = makeDataGetter(data, error?.graphQLErrors)
return (
Dashboard}>
{visibleLists.state === 'loading' ? (
) : (
{(() => {
if (visibleLists.state === 'error') {
return (
{visibleLists.error instanceof Error
? visibleLists.error.message
: visibleLists.error[0].message}
)
}
return Object.keys(lists).map(key => {
if (!visibleLists.lists.has(key)) {
return null
}
const result = dataGetter.get(key)
return (
list.key === key)
?.hideCreate ?? false
}
key={key}
listKey={key}
/>
)
})
})()}
)}
)
}