show ramdom background image

This commit is contained in:
2025-08-28 15:23:01 +08:00
parent 3dd042a752
commit 488a91e651
3 changed files with 25 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ import { useTranslation } from "../utils/i18n";
import { useAppContext } from "../components/AppContext.tsx";
import Select from "../components/select.tsx";
import { useNavigate } from "react-router";
import { useNavigator } from "../components/navigator.tsx";
export default function HomePage() {
useEffect(() => {
@@ -65,6 +66,7 @@ let cachedPinnedResources: Resource[] | null = null;
function PinnedResources() {
const [pinnedResources, setPinnedResources] = useState<Resource[]>([]);
const navigator = useNavigator();
useEffect(() => {
if (cachedPinnedResources != null) {
@@ -72,6 +74,9 @@ function PinnedResources() {
return;
}
const prefetchData = app.getPreFetchData();
if (prefetchData && prefetchData.background) {
navigator.setBackground(network.getResampledImageUrl(prefetchData.background));
}
if (prefetchData && prefetchData.pinned) {
cachedPinnedResources = prefetchData.pinned;
setPinnedResources(cachedPinnedResources!);

View File

@@ -157,9 +157,11 @@ func serveIndexHtml(c fiber.Ctx) error {
}
} else if path == "/" || path == "" {
pinned, err := service.GetPinnedResources()
if err == nil {
random, err1 := service.RandomCover()
if err == nil && err1 == nil {
preFetchDataJson, _ := json.Marshal(map[string]interface{}{
"pinned": pinned,
"background": random,
})
preFetchData = url.PathEscape(string(preFetchDataJson))
}

View File

@@ -288,6 +288,22 @@ func RandomResource(host string) (*model.ResourceDetailView, error) {
return &v, nil
}
var lastSuccessCover uint
func RandomCover() (uint, error) {
for retries := 0; retries < 5; retries++ {
v, err := dao.RandomResource()
if err != nil {
return 0, err
}
if len(v.Images) > 0 {
lastSuccessCover = v.Images[0].ID
return v.Images[0].ID, nil
}
}
return lastSuccessCover, nil
}
func GetPinnedResources() ([]model.ResourceView, error) {
ids := config.PinnedResources()
var views []model.ResourceView