import React, { useState, useRef, useEffect, useContext } from "react" import { Flex, Box, Heading, Text, Button } from "theme-ui" import Method from "./method" import Parameters from "./parameters" import { convertToKebabCase } from "../../utils/convert-to-kebab-case" import EndpointContainer from "./endpoint-container" import Markdown from "react-markdown" import JsonContainer from "./json-container" import ResponsiveContainer from "./responsive-container" import Description from "./description" import NavigationContext from "../../context/navigation-context" import ChevronDown from "../icons/chevron-down" import useInView from "../../hooks/use-in-view" const Section = ({ data }) => { const { section } = data const [isExpanded, setIsExpanded] = useState(true) const { openSections, updateSection, updateMetadata } = useContext( NavigationContext ) const endpoints = section.paths .map(p => { let path = p.name let ep = [] p.methods.forEach(m => { ep.push({ method: m.method, endpoint: path }) }) return ep }) .flat() const sectionRef = useRef(null) const scrollIntoView = () => { if (sectionRef.current) { sectionRef.current.scrollIntoView({ behavior: "smooth", }) } } const handleExpand = () => { updateMetadata({ title: section.section_name, description: section.schema?.description, }) setIsExpanded(true) scrollIntoView() } useEffect(() => { setIsExpanded(false) }, []) useEffect(() => { const shouldOpen = openSections.includes( convertToKebabCase(section.section_name) ) if (shouldOpen) { setIsExpanded(true) } }, [section.section_name, openSections, openSections.length]) const [containerRef, isInView] = useInView({ root: null, rootMargin: "0px 0px -80% 0px", threshold: 1.0, }) useEffect(() => { const handleInView = () => { if (isInView) { updateSection(convertToKebabCase(section.section_name)) } } handleInView() // eslint-disable-next-line react-hooks/exhaustive-deps }, [isInView]) return (
{section.section_name} {section.schema?.description} {isExpanded && section.schema ? ( ) : null} {isExpanded ? ( ) : null} {!isExpanded ? ( ) : ( {section.paths.map((p, i) => { return ( {p.methods.map((m, i) => { return ( ) })} ) })} )}
) } export default Section