|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + DocumentDuplicateIcon, |
| 5 | + CodeBracketIcon, |
| 6 | +} from '@heroicons/react/24/outline'; |
| 7 | +import { useTranslations } from 'next-intl'; |
| 8 | +import type { FC, PropsWithChildren, ReactNode } from 'react'; |
| 9 | +import { Fragment, isValidElement, useRef } from 'react'; |
| 10 | + |
| 11 | +import Button from '@/components/Common/Button'; |
| 12 | +import { useCopyToClipboard, useNotification } from '@/hooks'; |
| 13 | +import { ENABLE_WEBSITE_REDESIGN } from '@/next.constants.mjs'; |
| 14 | + |
| 15 | +import styles from './index.module.css'; |
| 16 | + |
| 17 | +// Transforms a code element with plain text content into a more structured |
| 18 | +// format for rendering with line numbers |
| 19 | +const transformCode = (code: ReactNode): ReactNode => { |
| 20 | + if (!isValidElement(code)) { |
| 21 | + // Early return when the `CodeBox` child is not a valid element since the |
| 22 | + // type is a ReactNode, and can assume any value |
| 23 | + return code; |
| 24 | + } |
| 25 | + |
| 26 | + const content = code.props?.children; |
| 27 | + |
| 28 | + if (code.type !== 'code' || typeof content !== 'string') { |
| 29 | + // There is no need to transform an element that is not a code element or |
| 30 | + // a content that is not a string |
| 31 | + return code; |
| 32 | + } |
| 33 | + |
| 34 | + const lines = content.split('\n'); |
| 35 | + |
| 36 | + return ( |
| 37 | + <code> |
| 38 | + {lines.flatMap((line, lineIndex) => { |
| 39 | + const columns = line.split(' '); |
| 40 | + |
| 41 | + return [ |
| 42 | + <span key={lineIndex} className="line"> |
| 43 | + {columns.map((column, columnIndex) => ( |
| 44 | + <Fragment key={columnIndex}> |
| 45 | + <span>{column}</span> |
| 46 | + {columnIndex < columns.length - 1 && <span> </span>} |
| 47 | + </Fragment> |
| 48 | + ))} |
| 49 | + </span>, |
| 50 | + // Add a break line so the text content is formatted correctly |
| 51 | + // when copying to clipboard |
| 52 | + '\n', |
| 53 | + ]; |
| 54 | + })} |
| 55 | + </code> |
| 56 | + ); |
| 57 | +}; |
| 58 | + |
| 59 | +type CodeBoxProps = { language: string; showCopyButton?: boolean }; |
| 60 | + |
| 61 | +const CodeBox: FC<PropsWithChildren<CodeBoxProps>> = ({ |
| 62 | + children, |
| 63 | + language, |
| 64 | + // For now we only want to render the Copy Button by default |
| 65 | + // if the Website Redesign is Enabled |
| 66 | + showCopyButton = ENABLE_WEBSITE_REDESIGN, |
| 67 | +}) => { |
| 68 | + const ref = useRef<HTMLPreElement>(null); |
| 69 | + |
| 70 | + const notify = useNotification(); |
| 71 | + const [, copyToClipboard] = useCopyToClipboard(); |
| 72 | + const t = useTranslations(); |
| 73 | + |
| 74 | + const onCopy = async () => { |
| 75 | + if (ref.current?.textContent) { |
| 76 | + copyToClipboard(ref.current.textContent); |
| 77 | + |
| 78 | + notify({ |
| 79 | + duration: 3000, |
| 80 | + message: ( |
| 81 | + <div className={styles.notification}> |
| 82 | + <CodeBracketIcon className={styles.icon} /> |
| 83 | + {t('components.common.codebox.copied')} |
| 84 | + </div> |
| 85 | + ), |
| 86 | + }); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <div className={styles.root}> |
| 92 | + <pre ref={ref} className={styles.content} tabIndex={0}> |
| 93 | + {transformCode(children)} |
| 94 | + </pre> |
| 95 | + |
| 96 | + {language && ( |
| 97 | + <div className={styles.footer}> |
| 98 | + <span className={styles.language}>{language}</span> |
| 99 | + |
| 100 | + {showCopyButton && ( |
| 101 | + <Button type="button" className={styles.action} onClick={onCopy}> |
| 102 | + <DocumentDuplicateIcon className={styles.icon} /> |
| 103 | + {t('components.common.codebox.copy')} |
| 104 | + </Button> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + )} |
| 108 | + </div> |
| 109 | + ); |
| 110 | +}; |
| 111 | + |
| 112 | +export default CodeBox; |
0 commit comments