Skip to content

Commit 783b715

Browse files
authored
fix(ref docs): make unions more intuitive (supabase#33332)
Change the description for unions to make it clearer what is happening + auto-expand them
1 parent ff25c9c commit 783b715

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

apps/docs/features/docs/Reference.ui.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ function ParamOrTypeDetails({ paramOrType }: { paramOrType: object }) {
204204
? getSubDetails(paramOrType)
205205
: undefined
206206

207+
const defaultOpen = isDefaultExpanded(paramOrType)
208+
207209
return (
208210
<>
209211
<div className="flex flex-wrap items-baseline gap-3">
@@ -228,7 +230,9 @@ function ParamOrTypeDetails({ paramOrType }: { paramOrType: object }) {
228230
<MDXRemoteBase source={description} customPreprocess={normalizeMarkdown} />
229231
</div>
230232
)}
231-
{subContent && subContent.length > 0 && <TypeSubDetails details={subContent} />}
233+
{subContent && subContent.length > 0 && (
234+
<TypeSubDetails details={subContent} defaultOpen={defaultOpen} />
235+
)}
232236
</>
233237
)
234238
}
@@ -240,6 +244,7 @@ export function ReturnTypeDetails({ returnType }: { returnType: MethodTypes['ret
240244
if (isNameOnlyType) return
241245

242246
const subContent = getSubDetails(returnType)
247+
const isDefaultOpen = isDefaultExpanded(returnType)
243248

244249
return (
245250
<div>
@@ -254,7 +259,9 @@ export function ReturnTypeDetails({ returnType }: { returnType: MethodTypes['ret
254259
/>
255260
</div>
256261
)}
257-
{subContent && subContent.length > 0 && <TypeSubDetails details={subContent} />}
262+
{subContent && subContent.length > 0 && (
263+
<TypeSubDetails defaultOpen={isDefaultOpen} details={subContent} />
264+
)}
258265
</div>
259266
</div>
260267
)
@@ -263,12 +270,14 @@ export function ReturnTypeDetails({ returnType }: { returnType: MethodTypes['ret
263270
function TypeSubDetails({
264271
details,
265272
className,
273+
defaultOpen = false,
266274
}: {
267275
details: Array<SubContent> | Array<CustomTypePropertyType> | Array<TypeDetails>
268276
className?: string
277+
defaultOpen?: boolean
269278
}) {
270279
return (
271-
<Collapsible_Shadcn_>
280+
<Collapsible_Shadcn_ defaultOpen={defaultOpen}>
272281
<CollapsibleTrigger_Shadcn_
273282
className={cn(
274283
'group',
@@ -616,7 +625,7 @@ function getTypeName(parameter: object): string {
616625
// @ts-ignore
617626
return `Promise<${getTypeName({ type: type.awaited })}>`
618627
case 'union':
619-
return 'Union: expand to see options'
628+
return 'One of the following options'
620629
case 'index signature':
621630
// Needs an extra level of wrapping to fake the wrapping parameter
622631
// @ts-ignore
@@ -667,15 +676,15 @@ function getSubDetails(parentType: MethodTypes['params'][number] | MethodTypes['
667676
break
668677
case 'union':
669678
subDetails = parentType.type.subTypes.map((subType, index) => ({
670-
name: `union option ${index + 1}`,
679+
name: `Option ${index + 1}`,
671680
type: { ...subType },
672681
isOptional: 'NA',
673682
}))
674683
break
675684
case 'promise':
676685
if (parentType.type.awaited.type === 'union') {
677686
subDetails = parentType.type.awaited.subTypes.map((subType, index) => ({
678-
name: `union option ${index + 1}`,
687+
name: `Option ${index + 1}`,
679688
type: { ...subType },
680689
isOptional: 'NA',
681690
}))
@@ -693,7 +702,7 @@ function getSubDetails(parentType: MethodTypes['params'][number] | MethodTypes['
693702
case 'array':
694703
if (parentType.type.elemType?.type === 'union') {
695704
subDetails = parentType.type.elemType.subTypes.map((subType, index) => ({
696-
name: `union option ${index + 1}`,
705+
name: `Option ${index + 1}`,
697706
type: { ...subType },
698707
isOptional: 'NA',
699708
}))
@@ -871,3 +880,17 @@ function applyParameterMergeStrategy(
871880
}
872881
}
873882
}
883+
884+
function isDefaultExpanded(meta: object) {
885+
return (
886+
'type' in meta &&
887+
typeof meta.type === 'object' &&
888+
'type' in meta.type &&
889+
(meta.type.type == 'union' ||
890+
(meta.type.type === 'promise' &&
891+
'awaited' in meta.type &&
892+
typeof meta.type.awaited === 'object' &&
893+
'type' in meta.type.awaited &&
894+
meta.type.awaited.type === 'union'))
895+
)
896+
}

0 commit comments

Comments
 (0)