Skip to content

Commit

Permalink
Fix playground (twentyhq#5043)
Browse files Browse the repository at this point in the history
Some code quality updates on the doc api playgrounds
  • Loading branch information
martmull authored Apr 19, 2024
1 parent 05cf777 commit b71be7d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
16 changes: 9 additions & 7 deletions packages/twenty-docs/src/components/graphql-playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BrowserOnly from '@docusaurus/BrowserOnly';
import { explorerPlugin } from '@graphiql/plugin-explorer';
import { Theme, useTheme } from '@graphiql/react';
import { createGraphiQLFetcher } from '@graphiql/toolkit';
import { SubDoc } from '@site/src/components/token-form';
import Layout from '@theme/Layout';
import { GraphiQL } from 'graphiql';

Expand All @@ -21,9 +22,6 @@ const GraphQlComponent = ({ token, baseUrl, path }) => {
const explorer = explorerPlugin({
showAttribution: true,
});
if (!baseUrl || !token) {
return <></>;
}

const fetcher = createGraphiQLFetcher({
url: baseUrl + '/' + path,
Expand All @@ -47,6 +45,10 @@ const GraphQlComponent = ({ token, baseUrl, path }) => {
};
}, []);

if (!baseUrl || !token) {
return <></>;
}

return (
<div className="fullHeightPlayground">
<GraphiQL
Expand All @@ -58,9 +60,9 @@ const GraphQlComponent = ({ token, baseUrl, path }) => {
);
};

const GraphQlPlayground = ({ subDoc }: { subDoc: 'core' | 'metadata' }) => {
const [token, setToken] = useState();
const [baseUrl, setBaseUrl] = useState();
const GraphQlPlayground = ({ subDoc }: { subDoc: SubDoc }) => {
const [token, setToken] = useState<string>();
const [baseUrl, setBaseUrl] = useState<string>();
const { setTheme } = useTheme();

useEffect(() => {
Expand Down Expand Up @@ -99,7 +101,7 @@ const GraphQlPlayground = ({ subDoc }: { subDoc: 'core' | 'metadata' }) => {
children={children}
setToken={setToken}
setBaseUrl={setBaseUrl}
subdocName={subDoc}
subDoc={subDoc}
/>
)}
</BrowserOnly>
Expand Down
8 changes: 5 additions & 3 deletions packages/twenty-docs/src/components/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const Playground = ({
setToken,
setBaseUrl,
subDoc,
}: Partial<React.PropsWithChildren | TokenFormProps> & {
subDoc: string;
}) => {
}: Partial<React.PropsWithChildren> &
Omit<
TokenFormProps,
'isTokenValid' | 'setIsTokenValid' | 'setLoadingState'
>) => {
const [isTokenValid, setIsTokenValid] = useState(false);
const [isLoading, setIsLoading] = useState(false);
return (
Expand Down
45 changes: 22 additions & 23 deletions packages/twenty-docs/src/components/token-form.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { useEffect, useState } from 'react';
import { TbApi, TbChevronLeft, TbLink } from 'react-icons/tb';
import { useHistory, useLocation } from '@docusaurus/router';
import { TbApi, TbChevronLeft, TbLink } from '@theme/icons';
import { parseJson } from 'nx/src/utils/json';

import tokenForm from '!css-loader!./token-form.css';

export type SubDoc = 'core' | 'metadata';
export type TokenFormProps = {
setOpenApiJson?: (json: object) => void;
setToken?: (token: string) => void;
setBaseUrl?: (baseUrl: string) => void;
isTokenValid: boolean;
setIsTokenValid: (boolean) => void;
setLoadingState: (boolean) => void;
subDoc?: string;
isTokenValid?: boolean;
setIsTokenValid?: (boolean) => void;
setLoadingState?: (boolean) => void;
subDoc?: SubDoc;
};

const TokenForm = ({
Expand Down Expand Up @@ -141,25 +142,23 @@ const TokenForm = ({
onBlur={() => submitToken(token)}
/>
</div>
{!location.pathname.includes('rest-api') && (
<div className="inputWrapper" style={{ maxWidth: '100px' }}>
<select
className="select"
onChange={(event) =>
history.replace(
<div className="inputWrapper" style={{ maxWidth: '100px' }}>
<select
className="select"
onChange={(event) =>
history.replace(
'/' +
location.pathname.split('/').at(-2) +
'/' +
location.pathname.split('/').at(-2) +
'/' +
event.target.value,
)
}
value={location.pathname.split('/').at(-1)}
>
<option value="core">Core</option>
<option value="metadata">Metadata</option>
</select>
</div>
)}
event.target.value,
)
}
value={location.pathname.split('/').at(-1)}
>
<option value="core">Core</option>
<option value="metadata">Metadata</option>
</select>
</div>
</form>
</div>
);
Expand Down

0 comments on commit b71be7d

Please sign in to comment.