Chat Failed Delivery
A thread with an offline alert and failed message bubbles that can be retried or discarded, moving through sending to sent.
"use client"
import { useState } from "react"
import {
ArrowClockwiseIcon,
WarningIcon,
WifiSlashIcon,
} from "@phosphor-icons/react"
import {
Alert,
AlertAction,
AlertDescription,
AlertTitle,
} from "@/components/ui/alert"
import { Button } from "@/components/ui/button"
import { Spinner } from "@/components/ui/spinner"
// The copy: change text here, not in the markup.
const COPY = {
offlineTitle: "You are offline",
offlineBody: "Messages will send when your connection returns.",
retry: "Retry",
retryAll: "Retry all",
discard: "Discard",
failed: "Not delivered",
sending: "Sending",
sent: "Sent",
} as const
const MESSAGES = [
{
id: "m1",
text: "Pushed the fix to staging, can someone verify?",
state: "sent",
},
{
id: "m2",
text: "Also bumping the rate limit for the Acme workspace.",
state: "failed",
},
{
id: "m3",
text: "Attaching the trace from the failing run.",
state: "failed",
},
{ id: "m4", text: "Ping me when it is green.", state: "sending" },
] as const
type State = (typeof MESSAGES)[number]["state"]
/**
* A chat thread showing delivery errors. An offline alert sits above the
* messages, and each failed bubble has retry and discard actions. Retrying
* moves a message to sending and then sent.
*
* The words and messages live in `COPY` and `MESSAGES` at the top of the file.
*/
export default function ChatFailedDelivery01() {
const [states, setStates] = useState<Record<string, State>>(
Object.fromEntries(MESSAGES.map((m) => [m.id, m.state]))
)
const [hidden, setHidden] = useState<string[]>([])
function retry(id: string) {
setStates((s) => ({ ...s, [id]: "sending" }))
window.setTimeout(() => setStates((s) => ({ ...s, [id]: "sent" })), 1200)
}
const failedIds = MESSAGES.filter(
(m) => states[m.id] === "failed" && !hidden.includes(m.id)
).map((m) => m.id)
return (
<section className="mx-auto flex w-full max-w-lg flex-col gap-4 rounded-2xl border bg-card p-4 text-card-foreground">
{failedIds.length > 0 ? (
<Alert variant="destructive">
<WifiSlashIcon />
<AlertTitle>{COPY.offlineTitle}</AlertTitle>
<AlertDescription>{COPY.offlineBody}</AlertDescription>
<AlertAction>
<Button
size="xs"
variant="outline"
onClick={() => failedIds.forEach(retry)}
>
{COPY.retryAll}
</Button>
</AlertAction>
</Alert>
) : null}
<ul className="flex flex-col items-end gap-3">
{MESSAGES.filter((m) => !hidden.includes(m.id)).map((m) => {
const state = states[m.id]
return (
<li
key={m.id}
className="flex max-w-[85%] flex-col items-end gap-1"
>
<p
className={
state === "failed"
? "rounded-2xl border border-destructive/40 bg-destructive/10 px-3 py-2 text-sm"
: "rounded-2xl bg-primary px-3 py-2 text-sm text-primary-foreground"
}
>
{m.text}
</p>
{state === "failed" ? (
<div className="flex items-center gap-2 text-xs text-destructive">
<WarningIcon className="size-3.5" />
{COPY.failed}
<Button size="xs" variant="ghost" onClick={() => retry(m.id)}>
<ArrowClockwiseIcon />
{COPY.retry}
</Button>
<Button
size="xs"
variant="ghost"
onClick={() => setHidden((h) => [...h, m.id])}
>
{COPY.discard}
</Button>
</div>
) : state === "sending" ? (
<span className="flex items-center gap-1.5 text-xs text-muted-foreground">
<Spinner className="size-3" />
{COPY.sending}
</span>
) : (
<span className="text-xs text-muted-foreground">
{COPY.sent}
</span>
)}
</li>
)
})}
</ul>
</section>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/chat-failed-delivery-01npx shadcn@latest add @01orbital/chat-failed-delivery-01yarn dlx shadcn@latest add @01orbital/chat-failed-delivery-01bunx --bun shadcn@latest add @01orbital/chat-failed-delivery-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/alert/chat-failed-delivery-01/index.tsx: change them there, not in the markup. MESSAGES, also at the top of the file, holds its lists and settings.
Built from
npm: @phosphor-icons/react. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on