fix: smooth mobile footer tab switching
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user