// TODO remove this component it is not good and I don't like that I have to add it
import React from 'react'
import { Button, Form, Tooltip } from '@patternfly/react-core'
import {
CodeEditor,
CodeEditorControl,
Language,
} from '@patternfly/react-code-editor'
import { convertToJSX } from '@patternfly/ast-helpers'
import ExternalLinkAltIcon from '@patternfly/react-icons/dist/esm/icons/external-link-alt-icon'
import CodepenIcon from '@patternfly/react-icons/dist/esm/icons/codepen-icon'
import CopyIcon from '@patternfly/react-icons/dist/esm/icons/copy-icon'
import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon'
import AngleDoubleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-double-right-icon'
import ReplyAllIcon from '@patternfly/react-icons/dist/esm/icons/reply-all-icon'
const copy = (textToCopy: string) => {
navigator.clipboard.writeText(textToCopy)
}
interface TrackEventValue {
event_category: string
event_label: string
value?: number
}
interface WindowWithGtag extends Window {
gtag?: (type: string, name: string, value: TrackEventValue) => void
}
const _window = window as WindowWithGtag
/**
* Sends network call using Google Analytic's gtag function
* https://developers.google.com/analytics/devguides/collection/gtagjs/events#send_events
*
* @param name {string} - The value that will appear as the event action in Google Analytics Event reports.
* @param eventCategory {string} - The category of the event.
* @param eventLabel {string} - The label of the event.
* @param [value] {number} - An optional non-negative integer that will appear as the event value.
*/
const trackEvent = (
name: string,
eventCategory: string,
eventLabel: string,
value?: number,
) => {
value ||= 0
if (_window?.gtag) {
_window?.gtag('event', name, {
// eslint-disable-next-line camelcase
event_category: eventCategory,
// eslint-disable-next-line camelcase
event_label: eventLabel,
// Optional non-negative integer
...(value >= 0 && { value }),
})
}
}
function getLanguage(lang: string) {
if (lang === 'js') {
return Language.javascript
} else if (lang === 'ts') {
return Language.typescript
}
return lang as Language
}
interface ExampleToolbarProps {
fullscreenLink?: string
codeBoxParams?: any
lang: string
isFullscreen?: boolean
originalCode: string
code: string
setCode: (code: string) => void
exampleTitle?: string
}
export const ExampleToolbar = ({
// Link to fullscreen example page (each example has one)
fullscreenLink,
// Params to pass to codesandbox
codeBoxParams,
// Language of code
lang,
// Whether the example is fullscreen only
isFullscreen,
// Original version of the code
originalCode,
// Current code in editor
code,
// Callback to set code in parent component
setCode,
// Title of example used in creating unique labels
exampleTitle,
}: ExampleToolbarProps) => {
const [isEditorOpen, setIsEditorOpen] = React.useState(false)
const [isCopied, setCopied] = React.useState(false)
const copyLabel = 'Copy code to clipboard'
const languageLabel = `Toggle ${lang.toUpperCase()} code`
const codesandboxLabel = 'Open example in CodeSandbox'
const fullscreenLabel = 'Open example in new window'
const convertLabel = 'Convert example from Typescript to JavaScript'
const undoAllLabel = 'Undo all changes'
const copyAriaLabel = `Copy ${exampleTitle} example code to clipboard`
const languageAriaLabel = `Toggle ${lang.toUpperCase()} code in ${exampleTitle} example`
const codesandboxAriaLabel = `Open ${exampleTitle} example in CodeSandbox`
const fullscreenAriaLabel = `Open ${exampleTitle} example in new window`
const convertAriaLabel = `Convert ${exampleTitle} example from Typescript to JavaScript`
const undoAllAriaLabel = `Undo all changes to ${exampleTitle}`
const editorControlProps = {
className: 'ws-code-editor-control',
}
const commonTooltipProps = {
exitDelay: 300,
maxWidth: 'var(--ws-code-editor--tooltip--MaxWidth)',
}
const copyCode = () => {
copy(code)
setCopied(true)
}
const customControls = (