"use client"
import * as React from "react"
import {
useEmailComposer,
type EmailComposerContext,
type EmailSendPayload,
} from "@/components/templates/email-composer"
import { LocalBanner } from "@/components/ui/banner"
import { useAltKeyLabel, useModKeyLabel } from "@/hooks/use-mod-key-label"
/** One command on a hub's PageHeader / toolbar `actionItems`. */
interface HubAction {
id: string
label: string
icon: string
variant: "outline"
shortcut: string
onSelect: () => void
}
/**
* Everything a hub needs to offer "Email": one header action and one inline confirmation.
*
* ```tsx
* const email = useEmailHubAction(() => buildPeopleEmailContext(...))
*
* {email.banner}
* ```
*
* The composer itself is mounted once by `EmailComposerProvider` (app shell). Delivery is not
* wired here: add your API call in `onSend`. Outcomes are shown inline, never as a toast
* (`exxat-no-toast.mdc`).
*/
export function useEmailHubAction(
/** Built at click time, so it reads the hub's current rows. Omit to hide the action. */
getContext?: () => EmailComposerContext,
options?: { onSend?: (payload: EmailSendPayload) => void | Promise },
): { actionItems: HubAction[]; banner: React.ReactNode } {
const { openComposer } = useEmailComposer()
const mod = useModKeyLabel()
const alt = useAltKeyLabel()
const [note, setNote] = React.useState(null)
const open = React.useCallback(() => {
if (!getContext) return
openComposer(getContext(), {
onSend: payload => {
void options?.onSend?.(payload)
const count = payload.recipients.length + payload.extraTo.length
setNote(`${payload.scheduledFor ? "Scheduled for" : "Sent to"} ${count} ${count === 1 ? "person" : "people"}.`)
},
// Prototype: nothing is delivered. The composer confirms the test inline.
onSendTest: () => {},
})
}, [getContext, openComposer, options])
const actionItems = React.useMemo(
() =>
getContext
? [{ id: "email", label: "Email", icon: "fa-envelope", variant: "outline", shortcut: `${mod}${alt}M`, onSelect: open }]
: [],
[getContext, mod, alt, open],
)
const banner = note ? (
setNote(null)}>
{note}
) : null
return { actionItems, banner }
}