This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import type { BlockquoteBlockPayload } from "@/lib/blog-blocks";
|
||||
import { Quote } from "lucide-react";
|
||||
|
||||
interface BlogBlockquoteProps {
|
||||
payload: BlockquoteBlockPayload;
|
||||
}
|
||||
|
||||
export function BlogBlockquote({ payload }: BlogBlockquoteProps) {
|
||||
const { content, citation } = payload;
|
||||
if (!content.trim()) return null;
|
||||
|
||||
return (
|
||||
<figure className="my-8 not-prose">
|
||||
<blockquote className="relative rounded-xl border border-border bg-muted/40 px-6 py-2 pr-14 text-foreground shadow-sm">
|
||||
<Quote
|
||||
className="absolute right-4 top-4 size-8 text-muted-foreground/40"
|
||||
aria-hidden
|
||||
/>
|
||||
<div
|
||||
className="blog-blockquote-content text-lg leading-relaxed text-justify [&_p]:my-2 [&_a]:text-primary [&_a]:underline hover:[&_a]:no-underline"
|
||||
dangerouslySetInnerHTML={{ __html: content }}
|
||||
/>
|
||||
{citation ? (
|
||||
<footer className="mt-4 border-t border-border/60 pt-4">
|
||||
<cite className="text-sm text-muted-foreground not-italic">
|
||||
— {citation}
|
||||
</cite>
|
||||
</footer>
|
||||
) : null}
|
||||
</blockquote>
|
||||
</figure>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import Link from "next/link";
|
||||
import Image from "next/image";
|
||||
import { Calendar, Clock, ArrowLeft } from "lucide-react";
|
||||
import { Card, CardContent, CardFooter, CardHeader } from "@/components/ui/card";
|
||||
|
||||
interface BlogCardProps {
|
||||
slug: string;
|
||||
title: string;
|
||||
excerpt: string;
|
||||
date: string;
|
||||
readTime: string;
|
||||
category: string;
|
||||
image: string;
|
||||
}
|
||||
|
||||
export function BlogCard({
|
||||
slug,
|
||||
title,
|
||||
excerpt,
|
||||
date,
|
||||
readTime,
|
||||
category,
|
||||
image,
|
||||
}: BlogCardProps) {
|
||||
return (
|
||||
<Link href={`/blog/${slug}`} className="group block h-full">
|
||||
<Card className="h-full flex flex-col overflow-hidden transition-all duration-300 hover:shadow-lg hover:-translate-y-1 dark:border-transparent">
|
||||
{/* Image with 3:2 aspect ratio */}
|
||||
<div className="relative aspect-[5/3] w-full overflow-hidden bg-muted">
|
||||
<Image
|
||||
src={image}
|
||||
alt={title}
|
||||
fill
|
||||
className="object-cover transition-transform duration-300 group-hover:scale-105"
|
||||
quality={100}
|
||||
sizes="(max-width: 768px) 100vw, (max-width: 1024px) 50vw, 400px"
|
||||
/>
|
||||
{/* Category Badge */}
|
||||
<div className="absolute top-4 right-4">
|
||||
<span className="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-primary/90 text-primary-foreground backdrop-blur-sm">
|
||||
{category}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<CardHeader className="flex-1 flex flex-col">
|
||||
<h3 className="text-base py-2 font-bold leading-tight tracking-normal group-hover:text-primary transition-colors line-clamp-2">
|
||||
{title}
|
||||
</h3>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="flex-1">
|
||||
<p className="text-muted-foreground leading-relaxed tracking-normal line-clamp-3 text-sm">
|
||||
{excerpt}
|
||||
</p>
|
||||
</CardContent>
|
||||
|
||||
<CardFooter className="flex items-center justify-between gap-4 text-xs text-muted-foreground border-t pt-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Calendar className="size-4" />
|
||||
<span>{date}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Clock className="size-4" />
|
||||
<span>{readTime} دقیقه</span>
|
||||
</div>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
Accordion,
|
||||
AccordionContent,
|
||||
AccordionItem,
|
||||
AccordionTrigger,
|
||||
} from "@/components/ui/accordion";
|
||||
import type { CollapseBlockPayload } from "@/lib/blog-blocks";
|
||||
import { Icon } from "@iconify/react";
|
||||
|
||||
interface BlogCollapseSectionProps {
|
||||
section: CollapseBlockPayload;
|
||||
/** Optional prefix for accordion item ids (e.g. for multiple sections on one page). */
|
||||
idPrefix?: string;
|
||||
}
|
||||
|
||||
|
||||
export function BlogCollapseSection({
|
||||
section,
|
||||
idPrefix = "collapse",
|
||||
}: BlogCollapseSectionProps) {
|
||||
if (!section.items.length) return null;
|
||||
|
||||
return (
|
||||
<div className="my-8 space-y-4 border rounded-xl p-6">
|
||||
{section.title ? (
|
||||
<h2 className="text-2xl font-bold !mt-0 mb-4 scroll-mt-24">
|
||||
{section.title}
|
||||
</h2>
|
||||
) : null}
|
||||
<Accordion type="single" collapsible className="w-full gap-1">
|
||||
{section.items.map((item, i) => (
|
||||
<AccordionItem
|
||||
key={i}
|
||||
value={`${idPrefix}-${i}`}
|
||||
className="border-none !m-0 hover:bg-muted has-[[data-state=open]]:bg-muted px-4 rounded-md"
|
||||
>
|
||||
<AccordionTrigger className="group/trigger text-right hover:no-underline cursor-pointer py-4 [&>svg]:shrink-0">
|
||||
<span className="flex gap-1 items-center">
|
||||
<Icon
|
||||
icon="lucide:circle-small"
|
||||
className="size-5 group-data-[state=open]/trigger:hidden"
|
||||
/>
|
||||
<Icon
|
||||
icon="lucide:dot"
|
||||
className="size-5 hidden group-data-[state=open]/trigger:inline-block"
|
||||
/>
|
||||
{item.question}
|
||||
</span>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="text-muted-foreground leading-relaxed text-justify">
|
||||
<div dangerouslySetInnerHTML={{ __html: item.answer }} />
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
))}
|
||||
</Accordion>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import Link from "next/link";
|
||||
import Image from "next/image";
|
||||
import { ImageIcon, User } from "lucide-react";
|
||||
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
|
||||
import { getFeaturedImageUrl, type WpPost } from "@/lib/wp-api";
|
||||
import { formatDateShamsi } from "@/lib/format-date";
|
||||
|
||||
function stripHtml(html: string): string {
|
||||
return html.replace(/<[^>]*>/g, "").trim();
|
||||
}
|
||||
|
||||
type BlogPostCardVariant = "default" | "tiny";
|
||||
|
||||
export function BlogPostCard({
|
||||
post,
|
||||
variant = "default",
|
||||
}: {
|
||||
post: WpPost;
|
||||
variant?: BlogPostCardVariant;
|
||||
}) {
|
||||
const excerpt = stripHtml(post.excerpt.rendered) || stripHtml(post.content.rendered).slice(0, 160);
|
||||
const modifiedDate = post.modified ? formatDateShamsi(post.modified) : null;
|
||||
const authorName = post._embedded?.author?.[0]?.name ?? null;
|
||||
const thumbnailUrl = getFeaturedImageUrl(post, "full");
|
||||
|
||||
if (variant === "tiny") {
|
||||
return (
|
||||
<Link href={`/blog/${encodeURIComponent(post.slug)}`} className="block h-full">
|
||||
<Card className="h-full overflow-hidden transition-all rounded-md duration-300 hover:shadow-md flex flex-col p-2">
|
||||
<CardContent className="p-2 grid grid-cols-3 gap-4">
|
||||
{thumbnailUrl ? (
|
||||
<div className="relative rounded-md aspect-[4/3] w-full overflow-hidden bg-muted shrink-0 col-span-1">
|
||||
<Image
|
||||
src={thumbnailUrl}
|
||||
alt=""
|
||||
fill
|
||||
className="object-cover"
|
||||
sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw"
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div
|
||||
className="relative rounded-md flex aspect-[4/3] w-full items-center justify-center overflow-hidden bg-muted shrink-0"
|
||||
aria-hidden
|
||||
>
|
||||
<ImageIcon className="size-8 text-muted-foreground/50" />
|
||||
</div>
|
||||
)}
|
||||
<div className="col-span-2">
|
||||
<h4 className="text-sm font-semibold leading-snug line-clamp-2">
|
||||
{post.title.rendered}
|
||||
</h4>
|
||||
<div className="flex flex-wrap w-full items-center gap-x-4 gap-y-1 text-xs text-muted-foreground bg-muted inline-flex p-1 mt-2 rounded-sm">
|
||||
{authorName && (
|
||||
<span className="inline-flex flex-1 items-center gap-1.5">
|
||||
<User className="size-3.5 shrink-0" aria-hidden />
|
||||
{authorName}
|
||||
</span>
|
||||
)}
|
||||
{modifiedDate && (
|
||||
<time dateTime={post.modified}>
|
||||
{modifiedDate}
|
||||
</time>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Link href={`/blog/${encodeURIComponent(post.slug)}`} className="block h-full">
|
||||
<Card className="h-full overflow-hidden transition-all duration-300 hover:-translate-y-1 hover:shadow-lg pt-0 gap-1">
|
||||
{thumbnailUrl ? (
|
||||
<div className="relative aspect-video w-full overflow-hidden bg-muted">
|
||||
<Image
|
||||
src={thumbnailUrl}
|
||||
alt=""
|
||||
fill
|
||||
className="object-cover"
|
||||
quality={100}
|
||||
priority
|
||||
sizes="(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 350px"
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div
|
||||
className="relative flex aspect-video w-full items-center justify-center overflow-hidden bg-muted"
|
||||
aria-hidden
|
||||
>
|
||||
<ImageIcon className="size-12 text-muted-foreground/50" />
|
||||
</div>
|
||||
)}
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-base font-bold leading-tight line-clamp-2 my-2">
|
||||
{post.title.rendered}
|
||||
</CardTitle>
|
||||
<div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-muted-foreground bg-muted inline-flex p-1 rounded-sm">
|
||||
{authorName && (
|
||||
<span className="inline-flex flex-1 items-center gap-1.5">
|
||||
<User className="size-3.5 shrink-0" aria-hidden />
|
||||
{authorName}
|
||||
</span>
|
||||
)}
|
||||
{modifiedDate && (
|
||||
<time dateTime={post.modified}>
|
||||
{modifiedDate}
|
||||
</time>
|
||||
)}
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="pt-0">
|
||||
<p className="text-muted-foreground text-sm leading-relaxed line-clamp-3">
|
||||
{excerpt}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useCallback, useEffect } from "react";
|
||||
import {
|
||||
Link2,
|
||||
Clock,
|
||||
Calendar,
|
||||
User,
|
||||
Share2,
|
||||
Check,
|
||||
} from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipProvider,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export interface BlogPostMetaProps {
|
||||
/** Path (e.g. /blog/slug). Full URL is built on client for share/copy. */
|
||||
url: string;
|
||||
title: string;
|
||||
readingTimeMinutes: number;
|
||||
/** Shamsi publish date (e.g. "۱۴۰۳/۰۸/۱۵") */
|
||||
date?: string | null;
|
||||
/** ISO date for <time dateTime> */
|
||||
dateTime?: string | null;
|
||||
modifiedDate?: string | null;
|
||||
authorName?: string | null;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function BlogPostMeta({
|
||||
url,
|
||||
title,
|
||||
readingTimeMinutes,
|
||||
date,
|
||||
dateTime,
|
||||
modifiedDate,
|
||||
authorName,
|
||||
className,
|
||||
}: BlogPostMetaProps) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const [shared, setShared] = useState(false);
|
||||
const [fullUrl, setFullUrl] = useState(url);
|
||||
const [canNativeShare, setCanNativeShare] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (url.startsWith("/")) {
|
||||
setFullUrl(window.location.origin + url);
|
||||
} else {
|
||||
setFullUrl(url);
|
||||
}
|
||||
setCanNativeShare(Boolean(typeof navigator !== "undefined" && navigator.share));
|
||||
}, [url]);
|
||||
|
||||
const copyLink = useCallback(async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(fullUrl);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
} catch {
|
||||
setCopied(false);
|
||||
}
|
||||
}, [fullUrl]);
|
||||
|
||||
const handleShare = useCallback(async () => {
|
||||
if (typeof navigator !== "undefined" && navigator.share) {
|
||||
try {
|
||||
await navigator.share({
|
||||
title,
|
||||
url: fullUrl,
|
||||
text: title,
|
||||
});
|
||||
setShared(true);
|
||||
setTimeout(() => setShared(false), 2000);
|
||||
} catch (err) {
|
||||
if ((err as Error).name !== "AbortError") copyLink();
|
||||
}
|
||||
} else {
|
||||
copyLink();
|
||||
}
|
||||
}, [title, fullUrl, copyLink]);
|
||||
|
||||
return (
|
||||
<TooltipProvider delayDuration={300}>
|
||||
<div
|
||||
className={cn(
|
||||
"flex flex-wrap items-center gap-x-5 gap-y-2 text-sm text-muted-foreground",
|
||||
className
|
||||
)}
|
||||
>
|
||||
{/* Reading time */}
|
||||
<span className="inline-flex items-center gap-1.5">
|
||||
<Clock className="size-4 shrink-0" aria-hidden />
|
||||
<span>
|
||||
{readingTimeMinutes === 1
|
||||
? "۱ دقیقه مطالعه"
|
||||
: `${readingTimeMinutes.toLocaleString("fa-IR")} دقیقه مطالعه`}
|
||||
</span>
|
||||
</span>
|
||||
|
||||
{/* Publish date */}
|
||||
{date && (
|
||||
<time
|
||||
className="inline-flex items-center gap-1.5"
|
||||
dateTime={dateTime ?? undefined}
|
||||
>
|
||||
<Calendar className="size-4 shrink-0" aria-hidden />
|
||||
<span>انتشار: {date}</span>
|
||||
</time>
|
||||
)}
|
||||
|
||||
{/* Last updated */}
|
||||
{modifiedDate && (
|
||||
<span className="inline-flex items-center gap-1.5">
|
||||
<Calendar className="size-4 shrink-0" aria-hidden />
|
||||
<span>آخرین بروزرسانی: {modifiedDate}</span>
|
||||
</span>
|
||||
)}
|
||||
|
||||
{/* Author */}
|
||||
{authorName && (
|
||||
<span className="inline-flex items-center gap-1.5">
|
||||
<User className="size-4 shrink-0" aria-hidden />
|
||||
<span>{authorName}</span>
|
||||
</span>
|
||||
)}
|
||||
|
||||
{/* Divider + actions */}
|
||||
<span className="mr-auto inline-flex items-center gap-1">
|
||||
<span
|
||||
className="hidden sm:inline h-4 w-px bg-border ml-2"
|
||||
aria-hidden
|
||||
/>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon-sm"
|
||||
className="size-8 text-muted-foreground hover:text-foreground"
|
||||
onClick={copyLink}
|
||||
aria-label="کپی لینک"
|
||||
>
|
||||
{copied ? (
|
||||
<Check className="size-4 text-green-600" />
|
||||
) : (
|
||||
<Link2 className="size-4" />
|
||||
)}
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="bottom">
|
||||
{copied ? "کپی شد" : "کپی لینک"}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon-sm"
|
||||
className="size-8 text-muted-foreground hover:text-foreground"
|
||||
onClick={handleShare}
|
||||
aria-label="اشتراکگذاری"
|
||||
>
|
||||
{shared ? (
|
||||
<Check className="size-4 text-green-600" />
|
||||
) : (
|
||||
<Share2 className="size-4" />
|
||||
)}
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="bottom">
|
||||
{shared ? "اشتراک گذاشته شد" : canNativeShare ? "اشتراکگذاری" : "کپی لینک"}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</span>
|
||||
</div>
|
||||
</TooltipProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
"use client";
|
||||
|
||||
import type { BlockquoteBlockPayload } from "@/lib/blog-blocks";
|
||||
|
||||
interface BlogPullquoteProps {
|
||||
payload: BlockquoteBlockPayload;
|
||||
}
|
||||
|
||||
export function BlogPullquote({ payload }: BlogPullquoteProps) {
|
||||
const { content, citation } = payload;
|
||||
if (!content.trim()) return null;
|
||||
|
||||
return (
|
||||
<figure className="my-10 not-prose">
|
||||
<blockquote className="relative rounded-xl border border-border bg-muted/40 px-6 py-5 text-center text-foreground shadow-sm">
|
||||
<div
|
||||
className=""
|
||||
dangerouslySetInnerHTML={{ __html: content }}
|
||||
/>
|
||||
{citation ? (
|
||||
<footer className="mt-4">
|
||||
<cite className="text-sm text-muted-foreground not-italic">
|
||||
— {citation}
|
||||
</cite>
|
||||
</footer>
|
||||
) : null}
|
||||
</blockquote>
|
||||
</figure>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
"use client";
|
||||
|
||||
import type { TipBlockPayload } from "@/lib/blog-blocks";
|
||||
import { Info, AlertTriangle, AlertCircle } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface BlogTipProps {
|
||||
payload: TipBlockPayload;
|
||||
}
|
||||
|
||||
const variantConfig = {
|
||||
info: {
|
||||
icon: Info,
|
||||
className:
|
||||
"border-primary/40 bg-primary/5 text-foreground [&_strong]:text-primary",
|
||||
iconClassName: "text-primary",
|
||||
},
|
||||
warn: {
|
||||
icon: AlertTriangle,
|
||||
className:
|
||||
"border-amber-500/50 bg-amber-500/10 text-foreground [&_strong]:text-amber-600 dark:[&_strong]:text-amber-400",
|
||||
iconClassName: "text-amber-500",
|
||||
},
|
||||
danger: {
|
||||
icon: AlertCircle,
|
||||
className:
|
||||
"border-destructive/50 bg-destructive/10 text-foreground [&_strong]:text-destructive",
|
||||
iconClassName: "text-destructive",
|
||||
},
|
||||
} as const;
|
||||
|
||||
export function BlogTip({ payload }: BlogTipProps) {
|
||||
const { variant, content } = payload;
|
||||
if (!content.trim()) return null;
|
||||
|
||||
const config = variantConfig[variant ?? 'info'];
|
||||
const Icon = config.icon;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"my-6 flex gap-3 rounded-xl border p-4 pr-4 shadow-sm",
|
||||
config.className
|
||||
)}
|
||||
role="note"
|
||||
>
|
||||
<Icon
|
||||
className={cn("mt-0.5 size-8 shrink-0", config.iconClassName)}
|
||||
aria-hidden
|
||||
/>
|
||||
<div
|
||||
className="blog-tip-content min-w-0 text-sm leading-relaxed [&_p]:my-2 [&_p:first-child]:mt-0 [&_p:last-child]:mb-0 [&_a]:text-primary [&_a]:underline hover:[&_a]:no-underline"
|
||||
dangerouslySetInnerHTML={{ __html: content }}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { ChevronDown, List } from "lucide-react";
|
||||
import {
|
||||
Collapsible,
|
||||
CollapsibleContent,
|
||||
CollapsibleTrigger,
|
||||
} from "@/components/ui/collapsible";
|
||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { TocItem } from "@/lib/blog-utils";
|
||||
|
||||
function useScrollProgress() {
|
||||
const [progress, setProgress] = useState(0);
|
||||
|
||||
const update = useCallback(() => {
|
||||
const { scrollY } = window;
|
||||
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
|
||||
if (docHeight <= 0) {
|
||||
setProgress(0);
|
||||
return;
|
||||
}
|
||||
const value = Math.min(100, Math.max(0, (scrollY / docHeight) * 100));
|
||||
setProgress(value);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
update();
|
||||
window.addEventListener("scroll", update, { passive: true });
|
||||
window.addEventListener("resize", update);
|
||||
return () => {
|
||||
window.removeEventListener("scroll", update);
|
||||
window.removeEventListener("resize", update);
|
||||
};
|
||||
}, [update]);
|
||||
|
||||
return progress;
|
||||
}
|
||||
|
||||
interface TocGroup {
|
||||
heading: TocItem;
|
||||
children: TocItem[];
|
||||
}
|
||||
|
||||
function groupByLevel2(items: TocItem[]): TocGroup[] {
|
||||
const groups: TocGroup[] = [];
|
||||
let current: TocGroup | null = null;
|
||||
for (const item of items) {
|
||||
if (item.level === 2) {
|
||||
current = { heading: item, children: [] };
|
||||
groups.push(current);
|
||||
} else if (current) {
|
||||
current.children.push(item);
|
||||
}
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
interface BlogTocProps {
|
||||
items: TocItem[];
|
||||
className?: string;
|
||||
}
|
||||
|
||||
const tocLinkClass = (
|
||||
level: number,
|
||||
isActive: boolean
|
||||
) =>
|
||||
cn(
|
||||
"block rounded-md border-r-2 rounded-s-none border-transparent py-1.5 text-sm transition-colors hover:text-foreground hover:bg-muted/60",
|
||||
level === 2 && "pr-3",
|
||||
level === 3 && "pr-5",
|
||||
level === 4 && "pr-7",
|
||||
isActive
|
||||
? "font-medium text-foreground border-primary bg-muted/50"
|
||||
: "text-muted-foreground"
|
||||
);
|
||||
|
||||
export function BlogToc({ items, className }: BlogTocProps) {
|
||||
const [activeId, setActiveId] = useState<string | null>(items[0]?.id ?? null);
|
||||
const [mobileOpen, setMobileOpen] = useState(false);
|
||||
const [openSections, setOpenSections] = useState<Set<string>>(() => new Set());
|
||||
const scrollProgress = useScrollProgress();
|
||||
|
||||
const groups = useMemo(() => groupByLevel2(items), [items]);
|
||||
|
||||
useEffect(() => {
|
||||
setOpenSections(new Set(groups.map((g) => g.heading.id)));
|
||||
}, [groups]);
|
||||
|
||||
useEffect(() => {
|
||||
if (items.length === 0) return;
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
for (const entry of entries) {
|
||||
if (entry.isIntersecting) {
|
||||
const id = entry.target.getAttribute("id");
|
||||
if (id) setActiveId(id);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ rootMargin: "-80px 0% -70% 0%", threshold: 0 }
|
||||
);
|
||||
|
||||
items.forEach(({ id }) => {
|
||||
const el = document.getElementById(id);
|
||||
if (el) observer.observe(el);
|
||||
});
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, [items]);
|
||||
|
||||
useEffect(() => {
|
||||
if (activeId && groups.length > 0) {
|
||||
const group = groups.find(
|
||||
(g) => g.heading.id === activeId || g.children.some((c) => c.id === activeId)
|
||||
);
|
||||
if (group) {
|
||||
setOpenSections((prev) => new Set(prev).add(group.heading.id));
|
||||
}
|
||||
}
|
||||
}, [activeId, groups]);
|
||||
|
||||
if (items.length === 0) return null;
|
||||
|
||||
const renderTocLink = (item: TocItem, onNav?: () => void) => (
|
||||
<Link
|
||||
key={item.id}
|
||||
href={`#${item.id}`}
|
||||
onClick={() => {
|
||||
setMobileOpen(false);
|
||||
onNav?.();
|
||||
}}
|
||||
className={tocLinkClass(item.level, activeId === item.id)}
|
||||
>
|
||||
<span className="line-clamp-2">{item.text}</span>
|
||||
</Link>
|
||||
);
|
||||
|
||||
const tocList = (
|
||||
<nav aria-label="فهرست مطالب" className="space-y-1">
|
||||
{groups.map(({ heading, children }) => {
|
||||
const hasChildren = children.length > 0;
|
||||
const isOpen = openSections.has(heading.id);
|
||||
|
||||
if (!hasChildren) {
|
||||
return renderTocLink(heading);
|
||||
}
|
||||
|
||||
return (
|
||||
<Collapsible
|
||||
key={heading.id}
|
||||
open={isOpen}
|
||||
onOpenChange={(open) => {
|
||||
setOpenSections((prev) => {
|
||||
const next = new Set(prev);
|
||||
if (open) next.add(heading.id);
|
||||
else next.delete(heading.id);
|
||||
return next;
|
||||
});
|
||||
}}
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
"flex items-center gap-0.5 rounded-md border-r-2 rounded-s-none py-1.5 pr-3",
|
||||
activeId === heading.id ? "border-primary bg-muted/50" : "border-transparent"
|
||||
)}
|
||||
>
|
||||
<Link
|
||||
href={`#${heading.id}`}
|
||||
onClick={() => setMobileOpen(false)}
|
||||
className={cn(
|
||||
"min-w-0 flex-1 text-sm transition-colors hover:text-foreground hover:bg-muted/60 rounded-md py-0.5 -my-0.5",
|
||||
activeId === heading.id ? "font-medium text-foreground" : "text-muted-foreground"
|
||||
)}
|
||||
>
|
||||
<span className="line-clamp-2">{heading.text}</span>
|
||||
</Link>
|
||||
<CollapsibleTrigger
|
||||
className="shrink-0 rounded p-1 text-muted-foreground hover:bg-muted/60 hover:text-foreground transition-colors"
|
||||
aria-label={isOpen ? "بستن" : "باز کردن"}
|
||||
>
|
||||
<ChevronDown
|
||||
className={cn("size-4 transition-transform", isOpen && "rotate-180")}
|
||||
/>
|
||||
</CollapsibleTrigger>
|
||||
</div>
|
||||
<CollapsibleContent>
|
||||
<div className="space-y-1 pt-0.5 pr-2 border-r border-border/50">
|
||||
{children.map((item) => renderTocLink(item))}
|
||||
</div>
|
||||
</CollapsibleContent>
|
||||
</Collapsible>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* Desktop: sticky sidebar with scroll progress bar */}
|
||||
<aside
|
||||
className={cn(
|
||||
"hidden lg:block shrink-0 relative",
|
||||
"sticky top-24 self-start",
|
||||
className
|
||||
)}
|
||||
>
|
||||
{/* Horizontal scroll progress indicator */}
|
||||
<div
|
||||
className="absolute -top-2 left-0 right-0 h-1 rounded-full bg-muted overflow-hidden"
|
||||
aria-hidden
|
||||
>
|
||||
<div
|
||||
className="h-full min-w-[2px] bg-primary transition-[width] duration-150 ease-out rounded-full"
|
||||
style={{ width: `${scrollProgress}%` }}
|
||||
/>
|
||||
</div>
|
||||
<p className="mb-3 text-md ms-2 font-semibold uppercase tracking-wider text-muted-foreground">
|
||||
فهرست مطالب
|
||||
</p>
|
||||
<ScrollArea className="h-full pb-8" dir="rtl">
|
||||
{tocList}
|
||||
</ScrollArea>
|
||||
</aside>
|
||||
|
||||
{/* Mobile: collapsible with horizontal scroll progress bar */}
|
||||
<Collapsible
|
||||
open={mobileOpen}
|
||||
onOpenChange={setMobileOpen}
|
||||
className={cn(
|
||||
"lg:hidden w-full sticky top-14 z-10 bg-background",
|
||||
className
|
||||
)}
|
||||
>
|
||||
|
||||
<CollapsibleTrigger
|
||||
className={cn(
|
||||
"flex w-full items-center justify-between gap-2 rounded-lg border border-border bg-muted/30 px-4 py-3 text-sm font-medium transition-colors hover:bg-muted/50",
|
||||
mobileOpen && "rounded-b-none border-b-0"
|
||||
)}
|
||||
>
|
||||
{/* Horizontal scroll progress indicator (mobile) */}
|
||||
<div
|
||||
className="absolute right-1 top-1 inset-inline-0 h-1 w-[97%] bg-muted overflow-hidden rounded-full"
|
||||
aria-hidden
|
||||
>
|
||||
<div
|
||||
className="h-full bg-primary transition-[width] duration-150 ease-out rounded-full"
|
||||
style={{ width: `${scrollProgress}%` }}
|
||||
/>
|
||||
</div>
|
||||
<span className="flex items-center gap-2">
|
||||
<List className="size-4 text-muted-foreground" />
|
||||
فهرست مطالب
|
||||
</span>
|
||||
<ChevronDown
|
||||
className={cn("size-4 text-muted-foreground transition-transform", mobileOpen && "rotate-180")}
|
||||
/>
|
||||
</CollapsibleTrigger>
|
||||
<CollapsibleContent>
|
||||
<div className="max-h-64 overflow-y-auto rounded-b-lg border border-t-0 border-border bg-muted/20 p-3">
|
||||
{tocList}
|
||||
</div>
|
||||
</CollapsibleContent>
|
||||
</Collapsible>
|
||||
</>
|
||||
);
|
||||
}
|
||||
+9
-17
@@ -6,6 +6,10 @@ const links = [
|
||||
title: "درباره ما",
|
||||
href: "/about",
|
||||
},
|
||||
{
|
||||
title: "بلاگ",
|
||||
href: "/blog",
|
||||
},
|
||||
{
|
||||
title: "راه حلها",
|
||||
href: "/solutions",
|
||||
@@ -14,13 +18,11 @@ const links = [
|
||||
|
||||
export default function FooterSection() {
|
||||
return (
|
||||
<footer className="py-16 md:py-32">
|
||||
<div className="mx-auto max-w-5xl px-6">
|
||||
<Link href="/" aria-label="go home" className="mx-auto block size-fit">
|
||||
<Logo />
|
||||
</Link>
|
||||
<footer className="py-16 md:py-16 bg-muted border-t">
|
||||
<div className="mx-auto max-w-5xl px-6 flex items-center flex-col">
|
||||
<Logo className="mx-auto" />
|
||||
|
||||
<div className="my-8 flex flex-wrap justify-center gap-6 text-sm">
|
||||
<div className="my-4 flex flex-wrap justify-center gap-6 text-sm">
|
||||
{links.map((link, index) => (
|
||||
<Link
|
||||
key={index}
|
||||
@@ -31,18 +33,8 @@ export default function FooterSection() {
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
<div className="my-8 flex flex-wrap justify-center items-center gap-2 text-sm">
|
||||
<span className="text-muted-foreground">تماس با تیم فیپ:</span>
|
||||
<a
|
||||
href="tel:09162949770"
|
||||
className="text-foreground hover:text-accent transition-colors duration-150 font-medium"
|
||||
>
|
||||
09162949770
|
||||
</a>
|
||||
</div>
|
||||
<span className="text-muted-foreground block text-center text-sm">
|
||||
{" "}
|
||||
© {new Date().getFullYear()} فیپ، تمامی حقوق محفوظ است
|
||||
© 1404 فیپ، تمامی حقوق محفوظ است
|
||||
</span>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
+57
-48
@@ -1,5 +1,6 @@
|
||||
'use client'
|
||||
import Link from 'next/link'
|
||||
import { usePathname } from 'next/navigation'
|
||||
import { Logo } from '@/components/logo'
|
||||
import { Menu, X } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
@@ -9,12 +10,19 @@ import { ThemeToggle } from '@/components/theme-toggle'
|
||||
|
||||
const menuItems = [
|
||||
{ name: 'صفحه اصلی', href: '/' },
|
||||
{ name: 'درباره ما', href: '/about' },
|
||||
{ name: 'بلاگ', href: '/blog' },
|
||||
{ name: 'راه حلها', href: '/solutions' },
|
||||
{ name: 'قیمتگذاری', href: '/pricing' },
|
||||
{ name: 'درباره ما', href: '/about' },
|
||||
]
|
||||
|
||||
function isLinkActive(pathname: string, href: string): boolean {
|
||||
if (href === '/') return pathname === '/'
|
||||
return pathname === href || pathname.startsWith(href + '/')
|
||||
}
|
||||
|
||||
export const HeroHeader = () => {
|
||||
const pathname = usePathname()
|
||||
const [menuState, setMenuState] = React.useState(false)
|
||||
const [isScrolled, setIsScrolled] = React.useState(false)
|
||||
|
||||
@@ -30,69 +38,70 @@ export const HeroHeader = () => {
|
||||
<nav
|
||||
data-state={menuState && 'active'}
|
||||
className="fixed z-20 w-full px-2">
|
||||
<div className={cn('mx-auto mt-2 max-w-6xl px-6 transition-all duration-300 lg:px-12 bg-background/80 backdrop-blur-md rounded-2xl border border-border/50', isScrolled && 'bg-background/95 max-w-4xl shadow-lg lg:px-5')}>
|
||||
<div className="relative flex flex-wrap items-center justify-between gap-6 py-2 lg:gap-0 lg:py-3">
|
||||
<div className={cn('mx-auto mt-2 max-w-6xl px-2 transition-all duration-300 lg:px-2 bg-background/30 backdrop-blur-md rounded-md border border-border/10', isScrolled && 'bg-background/20 max-w-4xl shadow-lg lg:px-5')}>
|
||||
<div className="relative flex flex-wrap items-center justify-between gap-2 lg:gap-0">
|
||||
<div className="flex w-full justify-between lg:w-auto">
|
||||
<Link
|
||||
href="/"
|
||||
aria-label="home"
|
||||
className="flex items-center space-x-2">
|
||||
<Logo />
|
||||
</Link>
|
||||
|
||||
<Logo />
|
||||
<button
|
||||
onClick={() => setMenuState(!menuState)}
|
||||
aria-label={menuState == true ? 'بستن منو' : 'باز کردن منو'}
|
||||
className="relative z-20 -m-2.5 -mr-4 block cursor-pointer p-2.5 lg:hidden">
|
||||
<Menu className="in-data-[state=active]:rotate-180 in-data-[state=active]:scale-0 in-data-[state=active]:opacity-0 m-auto size-6 duration-200" />
|
||||
<X className="in-data-[state=active]:rotate-0 in-data-[state=active]:scale-100 in-data-[state=active]:opacity-100 absolute inset-0 m-auto size-6 -rotate-180 scale-0 opacity-0 duration-200" />
|
||||
<Menu className="in-data-[state=active]:rotate-180 in-data-[state=active]:scale-0 in-data-[state=active]:opacity-0 m-auto size-4 duration-200" />
|
||||
<X className="in-data-[state=active]:rotate-0 in-data-[state=active]:scale-100 in-data-[state=active]:opacity-100 absolute inset-0 m-auto size-4 -rotate-180 scale-0 opacity-0 duration-200" />
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
<div className="absolute inset-0 m-auto hidden size-fit lg:flex items-center gap-8">
|
||||
<ul className="flex gap-8 text-sm">
|
||||
{menuItems.map((item, index) => (
|
||||
<li key={index}>
|
||||
<Link
|
||||
href={item.href}
|
||||
className="text-muted-foreground hover:text-foreground transition-all duration-300 relative group">
|
||||
<span className="relative z-10">{item.name}</span>
|
||||
<span className="absolute bottom-0 right-0 w-0 h-0.5 bg-accent transition-all duration-300 group-hover:w-full"></span>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<ThemeToggle />
|
||||
</div>
|
||||
|
||||
<div className="bg-background in-data-[state=active]:block lg:in-data-[state=active]:flex mb-6 hidden w-full flex-wrap items-center justify-end space-y-8 rounded-3xl border p-6 shadow-2xl shadow-zinc-300/20 md:flex-nowrap lg:m-0 lg:flex lg:w-fit lg:gap-6 lg:space-y-0 lg:border-transparent lg:bg-transparent lg:p-0 lg:shadow-none dark:shadow-none dark:lg:bg-transparent">
|
||||
<div className="lg:hidden w-full">
|
||||
<ul className="space-y-6 text-base">
|
||||
{menuItems.map((item, index) => (
|
||||
<div className="absolute inset-0 m-auto hidden size-fit lg:flex items-center">
|
||||
<ul className="flex gap-2 text-sm">
|
||||
{menuItems.map((item, index) => {
|
||||
const active = isLinkActive(pathname ?? '', item.href)
|
||||
return (
|
||||
<li key={index}>
|
||||
<Link
|
||||
href={item.href}
|
||||
className="text-muted-foreground hover:text-foreground transition-all duration-300 relative group block">
|
||||
className={cn(
|
||||
'text-muted-foreground hover:text-foreground hover:bg-accent/20 transition-all duration-300 relative group p-2 px-4 rounded-md',
|
||||
active && 'text-foreground bg-accent/40'
|
||||
)}>
|
||||
<span className="relative z-10">{item.name}</span>
|
||||
<span className="absolute bottom-0 right-0 w-0 h-0.5 bg-accent transition-all duration-300 group-hover:w-full"></span>
|
||||
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
|
||||
</div>
|
||||
|
||||
<div className="in-data-[state=active]:block lg:in-data-[state=active]:flex mb-3 hidden w-full flex-wrap items-center justify-end space-y-8 md:flex-nowrap lg:m-0 lg:flex lg:w-fit lg:gap-6 lg:space-y-0 lg:border-transparent lg:bg-transparent lg:p-0 lg:shadow-none dark:shadow-none dark:lg:bg-transparent">
|
||||
<div className="lg:hidden w-full">
|
||||
|
||||
<ul className="text-base flex flex-col items-stretch">
|
||||
{menuItems.map((item, index) => {
|
||||
const active = isLinkActive(pathname ?? '', item.href)
|
||||
return (
|
||||
<li key={index}>
|
||||
<Link
|
||||
href={item.href}
|
||||
onClick={() => setMenuState(false)}
|
||||
className={cn(
|
||||
'text-muted-foreground block w-full hover:text-foreground hover:bg-accent/20 transition-all duration-300 relative group p-2 px-4 rounded-md',
|
||||
active && 'text-foreground bg-accent/40'
|
||||
)}>
|
||||
<span className="relative z-10">{item.name}</span>
|
||||
|
||||
</Link>
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
<div className="mt-6 flex justify-end">
|
||||
<ThemeToggle />
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex w-full flex-col space-y-3 sm:flex-row sm:gap-3 sm:space-y-0 md:w-fit">
|
||||
<Button
|
||||
asChild
|
||||
size="sm"
|
||||
className={cn(isScrolled ? 'lg:inline-flex' : 'hidden', 'transition-all duration-300 hover:scale-105')}>
|
||||
<Link href="tel:09162949770">
|
||||
<span>تماس با فیپ</span>
|
||||
</Link>
|
||||
</Button>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div className={`flex justify-end max-md:absolute top-0.5 left-8 in-data-[state=active]:block`}>
|
||||
<ThemeToggle />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -168,14 +168,13 @@ export default function HeroSection() {
|
||||
...transitionVariants,
|
||||
}}
|
||||
>
|
||||
<div className="relative -mr-56 mt-8 overflow-hidden px-2 sm:mr-0 sm:mt-12 md:mt-20">
|
||||
<div className="ring-background bg-background relative mx-auto max-w-6xl overflow-hidden rounded-2xl border ring-1 shadow-[0_20px_50px_rgba(59,130,246,0.4)]">
|
||||
<div className="relative mt-8 overflow-hidden px-2 sm:mt-12 md:mt-20 p-2">
|
||||
<div className="ring-background bg-background relative max-w-5xl mx-auto overflow-hidden rounded-2xl border ring-1 aspect-[3/2]">
|
||||
<Image
|
||||
className="bg-background relative w-full h-full rounded-2xl object-contain object-center"
|
||||
src="/assets/main-pic/Untitled-1 copy-min.jpg"
|
||||
alt="یکپارچهسازی سیستمها و شبکه دیجیتال"
|
||||
width={1150}
|
||||
height={613}
|
||||
fill
|
||||
className="object-cover rounded-2xl inline-block my-0"
|
||||
quality={100}
|
||||
priority
|
||||
/>
|
||||
@@ -233,13 +232,13 @@ export default function HeroSection() {
|
||||
alt="Microsoft Windows Logo"
|
||||
size="md"
|
||||
className="translate-x-2"
|
||||
/>
|
||||
<LogoItem
|
||||
/>
|
||||
<LogoItem
|
||||
icon="skill-icons:github-dark"
|
||||
alt="github Logo"
|
||||
size="md"
|
||||
className="translate-x-4"
|
||||
/>
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+8
-8
@@ -10,9 +10,9 @@ export const Logo = ({
|
||||
uniColor?: boolean;
|
||||
}) => {
|
||||
return (
|
||||
<Link href="/" className="flex items-center space-x-2">
|
||||
<Link href="/" className="flex items-center">
|
||||
<LogoIcon className={className} uniColor={uniColor} />
|
||||
<span className="font-bold text-sm">فیپ</span>
|
||||
<span className="font-bold text-xl">فـــیــپ</span>
|
||||
</Link>
|
||||
);
|
||||
};
|
||||
@@ -25,12 +25,12 @@ export const LogoIcon = ({
|
||||
uniColor?: boolean;
|
||||
}) => {
|
||||
return (
|
||||
<Image
|
||||
src="/assets/feep-Logo.svg"
|
||||
alt="Logo"
|
||||
width={50}
|
||||
height={50}
|
||||
className={cn("h-10 w-auto md:h-12 dark:invert", className)}
|
||||
<Image
|
||||
src="/assets/feep-Logo.svg"
|
||||
alt="Logo"
|
||||
width={50}
|
||||
height={50}
|
||||
className={cn("h-10 w-auto md:h-12 dark:invert", className)}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { ChevronUp } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const SCROLL_THRESHOLD = 0.4; // show after 40% scroll
|
||||
|
||||
export function ScrollToTop({ className }: { className?: string }) {
|
||||
const [visible, setVisible] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
function onScroll() {
|
||||
const maxScroll =
|
||||
document.documentElement.scrollHeight - window.innerHeight;
|
||||
if (maxScroll <= 0) {
|
||||
setVisible(false);
|
||||
return;
|
||||
}
|
||||
const ratio = window.scrollY / maxScroll;
|
||||
setVisible(ratio >= SCROLL_THRESHOLD);
|
||||
}
|
||||
|
||||
onScroll();
|
||||
window.addEventListener("scroll", onScroll, { passive: true });
|
||||
return () => window.removeEventListener("scroll", onScroll);
|
||||
}, []);
|
||||
|
||||
const scrollToTop = () => {
|
||||
window.scrollTo({ top: 0, behavior: "smooth" });
|
||||
};
|
||||
|
||||
if (!visible) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"fixed bottom-6 start-6 z-50 transition-opacity duration-300",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<Button
|
||||
size="icon"
|
||||
variant="outline"
|
||||
onClick={scrollToTop}
|
||||
aria-label="برگشت به بالا"
|
||||
className="size-10 rounded-md shadow-md"
|
||||
>
|
||||
<ChevronUp className="size-5" />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname } from "next/navigation";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
const TOP_BAR_HEIGHT = 3;
|
||||
const MAX_PERCENT = 90;
|
||||
const COMPLETE_PERCENT = 100;
|
||||
const TICK_MS = 50;
|
||||
const INCREMENT = 2;
|
||||
const COMPLETE_DURATION = 200;
|
||||
|
||||
export function TopProgressBar() {
|
||||
const pathname = usePathname();
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [percent, setPercent] = useState(0);
|
||||
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const completeTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const prevPathnameRef = useRef(pathname);
|
||||
|
||||
const clearTimers = useCallback(() => {
|
||||
if (timerRef.current) {
|
||||
clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
if (completeTimeoutRef.current) {
|
||||
clearTimeout(completeTimeoutRef.current);
|
||||
completeTimeoutRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const startProgress = useCallback(() => {
|
||||
clearTimers();
|
||||
setVisible(true);
|
||||
setPercent(0);
|
||||
timerRef.current = setInterval(() => {
|
||||
setPercent((p) => {
|
||||
if (p >= MAX_PERCENT) {
|
||||
if (timerRef.current) {
|
||||
clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
return MAX_PERCENT;
|
||||
}
|
||||
return Math.min(p + INCREMENT, MAX_PERCENT);
|
||||
});
|
||||
}, TICK_MS);
|
||||
}, [clearTimers]);
|
||||
|
||||
const completeProgress = useCallback(() => {
|
||||
clearTimers();
|
||||
setPercent(COMPLETE_PERCENT);
|
||||
completeTimeoutRef.current = setTimeout(() => {
|
||||
setVisible(false);
|
||||
setPercent(0);
|
||||
completeTimeoutRef.current = null;
|
||||
}, COMPLETE_DURATION);
|
||||
}, [clearTimers]);
|
||||
|
||||
// Start bar when user clicks an internal link
|
||||
useEffect(() => {
|
||||
const handleClick = (e: MouseEvent) => {
|
||||
const target = (e.target as Element).closest("a");
|
||||
if (!target?.href) return;
|
||||
try {
|
||||
const url = new URL(target.href);
|
||||
if (url.origin === window.location.origin && url.pathname !== pathname) {
|
||||
startProgress();
|
||||
}
|
||||
} catch {
|
||||
// ignore invalid URLs
|
||||
}
|
||||
};
|
||||
document.addEventListener("click", handleClick, true);
|
||||
return () => document.removeEventListener("click", handleClick, true);
|
||||
}, [pathname, startProgress]);
|
||||
|
||||
// Complete bar when pathname changes (route loaded)
|
||||
useEffect(() => {
|
||||
if (prevPathnameRef.current !== pathname) {
|
||||
prevPathnameRef.current = pathname;
|
||||
if (visible) completeProgress();
|
||||
}
|
||||
}, [pathname, visible, completeProgress]);
|
||||
|
||||
useEffect(() => {
|
||||
return clearTimers;
|
||||
}, [clearTimers]);
|
||||
|
||||
if (!visible) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
className="fixed left-0 right-0 z-[9999] overflow-hidden bg-transparent"
|
||||
style={{ height: TOP_BAR_HEIGHT }}
|
||||
aria-hidden
|
||||
>
|
||||
<div
|
||||
className="h-full bg-primary transition-[width] duration-150 ease-out"
|
||||
style={{ width: `${percent}%` }}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user