diff --git a/src-web/atoms/graphqlSchemaAtom.ts b/src-web/atoms/graphqlSchemaAtom.ts index b9761c06..256e162f 100644 --- a/src-web/atoms/graphqlSchemaAtom.ts +++ b/src-web/atoms/graphqlSchemaAtom.ts @@ -1,5 +1,3 @@ -import type { GraphQLSchema } from 'graphql'; import { atom } from 'jotai'; -export const graphqlSchemaAtom = atom(null); -export const graphqlDocStateAtom = atom(false); +export const showGraphQLDocExplorerAtom = atom(false); diff --git a/src-web/components/GraphQLDocsExplorer.tsx b/src-web/components/GraphQLDocsExplorer.tsx index 26dedc6e..92195fb5 100644 --- a/src-web/components/GraphQLDocsExplorer.tsx +++ b/src-web/components/GraphQLDocsExplorer.tsx @@ -1,7 +1,7 @@ /* eslint-disable */ import { Color } from '@yaakapp-internal/plugins'; import classNames from 'classnames'; -import type { GraphQLField, GraphQLInputField, GraphQLType } from 'graphql'; +import type { GraphQLField, GraphQLInputField, GraphQLSchema, GraphQLType } from 'graphql'; import { getNamedType, isEnumType, @@ -13,99 +13,88 @@ import { isScalarType, isUnionType, } from 'graphql'; -import { useAtomValue } from 'jotai'; -import { ReactNode, useState } from 'react'; -import { graphqlDocStateAtom, graphqlSchemaAtom } from '../atoms/graphqlSchemaAtom'; +import { CSSProperties, memo, ReactNode, useState } from 'react'; +import { showGraphQLDocExplorerAtom } from '../atoms/graphqlSchemaAtom'; import { jotaiStore } from '../lib/jotai'; import { CountBadge } from './core/CountBadge'; import { Icon } from './core/Icon'; import { IconButton } from './core/IconButton'; import { Markdown } from './Markdown'; +interface Props { + style?: CSSProperties; + schema: GraphQLSchema; + className?: string; +} + type ExplorerItem = | { kind: 'type'; type: GraphQLType; from: ExplorerItem } | { kind: 'field'; type: GraphQLField; from: ExplorerItem } | { kind: 'input_field'; type: GraphQLInputField; from: ExplorerItem } | null; -export function GraphQLDocsExplorer() { - const graphqlSchema = useAtomValue(graphqlSchemaAtom); +export const GraphQLDocsExplorer = memo(function ({ style, schema, className }: Props) { const [activeItem, setActiveItem] = useState(null); - if (!graphqlSchema) { - return
No GraphQL schema available
; - } - - const qryType = graphqlSchema.getQueryType(); - const mutType = graphqlSchema.getMutationType(); - const subType = graphqlSchema.getSubscriptionType(); + const qryType = schema.getQueryType(); + const mutType = schema.getMutationType(); + const subType = schema.getSubscriptionType(); const qryItem: ExplorerItem = qryType ? { kind: 'type', type: qryType, from: null } : null; const mutItem: ExplorerItem = mutType ? { kind: 'type', type: mutType, from: null } : null; const subItem: ExplorerItem = subType ? { kind: 'type', type: subType, from: null } : null; - const allTypes = graphqlSchema.getTypeMap(); + const allTypes = schema.getTypeMap(); return ( -
- { - jotaiStore.set(graphqlDocStateAtom, false); - }} - /> - {activeItem == null ? ( -
- Root Types - - - - - All Schema Types - - {graphqlSchema.description ?? null} -
- {Object.keys(allTypes).map((typeName) => { - const t = allTypes[typeName]!; - return ( - - ); - })} +
+
+ + {activeItem == null ? ( +
+ Root Types + + + + All Schema Types + {schema.description ?? null} +
+ {Object.keys(allTypes).map((typeName) => { + const t = allTypes[typeName]!; + return ( + + ); + })} +
-
- ) : ( -
- -
- + ) : ( +
+
-
- )} + )} +
); -} +}); function GraphQLExplorerHeader({ item, @@ -114,12 +103,32 @@ function GraphQLExplorerHeader({ item: ExplorerItem; setItem: (t: ExplorerItem) => void; }) { - if (item == null) return null; - return ( -