Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve-api should let you open all APIs
Browse files Browse the repository at this point in the history
abvthecity committed May 23, 2024
1 parent 06ba918 commit f614441
Showing 25 changed files with 323 additions and 326 deletions.
4 changes: 3 additions & 1 deletion packages/commons/fdr-utils/src/traverser.ts
Original file line number Diff line number Diff line change
@@ -97,7 +97,9 @@ function visitNode(
}
}

const apiSectionBreadcrumbs = [...sectionTitleBreadcrumbs, apiSection.title];
const apiSectionBreadcrumbs = apiSection.isSidebarFlattened
? sectionTitleBreadcrumbs
: [...sectionTitleBreadcrumbs, apiSection.title];

if (apiSection.changelog != null) {
traverseState = visitPage(apiSection.changelog, currentNode, traverseState, apiSectionBreadcrumbs);
1 change: 0 additions & 1 deletion packages/ui/app/src/api-page/ApiPage.tsx
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@ export const ApiPage: React.FC<ApiPage.Props> = ({ initialApi, showErrors }) =>
const hydrated = useIsReady();
const { isApiScrollingDisabled } = useFeatureFlags();
const setDefinitions = useSetAtom(APIS);
// const definition = apis[initialApi.api];

useEffect(() => {
setDefinitions((prev) => ({ ...prev, [initialApi.api]: initialApi }));
46 changes: 25 additions & 21 deletions packages/ui/app/src/api-playground/PlaygroundContext.tsx
Original file line number Diff line number Diff line change
@@ -2,12 +2,11 @@ import { useAtom } from "jotai";
import { atomWithStorage } from "jotai/utils";
import { mapValues, noop } from "lodash-es";
import dynamic from "next/dynamic";
import { FC, PropsWithChildren, createContext, useCallback, useContext, useMemo, useState } from "react";
import { FC, PropsWithChildren, createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
import urljoin from "url-join";
import { capturePosthogEvent } from "../analytics/posthog";
import { useFeatureFlags } from "../contexts/FeatureFlagContext";
import { useDocsContext } from "../contexts/docs-context/useDocsContext";
import { useNavigationContext } from "../contexts/navigation-context";
import {
ResolvedApiDefinition,
ResolvedRootPackage,
@@ -53,10 +52,29 @@ export const PLAYGROUND_FORM_STATE_ATOM = atomWithStorage<Record<string, Playgro
export const PlaygroundContextProvider: FC<PropsWithChildren> = ({ children }) => {
const { isApiPlaygroundEnabled } = useFeatureFlags();
const [apis, setApis] = useAtom(APIS);
const { basePath } = useDocsContext();
const { selectedSlug } = useNavigationContext();
const { basePath, apis: apiIds } = useDocsContext();
const [selectionState, setSelectionState] = useState<PlaygroundSelectionState | undefined>();

useEffect(() => {
const unfetchedApis = apiIds.filter((apiId) => apis[apiId] == null);
if (unfetchedApis.length === 0) {
return;
}

void Promise.all(
unfetchedApis.map(async (apiId) => {
const r = await fetch(
urljoin(basePath ?? "", "/api/fern-docs/resolve-api?path=" + (basePath ?? "/") + "&api=" + apiId),
);
const data: Record<string, ResolvedRootPackage> | null = await r.json();
if (data == null) {
return;
}
setApis((currentApis) => ({ ...currentApis, ...data }));
}),
);
}, [apiIds, apis, basePath, setApis]);

const flattenedApis = useMemo(() => mapValues(apis, flattenRootPackage), [apis]);

const [isPlaygroundOpen, setPlaygroundOpen] = useAtom(PLAYGROUND_OPEN_ATOM);
@@ -75,23 +93,9 @@ export const PlaygroundContextProvider: FC<PropsWithChildren> = ({ children }) =

const setSelectionStateAndOpen = useCallback(
async (newSelectionState: PlaygroundSelectionState) => {
let matchedPackage = flattenedApis[newSelectionState.api];
const matchedPackage = flattenedApis[newSelectionState.api];
if (matchedPackage == null) {
const r = await fetch(
urljoin(
basePath ?? "",
"/api/fern-docs/resolve-api?path=/" + selectedSlug + "&api=" + newSelectionState.api,
),
);

const data: ResolvedRootPackage | null = await r.json();

if (data == null) {
return;
}

setApis((currentApis) => ({ ...currentApis, [newSelectionState.api]: data }));
matchedPackage = flattenRootPackage(data);
return;
}

if (newSelectionState.type === "endpoint") {
@@ -131,7 +135,7 @@ export const PlaygroundContextProvider: FC<PropsWithChildren> = ({ children }) =
});
}
},
[basePath, expandPlayground, flattenedApis, globalFormState, selectedSlug, setApis, setGlobalFormState],
[expandPlayground, flattenedApis, globalFormState, setGlobalFormState],
);

if (!isApiPlaygroundEnabled) {
Loading

0 comments on commit f614441

Please sign in to comment.