Skip to content

Commit d92fdf6

Browse files
Load all line changed to load 10 lines (#3)
* Load all line changed to load 10 lines * Tooltip text fix * Chore: Remove magic number anti-pattern * Chore: try search flex on mobile view * Chore: add query params * Chore: Api route fix and vercel.json removed * chore: fix missing spots loadcount
1 parent e8b7490 commit d92fdf6

File tree

7 files changed

+128
-33
lines changed

7 files changed

+128
-33
lines changed

frontend/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"http-status-codes": "^2.3.0",
1818
"prism-react-renderer": "^2.1.0",
1919
"react": "^18.2.0",
20-
"react-dom": "^18.2.0"
20+
"react-dom": "^18.2.0",
21+
"react-router-dom": "^6.18.0"
2122
},
2223
"devDependencies": {
2324
"@types/node": "^20.8.0",

frontend/pnpm-lock.yaml

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/src/components/CodeContainer/index.tsx

+52-10
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,45 @@ type CodeContainerProps = {
3030
overlap_to: number;
3131
}[];
3232
};
33+
const loadCount = 10;
3334

3435
export function CodeContainer(props: CodeContainerProps) {
3536
const { context, line_from, sub_matches, line_to } = props;
3637
const [codeLineFrom, setCodeLineFrom] = useMountedState(line_from);
37-
const [codeLineTo, setCodeLineTo] = useMountedState(line_to);
38+
const [codeLineTo, setCodeLineTo] = useMountedState(0);
3839
const [code, setCode] = useMountedState(props.context.snippet);
3940

4041
const loadUpperCode = () => {
41-
setCodeLineFrom(1);
42-
setCode(`${context.upper_lines}${code}`);
42+
const upperCodeArray = context.upper_lines.split("\n");
43+
const upperCode = upperCodeArray
44+
.slice(
45+
codeLineFrom - loadCount + 1 > 0 ? codeLineFrom - loadCount + 1 : 0,
46+
codeLineFrom
47+
)
48+
.join("\n");
49+
setCodeLineFrom((number) => {
50+
return number - loadCount > 0 ? number - loadCount : 1;
51+
});
52+
setCode(`${upperCode}${code}`);
4353
};
4454

4555
const loadLowerCode = () => {
46-
setCodeLineTo(Infinity);
47-
setCode(`${code}${context.lower_lines}`);
56+
const lowerCodeArray = context.lower_lines.split("\n");
57+
if (lowerCodeArray.length > codeLineTo + loadCount) {
58+
const lowerCode = lowerCodeArray
59+
.slice(codeLineTo, codeLineTo + loadCount + 1)
60+
.join("\n");
61+
setCodeLineTo((number) => {
62+
return number + loadCount;
63+
});
64+
setCode(`${code}${lowerCode}`);
65+
} else {
66+
const lowerCode = lowerCodeArray
67+
.slice(codeLineTo, lowerCodeArray.length)
68+
.join("\n");
69+
setCodeLineTo(lowerCodeArray.length);
70+
setCode(`${code}${lowerCode}`);
71+
}
4872
};
4973

5074
return (
@@ -100,14 +124,19 @@ export function CodeContainer(props: CodeContainerProps) {
100124
}
101125
}
102126
>
103-
<Tooltip label={`Expand all`} withArrow>
127+
<Tooltip
128+
label={`Load ${
129+
codeLineFrom - loadCount > 0 ? codeLineFrom - loadCount : 1
130+
} to ${codeLineFrom - 1} `}
131+
withArrow
132+
>
104133
<span className={classes.codeLoad} onClick={loadUpperCode}>
105134
<IconFoldUp />
106135
</span>
107136
</Tooltip>
108137
<div className={classes.codeLine}>
109138
<span className={classes.codeNumber}>
110-
@@ {1} - {line_from} of {context.file_name}
139+
@@ {1} - {codeLineFrom - 1} of {context.file_name}
111140
</span>
112141
</div>
113142
</div>
@@ -145,7 +174,10 @@ export function CodeContainer(props: CodeContainerProps) {
145174
))}
146175
<div
147176
style={
148-
codeLineTo === Infinity
177+
codeLineTo === context.lower_lines.split("\n").length ||
178+
context.lower_lines === undefined ||
179+
context.lower_lines === null ||
180+
context.lower_lines === ""
149181
? { display: "none" }
150182
: {
151183
display: "flex",
@@ -158,7 +190,15 @@ export function CodeContainer(props: CodeContainerProps) {
158190
}
159191
}
160192
>
161-
<Tooltip label={`Expand all`} withArrow>
193+
<Tooltip
194+
label={`Load ${line_to + codeLineTo + 2} to ${
195+
line_to + codeLineTo + loadCount+1 <
196+
context.lower_lines.split("\n").length + line_to
197+
? line_to + codeLineTo + loadCount+1
198+
: context.lower_lines.split("\n").length + line_to
199+
}`}
200+
withArrow
201+
>
162202
<span
163203
className={classes.codeLoad}
164204
style={{
@@ -171,7 +211,9 @@ export function CodeContainer(props: CodeContainerProps) {
171211
</Tooltip>
172212
<div className={classes.codeLine}>
173213
<span className={classes.codeNumber}>
174-
@@ {line_to} - {"end of file"} of {context.file_name}
214+
@@ {line_to + codeLineTo + 2} -{" "}
215+
{context.lower_lines.split("\n").length + line_to} of{" "}
216+
{context.file_name}
175217
</span>
176218
</div>
177219
</div>

frontend/src/components/DemoSearch/DemoSearch.module.css

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
margin: 0 auto;
77
padding: var(--mantine-spacing-md);
88
gap: var(--mantine-spacing-sm);
9+
@media (max-width: 768px) {
10+
flex-direction: column;
11+
}
912
}
1013

1114
.demoBtn{

frontend/src/components/MainSection/index.tsx

+30-2
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,58 @@ import {
1111
import { IconSearch } from "@tabler/icons-react";
1212
import useMountedState from "@/hooks/useMountedState";
1313
import { useGetSearchResult } from "@/hooks/useGetSearchResult";
14-
import { getHotkeyHandler } from "@mantine/hooks";
14+
import { getHotkeyHandler, useHotkeys } from "@mantine/hooks";
1515
import { FileTree } from "../FIleTree";
1616
import { CodeContainer } from "../CodeContainer";
1717
import classes from "./Main.module.css";
1818
import DemoSearch from "../DemoSearch";
19+
import { useSearchParams } from "react-router-dom";
20+
import { useEffect } from "react";
1921

2022
export default function Main() {
2123
const [query, setQuery] = useMountedState("");
2224
const { data, getSearch, loading, error, resetData } = useGetSearchResult();
25+
const [searchParams, setSearchParams] = useSearchParams();
2326

27+
useHotkeys([
28+
[
29+
"/",
30+
() => {
31+
const input = document.querySelector("input");
32+
input?.focus();
33+
},
34+
],
35+
]);
2436
const handleSubmit = () => {
2537
resetData();
2638
if (query) {
2739
getSearch(query);
40+
setSearchParams({ query });
2841
}
2942
};
3043

3144
const handleDemoSearch = (query: string) => {
3245
resetData();
3346
if (query) {
47+
setSearchParams({ query: query });
3448
setQuery(query);
3549
getSearch(query);
3650
}
3751
};
3852

53+
useEffect(() => {
54+
if (searchParams.get("query")&&searchParams.get("query")!==query) {
55+
handleDemoSearch(searchParams.get("query") ?? "");
56+
}
57+
}, [searchParams.get("query")]);
58+
59+
useEffect(() => {
60+
if (query === "") {
61+
resetData();
62+
window.history.replaceState({}, "", "/");
63+
}
64+
}, [query]);
65+
3966
return (
4067
<Container size="lg">
4168
<TextInput
@@ -57,7 +84,7 @@ export default function Main() {
5784
}
5885
rightSectionWidth={"6rem"}
5986
value={query}
60-
pt={ data ? "1rem" : "5rem" }
87+
pt={data || loading ? "1rem" : "5rem"}
6188
required
6289
onChange={(event: any) => setQuery(event.currentTarget.value)}
6390
onKeyDown={getHotkeyHandler([["Enter", handleSubmit]])}
@@ -68,6 +95,7 @@ export default function Main() {
6895
zIndex: 100,
6996
backgroundColor: "#fff",
7097
}}
98+
ref={(input) => input && input.focus()}
7199
/>
72100
{data && (
73101
<Box

frontend/src/main.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom/client'
3-
import App from './App.tsx'
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
import App from "./App.tsx";
4+
import { BrowserRouter } from "react-router-dom";
45

5-
ReactDOM.createRoot(document.getElementById('root')!).render(
6+
ReactDOM.createRoot(document.getElementById("root")!).render(
67
<React.StrictMode>
7-
<App />
8-
</React.StrictMode>,
9-
)
8+
<BrowserRouter>
9+
<App />
10+
</BrowserRouter>
11+
</React.StrictMode>
12+
);

frontend/vercel.json

-13
This file was deleted.

0 commit comments

Comments
 (0)