forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroken-link-handler.tsx
More file actions
126 lines (113 loc) · 2.9 KB
/
Copy pathbroken-link-handler.tsx
File metadata and controls
126 lines (113 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"use client";
import { useState, useEffect } from "react";
import Link from "next/link";
import { AlertTriangle, ExternalLink } from "lucide-react";
interface BrokenLinkHandlerProps {
href: string;
children: React.ReactNode;
className?: string;
fallbackHref?: string;
showWarning?: boolean;
}
export function BrokenLinkHandler({
href,
children,
className,
fallbackHref = "/",
showWarning = true,
}: BrokenLinkHandlerProps) {
const [isBroken, setIsBroken] = useState(false);
const [isExternal, setIsExternal] = useState(false);
useEffect(() => {
// Check if it's an external link
if (
href.startsWith("http") ||
href.startsWith("mailto:") ||
href.startsWith("tel:")
) {
setIsExternal(true);
return;
}
// For internal links, we could add validation here
// For now, we'll assume internal links are valid
setIsExternal(false);
}, [href]);
const handleClick = (e: React.MouseEvent) => {
if (isExternal) {
// Let external links open normally
return;
}
// For internal links, we could add validation
// This is a placeholder for future link validation
};
if (isExternal) {
return (
<Link
href={href}
className={`${className} inline-flex items-center gap-1`}
target="_blank"
rel="noopener noreferrer"
onClick={handleClick}
>
{children}
<ExternalLink className="w-3 h-3 opacity-60" />
</Link>
);
}
return (
<Link href={href} className={className} onClick={handleClick}>
{children}
{showWarning && isBroken && (
<AlertTriangle className="w-3 h-3 text-yellow-500 ml-1" />
)}
</Link>
);
}
// Enhanced NavigationLink with better error handling
export function EnhancedNavigationLink({
href,
children,
className,
...props
}: {
href: string;
children: React.ReactNode;
className?: string;
[key: string]: any;
}) {
const [linkStatus, setLinkStatus] = useState<"loading" | "valid" | "broken">(
"loading",
);
useEffect(() => {
// Simple validation for internal links
if (href.startsWith("/") && !href.startsWith("//")) {
// This is a basic check - in a real implementation, you'd want to
// validate against your actual route structure
setLinkStatus("valid");
} else if (
href.startsWith("http") ||
href.startsWith("mailto:") ||
href.startsWith("tel:")
) {
setLinkStatus("valid");
} else {
setLinkStatus("broken");
}
}, [href]);
if (linkStatus === "broken") {
return (
<span
className={`${className} text-red-600 dark:text-red-400 cursor-not-allowed`}
title="This link appears to be broken"
>
{children}
<AlertTriangle className="w-3 h-3 inline ml-1" />
</span>
);
}
return (
<Link href={href} className={className} {...props}>
{children}
</Link>
);
}