| 31 | } |
| 32 | |
| 33 | export function Section({ |
| 34 | children, |
| 35 | collapsible = false, |
| 36 | defaultOpen = true, |
| 37 | sectionKey, |
| 38 | className, |
| 39 | showTopBorder = false, |
| 40 | showBottomBorder = true, |
| 41 | }: SectionProps) { |
| 42 | const cached = sectionKey ? sectionExpandedCache.get(sectionKey) : undefined; |
| 43 | const [isOpen, setIsOpen] = useState(cached ?? defaultOpen); |
| 44 | |
| 45 | useEffect(() => { |
| 46 | if (!sectionKey) return; |
| 47 | if ( |
| 48 | process.env.NODE_ENV !== "production" && |
| 49 | mountedSectionKeys.has(sectionKey) |
| 50 | ) { |
| 51 | console.error( |
| 52 | `[Section] duplicate sectionKey mounted simultaneously: "${sectionKey}"`, |
| 53 | ); |
| 54 | } |
| 55 | mountedSectionKeys.add(sectionKey); |
| 56 | return () => { |
| 57 | mountedSectionKeys.delete(sectionKey); |
| 58 | }; |
| 59 | }, [sectionKey]); |
| 60 | |
| 61 | const toggle = () => { |
| 62 | const next = !isOpen; |
| 63 | setIsOpen(next); |
| 64 | if (sectionKey) sectionExpandedCache.set(sectionKey, next); |
| 65 | }; |
| 66 | |
| 67 | return ( |
| 68 | <SectionCtx.Provider value={{ isOpen, toggle, collapsible }}> |
| 69 | <div |
| 70 | className={cn( |
| 71 | "flex flex-col", |
| 72 | showTopBorder && "border-t first:border-t-0", |
| 73 | showBottomBorder && "border-b", |
| 74 | className, |
| 75 | )} |
| 76 | > |
| 77 | {children} |
| 78 | </div> |
| 79 | </SectionCtx.Provider> |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | interface SectionHeaderProps { |
| 84 | children?: React.ReactNode; |