-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support undiscriminated unions (#168)
* Support undiscriminated unions * Sort undiscriminated union variants * Add icons for undiscriminated union variant types * Upgrade @fern-fern/registry-browser * Use the real name of variant * Improve icons
- Loading branch information
Showing
9 changed files
with
167 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file renamed
BIN
+395 KB
...23.1-6-gd18235e-0ea7ee19f5-22f27c0fc4.zip → ...26.0-1-g7cf6a78-a1a0540c22-57d13218d2.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
packages/ui/app/src/api-page/types/undiscriminated-union/UndiscriminatedUnionVariant.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import * as FernRegistryApiRead from "@fern-fern/registry-browser/api/resources/api/resources/v1/resources/read"; | ||
import classNames from "classnames"; | ||
import { useCallback } from "react"; | ||
import { ApiPageDescription } from "../../ApiPageDescription"; | ||
import { | ||
TypeDefinitionContext, | ||
TypeDefinitionContextValue, | ||
useTypeDefinitionContext, | ||
} from "../context/TypeDefinitionContext"; | ||
import { InternalTypeReferenceDefinitions } from "../type-reference/InternalTypeReferenceDefinitions"; | ||
import { TypeShorthand } from "../type-shorthand/TypeShorthand"; | ||
|
||
type IconInfo = { | ||
content: string; | ||
size: number; | ||
}; | ||
|
||
function getIconInfoForPrimitiveType(type: FernRegistryApiRead.PrimitiveType): IconInfo { | ||
switch (type.type) { | ||
case "integer": | ||
case "long": | ||
case "double": | ||
return type.type === "double" | ||
? { | ||
content: "1.2", | ||
size: 6, | ||
} | ||
: { | ||
content: "123", | ||
size: 6, | ||
}; | ||
case "boolean": | ||
return { content: "true", size: 6 }; | ||
case "uuid": | ||
case "date": | ||
case "datetime": | ||
case "base64": | ||
case "string": | ||
return { content: "abc", size: 6 }; | ||
} | ||
} | ||
|
||
function getIconInfoForTypeReference(typeRef: FernRegistryApiRead.TypeReference): IconInfo | null { | ||
switch (typeRef.type) { | ||
case "id": | ||
return null; | ||
case "primitive": | ||
return getIconInfoForPrimitiveType(typeRef.value); | ||
case "map": | ||
return { content: "{}", size: 9 }; | ||
case "literal": | ||
return { content: "!", size: 9 }; | ||
case "list": | ||
case "set": | ||
case "optional": | ||
return getIconInfoForTypeReference(typeRef.itemType); | ||
case "unknown": | ||
return { content: "{}", size: 9 }; | ||
} | ||
} | ||
|
||
function getIconForTypeReference(typeRef: FernRegistryApiRead.TypeReference): JSX.Element | null { | ||
const info = getIconInfoForTypeReference(typeRef); | ||
if (info == null) { | ||
return null; | ||
} | ||
const { content, size } = info; | ||
return ( | ||
<div | ||
className="border-border-default-light dark:border-border-default-dark flex h-6 w-6 items-center justify-center rounded border" | ||
style={{ fontSize: size }} | ||
> | ||
{content} | ||
</div> | ||
); | ||
} | ||
|
||
export declare namespace UndiscriminatedUnionVariant { | ||
export interface Props { | ||
unionVariant: FernRegistryApiRead.UndiscriminatedUnionVariant; | ||
anchorIdParts: string[]; | ||
} | ||
} | ||
|
||
export const UndiscriminatedUnionVariant: React.FC<UndiscriminatedUnionVariant.Props> = ({ | ||
unionVariant, | ||
anchorIdParts, | ||
}) => { | ||
const { isRootTypeDefinition } = useTypeDefinitionContext(); | ||
const contextValue = useTypeDefinitionContext(); | ||
const newContextValue = useCallback( | ||
(): TypeDefinitionContextValue => ({ | ||
...contextValue, | ||
jsonPropertyPath: [...contextValue.jsonPropertyPath], | ||
}), | ||
[contextValue] | ||
); | ||
|
||
return ( | ||
<div | ||
className={classNames("flex flex-col py-3", { | ||
"px-3": !isRootTypeDefinition, | ||
})} | ||
> | ||
<div className="flex flex-col"> | ||
<div className="t-muted flex items-center space-x-2.5"> | ||
{getIconForTypeReference(unionVariant.type)} | ||
{unionVariant.displayName == null ? null : ( | ||
<span className="t-primary text-sm">{unionVariant.displayName}</span> | ||
)} | ||
<span className="t-muted text-xs"> | ||
<TypeShorthand type={unionVariant.type} plural={false} /> | ||
</span> | ||
</div> | ||
<ApiPageDescription className="mt-2" description={unionVariant.description} isMarkdown /> | ||
<TypeDefinitionContext.Provider value={newContextValue}> | ||
<InternalTypeReferenceDefinitions | ||
type={unionVariant.type} | ||
anchorIdParts={anchorIdParts} | ||
isCollapsible | ||
/> | ||
</TypeDefinitionContext.Provider> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { capitalize, snakeCase } from "lodash-es"; | ||
|
||
export function toTitleCase(str: string): string { | ||
return snakeCase(str).split("_").map(capitalize).join(" "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
02201c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
fern-dev – ./packages/ui/fe-bundle
devtest.buildwithfern.com
fern-dev-git-main-buildwithfern.vercel.app
app-dev.buildwithfern.com
fern-dev-buildwithfern.vercel.app