From bccd5502758d13e57736df556762aa86a31bc52f Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:52:24 +0700 Subject: [PATCH] Add switch between markdown or raw files on rich preview and readme --- src/app/@markdown.tsx | 83 +++++++++++++++---------- src/app/@preview.layout.tsx | 75 ++++++++++++++++++++++ src/app/@preview.rich.tsx | 37 +++++++---- src/app/@readme.tsx | 37 +++++++++++ src/app/@rich-header.tsx | 39 ++++++++++++ src/app/[...rest]/page.tsx | 120 +++++++++++++----------------------- src/app/page.tsx | 34 +++++----- 7 files changed, 290 insertions(+), 135 deletions(-) create mode 100644 src/app/@preview.layout.tsx create mode 100644 src/app/@readme.tsx create mode 100644 src/app/@rich-header.tsx diff --git a/src/app/@markdown.tsx b/src/app/@markdown.tsx index 041763d..f70a7e5 100644 --- a/src/app/@markdown.tsx +++ b/src/app/@markdown.tsx @@ -10,6 +10,7 @@ import remarkGfm from "remark-gfm"; import remarkMath from "remark-math"; import remarkSlug from "remark-slug"; import remarkToc from "remark-toc"; +import { cn } from "~/utils"; import Icon from "~/components/Icon"; @@ -17,41 +18,59 @@ import "./highlight.css"; type Props = { content: string; + view: "markdown" | "raw"; }; -export default function Markdown({ content }: Props) { +export default function Markdown({ content, view }: Props) { return ( -
- ( -

- {children} -

- ), - pre: PreComponent, - code: ({ node, inline, className, children, ...props }) => ( - - {children} - - ), - }} +
+
- {content} - +
+          {content}
+        
+
+
+ ( +

+ {children} +

+ ), + pre: PreComponent, + code: ({ node, inline, className, children, ...props }) => ( + + {children} + + ), + }} + > + {content} +
+
); } diff --git a/src/app/@preview.layout.tsx b/src/app/@preview.layout.tsx new file mode 100644 index 0000000..287dfce --- /dev/null +++ b/src/app/@preview.layout.tsx @@ -0,0 +1,75 @@ +"use client"; + +import { useState } from "react"; +import { z } from "zod"; +import { Schema_File } from "~/schema"; + +import { Card, CardContent } from "~/components/ui/card"; + +import { getFileType } from "~/utils/previewHelper"; + +import PreviewAction from "./@preview.action"; +import PreviewAudio from "./@preview.audio"; +import PreviewDoc from "./@preview.doc"; +import PreviewImage from "./@preview.image"; +import PreviewManga from "./@preview.manga"; +import PreviewRich from "./@preview.rich"; +import PreviewUnknown from "./@preview.unknown"; +import PreviewVideo from "./@preview.video"; +import RichHeader from "./@rich-header"; + +type Props = { + data: z.infer; + fileType: "unknown" | ReturnType; +}; +export default function FilePreviewLayout({ data, fileType }: Props) { + const [view, setView] = useState<"markdown" | "raw">("markdown"); + + return ( + <> + + + +
+ {fileType === "image" ? ( + + ) : fileType === "audio" ? ( + + ) : fileType === "video" ? ( + + ) : fileType === "code" ? ( + + ) : fileType === "text" ? ( + + ) : fileType === "markdown" ? ( + + ) : fileType === "document" ? ( + + ) : fileType === "pdf" ? ( + + ) : fileType === "manga" ? ( + + ) : ( + + )} +
+
+
+ + + ); +} diff --git a/src/app/@preview.rich.tsx b/src/app/@preview.rich.tsx index ff2095f..a546939 100644 --- a/src/app/@preview.rich.tsx +++ b/src/app/@preview.rich.tsx @@ -13,9 +13,11 @@ import { GetContent } from "./actions"; type Props = { file: z.infer; + view: "markdown" | "raw"; code?: boolean; }; -export default function PreviewRich({ file, code }: Props) { +export default function PreviewRich({ file, code, view }: Props) { + const [fetchedContent, setFetchedContent] = useState(""); const [content, setContent] = useState(""); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); @@ -30,11 +32,12 @@ export default function PreviewRich({ file, code }: Props) { setError("Looks like there is no content to preview"); return; } - if (code) { - setContent(`\`\`\`${file.fileExtension}\n${text}\`\`\``); - } else { - setContent(text); - } + // setFetchedContent(text); + // if (code) { + // setContent(`\`\`\`${file.fileExtension}\n${text}\`\`\``); + // } else { + setContent(text); + // } } catch (error) { const e = error as Error; console.error(e); @@ -46,7 +49,7 @@ export default function PreviewRich({ file, code }: Props) { }, [file, code]); return ( -
+
{loading ? (
) : (
- +
("markdown"); + + return ( +
+ + + + + + +
+ ); +} diff --git a/src/app/@rich-header.tsx b/src/app/@rich-header.tsx new file mode 100644 index 0000000..e3a2179 --- /dev/null +++ b/src/app/@rich-header.tsx @@ -0,0 +1,39 @@ +"use client"; + +import { Button } from "~/components/ui/button"; +import { CardHeader, CardTitle } from "~/components/ui/card"; +import { Separator } from "~/components/ui/separator"; + +type Props = { + title: string; + view: "markdown" | "raw"; + onViewChange: (value: "markdown" | "raw") => void; +}; +export default function RichHeader({ title, view, onViewChange }: Props) { + return ( + +
+ {title} +
+ + +
+
+ +
+ ); +} diff --git a/src/app/[...rest]/page.tsx b/src/app/[...rest]/page.tsx index 97baa60..8d9cd9f 100644 --- a/src/app/[...rest]/page.tsx +++ b/src/app/[...rest]/page.tsx @@ -14,16 +14,9 @@ import { getFileType } from "~/utils/previewHelper"; import FileBrowser from "../@explorer"; import Header from "../@header"; import HeaderButton from "../@header.button"; -import Markdown from "../@markdown"; import Password from "../@password"; -import PreviewAction from "../@preview.action"; -import PreviewAudio from "../@preview.audio"; -import PreviewDoc from "../@preview.doc"; -import PreviewImage from "../@preview.image"; -import PreviewManga from "../@preview.manga"; -import PreviewRich from "../@preview.rich"; -import PreviewUnknown from "../@preview.unknown"; -import PreviewVideo from "../@preview.video"; +import FilePreviewLayout from "../@preview.layout"; +import Readme from "../@readme"; import { CheckPassword, CheckPaths, @@ -141,76 +134,51 @@ export default async function RestPage({ params: { rest } }: Props) { slot='content' className='w-full' > - - - {isFile ? ( -
- - {data.name} - -
- ) : ( -
- Browse files - -
- )} - -
- - {isFile ? ( -
- {fileType === "image" ? ( - - ) : fileType === "audio" ? ( - - ) : fileType === "video" ? ( - - ) : fileType === "code" ? ( - - ) : fileType === "text" ? ( - - ) : fileType === "markdown" ? ( - - ) : fileType === "document" ? ( - - ) : fileType === "pdf" ? ( - - ) : fileType === "manga" ? ( - - ) : ( - - )} -
- ) : ( - + ) : ( + <> + + +
+ Browse files + +
+ +
+ + + +
+ {readme && ( + + //
+ // + // + // README.md + // + // + // + // + // + // + //
)} -
-
+ + )}
- {readme && ( -
- - - README.md - - - - - - -
- )} - {isFile && }
); } diff --git a/src/app/page.tsx b/src/app/page.tsx index 3cd1812..b290e1b 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -6,7 +6,7 @@ import { Separator } from "~/components/ui/separator"; import FileBrowser from "./@explorer"; import Header from "./@header"; import HeaderButton from "./@header.button"; -import Markdown from "./@markdown"; +import Readme from "./@readme"; import { GetFiles, GetReadme } from "./actions"; export const revalidate = 300; @@ -43,20 +43,24 @@ export default async function RootPage() {
{readme && ( -
- - - README.md - - - - - - -
+ + //
+ // + // + // README.md + // + // + // + // + // + // + //
)}
);