feat(nvim): improve tree and file switching, and implements links

This commit is contained in:
Pihkaal
2024-05-31 22:56:08 +02:00
parent 0c79ea457c
commit 96207863a9
12 changed files with 361 additions and 309 deletions

View File

@@ -4,59 +4,23 @@ import { NvimEditor } from "./NvimEditor";
import { NvimInput } from "./NvimInput";
import { NvimStatusBar } from "./NvimStatusBar";
import { NvimTree } from "./NvimTree";
import { useLocation, useNavigate } from "react-router-dom";
import { useEffect, useState } from "react";
import axios from "axios";
const fetchData = async (
branch: string,
repo: string,
file: string,
): Promise<string | null> => {
try {
const response = await axios.get<string>(
`https://raw.githubusercontent.com/pihkaal/${repo}/${branch}/${file}`,
);
return response.data;
} catch {
return null;
}
};
import { useState } from "react";
import { File } from "~/utils/types";
export const Nvim = (_props: {}) => {
const kitty = useKitty();
const location = useLocation();
const navigate = useNavigate();
const [data, setData] = useState<string>();
const [activeFile, setActiveFile] = useState<string>();
useEffect(() => {
const params = new URLSearchParams(location.search);
const view = params.get("view");
if (!view) {
navigate("?view=README.md");
return;
const handleOpenFile = (file: File) => {
if (file.type === "link") {
window.open(file.url, "_blank")?.focus();
} else {
setActiveFile(
`https://raw.githubusercontent.com/pihkaal/${file.repo}/main/${file.fileName}`,
);
}
const path = view.split("/");
if (path.length === 1) {
path.splice(0, 0, "pihkaal");
}
const repo = path[0]!;
const file = path[1]!;
void (async () => {
const data =
(await fetchData("main", repo, file)) ??
(await fetchData("dev", repo, file));
if (!data) {
navigate("?view=README.md");
return;
}
setData(data);
})();
}, [location, navigate]);
};
return (
<div
@@ -69,10 +33,10 @@ export const Nvim = (_props: {}) => {
{kitty && (
<>
<div style={{ gridArea: "1 / 1 / 1 / 2" }}>
<NvimTree {...kitty} />
<NvimTree {...kitty} onOpen={handleOpenFile} />
</div>
<div style={{ gridArea: "1 / 2 / 1 / 3", overflow: "scroll" }}>
<NvimEditor data={data ?? ""} />
<NvimEditor source={activeFile} />
</div>
<div style={{ gridArea: "2 / 1 / 2 / 3" }}>
<NvimStatusBar