接近完美版本
This commit is contained in:
parent
e04d5842dd
commit
e2e48c9fea
|
@ -14,6 +14,10 @@ func main() {
|
||||||
eg.Use(Cors())
|
eg.Use(Cors())
|
||||||
eg.Static("/res", "movie/")
|
eg.Static("/res", "movie/")
|
||||||
eg.Static("/static", "../build/static")
|
eg.Static("/static", "../build/static")
|
||||||
|
eg.StaticFile("/manifest.json", "../build/manifest.json")
|
||||||
|
eg.StaticFile("/favicon.ico", "../build/favicon.ico")
|
||||||
|
eg.StaticFile("/logo512.png", "../build/logo512.png")
|
||||||
|
eg.StaticFile("/logo192.png", "../build/logo192.png")
|
||||||
eg.NoRoute(func(c *gin.Context) {
|
eg.NoRoute(func(c *gin.Context) {
|
||||||
path := c.Request.URL.Path
|
path := c.Request.URL.Path
|
||||||
if filepath.Ext(path) == "" {
|
if filepath.Ext(path) == "" {
|
||||||
|
|
175
src/Main.jsx
175
src/Main.jsx
|
@ -1,4 +1,4 @@
|
||||||
import React, { useState, useEffect, useContext, useCallback, useRef} from 'react';
|
import React, { useState, useEffect, useContext, useCallback } from 'react';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import Container from '@mui/material/Container';
|
import Container from '@mui/material/Container';
|
||||||
import Grid from '@mui/material/Grid';
|
import Grid from '@mui/material/Grid';
|
||||||
|
@ -28,13 +28,13 @@ const Main = () => {
|
||||||
return JSON.parse(storedParams);
|
return JSON.parse(storedParams);
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
lastPage: {
|
|
||||||
0: 1,
|
|
||||||
1: 1,
|
|
||||||
2: 1,
|
|
||||||
3: 1,
|
|
||||||
},
|
|
||||||
lastCategory: 0,
|
lastCategory: 0,
|
||||||
|
history: {
|
||||||
|
0: { lastPage: 1, scrollPos: 0 },
|
||||||
|
1: { lastPage: 1, scrollPos: 0 },
|
||||||
|
2: { lastPage: 1, scrollPos: 0 },
|
||||||
|
3: { lastPage: 1, scrollPos: 0 },
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -43,15 +43,11 @@ const Main = () => {
|
||||||
|
|
||||||
const [pagination, setPagination] = useState({
|
const [pagination, setPagination] = useState({
|
||||||
movies: [],
|
movies: [],
|
||||||
page: params.lastPage[params.lastCategory],
|
page: params.history[params.lastCategory].lastPage,
|
||||||
total: 1,
|
total: 1,
|
||||||
length: 1,
|
length: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const paramsRef = useRef(params);
|
|
||||||
paramsRef.current = params;
|
|
||||||
|
|
||||||
const fetchMovies = useCallback(async (category, page) => {
|
const fetchMovies = useCallback(async (category, page) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
|
@ -71,7 +67,6 @@ const Main = () => {
|
||||||
total: data.total,
|
total: data.total,
|
||||||
length: Math.ceil(data.total / limit),
|
length: Math.ceil(data.total / limit),
|
||||||
});
|
});
|
||||||
updateParams(category, page);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -82,41 +77,65 @@ const Main = () => {
|
||||||
}, [config.Host, limit]);
|
}, [config.Host, limit]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const currentCategory = params.lastCategory;
|
||||||
fetchMovies(paramsRef.current.lastCategory, paramsRef.current.lastPage[paramsRef.current.lastCategory]);
|
const currentPage = params.history[currentCategory].lastPage;
|
||||||
}, [fetchMovies]);
|
fetchMovies(currentCategory, currentPage);
|
||||||
|
|
||||||
const updateParams = (category, page) => {
|
|
||||||
setParams((prevParams) => {
|
|
||||||
const newParams = {
|
|
||||||
...prevParams,
|
|
||||||
lastPage: {
|
|
||||||
...prevParams.lastPage,
|
|
||||||
[category]: page,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
localStorage.setItem('params', JSON.stringify(newParams));
|
|
||||||
return newParams;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlePageChange = useCallback((event, page) => {
|
|
||||||
fetchMovies(params.lastCategory, page);
|
|
||||||
updateParams(params.lastCategory, page);
|
|
||||||
}, [fetchMovies, params]);
|
}, [fetchMovies, params]);
|
||||||
|
|
||||||
const handleCategoryChange = useCallback((category) => {
|
const updateParams = useCallback((newParams) => {
|
||||||
setParams((prevParams) => {
|
setParams((prevParams) => {
|
||||||
const newParams = {
|
const updatedParams = { ...prevParams, ...newParams };
|
||||||
...prevParams,
|
localStorage.setItem('params', JSON.stringify(updatedParams));
|
||||||
lastCategory: category,
|
return updatedParams;
|
||||||
};
|
|
||||||
localStorage.setItem('params', JSON.stringify(newParams));
|
|
||||||
return newParams;
|
|
||||||
});
|
});
|
||||||
// 从 paramsRef.current 获取当前 category 对应的页面
|
}, []);
|
||||||
fetchMovies(category, paramsRef.current.lastPage[category]);
|
|
||||||
}, [fetchMovies]);
|
const handlePageChange = useCallback(
|
||||||
|
(event, page) => {
|
||||||
|
const currentCategory = params.lastCategory;
|
||||||
|
updateParams({
|
||||||
|
history: {
|
||||||
|
...params.history,
|
||||||
|
[currentCategory]: { ...params.history[currentCategory], lastPage: page },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
fetchMovies(currentCategory, page);
|
||||||
|
},
|
||||||
|
[fetchMovies, params, updateParams]
|
||||||
|
);
|
||||||
|
|
||||||
|
// 导致递归刷新
|
||||||
|
const handleCategoryChange = useCallback(
|
||||||
|
(category) => {
|
||||||
|
const currentPage = params.history[category].lastPage;
|
||||||
|
fetchMovies(category, currentPage);
|
||||||
|
|
||||||
|
const currentCategory = params.lastCategory;
|
||||||
|
updateParams({
|
||||||
|
lastCategory: category,
|
||||||
|
history: {
|
||||||
|
...params.history,
|
||||||
|
[currentCategory]: { ...params.history[currentCategory], scrollPos: window.scrollY },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[fetchMovies, params, updateParams]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleRenderComplete = useCallback(() => {
|
||||||
|
const { scrollPos } = params.history[params.lastCategory];
|
||||||
|
window.scrollTo(0, scrollPos);
|
||||||
|
}, [params]);
|
||||||
|
|
||||||
|
const handleMovieCardClick = () => {
|
||||||
|
const currentCategory = params.lastCategory;
|
||||||
|
updateParams({
|
||||||
|
history: {
|
||||||
|
...params.history,
|
||||||
|
[currentCategory]: { ...params.history[currentCategory], scrollPos: window.scrollY },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container style={{ marginTop: 20 }}>
|
<Container style={{ marginTop: 20 }}>
|
||||||
|
@ -131,7 +150,12 @@ const Main = () => {
|
||||||
{loading ? (
|
{loading ? (
|
||||||
<Loading />
|
<Loading />
|
||||||
) : (
|
) : (
|
||||||
<MoviesGrid movies={pagination.movies} config={config} />
|
<MoviesGrid
|
||||||
|
movies={pagination.movies}
|
||||||
|
config={config}
|
||||||
|
onRenderComplete={handleRenderComplete}
|
||||||
|
onMovieCardClick={handleMovieCardClick}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<PaginationWrapper page={pagination.page} length={pagination.length} onPageChange={handlePageChange} />
|
<PaginationWrapper page={pagination.page} length={pagination.length} onPageChange={handlePageChange} />
|
||||||
|
@ -140,41 +164,40 @@ const Main = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const PaginationWrapper = ({ page, length, onPageChange }) => (
|
const PaginationWrapper = ({ page, length, onPageChange }) => (
|
||||||
<div style={{ textAlign: 'center', marginBottom: 20 }}>
|
<Grid container justifyContent="center" style={{ marginTop: 20, marginBottom: 20 }}>
|
||||||
<Pagination
|
<Pagination count={length} page={page} onChange={onPageChange} />
|
||||||
count={length}
|
</Grid>
|
||||||
page={page}
|
|
||||||
onChange={onPageChange}
|
|
||||||
shape="rounded"
|
|
||||||
color="primary"
|
|
||||||
size="large"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
const MoviesGrid = ({ movies, config, onRenderComplete, onMovieCardClick }) => {
|
||||||
|
useEffect(() => {
|
||||||
|
onRenderComplete();
|
||||||
|
}, [movies, onRenderComplete]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Grid container spacing={2} style={{ marginTop: 3 }}>
|
||||||
|
{movies.map((item) => (
|
||||||
|
<Grid item xs={6} sm={4} md={3} lg={2}
|
||||||
|
style={{ display: 'flex', justifyContent: 'space-between' }}
|
||||||
|
key={item.filename}
|
||||||
|
>
|
||||||
|
<Link
|
||||||
|
to={`/res/${item.filename}`}
|
||||||
|
style={{ textDecoration: 'none', paddingBottom: 10 }}
|
||||||
|
onClick={onMovieCardClick} // 修改这里
|
||||||
|
>
|
||||||
|
<MovieCard movie={item} config={config} />
|
||||||
|
</Link>
|
||||||
|
</Grid>
|
||||||
|
))}
|
||||||
|
</Grid>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const Loading = () => (
|
const Loading = () => (
|
||||||
<div
|
<Grid container justifyContent="center" style={{ marginTop: 20 }}>
|
||||||
style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '50vh' }}
|
|
||||||
>
|
|
||||||
<CircularProgress />
|
<CircularProgress />
|
||||||
</div>
|
|
||||||
);
|
|
||||||
|
|
||||||
const MoviesGrid = ({ movies, config }) => (
|
|
||||||
<Grid container spacing={2} style={{ marginTop: 3 }}>
|
|
||||||
{movies.map((item) => (
|
|
||||||
<Grid item xs={6} sm={4} md={3} lg={2}
|
|
||||||
style={{ display: 'flex', justifyContent: 'space-between' }}
|
|
||||||
key={item.filename}
|
|
||||||
>
|
|
||||||
<Link
|
|
||||||
to={`/res/${item.filename}`}
|
|
||||||
style={{ textDecoration: 'none', paddingBottom: 10 }}
|
|
||||||
>
|
|
||||||
<MovieCard movie={item} config={config} />
|
|
||||||
</Link>
|
|
||||||
</Grid>
|
|
||||||
))}
|
|
||||||
</Grid>
|
</Grid>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import {useEffect} from 'react';
|
||||||
|
|
||||||
|
|
||||||
import { Card, CardContent, CardMedia, Typography } from '@mui/material';
|
import { Card, CardContent, CardMedia, Typography } from '@mui/material';
|
||||||
import { styled, useTheme } from '@mui/system';
|
import { styled } from '@mui/system';
|
||||||
|
|
||||||
|
const MovieCard = ({ movie, config }) => {
|
||||||
|
|
||||||
|
|
||||||
const MovieCard = ({ movie, config }) => {
|
|
||||||
const truncateFilename = (filename, maxLength) => {
|
const truncateFilename = (filename, maxLength) => {
|
||||||
return filename.length > maxLength
|
return filename.length > maxLength
|
||||||
? filename.substring(0, maxLength - 3) + '...'
|
? filename.substring(0, maxLength - 3) + '...'
|
||||||
|
|
21
src/hooks/useScrollMemory.jsx
Normal file
21
src/hooks/useScrollMemory.jsx
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// hooks/useScrollMemory.js
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
const useScrollMemory = (key) => {
|
||||||
|
useEffect(() => {
|
||||||
|
const storedScrollPos = localStorage.getItem(key) || 0;
|
||||||
|
window.scrollTo(0, storedScrollPos);
|
||||||
|
|
||||||
|
const handleScroll = () => {
|
||||||
|
localStorage.setItem(key, window.scrollY);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('scroll', handleScroll);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('scroll', handleScroll);
|
||||||
|
};
|
||||||
|
}, [key]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useScrollMemory;
|
Loading…
Reference in New Issue
Block a user