fix: smooth mobile footer tab switching

This commit is contained in:
TerryM
2026-05-30 21:48:14 +08:00
parent d531ba40f3
commit cc58ee8aac
2 changed files with 55 additions and 19 deletions

View File

@@ -112,6 +112,17 @@ function streamKey(params: PostStreamParams): string {
return buildRealUrl(params);
}
function cacheFirstPage(
params: PostStreamParams,
page: PostListResponse,
): void {
streamCache.set(streamKey(params), {
items: itemsOrEmpty(page.items),
cursor: page.nextCursor,
hasMore: !!page.nextCursor,
});
}
/**
* Warm the cache for a stream view before the user navigates to it, so opening
* the page shows content immediately instead of starting to load on arrival.
@@ -119,20 +130,31 @@ function streamKey(params: PostStreamParams): string {
*/
export function prefetchPostStream(params: PostStreamParams): void {
if (USE_MOCK) return;
const key = streamKey(params);
if (streamCache.has(key)) return;
const url = buildRealUrl(params);
if (readJSONCache<PostListResponse>(url)) return;
getJSON<PostListResponse>(url).catch(() => {});
const cachedPage = readJSONCache<PostListResponse>(url);
if (cachedPage) {
cacheFirstPage(params, cachedPage);
return;
}
getJSON<PostListResponse>(url)
.then((page) => cacheFirstPage(params, page))
.catch(() => {});
}
export function usePostStream(params: PostStreamParams): PostStreamResult {
const [items, setItems] = useState<Post[]>([]);
const [hasMore, setHasMore] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const initialCached = streamCache.get(streamKey(params));
const [items, setItems] = useState<Post[]>(() => initialCached?.items ?? []);
const [hasMore, setHasMore] = useState(() => initialCached?.hasMore ?? true);
const [isLoading, setIsLoading] = useState(() => !initialCached);
const [error, setError] = useState<string | null>(null);
const reqIdRef = useRef(0);
const cursorRef = useRef<string | undefined>(undefined);
const hasMoreRef = useRef(true);
const cursorRef = useRef<string | undefined>(initialCached?.cursor);
const hasMoreRef = useRef(initialCached?.hasMore ?? true);
const loadingRef = useRef(false);
const fetchPage = useCallback(

View File

@@ -764,9 +764,8 @@ function BottomNavIcon({
icon: "home" | "document" | "bookmark" | "update";
active: boolean;
}) {
const src = active
? `${NAVBAR_ICON_BASE}/${icon}-active.svg`
: `${NAVBAR_ICON_BASE}/${icon}-inactive.svg`;
const activeSrc = `${NAVBAR_ICON_BASE}/${icon}-active.svg`;
const inactiveSrc = `${NAVBAR_ICON_BASE}/${icon}-inactive.svg`;
return (
<Link
to={to}
@@ -775,15 +774,30 @@ function BottomNavIcon({
active ? "text-ark-gold" : "text-[#908F92]",
].join(" ")}
>
<img
src={src}
alt=""
className="mx-auto h-6 w-6 object-contain"
width={24}
height={24}
loading="lazy"
decoding="async"
/>
<span className="relative h-6 w-6" aria-hidden>
<img
src={inactiveSrc}
alt=""
className={`absolute inset-0 h-6 w-6 object-contain ${
active ? "opacity-0" : "opacity-100"
}`}
width={24}
height={24}
loading="eager"
decoding="sync"
/>
<img
src={activeSrc}
alt=""
className={`absolute inset-0 h-6 w-6 object-contain ${
active ? "opacity-100" : "opacity-0"
}`}
width={24}
height={24}
loading="eager"
decoding="sync"
/>
</span>
<span className="leading-tight">{label}</span>
</Link>
);