feat: middle save

This commit is contained in:
WH64 2025-03-13 16:06:35 +09:00
parent 0caad5ea57
commit 9cdd058a49
7 changed files with 58 additions and 18 deletions

View file

@ -17,7 +17,7 @@ func New(app *gin.Engine) {
app.Use(static.Serve("/assets", static.LocalFile("./assets", false))) app.Use(static.Serve("/assets", static.LocalFile("./assets", false)))
app.NoRoute(func(ctx *gin.Context) { 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) { app.GET("favicon.ico", func(ctx *gin.Context) {
@ -52,8 +52,10 @@ func New(app *gin.Engine) {
entries := make([]service.DirEntry, 0) entries := make([]service.DirEntry, 0)
for _, fd := range entry { for _, fd := range entry {
var info, _ = fd.Info()
entries = append(entries, service.DirEntry{ entries = append(entries, service.DirEntry{
Name: fd.Name(), Name: fd.Name(),
FileSize: uint64(info.Size()),
}) })
} }

View file

@ -11,6 +11,7 @@ type WorkerService struct{}
type DirEntry struct { type DirEntry struct {
Name string `json:"name"` Name string `json:"name"`
FileSize uint64 `json:"file_size"`
} }
func NewWorkerService() *WorkerService { func NewWorkerService() *WorkerService {

View file

@ -1,7 +1,28 @@
import { useEffect, useState } from "react";
import Directory from "../directory"; import Directory from "../directory";
import { usePath } from "../../store/path";
import { useLocation } from "react-router";
import "./dashboard.scss"; import "./dashboard.scss";
function Dashboard() { 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 ( return (
<Directory /> <Directory />
); );

View file

@ -1,17 +1,6 @@
import { usePath } from "../../store/path";
import { useEffect } from "react";
import "./directory.scss";
function Directory() { function Directory() {
const path = usePath();
useEffect(() => {
path.update(location.pathname);
});
return ( return (
<div></div> <></>
); );
} }

View file

View file

@ -0,0 +1,7 @@
function FileView() {
return (
<></>
);
}
export default FileView;

View file

@ -1,14 +1,34 @@
import { create } from "zustand"; import { create } from "zustand";
interface PathState { interface PathState {
data: undefined; data: PathResponse | string | undefined;
update(path: string): Promise<void>; update(path: string): Promise<void>;
} }
interface PathResponse {
ok: number;
path: string;
entries: Array<DirEntry>
}
export interface DirEntry {
name: string;
file_size: number;
}
export const usePath = create<PathState>((set) => ({ export const usePath = create<PathState>((set) => ({
data: undefined, data: undefined,
update: async (path: string) => { update: async (path: string) => {
console.log(path); const res = await fetch(`/api/path/${path}`);
if (res.status !== 200 && res.status !== 304) {
set({ data: undefined }); set({ data: undefined });
return;
}
try {
set({ data: await res.json() });
} catch {
set({ data: `/api/path/${path}` });
}
} }
})); }));