'use client'
import type { HTMLAttributes, ReactNode } from 'react'
import { Fragment, useMemo, useState } from 'react'
import { usePathname } from 'next/navigation'
import { ActionGroup } from '@keystar/ui/action-group'
import { Breadcrumbs, Item } from '@keystar/ui/breadcrumbs'
import { AlertDialog, DialogContainer } from '@keystar/ui/dialog'
import { Icon } from '@keystar/ui/icon'
import { allIcons as KeystarIcons } from '@keystar/ui/icon/all'
import { Grid, HStack } from '@keystar/ui/layout'
import { breakpointQueries, css, tokenSchema } from '@keystar/ui/style'
import { toastQueue } from '@keystar/ui/toast'
import { Heading, Text } from '@keystar/ui/typography'
import { gql, useApolloClient } from '../../../../admin-ui/apollo'
import { Container, CONTAINER_MAX } from '../../../../admin-ui/components/Container'
import { ErrorDetailsDialog } from '../../../../admin-ui/components/Errors'
import type { ActionMeta, ListMeta } from '../../../../types'
import { useKeystone } from '../../../../admin-ui'
export function ItemPageHeader({
list,
actions,
item,
label,
title = label,
onAction,
}: {
list: ListMeta
actions: ActionMeta[]
item: Record | null
label: string
title: string
onAction: ((action: ActionMeta, resultId: string) => void) | null
}) {
const pathname = usePathname()
const { adminPath } = useKeystone()
return (
{list.isSingleton ? (
{list.label}
) : (
<>
- {list.label}
- {label}
{/* Every page must have an H1 for accessibility. */}
{title}
>
)}
{item && onAction && actions.length > 0 && (
)}
)
}
function replace(
s: string,
list: ListMeta,
args: {
itemLabel?: string
}
) {
if (s.includes('{Singular}')) s = s.replaceAll('{Singular}', list.singular)
if (s.includes('{Plural}')) s = s.replaceAll('{Plural}', list.plural)
if (s.includes('{singular}')) s = s.replaceAll('{singular}', list.singular.toLowerCase())
if (s.includes('{plural}')) s = s.replaceAll('{plural}', list.plural.toLowerCase())
if ('itemLabel' in args) s = s.replaceAll('{itemLabel}', args.itemLabel ?? '')
return s
}
type ActionError = {
action: ActionMeta
error: Error
}
export function ItemActions({
list,
item,
actions,
onAction,
}: {
list: ListMeta
item: Record
actions: ActionMeta[]
onAction: (action: ActionMeta, resultId: string) => void
}) {
const apolloClient = useApolloClient()
const actionItems = useMemo(
() =>
actions.map(action => ({
id: action.key,
label: action.label,
icon: action.icon ? KeystarIcons[action.icon] : null,
})),
[actions]
)
const [actionError, setActionError] = useState(null)
const [activeAction, setActiveAction] = useState(null)
const itemLabel_ = item[list.labelField] ?? item.id
const itemLabel = typeof itemLabel_ === 'string' ? itemLabel_ : (item.id as string)
const disabledKeys = useMemo(
() =>
actions.filter(action => action.itemView.actionMode === 'disabled').map(action => action.key),
[actions]
)
async function onTryAction(action: ActionMeta, confirmed: boolean) {
setActiveAction(null)
if (!confirmed && !action.itemView.hidePrompt) {
setActiveAction(action)
return
}
const { messages: m } = action
try {
const data = await apolloClient.mutate({
mutation: gql`mutation ${action.graphql.names.one}($id: ID!) {
result: ${action.graphql.names.one}(where: { id: $id }) {
id
}
}`,
variables: { id: item.id },
})
if (!action.itemView.hideToast) {
toastQueue.neutral(replace(m.success, list, { itemLabel }), { timeout: 5000 })
}
onAction(action, data.data?.result?.id)
} catch (error: any) {
toastQueue.critical(replace(m.fail, list, { itemLabel }), {
actionLabel: 'Details',
onAction: () => setActionError({ action, error }),
shouldCloseOnAction: true,
})
}
}
return (
{
const action = list.actions.find(action => action.key === key)
if (!action) return
onTryAction(action, false)
}}
>
{item => (
-
{item.icon && }
{item.label}
)}
setActiveAction(null)}>
{activeAction && (
{
await onTryAction(activeAction, true)
}}
>
{replace(activeAction.messages.prompt, list, { itemLabel })}
)}
setActionError(null)} isDismissable>
{actionError && (
)}
)
}
export function ColumnLayout(props: HTMLAttributes) {
return (
// this container must be relative to catch absolute children
// particularly the "expanded" document-field, which needs a height of 100%
)
}
export function StickySidebar(props: HTMLAttributes) {
return (
)
}
export function BaseToolbar(props: { children: ReactNode }) {
return (
{props.children}
)
}