|
| 1 | +import type { FC } from 'react'; |
| 2 | +import { GraphiQL } from 'graphiql'; |
| 3 | +import { ToolbarButton, useGraphiQL } from '@graphiql/react'; |
| 4 | +import { createFetcher } from './create-fetcher'; |
| 5 | +import 'graphiql/setup-workers/esm.sh'; |
| 6 | + |
| 7 | +export const graphiql = ( |
| 8 | + <GraphiQL |
| 9 | + dangerouslyAssumeSchemaIsValid |
| 10 | + defaultEditorToolsVisibility="variables" |
| 11 | + fetcher={createFetcher('https://graphql.earthdata.nasa.gov/api')} |
| 12 | + isHeadersEditorEnabled={false} |
| 13 | + > |
| 14 | + <GraphiQL.Logo>API Explorer</GraphiQL.Logo> |
| 15 | + <GraphiQL.Toolbar> |
| 16 | + {({ prettify, copy, merge }) => ( |
| 17 | + <> |
| 18 | + {prettify} |
| 19 | + {copy} |
| 20 | + {merge} |
| 21 | + <Button /> |
| 22 | + </> |
| 23 | + )} |
| 24 | + </GraphiQL.Toolbar> |
| 25 | + </GraphiQL> |
| 26 | +); |
| 27 | + |
| 28 | +const Button: FC = () => { |
| 29 | + const { queryEditor, variableEditor } = useGraphiQL(state => ({ |
| 30 | + queryEditor: state.queryEditor, |
| 31 | + variableEditor: state.variableEditor, |
| 32 | + })); |
| 33 | + |
| 34 | + async function onShareExplorer(): Promise<void> { |
| 35 | + const shareableURL = new URL('/explorer', location.origin); |
| 36 | + const operations = queryEditor!.getValue(); |
| 37 | + const variables = variableEditor!.getValue(); |
| 38 | + if (operations) { |
| 39 | + shareableURL.searchParams.set('query', encodeURIComponent(operations)); |
| 40 | + } |
| 41 | + if (variables) { |
| 42 | + shareableURL.searchParams.set('variables', encodeURIComponent(variables)); |
| 43 | + } |
| 44 | + const url = shareableURL.toString(); |
| 45 | + await navigator.clipboard.writeText(url); |
| 46 | + } |
| 47 | + |
| 48 | + return ( |
| 49 | + <ToolbarButton |
| 50 | + label="Share your Explorer query" |
| 51 | + onClick={onShareExplorer} |
| 52 | + style={{ textAlign: 'center' }} |
| 53 | + title="Share your Explorer query" |
| 54 | + > |
| 55 | + S |
| 56 | + </ToolbarButton> |
| 57 | + ); |
| 58 | +}; |
0 commit comments