import {
ActionIcon,
Alert,
Badge,
Box,
Card,
CopyButton,
Group,
ScrollArea,
Text,
Tooltip,
} from "@mantine/core";
import {
IconArrowsMaximize,
IconCheck,
IconCopy,
IconHandStop,
IconInfoCircle,
} from "@tabler/icons-react";
import { PublishFunction } from "create-pubsub";
import { lazy, ReactNode, Suspense, useMemo, useState } from "react";
import { match } from "ts-pattern";
const FormattedMarkdown = lazy(() => import("./FormattedMarkdown"));
export default function AiResponseContent({
textGenerationState,
response,
setTextGenerationState,
}: {
textGenerationState: string;
response: string;
setTextGenerationState: PublishFunction<
| "failed"
| "awaitingSearchResults"
| "preparingToGenerate"
| "idle"
| "loadingModel"
| "generating"
| "interrupted"
| "completed"
>;
}) {
const [isScrollAreaEnabled, setScrollAreaEnabled] = useState(true);
const ConditionalScrollArea = useMemo(
() =>
({ children }: { children: ReactNode }) => {
return isScrollAreaEnabled ? (
{children}
) : (
{children}
);
},
[isScrollAreaEnabled],
);
return (
{match(textGenerationState)
.with("generating", () => "Generating AI Response...")
.otherwise(() => "AI Response")}
{match(textGenerationState)
.with("interrupted", () => (
Interrupted
))
.otherwise(() => null)}
{match(textGenerationState)
.with("generating", () => (
setTextGenerationState("interrupted")}
variant="subtle"
color="gray"
>
))
.otherwise(() => null)}
{isScrollAreaEnabled && (
setScrollAreaEnabled(false)}
variant="subtle"
color="gray"
>
)}
{({ copied, copy }) => (
{copied ? : }
)}
{response}
{match(textGenerationState)
.with("failed", () => (
}
>
Could not generate response. It's possible that your browser or
your system is out of memory.
))
.otherwise(() => null)}
);
}