mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
show ramdom background image
This commit is contained in:
@@ -7,6 +7,7 @@ import { useTranslation } from "../utils/i18n";
|
|||||||
import { useAppContext } from "../components/AppContext.tsx";
|
import { useAppContext } from "../components/AppContext.tsx";
|
||||||
import Select from "../components/select.tsx";
|
import Select from "../components/select.tsx";
|
||||||
import { useNavigate } from "react-router";
|
import { useNavigate } from "react-router";
|
||||||
|
import { useNavigator } from "../components/navigator.tsx";
|
||||||
|
|
||||||
export default function HomePage() {
|
export default function HomePage() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -65,6 +66,7 @@ let cachedPinnedResources: Resource[] | null = null;
|
|||||||
|
|
||||||
function PinnedResources() {
|
function PinnedResources() {
|
||||||
const [pinnedResources, setPinnedResources] = useState<Resource[]>([]);
|
const [pinnedResources, setPinnedResources] = useState<Resource[]>([]);
|
||||||
|
const navigator = useNavigator();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (cachedPinnedResources != null) {
|
if (cachedPinnedResources != null) {
|
||||||
@@ -72,6 +74,9 @@ function PinnedResources() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const prefetchData = app.getPreFetchData();
|
const prefetchData = app.getPreFetchData();
|
||||||
|
if (prefetchData && prefetchData.background) {
|
||||||
|
navigator.setBackground(network.getResampledImageUrl(prefetchData.background));
|
||||||
|
}
|
||||||
if (prefetchData && prefetchData.pinned) {
|
if (prefetchData && prefetchData.pinned) {
|
||||||
cachedPinnedResources = prefetchData.pinned;
|
cachedPinnedResources = prefetchData.pinned;
|
||||||
setPinnedResources(cachedPinnedResources!);
|
setPinnedResources(cachedPinnedResources!);
|
||||||
|
@@ -157,9 +157,11 @@ func serveIndexHtml(c fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
} else if path == "/" || path == "" {
|
} else if path == "/" || path == "" {
|
||||||
pinned, err := service.GetPinnedResources()
|
pinned, err := service.GetPinnedResources()
|
||||||
if err == nil {
|
random, err1 := service.RandomCover()
|
||||||
|
if err == nil && err1 == nil {
|
||||||
preFetchDataJson, _ := json.Marshal(map[string]interface{}{
|
preFetchDataJson, _ := json.Marshal(map[string]interface{}{
|
||||||
"pinned": pinned,
|
"pinned": pinned,
|
||||||
|
"background": random,
|
||||||
})
|
})
|
||||||
preFetchData = url.PathEscape(string(preFetchDataJson))
|
preFetchData = url.PathEscape(string(preFetchDataJson))
|
||||||
}
|
}
|
||||||
|
@@ -288,6 +288,22 @@ func RandomResource(host string) (*model.ResourceDetailView, error) {
|
|||||||
return &v, nil
|
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) {
|
func GetPinnedResources() ([]model.ResourceView, error) {
|
||||||
ids := config.PinnedResources()
|
ids := config.PinnedResources()
|
||||||
var views []model.ResourceView
|
var views []model.ResourceView
|
||||||
|
Reference in New Issue
Block a user