Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(webhooks): add a WebhookExampleSegmentedControl for multiple examples #1952

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fern/apis/fdr/definition/api/v1/read/webhook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ types:

ExampleWebhookPayload:
properties:
name: optional<string>
payload: unknown
1 change: 1 addition & 0 deletions fern/apis/fdr/definition/api/v1/register/webhook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ types:

ExampleWebhookPayload:
properties:
name: optional<string>
payload: unknown

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function generateWebhookExample({
return typeDefinition;
};
return {
name: undefined,
payload: generateWebhookPayloadExample(
webhookDefinition.payload.type,
resolveTypeById
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ApiDefinition from "@fern-api/fdr-sdk/api-definition";
import * as FernNavigation from "@fern-api/fdr-sdk/navigation";
import cn from "clsx";
import { memo, useCallback, useRef } from "react";
import { memo, useCallback, useRef, useState } from "react";
import { FernBreadcrumbs } from "../../components/FernBreadcrumbs";
import { useHref } from "../../hooks/useHref";
import { Markdown } from "../../mdx/Markdown";
Expand All @@ -14,6 +14,7 @@ import { WebhookPayloadSection } from "./WebhookPayloadSection";
import { WebhookResponseSection } from "./WebhookResponseSection";
import { useWebhookContext } from "./webhook-context/useWebhookContext";
import { WebhookExample } from "./webhook-examples/WebhookExample";
import { WebhookExampleSegmentedControl } from "./webhook-examples/WebhookExampleSegementedControl";

export declare namespace WebhookContent {
export interface Props {
Expand Down Expand Up @@ -41,7 +42,10 @@ export const WebhookContent = memo<WebhookContent.Props>((props) => {
[setHoveredPayloadPropertyPath]
);

const example = webhook.examples?.[0]; // TODO: Need a way to show all the examples
const [selectedExampleIndex, setSelectedExampleIndex] = useState(0);
const selectedExample = webhook.examples?.[selectedExampleIndex];

const example = selectedExample; // TODO: Need a way to show all the examples

const webhookExample = example ? <WebhookExample example={example} /> : null;

Expand Down Expand Up @@ -130,22 +134,21 @@ export const WebhookContent = memo<WebhookContent.Props>((props) => {
</div>
</div>
</div>
<div
className={cn(
"max-w-content-width",
"top-header-offset sticky flex-1 self-start",
// the py-10 is the same as the 40px below
"pb-10 pt-8",
// the 4rem is the same as the h-10 as the Header
"max-h-content",
// hide on mobile,
"hidden md:flex"
<div className="fern-endpoint-code-snippets">
{(webhook?.examples ?? []).length > 1 && (
<WebhookExampleSegmentedControl
segmentedControlExamples={(webhook?.examples ?? []).map(
(example, index) => ({
exampleKey: String(index),
example,
})
)}
selectedExample={selectedExample}
onSelectExample={(exampleKey) => {
setSelectedExampleIndex(Number(exampleKey));
}}
/>
)}
>
{webhookExample}
</div>

<div className="mt-10 flex max-h-[150vh] md:mt-0 md:hidden">
{webhookExample}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { FernButton, FernButtonGroup } from "@fern-docs/components";
import { ReactElement } from "react";
import { ExampleWebhookPayload } from "../../../../../../fdr-sdk/src/client/APIV1Read";

export function WebhookExampleSegmentedControl({
segmentedControlExamples,
selectedExample,
onSelectExample,
}: {
segmentedControlExamples: {
exampleKey: string;
example: ExampleWebhookPayload;
}[];
selectedExample: ExampleWebhookPayload | undefined;
onSelectExample: (exampleKey: string) => void;
}): ReactElement {
return (
<FernButtonGroup className="min-w-0 shrink">
{segmentedControlExamples.map(({ exampleKey, example }) => {
return (
<FernButton
key={exampleKey}
rounded={true}
onClick={() => {
onSelectExample(exampleKey);
}}
className="min-w-0 shrink truncate"
mono
size="small"
variant={exampleKey === example?.name ? "outlined" : "minimal"}
intent={exampleKey === selectedExample?.name ? "primary" : "none"}
>
{exampleKey}
</FernButton>
);
})}
</FernButtonGroup>
);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading