import React, { useState } from 'react' import { API_ENDPOINT } from '@app/configs' import { GrCopy } from 'react-icons/gr' const methodColor = (method: string, t: 'color' | 'border') => { let c = '' switch (method) { case 'POST': { c = t === 'color' ? 'text-green-700' : 'border-green-700' } break case 'GET': { c = t === 'color' ? 'text-blue-700' : 'border-blue-700' } break case 'PUT': { c = t === 'color' ? 'text-yellow-700' : 'border-yellow-700' } break case 'DELETE': { c = t === 'color' ? 'text-red-700' : 'border-red-700' } break default: { c = t === 'color' ? 'text-blue-700' : 'border-blue-700' } break } return c } type ApiCellProps = { route?: any keyVisible?: boolean token?: string // api token to prefill curls, id?: string // id to use for dom, } // API doc collaspe cell with copy curl export const ApiCell = ({ route, keyVisible, token, id }: ApiCellProps) => { const [sectionVisible, setSectionVisible] = useState(false) const onToggleSection = () => { setSectionVisible((e) => !e) } const routeParams = route?.params const params = routeParams ? Object.keys(routeParams) : null const curlCommand = `curl --location --request ${ route.method ?? 'POST' } '${API_ENDPOINT}/${route.pathName}' \ --header 'Authorization: ${keyVisible ? token : '$A11YWATCH_TOKEN'}' \ ${ route.method === 'POST' ? `--header 'Content-Type: application/json' ` : '' } \ ${route.encodedParams}` const copyText = ( e: React.MouseEvent ): void => { e?.preventDefault() navigator.clipboard.writeText(curlCommand) } const targetCopyID = id + 'copy-text' return (
  • {API_ENDPOINT}/{route.pathName}

    {route.method}
    {params ? (
    {params?.map((item: any, i) => { const { desc, type, optional } = routeParams[item] ?? { desc: '', type: '', optional: false, } return ( {item} {type} {desc} {optional ? 'Optional' : 'Required'} ) })}
    ) : null}
    {curlCommand}
    Copy the curl command to clipboard
  • ) }