2024-08-28 00:28:35 +00:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { ScrollArea } from "@/components/ui/scroll-area";
|
2024-08-30 00:47:35 +00:00
|
|
|
import { useExtensionWithDependency } from "@/hooks/useExtensionWithDependency";
|
|
|
|
|
import { gutterWidthExtension } from "@/lib/extensions/gutterWidthExtension";
|
|
|
|
|
import { markMatches, searchResultHighlightExtension } from "@/lib/extensions/searchResultHighlightExtension";
|
|
|
|
|
import { ZoektMatch } from "@/lib/types";
|
2024-08-28 00:28:35 +00:00
|
|
|
import { defaultKeymap } from "@codemirror/commands";
|
|
|
|
|
import { javascript } from "@codemirror/lang-javascript";
|
2024-08-30 00:47:35 +00:00
|
|
|
import { search } from "@codemirror/search";
|
|
|
|
|
import { EditorView, keymap } from "@codemirror/view";
|
|
|
|
|
import { Cross1Icon, FileIcon } from "@radix-ui/react-icons";
|
2024-08-28 00:28:35 +00:00
|
|
|
import { Scrollbar } from "@radix-ui/react-scroll-area";
|
2024-08-30 00:47:35 +00:00
|
|
|
import { vim } from "@replit/codemirror-vim";
|
|
|
|
|
import CodeMirror, { ReactCodeMirrorRef } from '@uiw/react-codemirror';
|
2024-08-30 04:06:48 +00:00
|
|
|
import { ArrowDown, ArrowUp } from "lucide-react";
|
2024-08-30 00:47:35 +00:00
|
|
|
import { useTheme } from "next-themes";
|
2024-08-30 04:06:48 +00:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2024-08-28 00:28:35 +00:00
|
|
|
|
2024-08-30 00:47:35 +00:00
|
|
|
export interface CodePreviewFile {
|
|
|
|
|
content: string;
|
2024-08-28 00:28:35 +00:00
|
|
|
filepath: string;
|
2024-08-30 00:47:35 +00:00
|
|
|
matches: ZoektMatch[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface CodePreviewProps {
|
|
|
|
|
file?: CodePreviewFile;
|
2024-08-28 00:28:35 +00:00
|
|
|
keymapType: "default" | "vim";
|
2024-08-30 04:06:48 +00:00
|
|
|
selectedMatchIndex: number;
|
|
|
|
|
onSelectedMatchIndexChange: (index: number) => void;
|
2024-08-28 00:28:35 +00:00
|
|
|
onClose: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const CodePreview = ({
|
2024-08-30 00:47:35 +00:00
|
|
|
file,
|
2024-08-28 00:28:35 +00:00
|
|
|
keymapType,
|
2024-08-30 04:06:48 +00:00
|
|
|
selectedMatchIndex,
|
|
|
|
|
onSelectedMatchIndexChange,
|
2024-08-28 00:28:35 +00:00
|
|
|
onClose,
|
|
|
|
|
}: CodePreviewProps) => {
|
2024-08-30 00:47:35 +00:00
|
|
|
const editorRef = useRef<ReactCodeMirrorRef>(null);
|
2024-08-30 04:06:48 +00:00
|
|
|
|
2024-08-28 00:28:35 +00:00
|
|
|
const { theme: _theme, systemTheme } = useTheme();
|
2024-08-30 04:06:48 +00:00
|
|
|
|
2024-08-28 00:28:35 +00:00
|
|
|
const theme = useMemo(() => {
|
|
|
|
|
if (_theme === "system") {
|
|
|
|
|
return systemTheme ?? "light";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _theme ?? "light";
|
2024-08-28 04:09:25 +00:00
|
|
|
}, [_theme, systemTheme]);
|
2024-08-28 00:28:35 +00:00
|
|
|
|
|
|
|
|
const [gutterWidth, setGutterWidth] = useState(0);
|
2024-08-30 00:47:35 +00:00
|
|
|
|
|
|
|
|
const keymapExtension = useExtensionWithDependency(
|
|
|
|
|
editorRef.current?.view ?? null,
|
|
|
|
|
() => {
|
|
|
|
|
switch (keymapType) {
|
|
|
|
|
case "default":
|
|
|
|
|
return keymap.of(defaultKeymap);
|
|
|
|
|
case "vim":
|
|
|
|
|
return vim();
|
2024-08-28 00:28:35 +00:00
|
|
|
}
|
2024-08-30 00:47:35 +00:00
|
|
|
},
|
|
|
|
|
[keymapType]
|
|
|
|
|
);
|
|
|
|
|
|
2024-08-30 04:06:48 +00:00
|
|
|
const extensions = useMemo(() => {
|
|
|
|
|
return [
|
|
|
|
|
keymapExtension,
|
|
|
|
|
gutterWidthExtension,
|
|
|
|
|
javascript(),
|
|
|
|
|
searchResultHighlightExtension(),
|
|
|
|
|
search({
|
|
|
|
|
top: true,
|
|
|
|
|
}),
|
|
|
|
|
EditorView.updateListener.of(update => {
|
|
|
|
|
const width = update.view.plugin(gutterWidthExtension)?.width;
|
|
|
|
|
if (width) {
|
|
|
|
|
setGutterWidth(width);
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
];
|
|
|
|
|
}, [keymapExtension]);
|
|
|
|
|
|
2024-08-30 00:47:35 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!file || !editorRef.current?.view) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 04:06:48 +00:00
|
|
|
markMatches(selectedMatchIndex, file.matches, editorRef.current.view);
|
|
|
|
|
}, [file?.matches, selectedMatchIndex]);
|
|
|
|
|
|
|
|
|
|
const onUpClicked = useCallback(() => {
|
|
|
|
|
onSelectedMatchIndexChange(selectedMatchIndex - 1);
|
|
|
|
|
}, [selectedMatchIndex]);
|
|
|
|
|
|
|
|
|
|
const onDownClicked = useCallback(() => {
|
|
|
|
|
onSelectedMatchIndexChange(selectedMatchIndex + 1);
|
|
|
|
|
}, [selectedMatchIndex]);
|
2024-08-28 00:28:35 +00:00
|
|
|
|
|
|
|
|
return (
|
2024-08-30 04:06:48 +00:00
|
|
|
<div className="flex flex-col h-full">
|
2024-08-28 00:28:35 +00:00
|
|
|
<div className="flex flex-row bg-cyan-200 dark:bg-cyan-900 items-center justify-between pr-3">
|
|
|
|
|
<div className="flex flex-row">
|
|
|
|
|
<div
|
|
|
|
|
style={{ width: `${gutterWidth}px` }}
|
|
|
|
|
className="flex justify-center items-center"
|
|
|
|
|
>
|
|
|
|
|
<FileIcon className="h-4 w-4" />
|
|
|
|
|
</div>
|
2024-08-30 00:47:35 +00:00
|
|
|
<span>{file?.filepath}</span>
|
2024-08-28 00:28:35 +00:00
|
|
|
</div>
|
2024-08-30 04:06:48 +00:00
|
|
|
<div className="flex flex-row gap-1 items-center">
|
|
|
|
|
<p className="text-sm">{`${selectedMatchIndex + 1} of ${file?.matches.length}`}</p>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
className="h-6 w-6"
|
|
|
|
|
disabled={selectedMatchIndex === 0}
|
|
|
|
|
onClick={onUpClicked}
|
|
|
|
|
>
|
|
|
|
|
<ArrowUp className="h-4 w-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
className="h-6 w-6"
|
|
|
|
|
onClick={onDownClicked}
|
|
|
|
|
disabled={file ? selectedMatchIndex === file?.matches.length - 1 : true}
|
|
|
|
|
>
|
|
|
|
|
<ArrowDown className="h-4 w-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
className="h-6 w-6"
|
2024-08-28 00:28:35 +00:00
|
|
|
onClick={onClose}
|
2024-08-30 04:06:48 +00:00
|
|
|
>
|
|
|
|
|
<Cross1Icon className="h-4 w-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2024-08-28 00:28:35 +00:00
|
|
|
</div>
|
2024-08-30 04:06:48 +00:00
|
|
|
<ScrollArea className="h-full overflow-auto flex-1">
|
2024-08-28 00:28:35 +00:00
|
|
|
<CodeMirror
|
2024-08-30 00:47:35 +00:00
|
|
|
ref={editorRef}
|
2024-08-28 00:28:35 +00:00
|
|
|
readOnly={true}
|
2024-08-30 00:47:35 +00:00
|
|
|
value={file?.content}
|
2024-08-28 00:28:35 +00:00
|
|
|
theme={theme === "dark" ? "dark" : "light"}
|
2024-08-30 04:06:48 +00:00
|
|
|
extensions={extensions}
|
2024-08-28 00:28:35 +00:00
|
|
|
/>
|
|
|
|
|
<Scrollbar orientation="vertical" />
|
|
|
|
|
</ScrollArea>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|