From 9cdd058a4986cdb37a5bea566c2c4d48c2e7b925 Mon Sep 17 00:00:00 2001 From: Project_IO Date: Thu, 13 Mar 2025 16:06:35 +0900 Subject: [PATCH] feat: middle save --- internal/routes/mod.go | 6 ++++-- internal/service/worker.go | 3 ++- src/components/dashboard/index.tsx | 21 +++++++++++++++++++++ src/components/directory/index.tsx | 13 +------------ src/components/file-view/fview.scss | 0 src/components/file-view/index.tsx | 7 +++++++ src/store/path.ts | 26 +++++++++++++++++++++++--- 7 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 src/components/file-view/fview.scss create mode 100644 src/components/file-view/index.tsx diff --git a/internal/routes/mod.go b/internal/routes/mod.go index 8ad5d77..2cf1f99 100644 --- a/internal/routes/mod.go +++ b/internal/routes/mod.go @@ -17,7 +17,7 @@ func New(app *gin.Engine) { app.Use(static.Serve("/assets", static.LocalFile("./assets", false))) app.NoRoute(func(ctx *gin.Context) { - ctx.HTML(200, "index.html", nil) + ctx.File("./public/index.html") }) app.GET("favicon.ico", func(ctx *gin.Context) { @@ -52,8 +52,10 @@ func New(app *gin.Engine) { entries := make([]service.DirEntry, 0) for _, fd := range entry { + var info, _ = fd.Info() entries = append(entries, service.DirEntry{ - Name: fd.Name(), + Name: fd.Name(), + FileSize: uint64(info.Size()), }) } diff --git a/internal/service/worker.go b/internal/service/worker.go index 3e10908..a78dd09 100644 --- a/internal/service/worker.go +++ b/internal/service/worker.go @@ -10,7 +10,8 @@ import ( type WorkerService struct{} type DirEntry struct { - Name string `json:"name"` + Name string `json:"name"` + FileSize uint64 `json:"file_size"` } func NewWorkerService() *WorkerService { diff --git a/src/components/dashboard/index.tsx b/src/components/dashboard/index.tsx index 5058e63..2ae7cdc 100644 --- a/src/components/dashboard/index.tsx +++ b/src/components/dashboard/index.tsx @@ -1,7 +1,28 @@ +import { useEffect, useState } from "react"; import Directory from "../directory"; +import { usePath } from "../../store/path"; +import { useLocation } from "react-router"; + import "./dashboard.scss"; function Dashboard() { + const path = usePath(); + const location = useLocation(); + const [load, setLoad] = useState(false); + + useEffect(() => { + if (!load) { + path.update(location.pathname.substring(1, location.pathname.length)); + setLoad(true); + } + + const id = setInterval(() => { + path.update(location.pathname.substring(1, location.pathname.length)); + }, 5000); + + return () => clearInterval(id); + }, [load, path, location]); + return ( ); diff --git a/src/components/directory/index.tsx b/src/components/directory/index.tsx index 01b68cd..d505fa6 100644 --- a/src/components/directory/index.tsx +++ b/src/components/directory/index.tsx @@ -1,17 +1,6 @@ -import { usePath } from "../../store/path"; -import { useEffect } from "react"; - -import "./directory.scss"; - function Directory() { - const path = usePath(); - - useEffect(() => { - path.update(location.pathname); - }); - return ( -
+ <> ); } diff --git a/src/components/file-view/fview.scss b/src/components/file-view/fview.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/components/file-view/index.tsx b/src/components/file-view/index.tsx new file mode 100644 index 0000000..8b77489 --- /dev/null +++ b/src/components/file-view/index.tsx @@ -0,0 +1,7 @@ +function FileView() { + return ( + <> + ); +} + +export default FileView; diff --git a/src/store/path.ts b/src/store/path.ts index 14d6c8f..466bdf1 100644 --- a/src/store/path.ts +++ b/src/store/path.ts @@ -1,14 +1,34 @@ import { create } from "zustand"; interface PathState { - data: undefined; + data: PathResponse | string | undefined; update(path: string): Promise; } +interface PathResponse { + ok: number; + path: string; + entries: Array +} + +export interface DirEntry { + name: string; + file_size: number; +} + export const usePath = create((set) => ({ data: undefined, update: async (path: string) => { - console.log(path); - set({ data: undefined }); + const res = await fetch(`/api/path/${path}`); + if (res.status !== 200 && res.status !== 304) { + set({ data: undefined }); + return; + } + + try { + set({ data: await res.json() }); + } catch { + set({ data: `/api/path/${path}` }); + } } }));