57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import React, { type PropsWithChildren, isValidElement } from "react";
|
|
|
|
import { isEmptyValue } from "../../utils/attribute-presentation";
|
|
import { AccordionSection } from "./AccordionSection";
|
|
import { useSettingsStore } from "@/store/settings.store";
|
|
|
|
export interface SectionCardProps {
|
|
title: string;
|
|
defaultExpanded?: boolean;
|
|
}
|
|
|
|
function hasRenderableContent(node: React.ReactNode): boolean {
|
|
if (node === null || node === undefined || node === false) return false;
|
|
if (typeof node === "string") return node.trim().length > 0;
|
|
if (typeof node === "number" || node === true) return true;
|
|
if (Array.isArray(node)) return node.some(hasRenderableContent);
|
|
|
|
if (isValidElement(node)) {
|
|
const props = node.props as Record<string, unknown>;
|
|
|
|
if (Array.isArray(props.items)) return !isEmptyValue(props.items);
|
|
if (Array.isArray(props.rows)) {
|
|
if (props.rows.length === 0) return false;
|
|
return (props.rows as Array<Record<string, unknown>>).some((row) =>
|
|
Object.entries(row).some(([key, value]) => key !== "label" && !isEmptyValue(value)),
|
|
);
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(props, "value")) {
|
|
return !isEmptyValue(props.value);
|
|
}
|
|
return hasRenderableContent(props.children as React.ReactNode);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
export function SectionCard({
|
|
title,
|
|
defaultExpanded,
|
|
children,
|
|
}: PropsWithChildren<SectionCardProps>) {
|
|
if (!hasRenderableContent(children)) return null;
|
|
|
|
const accordionDefaultExpanded = useSettingsStore(
|
|
(state) => state.accordionDefaultExpanded
|
|
);
|
|
|
|
const isExpanded =
|
|
defaultExpanded !== undefined ? defaultExpanded : accordionDefaultExpanded;
|
|
|
|
return (
|
|
<AccordionSection title={title} defaultExpanded={isExpanded}>
|
|
{children}
|
|
</AccordionSection>
|
|
);
|
|
}
|