feat: refresh buttons invalidate cache and show spinner during refresh
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 1m56s
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 1m56s
- Add revalidateForRefresh(scope, id) server action for home/library/series - Library/Series wrappers: revalidate cache then router.refresh(), 400ms delay for animation - Home: revalidate home-data + path before refresh - RefreshButton uses refreshLibrary from RefreshContext when not passed as prop - Library/Series pages pass id to wrapper for context and pull-to-refresh - read-progress: pass 'max' to revalidateTag for Next 16 types Made-with: Cursor
This commit is contained in:
@@ -1,29 +1,44 @@
|
||||
"use client";
|
||||
|
||||
import { useState, type ReactNode } from "react";
|
||||
import { useState, useCallback, type ReactNode } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { PullToRefreshIndicator } from "@/components/common/PullToRefreshIndicator";
|
||||
import { usePullToRefresh } from "@/hooks/usePullToRefresh";
|
||||
import { RefreshProvider } from "@/contexts/RefreshContext";
|
||||
import { revalidateForRefresh } from "@/app/actions/refresh";
|
||||
|
||||
interface LibraryClientWrapperProps {
|
||||
children: ReactNode;
|
||||
libraryId?: string;
|
||||
}
|
||||
|
||||
export function LibraryClientWrapper({ children }: LibraryClientWrapperProps) {
|
||||
const REFRESH_ANIMATION_MS = 400;
|
||||
|
||||
export function LibraryClientWrapper({ children, libraryId }: LibraryClientWrapperProps) {
|
||||
const router = useRouter();
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
|
||||
const handleRefresh = async () => {
|
||||
try {
|
||||
setIsRefreshing(true);
|
||||
router.refresh();
|
||||
return { success: true };
|
||||
} catch {
|
||||
return { success: false, error: "Error refreshing library" };
|
||||
} finally {
|
||||
setIsRefreshing(false);
|
||||
}
|
||||
};
|
||||
const handleRefresh = useCallback(
|
||||
async (libraryIdArg?: string) => {
|
||||
const id = libraryIdArg ?? libraryId;
|
||||
if (!id) {
|
||||
router.refresh();
|
||||
return { success: true };
|
||||
}
|
||||
try {
|
||||
setIsRefreshing(true);
|
||||
await revalidateForRefresh("library", id);
|
||||
router.refresh();
|
||||
await new Promise((r) => setTimeout(r, REFRESH_ANIMATION_MS));
|
||||
return { success: true };
|
||||
} catch {
|
||||
return { success: false, error: "Error refreshing library" };
|
||||
} finally {
|
||||
setIsRefreshing(false);
|
||||
}
|
||||
},
|
||||
[libraryId, router]
|
||||
);
|
||||
|
||||
const pullToRefresh = usePullToRefresh({
|
||||
onRefresh: async () => {
|
||||
@@ -33,7 +48,9 @@ export function LibraryClientWrapper({ children }: LibraryClientWrapperProps) {
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
<RefreshProvider
|
||||
refreshLibrary={libraryId ? (id) => handleRefresh(id) : undefined}
|
||||
>
|
||||
<PullToRefreshIndicator
|
||||
isPulling={pullToRefresh.isPulling}
|
||||
isRefreshing={pullToRefresh.isRefreshing || isRefreshing}
|
||||
@@ -42,6 +59,6 @@ export function LibraryClientWrapper({ children }: LibraryClientWrapperProps) {
|
||||
isHiding={pullToRefresh.isHiding}
|
||||
/>
|
||||
{children}
|
||||
</>
|
||||
</RefreshProvider>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user