48 lines
1016 B
TypeScript
Raw Normal View History

2024-02-20 06:52:39 +00:00
"use client";
import { getFileExt } from "@/lib/utils";
import React from "react";
2024-02-20 18:03:43 +07:00
import CodeEditor from "../../../../components/ui/code-editor";
2024-02-20 06:52:39 +00:00
import trpc from "@/lib/trpc";
type Props = {
id: number;
2024-02-20 18:03:43 +07:00
onFileContentChange?: () => void;
2024-02-20 06:52:39 +00:00
};
2024-02-21 02:01:35 +07:00
const FileViewer = ({ id, onFileContentChange }: Props) => {
2024-02-20 06:52:39 +00:00
const { data, isLoading, refetch } = trpc.file.getById.useQuery(id);
const updateFileContent = trpc.file.update.useMutation({
onSuccess: () => {
2024-02-20 18:03:43 +07:00
if (onFileContentChange) onFileContentChange();
2024-02-20 06:52:39 +00:00
refetch();
},
});
if (isLoading) {
return <p>Loading...</p>;
}
2024-02-21 02:01:35 +07:00
if (!data || data.isDirectory) {
2024-02-20 06:52:39 +00:00
return <p>File not found.</p>;
}
const { filename } = data;
2024-02-21 02:01:35 +07:00
if (!data.isFile) {
2024-02-20 06:52:39 +00:00
const ext = getFileExt(filename);
return (
<CodeEditor
lang={ext}
value={data?.content || ""}
formatOnSave
onChange={(val) => updateFileContent.mutate({ id, content: val })}
/>
);
}
return null;
};
2024-02-21 02:01:35 +07:00
export default FileViewer;