/* @jsx jsx */ import { jsx, Flex } from "strict-ui" import { PageProps } from "gatsby" import { useQuery } from "urql" import { Spinner } from "theme-ui" import { Global } from "@emotion/react" import { useMutation } from "urql" import { useState, Fragment, useEffect, FormEvent } from "react" import { AnchorButton, Button, TextAreaField, TextAreaFieldControl, Text, Heading, Spacer, } from "gatsby-interface" import { navigate } from "gatsby-link" import { FaUsers as CommunityIcon } from "react-icons/fa" import { MdArrowDownward as ArrowDownwardIcon } from "react-icons/md" import ReactMarkdown from "react-markdown" import Highlight, { defaultProps } from "prism-react-renderer" import useNpmPackageData from "../utils/use-npm-data" import prismThemeCss from "../prism-theme" import gitHubIcon from "../github.svg" import isOfficialPackage from "../utils/is-official-package" import GatsbyIcon from "../components/gatsby-monogram" import { useTelemetry } from "../utils/use-telemetry" const markdownRenderers = { paragraph: (props: any): JSX.Element => ( ), heading: (props: any): JSX.Element => ( ), inlineCode: (props: any): JSX.Element => ( ), code: (props: any): JSX.Element => ( {({ className, style, tokens, getLineProps, getTokenProps, }): JSX.Element => ( {tokens.map((line, i) => ( // eslint-disable-next-line react/jsx-key {line.map((token, key) => ( // eslint-disable-next-line react/jsx-key ))} ))} )} ), } export default function PluginView( props: PageProps & { // This is the subpath, gatsby-plugin-create-client-paths is configured to match /plugins/* "*": string } ): JSX.Element { const pluginName = props[`*`] const [{ data, fetching, error }] = useQuery({ query: ` query GetGatsbyPlugin($id: String!) { gatsbyPlugin(id: $id) { name description options } } `, variables: { id: pluginName, }, }) const { fetching: fetchingNpmData, data: npmData } = useNpmPackageData(pluginName) const [{ fetching: updatingGatsbyPlugin }, updateGatsbyPlugin] = useMutation(` mutation updateGatsbyPlugin( $name: String! $options: JSONObject) { updateGatsbyPlugin(gatsbyPlugin: { name: $name, id: $name, options: $options }) { id name options } } `) const [{ fetching: deletingGatsbyPlugin }, deleteGatsbyPlugin] = useMutation(` mutation destroyGatsbyPlugin($name: String!) { destroyNpmPackage(npmPackage: { name: $name, id: $name, dependencyType: "production" }) { id name } destroyGatsbyPlugin(gatsbyPlugin: { name: $name, id: $name }) { id name } } `) const [{ fetching: installingGatsbyPlugin }, installGatsbyPlugin] = useMutation(` mutation installGatsbyPlugin($name: String!) { createNpmPackage(npmPackage: { name: $name, dependencyType: "production" }) { id name } createGatsbyPlugin(gatsbyPlugin: { name: $name }) { id name description options readme } } `) const [options, setOptions] = useState(null) const [validationError, setValidationError] = useState(null) const isInstalled = !fetching && !!data?.gatsbyPlugin?.name const isOfficial = !fetchingNpmData && npmData && isOfficialPackage(npmData) useEffect(() => { if (fetching) return setOptions( JSON.stringify(data?.gatsbyPlugin?.options || {}, null, 2).replace( `{}`, `{\n \n}` ) ) }, [fetching]) const telemetry = useTelemetry() if (error) { const errMsg = (error.networkError && error.networkError.message) || (Array.isArray(error.graphQLErrors) && error.graphQLErrors.map(e => e.message).join(` | `)) return Error: {errMsg} } return ( {fetchingNpmData ? ( `` ) : isOfficial ? ( Official ) : ( Community )} {` `} Gatsby plugin {} {npmData?.humanDownloadsLast30Days || `...`} {` `} downloads {pluginName} icon whiteSpace: `pre`, }} > View on GitHub {fetching ? ( ) : isInstalled ? ( { evt.preventDefault() if ( window.confirm( `Are you sure you want to uninstall ${pluginName}?` ) ) { telemetry.trackEvent(`PLUGIN_UNINSTALL`, { pluginName, }) deleteGatsbyPlugin({ name: pluginName }).then(() => navigate(`/`) ) } }} > Uninstall ) : ( { evt.preventDefault() installGatsbyPlugin({ name: pluginName }) telemetry.trackEvent(`PLUGIN_INSTALL`, { pluginName, }) }} > Install )} {fetchingNpmData ? ( ) : npmData?.readme ? ( ) : ( `No readme found.` )} { evt.preventDefault() setValidationError(null) let json try { // NOTE(@mxstbr): I use eval() to support JS object notation (`{ bla: true }`) and // not just strict JSON (`{ "bla": true }`) const js = eval(`(${options})`) // Validate that options isn't any JavaScript but an object json = JSON.parse(JSON.stringify(js)) } catch (err) { setValidationError(err) return } telemetry.trackEvent(`PLUGIN_CONFIGURE`, { pluginName, }) updateGatsbyPlugin({ name: props[`*`], options: json, }).catch(err => { setValidationError(err) }) }} gap={5} alignItems="flex-start" flexDirection="column" sx={{ borderRadius: 3, backgroundColor: `white`, borderColor: `grey.30`, borderWidth: 1, borderStyle: `solid`, padding: 7, width: `30%`, }} > Configuration options After installing this plugin, you can configure it here. Changes made here are applied to the gatsby-config.js file. {options === null ? ( ) : ( setOptions(evt.target.value)} /> )} {validationError !== null && ( Invalid JSON: {validationError.message} )} Save {!isInstalled && ( Install this plugin in order to save )} ) }
Error: {errMsg}