11import { ClientOnly } from '@tanstack/react-router'
22import * as React from 'react'
33import { ChartsCatalogPreview } from './ChartsCatalogPreview'
4+ import { Button } from '~/components/ds/ui'
45
56const LazyChartsCatalogDocExample = React . lazy ( ( ) =>
67 import ( './ChartsCatalogDocExample.client' ) . then ( ( module ) => ( {
@@ -19,45 +20,30 @@ export function ChartsCatalogDocExample({
1920 source ?: 'hidden' | 'collapsed' | 'expanded'
2021 title ?: string
2122} ) {
22- const containerRef = React . useRef < HTMLElement > ( null )
23- const [ shouldLoad , setShouldLoad ] = React . useState ( false )
23+ const [ activation , setActivation ] = React . useState < {
24+ caseId : string
25+ edit : boolean
26+ } > ( )
27+ const isActive = activation ?. caseId === caseId
2428 const fallback = (
2529 < ChartsCatalogDocExampleFallback
2630 caseId = { caseId }
2731 height = { height }
32+ onEdit = { ( ) => setActivation ( { caseId, edit : true } ) }
33+ onRun = { ( ) => setActivation ( { caseId, edit : false } ) }
2834 source = { source }
2935 title = { title }
3036 />
3137 )
3238
33- React . useEffect ( ( ) => {
34- const container = containerRef . current
35- if ( shouldLoad || ! container ) return
36-
37- if ( ! ( 'IntersectionObserver' in window ) ) {
38- setShouldLoad ( true )
39- return
40- }
41-
42- const observer = new IntersectionObserver (
43- ( entries ) => {
44- if ( ! entries . some ( ( entry ) => entry . isIntersecting ) ) return
45- setShouldLoad ( true )
46- observer . disconnect ( )
47- } ,
48- { rootMargin : '320px 0px' } ,
49- )
50- observer . observe ( container )
51- return ( ) => observer . disconnect ( )
52- } , [ shouldLoad ] )
53-
5439 return (
55- < section ref = { containerRef } className = "not-prose my-5" >
56- { shouldLoad ? (
40+ < section className = "not-prose my-5" >
41+ { isActive ? (
5742 < ClientOnly fallback = { fallback } >
5843 < React . Suspense fallback = { fallback } >
5944 < LazyChartsCatalogDocExample
6045 caseId = { caseId }
46+ edit = { activation . edit }
6147 fallback = { fallback }
6248 height = { height }
6349 source = { source }
@@ -75,11 +61,15 @@ export function ChartsCatalogDocExample({
7561function ChartsCatalogDocExampleFallback ( {
7662 caseId,
7763 height,
64+ onEdit,
65+ onRun,
7866 source,
7967 title,
8068} : {
8169 caseId : string
8270 height : number
71+ onEdit : ( ) => void
72+ onRun : ( ) => void
8373 source : 'hidden' | 'collapsed' | 'expanded'
8474 title ?: string
8575} ) {
@@ -90,21 +80,27 @@ function ChartsCatalogDocExampleFallback({
9080 className = "overflow-hidden rounded-lg border border-border-default bg-background-default"
9181 data-chart-example = { caseId }
9282 >
93- { source !== 'hidden' ? (
94- < div className = "flex min-h-10 items-center justify-between gap-3 border-b border-border-default px-3" >
83+ < div
84+ className = { `flex min-h-10 items-center gap-3 border-b border-border-default px-2 ${ source === 'hidden' ? 'justify-end' : 'justify-between pl-3' } ` }
85+ >
86+ { source !== 'hidden' ? (
9587 < span className = "min-w-0 truncate font-ds-mono text-xs text-text-muted" >
9688 { label }
9789 </ span >
98- < a
99- className = "shrink-0 text-xs font-medium text-text-secondary hover:text-text-primary"
100- href = { `/charts/catalog/charts/${ caseId } ` }
101- >
102- Open example
103- </ a >
90+ ) : null }
91+ < div className = "flex shrink-0 items-center gap-1" >
92+ { source !== 'hidden' ? (
93+ < Button type = "button" variant = "ghost" size = "xs" onClick = { onEdit } >
94+ Edit
95+ </ Button >
96+ ) : null }
97+ < Button type = "button" variant = "primary" size = "xs" onClick = { onRun } >
98+ Run
99+ </ Button >
104100 </ div >
105- ) : null }
101+ </ div >
106102 < div aria-label = { `${ label } chart preview` } role = "img" style = { { height } } >
107- < ChartsCatalogPreview caseId = { caseId } className = "p-8" family = "" />
103+ < ChartsCatalogPreview caseId = { caseId } />
108104 </ div >
109105 </ div >
110106 )
0 commit comments