mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-12 04:15:30 +00:00
convert file match container to link
This commit is contained in:
parent
ef46c0181d
commit
073954937e
2 changed files with 20 additions and 45 deletions
|
|
@ -1,27 +1,22 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useCallback } from "react";
|
|
||||||
import { SearchResultFile, SearchResultChunk } from "@/features/search/types";
|
import { SearchResultFile, SearchResultChunk } from "@/features/search/types";
|
||||||
import { LightweightCodeHighlighter } from "@/app/[domain]/components/lightweightCodeHighlighter";
|
import { LightweightCodeHighlighter } from "@/app/[domain]/components/lightweightCodeHighlighter";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { getBrowsePath } from "@/app/[domain]/browse/hooks/useBrowseNavigation";
|
||||||
|
import { useDomain } from "@/hooks/useDomain";
|
||||||
|
|
||||||
|
|
||||||
interface FileMatchProps {
|
interface FileMatchProps {
|
||||||
match: SearchResultChunk;
|
match: SearchResultChunk;
|
||||||
file: SearchResultFile;
|
file: SearchResultFile;
|
||||||
onOpen: (startLineNumber: number, endLineNumber: number, isCtrlKeyPressed: boolean) => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const FileMatch = ({
|
export const FileMatch = ({
|
||||||
match,
|
match,
|
||||||
file,
|
file,
|
||||||
onOpen: _onOpen,
|
|
||||||
}: FileMatchProps) => {
|
}: FileMatchProps) => {
|
||||||
const onOpen = useCallback((isCtrlKeyPressed: boolean) => {
|
const domain = useDomain();
|
||||||
const startLineNumber = match.contentStart.lineNumber;
|
|
||||||
const endLineNumber = match.content.trimEnd().split('\n').length + startLineNumber - 1;
|
|
||||||
|
|
||||||
_onOpen(startLineNumber, endLineNumber, isCtrlKeyPressed);
|
|
||||||
}, [match.content, match.contentStart.lineNumber, _onOpen]);
|
|
||||||
|
|
||||||
// If it's just the title, don't show a code preview
|
// If it's just the title, don't show a code preview
|
||||||
if (match.matchRanges.length === 0) {
|
if (match.matchRanges.length === 0) {
|
||||||
|
|
@ -29,19 +24,24 @@ export const FileMatch = ({
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<Link
|
||||||
tabIndex={0}
|
tabIndex={0}
|
||||||
className="cursor-pointer focus:ring-inset focus:ring-4 bg-background hover:bg-editor-lineHighlight"
|
className="cursor-pointer focus:ring-inset focus:ring-4 bg-background hover:bg-editor-lineHighlight"
|
||||||
onKeyDown={(e) => {
|
href={getBrowsePath({
|
||||||
if (e.key !== "Enter") {
|
repoName: file.repository,
|
||||||
return;
|
revisionName: file.branches?.[0] ?? 'HEAD',
|
||||||
|
path: file.fileName.text,
|
||||||
|
pathType: 'blob',
|
||||||
|
domain,
|
||||||
|
highlightRange: {
|
||||||
|
start: {
|
||||||
|
lineNumber: match.contentStart.lineNumber,
|
||||||
|
},
|
||||||
|
end: {
|
||||||
|
lineNumber: match.content.trimEnd().split('\n').length + match.contentStart.lineNumber - 1,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
onOpen(e.metaKey || e.ctrlKey);
|
})}
|
||||||
}}
|
|
||||||
onClick={(e) => {
|
|
||||||
onOpen(e.metaKey || e.ctrlKey);
|
|
||||||
}}
|
|
||||||
title="open file: click, open file preview: cmd/ctrl + click"
|
title="open file: click, open file preview: cmd/ctrl + click"
|
||||||
>
|
>
|
||||||
<LightweightCodeHighlighter
|
<LightweightCodeHighlighter
|
||||||
|
|
@ -53,6 +53,6 @@ export const FileMatch = ({
|
||||||
>
|
>
|
||||||
{match.content}
|
{match.content}
|
||||||
</LightweightCodeHighlighter>
|
</LightweightCodeHighlighter>
|
||||||
</div>
|
</Link>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -7,7 +7,6 @@ import { useMemo } from "react";
|
||||||
import { FileMatch } from "./fileMatch";
|
import { FileMatch } from "./fileMatch";
|
||||||
import { RepositoryInfo, SearchResultFile } from "@/features/search/types";
|
import { RepositoryInfo, SearchResultFile } from "@/features/search/types";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { useBrowseNavigation } from "@/app/[domain]/browse/hooks/useBrowseNavigation";
|
|
||||||
|
|
||||||
export const MAX_MATCHES_TO_PREVIEW = 3;
|
export const MAX_MATCHES_TO_PREVIEW = 3;
|
||||||
|
|
||||||
|
|
@ -33,7 +32,6 @@ export const FileMatchContainer = ({
|
||||||
const matchCount = useMemo(() => {
|
const matchCount = useMemo(() => {
|
||||||
return file.chunks.length;
|
return file.chunks.length;
|
||||||
}, [file]);
|
}, [file]);
|
||||||
const { navigateToPath } = useBrowseNavigation();
|
|
||||||
|
|
||||||
const matches = useMemo(() => {
|
const matches = useMemo(() => {
|
||||||
const sortedMatches = file.chunks.sort((a, b) => {
|
const sortedMatches = file.chunks.sort((a, b) => {
|
||||||
|
|
@ -123,29 +121,6 @@ export const FileMatchContainer = ({
|
||||||
<FileMatch
|
<FileMatch
|
||||||
match={match}
|
match={match}
|
||||||
file={file}
|
file={file}
|
||||||
onOpen={(startLineNumber, endLineNumber, isCtrlKeyPressed) => {
|
|
||||||
if (isCtrlKeyPressed) {
|
|
||||||
const matchIndex = matches.slice(0, index).reduce((acc, match) => {
|
|
||||||
return acc + match.matchRanges.length;
|
|
||||||
}, 0);
|
|
||||||
onOpenFilePreview(matchIndex);
|
|
||||||
} else {
|
|
||||||
navigateToPath({
|
|
||||||
repoName: file.repository,
|
|
||||||
revisionName: file.branches?.[0] ?? 'HEAD',
|
|
||||||
path: file.fileName.text,
|
|
||||||
pathType: 'blob',
|
|
||||||
highlightRange: {
|
|
||||||
start: {
|
|
||||||
lineNumber: startLineNumber,
|
|
||||||
},
|
|
||||||
end: {
|
|
||||||
lineNumber: endLineNumber,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
{(index !== matches.length - 1 || isMoreContentButtonVisible) && (
|
{(index !== matches.length - 1 || isMoreContentButtonVisible) && (
|
||||||
<Separator className="bg-accent" />
|
<Separator className="bg-accent" />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue