190 lines
6.9 KiB
TypeScript
190 lines
6.9 KiB
TypeScript
import { useTranslation } from "react-i18next";
|
|
import { FeatherAtSign, FeatherChevronLeft, FeatherMessageSquare, FeatherSearch } from "@subframe/core";
|
|
import type { ChatMessageDiscoveryRowVM, ChatMessageDiscoveryVM } from "@hivegrid/view-models";
|
|
|
|
import { useLocaleFormatters } from "~/locale/format";
|
|
import { Avatar } from "~/ui/components/Avatar";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { ButtonGroup } from "~/ui/components/ButtonGroup";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { LoadingIndicatorSkeleton } from "~/ui/components/LoadingIndicatorSkeleton";
|
|
import { SearchInput } from "~/ui/components/SearchInput";
|
|
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
|
|
|
export type ChatMessageDiscoveryActions = Readonly<{
|
|
onBack: (href: string) => void;
|
|
onOpenResult: (row: ChatMessageDiscoveryRowVM) => void;
|
|
onQueryChange: (query: string) => void;
|
|
onSwitchMode: (href: string) => void;
|
|
onLoadMore?: (cursor: string) => void | Promise<void>;
|
|
onRetry: () => void | Promise<void>;
|
|
}>;
|
|
|
|
export function ChatMessageDiscoveryPage({ vm, actions }: { readonly vm: ChatMessageDiscoveryVM; readonly actions: ChatMessageDiscoveryActions }) {
|
|
const { t } = useTranslation();
|
|
|
|
if (vm.status === "idle" || vm.status === "loading") {
|
|
return (
|
|
<DiscoveryShell title={t("chat.nav.title")}>
|
|
<LoadingIndicatorSkeleton variant="list" role="status" aria-label={t("shared.loading")} />
|
|
</DiscoveryShell>
|
|
);
|
|
}
|
|
if (vm.status === "error") {
|
|
return (
|
|
<DiscoveryShell title={t("chat.nav.title")}>
|
|
<EmptyState
|
|
outlined
|
|
variant="error"
|
|
title={t("shared.error")}
|
|
body={t(vm.error.messageKey)}
|
|
action={
|
|
<Button variant="tertiary" onClick={() => void actions.onRetry()}>
|
|
{t("shared.retry")}
|
|
</Button>
|
|
}
|
|
/>
|
|
</DiscoveryShell>
|
|
);
|
|
}
|
|
if (vm.status === "empty" || !vm.data) {
|
|
return (
|
|
<DiscoveryShell title={t("chat.nav.title")}>
|
|
<EmptyState outlined title={t("chat.messageDiscovery.mentions.emptyTitle")} body={t("chat.messageDiscovery.mentions.emptyBody")} />
|
|
</DiscoveryShell>
|
|
);
|
|
}
|
|
|
|
const data = vm.data;
|
|
const isEmptySearch = data.mode === "search" && data.query.length === 0;
|
|
const emptyTitleKey = isEmptySearch ? data.keys.emptyTitleKey : data.keys.noResultsTitleKey;
|
|
const emptyBodyKey = isEmptySearch ? data.keys.emptyBodyKey : data.keys.noResultsBodyKey;
|
|
const switchHref = data.mode === "search" ? data.hrefs.mentions : data.hrefs.search;
|
|
const switchLabelKey = data.mode === "search" ? "chat.messageDiscovery.actions.mentions" : "chat.messageDiscovery.actions.search";
|
|
|
|
return (
|
|
<DiscoveryShell
|
|
title={t(data.keys.titleKey)}
|
|
subtitle={t(data.keys.subtitleKey)}
|
|
icon={data.mode === "search" ? <FeatherSearch className={PAGE_HEADER_ICON_CLASS} /> : <FeatherAtSign className={PAGE_HEADER_ICON_CLASS} />}
|
|
actions={
|
|
<ButtonGroup align="end">
|
|
<Button variant="tertiary" onClick={() => actions.onSwitchMode(switchHref)}>
|
|
{t(switchLabelKey)}
|
|
</Button>
|
|
<Button variant="tertiary" icon={<FeatherChevronLeft />} onClick={() => actions.onBack(data.hrefs.chat)}>
|
|
{t("chat.messageDiscovery.actions.back")}
|
|
</Button>
|
|
</ButtonGroup>
|
|
}
|
|
>
|
|
{data.mode === "search" ? (
|
|
<SearchInput
|
|
fillContainer
|
|
hideShortcut
|
|
autoFocus
|
|
placeholder={t("chat.messageDiscovery.search.placeholder")}
|
|
value={data.query}
|
|
onChange={(event) => actions.onQueryChange(event.target.value)}
|
|
data-ui-id="ui.chat.messageDiscovery.search"
|
|
/>
|
|
) : null}
|
|
|
|
{data.rows.length === 0 ? (
|
|
<EmptyState
|
|
outlined
|
|
variant={!isEmptySearch && data.mode === "search" ? "no-results" : "default"}
|
|
title={t(emptyTitleKey)}
|
|
body={t(emptyBodyKey)}
|
|
data-ui-id="ui.chat.messageDiscovery.empty"
|
|
/>
|
|
) : (
|
|
<ButtonGroup align="stack" data-ui-id="ui.chat.messageDiscovery.results">
|
|
{data.rows.map((row) => (
|
|
<DiscoveryRow key={row.id} row={row} onOpen={actions.onOpenResult} />
|
|
))}
|
|
</ButtonGroup>
|
|
)}
|
|
|
|
{data.nextCursor && actions.onLoadMore ? (
|
|
<ButtonGroup align="center">
|
|
<Button variant="tertiary" onClick={() => void actions.onLoadMore?.(data.nextCursor!)}>
|
|
{t("shared.loadMore")}
|
|
</Button>
|
|
</ButtonGroup>
|
|
) : null}
|
|
</DiscoveryShell>
|
|
);
|
|
}
|
|
|
|
function DiscoveryRow({ row, onOpen }: { readonly row: ChatMessageDiscoveryRowVM; readonly onOpen: (row: ChatMessageDiscoveryRowVM) => void }) {
|
|
const { t } = useTranslation();
|
|
const formatters = useLocaleFormatters();
|
|
return (
|
|
<Card
|
|
role="link"
|
|
tabIndex={0}
|
|
aria-label={t("chat.messageDiscovery.result.openAria", { channel: row.channelTitle, author: row.authorLabel })}
|
|
onClick={() => onOpen(row)}
|
|
onKeyDown={(event) => {
|
|
if (event.key !== "Enter" && event.key !== " ") return;
|
|
event.preventDefault();
|
|
onOpen(row);
|
|
}}
|
|
data-message-id={row.id}
|
|
>
|
|
<ButtonGroup align="space-between">
|
|
<ButtonGroup>
|
|
<Avatar size="small" image={row.authorAvatar.imageUrl ?? undefined}>
|
|
{row.authorAvatar.fallbackLabel}
|
|
</Avatar>
|
|
<BadgeFeedback farbe="ghost-neutral" size="large">
|
|
{row.authorLabel}
|
|
</BadgeFeedback>
|
|
<BadgeFeedback farbe="subtle-neutral" size="small">
|
|
{row.channelTitle}
|
|
</BadgeFeedback>
|
|
<BadgeFeedback farbe="ghost-neutral" size="small">
|
|
{t(row.channelKindLabelKey)}
|
|
</BadgeFeedback>
|
|
{row.isThreadReply ? (
|
|
<BadgeFeedback farbe="ghost-neutral" size="small">
|
|
{t("chat.messageDiscovery.result.threadReply")}
|
|
</BadgeFeedback>
|
|
) : null}
|
|
</ButtonGroup>
|
|
<BadgeFeedback farbe="ghost-neutral" size="small">
|
|
{formatters.date(row.createdAtIso, { dateStyle: "medium", timeStyle: "short" })}
|
|
</BadgeFeedback>
|
|
</ButtonGroup>
|
|
<ButtonGroup>
|
|
<span>{row.snippet}</span>
|
|
</ButtonGroup>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
function DiscoveryShell({
|
|
title,
|
|
subtitle,
|
|
icon = <FeatherMessageSquare className={PAGE_HEADER_ICON_CLASS} />,
|
|
actions,
|
|
children,
|
|
}: {
|
|
readonly title: string;
|
|
readonly subtitle?: string;
|
|
readonly icon?: React.ReactNode;
|
|
readonly actions?: React.ReactNode;
|
|
readonly children: React.ReactNode;
|
|
}) {
|
|
return (
|
|
<PageShell uiId="ui.chat.messageDiscovery" className="mobile:[&_button]:min-h-11 mobile:[&_button]:min-w-11">
|
|
<PageHeader icon={icon} title={title} subtitle={subtitle} actions={actions} />
|
|
<PageBody>{children}</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|