Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 9, 2024
1 parent 80614e5 commit f18f0d3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
14 changes: 11 additions & 3 deletions packages/ui/app/src/api-reference/ApiEndpointPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type * as FernNavigation from "@fern-api/fdr-sdk/navigation";
import { EMPTY_OBJECT } from "@fern-api/ui-core-utils";
import { useSetAtom } from "jotai";
import { useEffect } from "react";
import { WRITE_API_DEFINITION_ATOM } from "../atoms";
import { WRITE_API_DEFINITION_ATOM, useNavigationNodes } from "../atoms";
import { ALL_ENVIRONMENTS_ATOM } from "../atoms/environment";
import { BottomNavigationNeighbors } from "../components/BottomNavigationNeighbors";
import { FernErrorBoundary } from "../components/FernErrorBoundary";
import { ApiPageContext } from "../contexts/api-page";
import { DocsContent } from "../resolver/DocsContent";
import { BuiltWithFern } from "../sidebar/BuiltWithFern";
import { ApiPackageContent } from "./ApiPackageContent";
import { ApiPackageContent, isApiPackageContentNode } from "./ApiPackageContent";

export declare namespace ApiEndpointPage {
export interface Props {
Expand Down Expand Up @@ -37,11 +37,19 @@ export const ApiEndpointPage: React.FC<ApiEndpointPage.Props> = ({ content }) =>
});
}, [content.apiDefinition.endpoints, content.apiDefinition.websockets, setEnvironmentIds]);

const node = useNavigationNodes().get(content.nodeId);
if (!node || !isApiPackageContentNode(node)) {
// TODO: sentry
// eslint-disable-next-line no-console
console.error("Expected node to be an api reference node");
return null;
}

return (
<ApiPageContext.Provider value={true}>
<FernErrorBoundary component="ApiEndpointPage">
<ApiPackageContent
node={content.item}
node={node}
apiDefinition={content.apiDefinition}
breadcrumb={content.breadcrumb}
mdxs={EMPTY_OBJECT}
Expand Down
12 changes: 12 additions & 0 deletions packages/ui/app/src/api-reference/ApiPackageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export type ApiPackageContentNode =
| FernNavigation.ApiReferenceNode
| Exclude<FernNavigation.ApiPackageChild, FernNavigation.LinkNode>;

export function isApiPackageContentNode(node: FernNavigation.NavigationNode): node is ApiPackageContentNode {
return (
node.type === "apiReference" ||
node.type === "apiPackage" ||
node.type === "endpoint" ||
node.type === "webhook" ||
node.type === "webSocket" ||
node.type === "page" ||
node.type === "endpointPair"
);
}

interface ApiPackageContentProps {
node: ApiPackageContentNode;
apiDefinition: ApiDefinition;
Expand Down
15 changes: 12 additions & 3 deletions packages/ui/app/src/api-reference/ApiReferencePage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useSetAtom } from "jotai";
import { useEffect } from "react";
import { WRITE_API_DEFINITION_ATOM, useIsReady } from "../atoms";
import { WRITE_API_DEFINITION_ATOM, useIsReady, useNavigationNodes } from "../atoms";
import { ApiPageContext } from "../contexts/api-page";
import { DocsContent } from "../resolver/DocsContent";
import { BuiltWithFern } from "../sidebar/BuiltWithFern";
Expand All @@ -18,12 +18,21 @@ export const ApiReferencePage: React.FC<ApiReferencePage.Props> = ({ content })
const set = useSetAtom(WRITE_API_DEFINITION_ATOM);
useEffect(() => set(content.apiDefinition), [content.apiDefinition, set]);

const node = useNavigationNodes().get(content.apiReferenceNodeId);

if (node?.type !== "apiReference") {
// TODO: sentry
// eslint-disable-next-line no-console
console.error("Expected node to be an api reference node");
return null;
}

return (
<ApiPageContext.Provider value={true}>
<ApiReferenceContent
apiDefinition={content.apiDefinition}
showErrors={content.apiReferenceNode.showErrors ?? false}
node={content.apiReferenceNode}
showErrors={node.showErrors ?? false}
node={node}
breadcrumb={content.breadcrumb}
mdxs={content.mdxs}
slug={content.slug}
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/app/src/resolver/DocsContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export declare namespace DocsContent {
interface ApiEndpointPage {
type: "api-endpoint-page";
slug: FernNavigation.Slug;
item: FernNavigation.EndpointPairNode | FernNavigation.NavigationNodeApiLeaf;
nodeId: FernNavigation.NodeId;
breadcrumb: readonly FernNavigation.BreadcrumbItem[];
apiDefinition: ApiDefinition;
showErrors: boolean;
Expand All @@ -63,7 +63,7 @@ export declare namespace DocsContent {
// TODO: the api reference node is probably duplicated in the initial props
// so we should deduplicate it to avoid sending it twice
breadcrumb: readonly FernNavigation.BreadcrumbItem[]; // this is the breadcrumb up to the api reference node
apiReferenceNode: FernNavigation.ApiReferenceNode;
apiReferenceNodeId: FernNavigation.NodeId;
apiDefinition: ApiDefinition;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/resolver/resolveApiEndpointPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function resolveApiEndpointPage({
return {
type: "api-endpoint-page",
slug: node.slug,
item: parent?.type === "endpointPair" ? parent : node,
nodeId: parent?.type === "endpointPair" ? parent.id : node.id,
apiDefinition,
showErrors: showErrors ?? false,
neighbors,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/resolver/resolveApiReferencePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function resolveApiReferencePage({
return {
type: "api-reference-page",
slug: node.slug,
apiReferenceNode,
apiReferenceNodeId: apiReferenceNode.id,
apiDefinition,
mdxs,
breadcrumb,
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/docs-bundle/src/server/DocsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class DocsLoader {
if (!this.auth) {
return [undefined, undefined];
}
if (!this.fernToken) {
return [undefined, this.auth];
}
try {
return [await withAuthProps(this.auth, this.fernToken), this.auth];
} catch (e) {
Expand Down

0 comments on commit f18f0d3

Please sign in to comment.