'use client'; import { EditorContextMenu } from "@/app/[domain]/components/editorContextMenu"; import { Button } from "@/components/ui/button"; import { ScrollArea } from "@/components/ui/scroll-area"; import { useCodeMirrorTheme } from "@/hooks/useCodeMirrorTheme"; import { useKeymapExtension } from "@/hooks/useKeymapExtension"; import { useSyntaxHighlightingExtension } from "@/hooks/useSyntaxHighlightingExtension"; import { gutterWidthExtension } from "@/lib/extensions/gutterWidthExtension"; import { highlightRanges, searchResultHighlightExtension } from "@/lib/extensions/searchResultHighlightExtension"; import { SearchResultFileMatch } from "@/lib/types"; import { search } from "@codemirror/search"; import { EditorView } from "@codemirror/view"; import { Cross1Icon, FileIcon } from "@radix-ui/react-icons"; import { Scrollbar } from "@radix-ui/react-scroll-area"; import CodeMirror, { ReactCodeMirrorRef, SelectionRange } from '@uiw/react-codemirror'; import clsx from "clsx"; import { ArrowDown, ArrowUp } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; export interface CodePreviewFile { content: string; filepath: string; link?: string; matches: SearchResultFileMatch[]; language: string; revision: string; } interface CodePreviewProps { file?: CodePreviewFile; repoName?: string; selectedMatchIndex: number; onSelectedMatchIndexChange: (index: number) => void; onClose: () => void; } export const CodePreview = ({ file, repoName, selectedMatchIndex, onSelectedMatchIndexChange, onClose, }: CodePreviewProps) => { const [editorRef, setEditorRef] = useState(null); const [gutterWidth, setGutterWidth] = useState(0); const theme = useCodeMirrorTheme(); const keymapExtension = useKeymapExtension(editorRef?.view); const syntaxHighlighting = useSyntaxHighlightingExtension(file?.language ?? '', editorRef?.view); const [currentSelection, setCurrentSelection] = useState(); const extensions = useMemo(() => { return [ keymapExtension, gutterWidthExtension, syntaxHighlighting, EditorView.lineWrapping, searchResultHighlightExtension(), search({ top: true, }), EditorView.updateListener.of((update) => { const width = update.view.plugin(gutterWidthExtension)?.width; if (width) { setGutterWidth(width); } }), EditorView.updateListener.of((update) => { // @note: it's important we reset the selection when // the document changes... otherwise we will get a floating // context menu where it shouldn't be. if (update.selectionSet || update.docChanged) { setCurrentSelection(update.state.selection.main); } }) ]; }, [keymapExtension, syntaxHighlighting]); const ranges = useMemo(() => { if (!file || !file.matches.length) { return []; } return file.matches.flatMap((match) => { return match.Ranges; }) }, [file]); useEffect(() => { if (!file || !editorRef?.view) { return; } highlightRanges(selectedMatchIndex, ranges, editorRef.view); }, [ranges, selectedMatchIndex, file, editorRef]); const onUpClicked = useCallback(() => { onSelectedMatchIndexChange(selectedMatchIndex - 1); }, [onSelectedMatchIndexChange, selectedMatchIndex]); const onDownClicked = useCallback(() => { onSelectedMatchIndexChange(selectedMatchIndex + 1); }, [onSelectedMatchIndexChange, selectedMatchIndex]); return (
{/* Gutter icon */}
{/* File path */}
{ if (file?.link) { window.open(file.link, "_blank"); } }} title={file?.filepath} > {file?.filepath}
{/* Match selector */} {file && file.matches.length > 0 && ( <>

{`${selectedMatchIndex + 1} of ${ranges.length}`}

)} {/* Close button */}
{ editorRef?.view && file?.filepath && repoName && currentSelection && ( ) }
) }