F(frontend): add route loading feedback
This commit is contained in:
120
src/components/RouteProgress.tsx
Normal file
120
src/components/RouteProgress.tsx
Normal file
@@ -0,0 +1,120 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
import { usePathname, useSearchParams } from "next/navigation";
|
||||
import {
|
||||
completeNavigationProgress,
|
||||
subscribeNavigationProgress,
|
||||
} from "@/lib/navigation-progress";
|
||||
|
||||
const START_VALUE = 18;
|
||||
const MAX_ACTIVE_VALUE = 90;
|
||||
|
||||
export default function RouteProgress() {
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const [visible, setVisible] = React.useState(false);
|
||||
const [progress, setProgress] = React.useState(0);
|
||||
const intervalRef = React.useRef<number | null>(null);
|
||||
const finishTimeoutRef = React.useRef<number | null>(null);
|
||||
const safetyTimeoutRef = React.useRef<number | null>(null);
|
||||
const activeRef = React.useRef(false);
|
||||
const routeKey = `${pathname ?? ""}?${searchParams?.toString() ?? ""}`;
|
||||
|
||||
const clearTimers = React.useCallback(() => {
|
||||
if (intervalRef.current !== null) {
|
||||
window.clearInterval(intervalRef.current);
|
||||
intervalRef.current = null;
|
||||
}
|
||||
|
||||
if (finishTimeoutRef.current !== null) {
|
||||
window.clearTimeout(finishTimeoutRef.current);
|
||||
finishTimeoutRef.current = null;
|
||||
}
|
||||
|
||||
if (safetyTimeoutRef.current !== null) {
|
||||
window.clearTimeout(safetyTimeoutRef.current);
|
||||
safetyTimeoutRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const finish = React.useCallback(() => {
|
||||
if (!activeRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
activeRef.current = false;
|
||||
clearTimers();
|
||||
setProgress(100);
|
||||
|
||||
finishTimeoutRef.current = window.setTimeout(() => {
|
||||
setVisible(false);
|
||||
setProgress(0);
|
||||
}, 220);
|
||||
}, [clearTimers]);
|
||||
|
||||
const start = React.useCallback(() => {
|
||||
clearTimers();
|
||||
activeRef.current = true;
|
||||
setVisible(true);
|
||||
setProgress(START_VALUE);
|
||||
|
||||
window.requestAnimationFrame(() => {
|
||||
setProgress(28);
|
||||
});
|
||||
|
||||
intervalRef.current = window.setInterval(() => {
|
||||
setProgress((current) => {
|
||||
if (current >= MAX_ACTIVE_VALUE) {
|
||||
return current;
|
||||
}
|
||||
|
||||
const delta = Math.max((MAX_ACTIVE_VALUE - current) * 0.14, 1.5);
|
||||
return Math.min(MAX_ACTIVE_VALUE, current + delta);
|
||||
});
|
||||
}, 180);
|
||||
|
||||
safetyTimeoutRef.current = window.setTimeout(() => {
|
||||
finish();
|
||||
}, 12000);
|
||||
}, [clearTimers, finish]);
|
||||
|
||||
React.useEffect(() => {
|
||||
return subscribeNavigationProgress((event) => {
|
||||
if (event === "start") {
|
||||
start();
|
||||
return;
|
||||
}
|
||||
|
||||
finish();
|
||||
});
|
||||
}, [finish, start]);
|
||||
|
||||
React.useEffect(() => {
|
||||
finish();
|
||||
}, [finish, routeKey]);
|
||||
|
||||
React.useEffect(() => {
|
||||
return () => {
|
||||
clearTimers();
|
||||
};
|
||||
}, [clearTimers]);
|
||||
|
||||
return (
|
||||
<div
|
||||
aria-hidden="true"
|
||||
className="pointer-events-none fixed inset-x-0 top-0 z-[100] h-1 overflow-hidden"
|
||||
>
|
||||
<div
|
||||
className={`absolute inset-0 transition-opacity duration-200 ${
|
||||
visible ? "opacity-100" : "opacity-0"
|
||||
}`}
|
||||
>
|
||||
<div
|
||||
className="h-full origin-right bg-[linear-gradient(90deg,hsl(var(--primary))_0%,hsl(var(--route-progress))_100%)] shadow-[0_0_16px_hsl(var(--route-progress)/0.55)] transition-[transform] duration-200 ease-out"
|
||||
style={{ transform: `scaleX(${progress / 100})` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
69
src/components/page-loading.tsx
Normal file
69
src/components/page-loading.tsx
Normal file
@@ -0,0 +1,69 @@
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
|
||||
export function ListingPageLoading() {
|
||||
return (
|
||||
<div className="min-h-screen bg-background" dir="rtl">
|
||||
<div className="container mx-auto px-4 py-8">
|
||||
<Skeleton className="mb-8 h-10 w-40" />
|
||||
<Skeleton className="mb-8 h-10 w-full max-w-md" />
|
||||
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
|
||||
{Array.from({ length: 6 }).map((_, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="overflow-hidden rounded-lg border bg-card"
|
||||
>
|
||||
<Skeleton className="aspect-video w-full rounded-none" />
|
||||
<div className="space-y-4 p-6">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<Skeleton className="h-6 flex-1" />
|
||||
<Skeleton className="h-6 w-16" />
|
||||
</div>
|
||||
<Skeleton className="h-4 w-32" />
|
||||
<div className="space-y-3 pt-2">
|
||||
<Skeleton className="h-4 w-full" />
|
||||
<Skeleton className="h-4 w-5/6" />
|
||||
<Skeleton className="h-10 w-full" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function DetailPageLoading() {
|
||||
return (
|
||||
<div className="min-h-screen bg-background" dir="rtl">
|
||||
<div className="container mx-auto px-4 py-8">
|
||||
<div className="mb-6 flex items-center justify-between gap-3">
|
||||
<Skeleton className="h-10 w-36" />
|
||||
<Skeleton className="h-5 w-28" />
|
||||
</div>
|
||||
|
||||
<div className="overflow-hidden rounded-lg border bg-card">
|
||||
<Skeleton className="aspect-video w-full rounded-none" />
|
||||
<div className="space-y-6 p-6">
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Skeleton className="h-6 w-20" />
|
||||
<Skeleton className="h-6 w-24" />
|
||||
</div>
|
||||
<Skeleton className="h-10 w-4/5" />
|
||||
<Skeleton className="h-4 w-40" />
|
||||
<div className="space-y-3">
|
||||
<Skeleton className="h-4 w-full" />
|
||||
<Skeleton className="h-4 w-full" />
|
||||
<Skeleton className="h-4 w-11/12" />
|
||||
<Skeleton className="h-4 w-5/6" />
|
||||
</div>
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<Skeleton className="h-28 w-full" />
|
||||
<Skeleton className="h-28 w-full" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user