'use client' import { ReferenceContentInitiallyScrolledContext } from '~/features/docs/Reference.navigation.client' import { safeHistoryReplaceState } from '~/lib/historyUtils' import type { HTMLAttributes, PropsWithChildren } from 'react' import { useContext, useEffect, useRef, useState } from 'react' import { useInView } from 'react-intersection-observer' import { cn, Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from 'ui' import { type IApiEndPoint } from './Reference.api.utils' import { API_REFERENCE_REQUEST_BODY_SCHEMA_DATA_ATTRIBUTES } from './Reference.ui.shared' /** * Wrap a reference section with client-side functionality: * * - Intersection observer to auto-update the URL when the user scrolls the page * - An ID to scroll to programmatically. This is on the entire section rather * than the heading to avoid problems with scroll-to position when the heading * is sticky. */ export function ReferenceSectionWrapper({ id, link, children, className, }: PropsWithChildren<{ id: string; link: string; className?: string }> & HTMLAttributes) { const initialScrollHappened = useContext(ReferenceContentInitiallyScrolledContext) const { ref } = useInView({ threshold: 0, rootMargin: '-10% 0% -50% 0%', onChange: (inView) => { if ( inView && initialScrollHappened && window.scrollY > 0 /* Don't update on first navigation to introduction */ ) { safeHistoryReplaceState(link) } }, }) return (
{children}
) } export function ApiOperationBodySchemeSelector({ requestBody, className, }: { requestBody: IApiEndPoint['requestBody'] className?: string }) { const availableSchemes = Object.keys(requestBody?.content || {}) as Array< 'application/json' | 'application/x-www-form-urlencoded' > const [selectedScheme, setSelectedScheme] = useState(availableSchemes[0]) const containerRef = useRef(null) const allSchemeDetails = useRef([]) useEffect(() => { const elements = containerRef.current?.querySelectorAll( `[${API_REFERENCE_REQUEST_BODY_SCHEMA_DATA_ATTRIBUTES.KEY}]` ) allSchemeDetails.current = elements ? (Array.from(elements) as HTMLUListElement[]) : [] }, []) useEffect(() => { allSchemeDetails.current?.forEach((schemeDetails) => { schemeDetails.hidden = schemeDetails.getAttribute(API_REFERENCE_REQUEST_BODY_SCHEMA_DATA_ATTRIBUTES.KEY) !== selectedScheme }) }, [selectedScheme]) return (

Body

) }