mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-29 00:59:37 +07:00
48 lines
1016 B
TypeScript
48 lines
1016 B
TypeScript
"use client";
|
|
|
|
import { getFileExt } from "~/lib/utils";
|
|
import React from "react";
|
|
import CodeEditor from "../../../../components/ui/code-editor";
|
|
import trpc from "~/lib/trpc";
|
|
|
|
type Props = {
|
|
id: number;
|
|
onFileContentChange?: () => void;
|
|
};
|
|
|
|
const FileViewer = ({ id, onFileContentChange }: Props) => {
|
|
const { data, isLoading, refetch } = trpc.file.getById.useQuery(id);
|
|
const updateFileContent = trpc.file.update.useMutation({
|
|
onSuccess: () => {
|
|
if (onFileContentChange) onFileContentChange();
|
|
refetch();
|
|
},
|
|
});
|
|
|
|
if (isLoading) {
|
|
return <p>Loading...</p>;
|
|
}
|
|
if (!data || data.isDirectory) {
|
|
return <p>File not found.</p>;
|
|
}
|
|
|
|
const { filename } = data;
|
|
|
|
if (!data.isFile) {
|
|
const ext = getFileExt(filename);
|
|
|
|
return (
|
|
<CodeEditor
|
|
lang={ext}
|
|
value={data?.content || ""}
|
|
formatOnSave
|
|
onChange={(val) => updateFileContent.mutate({ id, content: val })}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
export default FileViewer;
|