Files
supabase/apps/docs/components/SharedData.tsx
Charis 33b9e1ed29 chore(docs): convert all scripts to esm (#35996)
Scripts currently use CJS, which is causing a bit of a mess when trying
to use shared utilities from the app. Converting everything to ESM so
there are fewer conflicts when adding new scripts going forward.
2025-05-29 15:44:55 -04:00

37 lines
1.2 KiB
TypeScript

import { at } from 'lodash-es'
import { ReactNode } from 'react'
import { config, logConstants } from 'shared-data'
const sharedData = {
config,
logConstants,
}
/**
* A wrapper component to access data from the `shared-data` package within MDX
* files.
*
* @param children - How to access the shared data. If it is a render function,
* it takes the data object as a param. If it is a string, it
* takes a path through the data object, formatted like
* `a[0].b.c`. This path should lead to either a renderable
* type or a nested object. If it leads to an object, the
* return value is `${object.value} ${object.unit}`.
*/
function SharedData({
data,
children,
}: {
data: keyof typeof sharedData
children: ((selectedData: (typeof sharedData)[keyof typeof sharedData]) => ReactNode) | string
}) {
let selectedData = sharedData[data] as any
return typeof children === 'string'
? ((typeof (selectedData = at(selectedData, [children])[0]) === 'object'
? `${selectedData.value ?? ''} ${selectedData.unit ?? ''}`.trim()
: selectedData) as unknown as ReactNode)
: children(selectedData)
}
export { SharedData }