6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
184 lines
5.9 KiB
TypeScript
184 lines
5.9 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useRef, useState } from 'react'
|
|
import { ChevronDown } from '@sim/emcn/icons'
|
|
import { cn } from '@/lib/utils'
|
|
|
|
interface ResponseSectionProps {
|
|
children: React.ReactNode
|
|
}
|
|
|
|
export function ResponseSection({ children }: ResponseSectionProps) {
|
|
const containerRef = useRef<HTMLDivElement>(null)
|
|
const [statusCodes, setStatusCodes] = useState<string[]>([])
|
|
const [selectedCode, setSelectedCode] = useState<string>('')
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
const dropdownRef = useRef<HTMLDivElement>(null)
|
|
|
|
function getAccordionItems() {
|
|
const root = containerRef.current?.querySelector('[data-orientation="vertical"]')
|
|
if (!root) return []
|
|
return Array.from(root.children).filter(
|
|
(el) => el.getAttribute('data-state') !== null
|
|
) as HTMLElement[]
|
|
}
|
|
|
|
function showStatusCode(code: string) {
|
|
const items = getAccordionItems()
|
|
for (const item of items) {
|
|
const triggerBtn = item.querySelector('h3 button') as HTMLButtonElement | null
|
|
const text = triggerBtn?.textContent?.trim() ?? ''
|
|
const itemCode = text.match(/^\d{3}/)?.[0]
|
|
|
|
if (itemCode === code) {
|
|
item.style.display = ''
|
|
if (item.getAttribute('data-state') === 'closed' && triggerBtn) {
|
|
triggerBtn.click()
|
|
}
|
|
} else {
|
|
item.style.display = 'none'
|
|
if (item.getAttribute('data-state') === 'open' && triggerBtn) {
|
|
triggerBtn.click()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Detect when the fumadocs accordion children mount via MutationObserver,
|
|
* then extract status codes and show the first one.
|
|
* Replaces the previous approach that used `children` as a dependency
|
|
* (which triggered on every render since children is a new object each time).
|
|
*/
|
|
useEffect(() => {
|
|
const container = containerRef.current
|
|
if (!container) return
|
|
|
|
const initialize = () => {
|
|
const items = getAccordionItems()
|
|
if (items.length === 0) return false
|
|
|
|
const codes: string[] = []
|
|
const seen = new Set<string>()
|
|
|
|
for (const item of items) {
|
|
const triggerBtn = item.querySelector('h3 button')
|
|
if (triggerBtn) {
|
|
const text = triggerBtn.textContent?.trim() ?? ''
|
|
const code = text.match(/^\d{3}/)?.[0]
|
|
if (code && !seen.has(code)) {
|
|
seen.add(code)
|
|
codes.push(code)
|
|
}
|
|
}
|
|
}
|
|
|
|
if (codes.length > 0) {
|
|
setStatusCodes(codes)
|
|
setSelectedCode(codes[0])
|
|
showStatusCode(codes[0])
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
if (initialize()) return
|
|
|
|
const observer = new MutationObserver(() => {
|
|
if (initialize()) {
|
|
observer.disconnect()
|
|
}
|
|
})
|
|
observer.observe(container, { childList: true, subtree: true })
|
|
|
|
return () => observer.disconnect()
|
|
}, []) // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
function handleSelectCode(code: string) {
|
|
setSelectedCode(code)
|
|
setIsOpen(false)
|
|
showStatusCode(code)
|
|
}
|
|
|
|
useEffect(() => {
|
|
function handleClickOutside(event: MouseEvent) {
|
|
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
|
|
setIsOpen(false)
|
|
}
|
|
}
|
|
function handleKeyDown(event: KeyboardEvent) {
|
|
if (event.key === 'Escape') {
|
|
setIsOpen(false)
|
|
}
|
|
}
|
|
document.addEventListener('mousedown', handleClickOutside)
|
|
document.addEventListener('keydown', handleKeyDown)
|
|
return () => {
|
|
document.removeEventListener('mousedown', handleClickOutside)
|
|
document.removeEventListener('keydown', handleKeyDown)
|
|
}
|
|
}, [])
|
|
|
|
return (
|
|
<div ref={containerRef} className='response-section-wrapper'>
|
|
{statusCodes.length > 0 && (
|
|
<div className='response-section-header'>
|
|
<h2 className='response-section-title'>Response</h2>
|
|
<div className='response-section-meta'>
|
|
<div ref={dropdownRef} className='response-section-dropdown-wrapper'>
|
|
<button
|
|
type='button'
|
|
className='response-section-dropdown-trigger'
|
|
aria-haspopup='listbox'
|
|
aria-expanded={isOpen}
|
|
aria-label={`Response status code, currently ${selectedCode}`}
|
|
onClick={() => setIsOpen(!isOpen)}
|
|
>
|
|
<span>{selectedCode}</span>
|
|
<ChevronDown
|
|
className={cn(
|
|
'response-section-chevron',
|
|
isOpen && 'response-section-chevron-open'
|
|
)}
|
|
/>
|
|
</button>
|
|
{isOpen && (
|
|
<div className='response-section-dropdown-menu' role='listbox'>
|
|
{statusCodes.map((code) => (
|
|
<button
|
|
key={code}
|
|
type='button'
|
|
role='option'
|
|
aria-selected={code === selectedCode}
|
|
className={cn(
|
|
'response-section-dropdown-item',
|
|
code === selectedCode && 'response-section-dropdown-item-selected'
|
|
)}
|
|
onClick={() => handleSelectCode(code)}
|
|
>
|
|
<span>{code}</span>
|
|
{code === selectedCode && (
|
|
<svg
|
|
className='response-section-check'
|
|
viewBox='0 0 24 24'
|
|
fill='none'
|
|
stroke='currentColor'
|
|
strokeWidth='2'
|
|
>
|
|
<polyline points='20 6 9 17 4 12' />
|
|
</svg>
|
|
)}
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
<span className='response-section-content-type'>application/json</span>
|
|
</div>
|
|
</div>
|
|
)}
|
|
<div className='response-section-content'>{children}</div>
|
|
</div>
|
|
)
|
|
}
|