import { Box, CircularProgress, Stack, StackProps, Text, TextProps, useColorModeValue } from "@chakra-ui/react"; import { MessageTableEntry } from "src/components/Messages/MessageTableEntry"; import { get } from "src/lib/api"; import { Message } from "src/types/Conversation"; import useSWRImmutable from "swr/immutable"; const MessageHeaderProps: TextProps = { fontSize: "xl", fontWeight: "bold", fontFamily: "Inter", py: "2", }; const MessageStackProps: StackProps = { spacing: "2", alignItems: "start", justifyContent: "center", }; interface MessageWithChildrenProps { id: string; depth?: number; maxDepth: number; isOnlyChild?: boolean; } export function MessageWithChildren(props: MessageWithChildrenProps) { const backgroundColor = useColorModeValue("white", "gray.800"); const childBackgroundColor = useColorModeValue("gray.200", "gray.700"); const { id, depth = 0, maxDepth, isOnlyChild = true } = props; const { isLoading, data: message } = useSWRImmutable(`/api/messages/${id}`, get); const { isLoading: isLoadingChildren, data: children = [] } = useSWRImmutable( `/api/messages/${id}/children`, get ); const renderRecursive = depth < maxDepth || depth === 0; const isFirst = depth === 0; const isFirstOrOnly = isFirst || !!isOnlyChild; if (isLoading || isLoadingChildren) { return ; } return ( <> {message && ( <> {isFirst ? "Message" : depth === 1 ? "Children" : "Ancestor"} )} {children.length > 0 && (renderRecursive ? ( {children.map((item) => ( ))} ) : ( <> {isFirstOrOnly ? "Children" : "Ancestor"} {children.map((item, idx) => ( ))} ))} ); }