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

feat: share current playground state #1442

Draft
wants to merge 1 commit 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
17 changes: 13 additions & 4 deletions packages/commons/react/react-commons/src/useCopyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { useTimeout } from "./useTimeout";

export declare namespace useCopyToClipboard {
Expand All @@ -11,17 +11,26 @@ export declare namespace useCopyToClipboard {
export function useCopyToClipboard(
content: string | (() => string | Promise<string>) | undefined,
): useCopyToClipboard.Return {
const contentRef = useRef(content);

useEffect(() => {
contentRef.current = content;
}, [content]);

const [wasJustCopied, setWasJustCopied] = useState(false);

const copyToClipboard = useMemo(() => {
if (content == null) {
const currentContent = contentRef.current;
if (currentContent == null) {
return undefined;
}
return async () => {
setWasJustCopied(true);
await navigator.clipboard.writeText(typeof content === "function" ? await content() : content);
await navigator.clipboard.writeText(
typeof currentContent === "function" ? await currentContent() : currentContent,
);
};
}, [content]);
}, []);

useTimeout(
() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/playground/PlaygroundEndpoint.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
} */

.playground-endpoint-copy-button {
@apply -mt-1 ml-auto -mr-3;
@apply ml-auto -mr-3;
}

.playground-endpoint-baseurl {
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/app/src/playground/PlaygroundEndpointPath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { HttpMethodTag } from "../components/HttpMethodTag";
import { MaybeEnvironmentDropdown } from "../components/MaybeEnvironmentDropdown";
import { ResolvedEndpointPathParts, ResolvedObjectProperty } from "../resolver/types";
import { PlaygroundSendRequestButton } from "./PlaygroundSendRequestButton";
import { PlaygroundShareButton } from "./PlaygroundShareButton";
import { PlaygroundRequestFormState } from "./types";
import { buildRequestUrl, unknownToString } from "./utils";

Expand Down Expand Up @@ -121,6 +122,10 @@ export const PlaygroundEndpointPath: FC<PlaygroundEndpointPathProps> = ({
/>
</div>

<div className="max-sm:hidden">
<PlaygroundShareButton />
</div>

<Dialog.Close asChild className="max-sm:hidden">
<FernButton icon={<Xmark />} size="large" rounded variant="outlined" />
</Dialog.Close>
Expand Down
53 changes: 53 additions & 0 deletions packages/ui/app/src/playground/PlaygroundShareButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { FernNavigation } from "@fern-api/fdr-sdk";
import { FernButton } from "@fern-ui/components";
import { useCopyToClipboard } from "@fern-ui/react-commons";
import { ShareIos } from "iconoir-react";
import { useAtom } from "jotai";
import { useSearchParams } from "next/navigation";
import { useEffect, type ReactElement } from "react";
import { usePlaygroundFormStateAtom, usePlaygroundNode } from "../atoms";
import { useToHref } from "../hooks/useHref";

interface PlaygroundShareButtonProps {
node: FernNavigation.NavigationNodeApiLeaf;
}

export const PlaygroundShareButtonInternal = ({ node }: PlaygroundShareButtonProps): ReactElement => {
const searchParams = useSearchParams();
const initialState = searchParams.get("initialState");
const initialPlaygroundHref = searchParams.get("playground");
const toHref = useToHref();

const [formState, setFormState] = useAtom(usePlaygroundFormStateAtom(node.id));
const { copyToClipboard } = useCopyToClipboard(() => {
const url = new URL(window.location.href);

url.searchParams.set("playground", toHref(node.slug));
url.searchParams.set("initialState", btoa(JSON.stringify(formState)));

return url.toString();
});

useEffect(() => {
if (formState == null && initialState != null && initialPlaygroundHref === toHref(node.slug)) {
try {
setFormState(JSON.parse(atob(initialState)));
} catch (e) {
// eslint-disable-next-line no-console
console.error("Failed to parse initial state", e);
}
}
}, [formState, initialPlaygroundHref, initialState, node.slug, setFormState, toHref]);

return <FernButton icon={<ShareIos />} onClick={copyToClipboard} rounded variant="outlined" size="large" />;
};

export const PlaygroundShareButton = (): ReactElement | null => {
const node = usePlaygroundNode();

if (node == null) {
return null;
}

return <PlaygroundShareButtonInternal node={node} />;
};
Loading