Skip to content

Commit

Permalink
consolidate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Oct 25, 2024
1 parent 4413f17 commit 92752a8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 168 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { APIV1Read } from "@fern-api/fdr-sdk/client/types";
import { CopyToClipboardButton } from "@fern-ui/components";
import * as Accordion from "@radix-ui/react-accordion";
import cn from "clsx";
import { ArrowDown, ArrowUp, NavArrowDown } from "iconoir-react";
import { FC } from "react";
import { FernSyntaxHighlighter } from "../../syntax-highlighting/FernSyntaxHighlighter";
import { WebSocketMessage } from "./WebSocketMessages";

export interface WebSocketMessageAccordionItemProps {
message: WebSocketMessage;
index: number;
messagesLength: number;
}

export const WebsocketMessageAccordionItem: FC<WebSocketMessageAccordionItemProps> = ({
message,
index,
messagesLength,
}) => {
return (
<Accordion.Item value={index.toString()} key={index} className={cn("group relative")}>
<Accordion.Trigger
className={cn("fern-web-socket-trigger", {
"data-[state=open]:bg-tag-success": message.origin === APIV1Read.WebSocketMessageOrigin.Client,
"data-[state=open]:bg-tag-primary": message.origin === APIV1Read.WebSocketMessageOrigin.Server,
"data-[state=open]:bg-tag-default": message.origin == null,
})}
>
{message.origin === APIV1Read.WebSocketMessageOrigin.Client ? (
<span className="fern-web-socket-client">
<ArrowUp className="size-icon" />
</span>
) : message.origin === APIV1Read.WebSocketMessageOrigin.Server ? (
<span className="fern-web-socket-server">
<ArrowDown className="size-icon" />
</span>
) : null}
<span className="fern-web-socket-trigger-data">{JSON.stringify(message.data)}</span>
{message.displayName != null || message.type != null ? (
<span className="fern-web-socket-type">
<span className="fern-web-socket-badge">{message.displayName ?? message.type}</span>
</span>
) : null}

<CopyToClipboardButton
className="fern-web-socket-copy"
content={() => JSON.stringify(message.data, null, 2)}
onClick={(e) => e.stopPropagation()}
/>

<NavArrowDown className="fern-web-socket-chevron group-data-[state=open]:rotate-180" aria-hidden />
</Accordion.Trigger>
<Accordion.Content className="fern-web-socket-content">
<div className="group/cb-container relative">
<FernSyntaxHighlighter
className="w-0 min-w-full overflow-y-auto"
code={JSON.stringify(message.data, null, 2)}
language="json"
fontSize="sm"
/>
</div>
</Accordion.Content>
<div
className={cn(
"mx-px group-focus-within:ring-1 ring-transparent ring-inset absolute inset-0 pointer-events-none z-auto rounded-[inherit]",
{
"group-focus-within:ring-border-success":
message.origin === APIV1Read.WebSocketMessageOrigin.Client,
"group-focus-within:ring-border-primary":
message.origin === APIV1Read.WebSocketMessageOrigin.Server,
"group-focus-within:ring-default": message.origin == null,
"mb-px rounded-b-xl": index === messagesLength,
},
)}
/>
</Accordion.Item>
);
};
94 changes: 26 additions & 68 deletions packages/ui/app/src/api-reference/web-socket/WebSocketMessages.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { APIV1Read } from "@fern-api/fdr-sdk/client/types";
import { CopyToClipboardButton } from "@fern-ui/components";
import * as Accordion from "@radix-ui/react-accordion";
import cn from "clsx";
import { ArrowDown, ArrowUp, NavArrowDown } from "iconoir-react";
import { FC } from "react";
import { FernSyntaxHighlighter } from "../../syntax-highlighting/FernSyntaxHighlighter";
import { Virtuoso } from "react-virtuoso";
import { WebsocketMessageAccordionItem } from "./WebSocketMessageAccordionItem";

export interface WebSocketMessage {
type: string | undefined;
Expand All @@ -16,10 +14,11 @@ export interface WebSocketMessage {

export interface WebSocketMessagesProps {
messages: WebSocketMessage[];
virtualized?: boolean;
// types: Record<string, ResolvedTypeDefinition>;
}

export const WebSocketMessages: FC<WebSocketMessagesProps> = ({ messages }) => {
export const WebSocketMessages: FC<WebSocketMessagesProps> = ({ messages, virtualized }) => {
return (
<Accordion.Root type="multiple" className="divide-default relative z-0 table size-full table-fixed divide-y">
{messages.length === 0 && (
Expand All @@ -30,71 +29,30 @@ export const WebSocketMessages: FC<WebSocketMessagesProps> = ({ messages }) => {
</div>
</div>
)}
{messages.map((message, index) => {
return (
<Accordion.Item value={index.toString()} key={index} className={cn("group relative")}>
<Accordion.Trigger
className={cn("fern-web-socket-trigger", {
"data-[state=open]:bg-tag-success":
message.origin === APIV1Read.WebSocketMessageOrigin.Client,
"data-[state=open]:bg-tag-primary":
message.origin === APIV1Read.WebSocketMessageOrigin.Server,
"data-[state=open]:bg-tag-default": message.origin == null,
})}
>
{message.origin === APIV1Read.WebSocketMessageOrigin.Client ? (
<span className="fern-web-socket-client">
<ArrowUp className="size-icon" />
</span>
) : message.origin === APIV1Read.WebSocketMessageOrigin.Server ? (
<span className="fern-web-socket-server">
<ArrowDown className="size-icon" />
</span>
) : null}
<span className="fern-web-socket-trigger-data">{JSON.stringify(message.data)}</span>
{message.displayName != null || message.type != null ? (
<span className="fern-web-socket-type">
<span className="fern-web-socket-badge">{message.displayName ?? message.type}</span>
</span>
) : null}

<CopyToClipboardButton
className="fern-web-socket-copy"
content={() => JSON.stringify(message.data, null, 2)}
onClick={(e) => e.stopPropagation()}
/>

<NavArrowDown
className="fern-web-socket-chevron group-data-[state=open]:rotate-180"
aria-hidden
/>
</Accordion.Trigger>
<Accordion.Content className="fern-web-socket-content">
<div className="group/cb-container relative">
<FernSyntaxHighlighter
className="w-0 min-w-full overflow-y-auto"
code={JSON.stringify(message.data, null, 2)}
language="json"
fontSize="sm"
/>
</div>
</Accordion.Content>
<div
className={cn(
"mx-px group-focus-within:ring-1 ring-transparent ring-inset absolute inset-0 pointer-events-none z-auto rounded-[inherit]",
{
"group-focus-within:ring-border-success":
message.origin === APIV1Read.WebSocketMessageOrigin.Client,
"group-focus-within:ring-border-primary":
message.origin === APIV1Read.WebSocketMessageOrigin.Server,
"group-focus-within:ring-default": message.origin == null,
"mb-px rounded-b-xl": index === messages.length - 1,
},
)}
{virtualized ? (
<Virtuoso
data={messages}
itemContent={(index, message) => (
<WebsocketMessageAccordionItem
key={index}
message={message}
index={index}
messagesLength={messages.length - 1}
/>
</Accordion.Item>
);
})}
)}
followOutput={"smooth"}
/>
) : (
messages.map((message, index) => (
<WebsocketMessageAccordionItem
key={index}
message={message}
index={index}
messagesLength={messages.length - 1}
/>
))
)}
</Accordion.Root>
);
};
Original file line number Diff line number Diff line change
@@ -1,104 +1,6 @@
import { APIV1Read } from "@fern-api/fdr-sdk/client/types";
import { CopyToClipboardButton } from "@fern-ui/components";
import * as Accordion from "@radix-ui/react-accordion";
import cn from "clsx";
import { ArrowDown, ArrowUp, NavArrowDown, Xmark } from "iconoir-react";
import { FC } from "react";
import { Virtuoso } from "react-virtuoso";
import { FernSyntaxHighlighter } from "../../syntax-highlighting/FernSyntaxHighlighter";
import { WebSocketMessagesProps } from "./WebSocketMessages";
import { WebSocketMessages, WebSocketMessagesProps } from "./WebSocketMessages";

export const WebSocketMessagesVirtualized: FC<WebSocketMessagesProps> = ({ messages }) => {
return (
<Accordion.Root
type="multiple"
className="divide-default relative z-0 table size-full table-fixed divide-y grow"
>
{messages.length === 0 && (
<div className="absolute inset-0 flex size-full items-center justify-center">
<div className="flex flex-col items-center space-y-4">
{/* <WifiOff className="t-muted" size={28} /> */}
<h4 className="m-0">No messages...</h4>
</div>
</div>
)}
<Virtuoso
data={messages}
itemContent={(index, message) => {
return (
<Accordion.Item value={index.toString()} key={index} className={cn("group relative")}>
<Accordion.Trigger
className={cn("fern-web-socket-trigger", {
"data-[state=open]:bg-tag-success":
message.origin === APIV1Read.WebSocketMessageOrigin.Client,
"data-[state=open]:bg-tag-primary":
message.origin === APIV1Read.WebSocketMessageOrigin.Server,
"data-[state=open]:bg-tag-danger": message.origin === "endSample",
"data-[state=open]:bg-tag-default": message.origin == null,
})}
>
{message.origin === APIV1Read.WebSocketMessageOrigin.Client ? (
<span className="fern-web-socket-client">
<ArrowUp className="size-icon" />
</span>
) : message.origin === APIV1Read.WebSocketMessageOrigin.Server ? (
<span className="fern-web-socket-server">
<ArrowDown className="size-icon" />
</span>
) : message.origin === "endSample" ? (
<span className="fern-web-socket-end-sample">
<Xmark className="size-icon" />
</span>
) : null}
<span className="fern-web-socket-trigger-data">{JSON.stringify(message.data)}</span>
{message.displayName != null || message.type != null ? (
<span className="fern-web-socket-type">
<span className="fern-web-socket-badge">
{message.displayName ?? message.type}
</span>
</span>
) : null}

<CopyToClipboardButton
className="fern-web-socket-copy"
content={() => JSON.stringify(message.data, null, 2)}
onClick={(e) => e.stopPropagation()}
/>

<NavArrowDown
className="fern-web-socket-chevron group-data-[state=open]:rotate-180"
aria-hidden
/>
</Accordion.Trigger>
<Accordion.Content className="fern-web-socket-content">
<div className="group/cb-container relative">
<FernSyntaxHighlighter
className="w-0 min-w-full overflow-y-auto"
code={JSON.stringify(message.data, null, 2)}
language="json"
fontSize="sm"
/>
</div>
</Accordion.Content>
<div
className={cn(
"mx-px group-focus-within:ring-1 ring-transparent ring-inset absolute inset-0 pointer-events-none z-auto rounded-[inherit]",
{
"group-focus-within:ring-border-success":
message.origin === APIV1Read.WebSocketMessageOrigin.Client,
"group-focus-within:ring-border-primary":
message.origin === APIV1Read.WebSocketMessageOrigin.Server,
"group-focus-within:ring-border-danger": message.origin === "endSample",
"group-focus-within:ring-default": message.origin == null,
"mb-px rounded-b-xl": index === messages.length - 1,
},
)}
/>
</Accordion.Item>
);
}}
followOutput={"smooth"}
/>
</Accordion.Root>
);
return <WebSocketMessages messages={messages} virtualized />;
};

0 comments on commit 92752a8

Please sign in to comment.