1"use client"
2
3import type React from "react"
4import { useEffect, useState } from "react"
5import Image from "next/image"
6import {
7 Battery,
8 BookmarkIcon,
9 ChevronLeft,
10 ChevronRight,
11 Download,
12 Globe,
13 History,
14 Home,
15 Lock,
16 Maximize2,
17 Minimize2,
18 MoreHorizontal,
19 Plus,
20 RotateCcw,
21 Search,
22 Settings,
23 Shield,
24 Square,
25 Star,
26 StarOff,
27 Volume2,
28 Wifi,
29 X,
30} from "lucide-react"
31
32import { cn } from "@/lib/utils"
33import { Badge } from "@/components/ui/badge"
34import { Button } from "@/components/ui/button"
35import { Card } from "@/components/ui/card"
36import { Input } from "@/components/ui/input"
37import { Separator } from "@/components/ui/separator"
38
39interface Tab {
40 id: string
41 title: string
42 url: string
43 favicon?: string
44 isActive: boolean
45 isLoading: boolean
46}
47
48interface Bookmark {
49 id: string
50 title: string
51 url: string
52 favicon?: string
53}
54
55interface HistoryItem {
56 id: string
57 title: string
58 url: string
59 timestamp: Date
60 favicon?: string
61}
62
63interface BrowserProps {
64 image?: string
65 initialUrl?: string
66 initialTabs?: Partial<Tab>[]
67 theme?: "light" | "dark" | "system"
68 showWindowControls?: boolean
69 showBookmarksBar?: boolean
70 showStatusBar?: boolean
71 className?: string
72 enableTabManagement?: boolean
73 enableBookmarks?: boolean
74 enableHistory?: boolean
75 enableDownloads?: boolean
76 enableSettings?: boolean
77 maxTabs?: number
78 customBookmarks?: Bookmark[]
79 customHistory?: HistoryItem[]
80 onNavigate?: (url: string, tabId: string) => void
81 onTabCreate?: (tab: Tab) => void
82 onTabClose?: (tabId: string) => void
83 onTabSwitch?: (tabId: string) => void
84 onBookmarkToggle?: (url: string, isBookmarked: boolean) => void
85 onDownload?: (url: string) => void
86 renderContent?: (url: string, isLoading: boolean) => React.ReactNode
87 customFavicons?: Record<string, string>
88 openLinksInNewTab?: boolean
89 autoFocusAddressBar?: boolean
90 simulateLoading?: boolean
91 loadingDuration?: number
92}
93
94export function Browser({
95 image = "/placeholder.svg",
96 initialUrl = "https://eldoraui.site",
97 initialTabs,
98 showWindowControls = false,
99 showBookmarksBar = false,
100 showStatusBar = true,
101 className,
102 enableTabManagement = false,
103 enableBookmarks = true,
104 enableHistory = true,
105 enableDownloads = true,
106 enableSettings = true,
107 maxTabs = 10,
108 customBookmarks,
109 customHistory,
110 onNavigate,
111 onTabCreate,
112 onTabClose,
113 onTabSwitch,
114 onBookmarkToggle,
115 onDownload,
116 renderContent,
117 autoFocusAddressBar = false,
118 simulateLoading = true,
119 loadingDuration = 1000,
120}: BrowserProps = {}) {
121 const [tabs, setTabs] = useState<Tab[]>(() => {
122 if (initialTabs && initialTabs.length > 0) {
123 return initialTabs.map((tab, index) => ({
124 id: tab.id || Date.now().toString() + index,
125 title: tab.title || "New Tab",
126 url: tab.url || initialUrl,
127 favicon: tab.favicon,
128 isActive: index === 0,
129 isLoading: false,
130 }))
131 }
132 return [
133 {
134 id: "1",
135 title: "New Tab",
136 url: initialUrl,
137 isActive: true,
138 isLoading: false,
139 },
140 ]
141 })
142
143 const [currentUrl, setCurrentUrl] = useState(initialUrl)
144 const [inputUrl, setInputUrl] = useState(initialUrl)
145 const [isSecure, setIsSecure] = useState(true)
146 const [canGoBack, setCanGoBack] = useState(false)
147 const [canGoForward, setCanGoForward] = useState(false)
148 const [isBookmarked, setIsBookmarked] = useState(false)
149 const [showBookmarks, setShowBookmarks] = useState(false)
150 const [showHistory, setShowHistory] = useState(false)
151 const [showSettings, setShowSettings] = useState(false)
152 const [isFullscreen, setIsFullscreen] = useState(false)
153 const [downloadProgress, setDownloadProgress] = useState(0)
154 const [isDownloading, setIsDownloading] = useState(false)
155
156 const [bookmarks] = useState<Bookmark[]>(
157 customBookmarks || [
158 {
159 id: "1",
160 title: "Google",
161 url: "https://www.google.com",
162 favicon: "๐",
163 },
164 { id: "2", title: "GitHub", url: "https://github.com", favicon: "๐" },
165 {
166 id: "3",
167 title: "Stack Overflow",
168 url: "https://stackoverflow.com",
169 favicon: "๐",
170 },
171 {
172 id: "4",
173 title: "MDN Web Docs",
174 url: "https://developer.mozilla.org",
175 favicon: "๐",
176 },
177 ]
178 )
179
180 const [history] = useState<HistoryItem[]>(
181 customHistory || [
182 {
183 id: "1",
184 title: "Google",
185 url: "https://www.google.com",
186 timestamp: new Date(Date.now() - 3600000),
187 favicon: "๐",
188 },
189 {
190 id: "2",
191 title: "GitHub",
192 url: "https://github.com",
193 timestamp: new Date(Date.now() - 7200000),
194 favicon: "๐",
195 },
196 {
197 id: "3",
198 title: "Stack Overflow",
199 url: "https://stackoverflow.com",
200 timestamp: new Date(Date.now() - 10800000),
201 favicon: "๐",
202 },
203 ]
204 )
205
206 const activeTab = tabs.find((tab) => tab.isActive)
207
208 useEffect(() => {
209 if (autoFocusAddressBar) {
210 const addressBar = document.querySelector(
211 'input[placeholder*="Search or enter address"]'
212 ) as HTMLInputElement
213 if (addressBar) {
214 addressBar.focus()
215 }
216 }
217 }, [autoFocusAddressBar])
218
219 const createNewTab = () => {
220 if (tabs.length >= maxTabs) return
221
222 const newTab: Tab = {
223 id: Date.now().toString(),
224 title: "New Tab",
225 url: "about:blank",
226 isActive: true,
227 isLoading: false,
228 }
229
230 setTabs((prev) =>
231 prev.map((tab) => ({ ...tab, isActive: false })).concat(newTab)
232 )
233 setCurrentUrl("about:blank")
234 setInputUrl("")
235
236 onTabCreate?.(newTab)
237 }
238
239 const closeTab = (tabId: string) => {
240 if (tabs.length === 1) return
241
242 const tabIndex = tabs.findIndex((tab) => tab.id === tabId)
243 const wasActive = tabs[tabIndex].isActive
244
245 const newTabs = tabs.filter((tab) => tab.id !== tabId)
246
247 if (wasActive && newTabs.length > 0) {
248 const nextActiveIndex = Math.min(tabIndex, newTabs.length - 1)
249 newTabs[nextActiveIndex].isActive = true
250 setCurrentUrl(newTabs[nextActiveIndex].url)
251 setInputUrl(newTabs[nextActiveIndex].url)
252 }
253
254 setTabs(newTabs)
255
256 onTabClose?.(tabId)
257 }
258
259 const switchTab = (tabId: string) => {
260 const newTabs = tabs.map((tab) => ({
261 ...tab,
262 isActive: tab.id === tabId,
263 }))
264
265 const activeTab = newTabs.find((tab) => tab.isActive)
266 if (activeTab) {
267 setCurrentUrl(activeTab.url)
268 setInputUrl(activeTab.url)
269 }
270
271 setTabs(newTabs)
272
273 onTabSwitch?.(tabId)
274 }
275
276 const navigateToUrl = (url: string) => {
277 if (
278 !url.startsWith("http://") &&
279 !url.startsWith("https://") &&
280 !url.startsWith("about:")
281 ) {
282 url = `https://www.google.com/search?q=${encodeURIComponent(url)}`
283 }
284
285 setCurrentUrl(url)
286 setInputUrl(url)
287 setIsSecure(url.startsWith("https://"))
288
289 setTabs((prev) =>
290 prev.map((tab) =>
291 tab.isActive
292 ? {
293 ...tab,
294 url,
295 title: new URL(url).hostname || "New Tab",
296 isLoading: simulateLoading,
297 }
298 : tab
299 )
300 )
301
302 const activeTabId = tabs.find((tab) => tab.isActive)?.id || ""
303 onNavigate?.(url, activeTabId)
304
305 if (simulateLoading) {
306 setTimeout(() => {
307 setTabs((prev) =>
308 prev.map((tab) => (tab.isActive ? { ...tab, isLoading: false } : tab))
309 )
310 }, loadingDuration)
311 }
312 }
313
314 const handleUrlSubmit = (e: React.FormEvent) => {
315 e.preventDefault()
316 navigateToUrl(inputUrl)
317 }
318
319 const goBack = () => {
320 setCanGoForward(true)
321 }
322
323 const goForward = () => {
324 setCanGoBack(true)
325 }
326
327 const refresh = () => {
328 setTabs((prev) =>
329 prev.map((tab) => (tab.isActive ? { ...tab, isLoading: true } : tab))
330 )
331
332 setTimeout(() => {
333 setTabs((prev) =>
334 prev.map((tab) => (tab.isActive ? { ...tab, isLoading: false } : tab))
335 )
336 }, 1000)
337 }
338
339 const toggleBookmark = () => {
340 const newBookmarkedState = !isBookmarked
341 setIsBookmarked(newBookmarkedState)
342
343 onBookmarkToggle?.(currentUrl, newBookmarkedState)
344 }
345
346 const simulateDownload = () => {
347 onDownload?.(currentUrl)
348
349 if (!enableDownloads) return
350
351 setIsDownloading(true)
352 setDownloadProgress(0)
353
354 const interval = setInterval(() => {
355 setDownloadProgress((prev) => {
356 if (prev >= 100) {
357 clearInterval(interval)
358 setIsDownloading(false)
359 return 0
360 }
361 return prev + 10
362 })
363 }, 200)
364 }
365
366 return (
367 <div
368 className={cn(
369 "bg-background border-border flex h-full flex-col overflow-hidden rounded-lg border",
370 isFullscreen && "fixed inset-0 z-50 rounded-none border-0",
371 className
372 )}
373 >
374 {showWindowControls && (
375 <div className="bg-muted/50 border-border flex items-center justify-between border-b px-4 py-2">
376 <div className="flex items-center gap-2">
377 <div className="flex gap-2">
378 <div className="h-3 w-3 rounded-full bg-red-500"></div>
379 <div className="h-3 w-3 rounded-full bg-yellow-500"></div>
380 <div className="h-3 w-3 rounded-full bg-green-500"></div>
381 </div>
382 </div>
383
384 <div className="text-muted-foreground flex items-center gap-2 text-sm">
385 <Wifi className="h-4 w-4" />
386 <Volume2 className="h-4 w-4" />
387 <Battery className="h-4 w-4" />
388 <span>
389 {new Date().toLocaleTimeString([], {
390 hour: "2-digit",
391 minute: "2-digit",
392 })}
393 </span>
394 </div>
395
396 <div className="flex items-center gap-1">
397 <Button
398 variant="ghost"
399 size="sm"
400 onClick={() => setIsFullscreen(!isFullscreen)}
401 >
402 {isFullscreen ? (
403 <Minimize2 className="h-4 w-4" />
404 ) : (
405 <Maximize2 className="h-4 w-4" />
406 )}
407 </Button>
408 <Button variant="ghost" size="sm">
409 <Square className="h-4 w-4" />
410 </Button>
411 <Button variant="ghost" size="sm">
412 <X className="h-4 w-4" />
413 </Button>
414 </div>
415 </div>
416 )}
417
418 {enableTabManagement && (
419 <div className="bg-muted/30 border-border flex items-center border-b">
420 <div className="flex flex-1 items-center overflow-x-auto">
421 {tabs.map((tab) => (
422 <div
423 key={tab.id}
424 className={cn(
425 "border-border flex max-w-64 min-w-0 cursor-pointer items-center gap-2 border-r px-4 py-2",
426 tab.isActive ? "bg-background" : "hover:bg-muted/50"
427 )}
428 onClick={() => switchTab(tab.id)}
429 >
430 <div className="flex min-w-0 flex-1 items-center gap-2">
431 {tab.isLoading ? (
432 <div className="border-primary h-4 w-4 animate-spin rounded-full border-2 border-t-transparent" />
433 ) : (
434 <Globe className="text-muted-foreground h-4 w-4 flex-shrink-0" />
435 )}
436 <span className="truncate text-sm">{tab.title}</span>
437 </div>
438 {tabs.length > 1 && (
439 <Button
440 variant="ghost"
441 size="sm"
442 className="hover:bg-muted h-4 w-4 p-0"
443 onClick={(e) => {
444 e.stopPropagation()
445 closeTab(tab.id)
446 }}
447 >
448 <X className="h-3 w-3" />
449 </Button>
450 )}
451 </div>
452 ))}
453 </div>
454
455 <Button
456 variant="ghost"
457 size="sm"
458 onClick={createNewTab}
459 className="border-border border-l px-3 py-2"
460 >
461 <Plus className="h-4 w-4" />
462 </Button>
463 </div>
464 )}
465
466 <div className="bg-background border-border flex items-center gap-2 border-b p-2">
467 <div className="flex items-center gap-1">
468 <Button
469 variant="ghost"
470 size="sm"
471 onClick={goBack}
472 disabled={!canGoBack}
473 >
474 <ChevronLeft className="h-4 w-4" />
475 </Button>
476 <Button
477 variant="ghost"
478 size="sm"
479 onClick={goForward}
480 disabled={!canGoForward}
481 >
482 <ChevronRight className="h-4 w-4" />
483 </Button>
484 <Button variant="ghost" size="sm" onClick={refresh}>
485 <RotateCcw className="h-4 w-4" />
486 </Button>
487 <Button
488 variant="ghost"
489 size="sm"
490 onClick={() => navigateToUrl("about:home")}
491 >
492 <Home className="h-4 w-4" />
493 </Button>
494 </div>
495
496 <form onSubmit={handleUrlSubmit} className="flex flex-1 items-center">
497 <div className="relative flex flex-1 items-center">
498 <div className="absolute left-3 flex items-center gap-2">
499 {isSecure ? (
500 <Lock className="h-4 w-4 text-green-600" />
501 ) : (
502 <Shield className="text-muted-foreground h-4 w-4" />
503 )}
504 </div>
505 <Input
506 value={inputUrl}
507 onChange={(e) => setInputUrl(e.target.value)}
508 placeholder="Search or enter address"
509 className="pr-4 pl-10"
510 />
511 </div>
512 </form>
513
514 <div className="flex items-center gap-1">
515 {enableBookmarks && (
516 <Button variant="ghost" size="sm" onClick={toggleBookmark}>
517 {isBookmarked ? (
518 <Star className="h-4 w-4 fill-yellow-400 text-yellow-400" />
519 ) : (
520 <StarOff className="h-4 w-4" />
521 )}
522 </Button>
523 )}
524 {enableDownloads && (
525 <Button variant="ghost" size="sm" onClick={simulateDownload}>
526 <Download className="h-4 w-4" />
527 </Button>
528 )}
529 {enableSettings && (
530 <Button
531 variant="ghost"
532 size="sm"
533 onClick={() => setShowSettings(!showSettings)}
534 >
535 <MoreHorizontal className="h-4 w-4" />
536 </Button>
537 )}
538 </div>
539 </div>
540
541 {showBookmarksBar && enableBookmarks && (
542 <div className="bg-muted/20 border-border flex items-center gap-2 border-b px-4 py-1 text-sm">
543 <Button
544 variant="ghost"
545 size="sm"
546 onClick={() => setShowBookmarks(!showBookmarks)}
547 className="text-xs"
548 >
549 <BookmarkIcon className="mr-1 h-3 w-3" />
550 Bookmarks
551 </Button>
552 {enableHistory && (
553 <Button
554 variant="ghost"
555 size="sm"
556 onClick={() => setShowHistory(!showHistory)}
557 className="text-xs"
558 >
559 <History className="mr-1 h-3 w-3" />
560 History
561 </Button>
562 )}
563 <Separator orientation="vertical" className="h-4" />
564 {bookmarks.slice(0, 4).map((bookmark) => (
565 <Button
566 key={bookmark.id}
567 variant="ghost"
568 size="sm"
569 onClick={() => navigateToUrl(bookmark.url)}
570 className="text-xs"
571 >
572 <span className="mr-1">{bookmark.favicon}</span>
573 {bookmark.title}
574 </Button>
575 ))}
576 </div>
577 )}
578
579 {isDownloading && enableDownloads && (
580 <div className="border-border border-b bg-blue-50 px-4 py-2 dark:bg-blue-950">
581 <div className="flex items-center gap-2 text-sm">
582 <Download className="h-4 w-4" />
583 <span>Downloading file...</span>
584 <div className="bg-muted h-2 flex-1 rounded-full">
585 <div
586 className="h-2 rounded-full bg-blue-600 transition-all duration-200"
587 style={{ width: `${downloadProgress}%` }}
588 />
589 </div>
590 <span>{downloadProgress}%</span>
591 </div>
592 </div>
593 )}
594
595 <div className="flex flex-1 overflow-hidden">
596 {showBookmarks && enableBookmarks && (
597 <Card className="m-2 mr-0 w-80 overflow-y-auto p-4">
598 <h3 className="mb-4 flex items-center gap-2 font-semibold">
599 <BookmarkIcon className="h-4 w-4" />
600 Bookmarks
601 </h3>
602 <div className="space-y-2">
603 {bookmarks.map((bookmark) => (
604 <div
605 key={bookmark.id}
606 className="hover:bg-muted flex cursor-pointer items-center gap-2 rounded p-2"
607 onClick={() => navigateToUrl(bookmark.url)}
608 >
609 <span>{bookmark.favicon}</span>
610 <div className="min-w-0 flex-1">
611 <div className="truncate text-sm font-medium">
612 {bookmark.title}
613 </div>
614 <div className="text-muted-foreground truncate text-xs">
615 {bookmark.url}
616 </div>
617 </div>
618 </div>
619 ))}
620 </div>
621 </Card>
622 )}
623
624 {showHistory && enableHistory && (
625 <Card className="m-2 mr-0 w-80 overflow-y-auto p-4">
626 <h3 className="mb-4 flex items-center gap-2 font-semibold">
627 <History className="h-4 w-4" />
628 History
629 </h3>
630 <div className="space-y-2">
631 {history.map((item) => (
632 <div
633 key={item.id}
634 className="hover:bg-muted flex cursor-pointer items-center gap-2 rounded p-2"
635 onClick={() => navigateToUrl(item.url)}
636 >
637 <span>{item.favicon}</span>
638 <div className="min-w-0 flex-1">
639 <div className="truncate text-sm font-medium">
640 {item.title}
641 </div>
642 <div className="text-muted-foreground truncate text-xs">
643 {item.url}
644 </div>
645 <div className="text-muted-foreground text-xs">
646 {item.timestamp.toLocaleTimeString()}
647 </div>
648 </div>
649 </div>
650 ))}
651 </div>
652 </Card>
653 )}
654
655 {showSettings && enableSettings && (
656 <Card className="m-2 mr-0 w-80 overflow-y-auto p-4">
657 <h3 className="mb-4 flex items-center gap-2 font-semibold">
658 <Settings className="h-4 w-4" />
659 Settings
660 </h3>
661 <div className="space-y-4">
662 <div>
663 <h4 className="mb-2 font-medium">Privacy & Security</h4>
664 <div className="space-y-2 text-sm">
665 <div className="flex items-center justify-between">
666 <span>Block pop-ups</span>
667 <Badge variant="secondary">On</Badge>
668 </div>
669 <div className="flex items-center justify-between">
670 <span>Safe browsing</span>
671 <Badge variant="secondary">Enhanced</Badge>
672 </div>
673 </div>
674 </div>
675 <Separator />
676 <div>
677 <h4 className="mb-2 font-medium">Appearance</h4>
678 <div className="space-y-2 text-sm">
679 <div className="flex items-center justify-between">
680 <span>Theme</span>
681 <Badge variant="outline">System</Badge>
682 </div>
683 <div className="flex items-center justify-between">
684 <span>Zoom</span>
685 <Badge variant="outline">100%</Badge>
686 </div>
687 </div>
688 </div>
689 </div>
690 </Card>
691 )}
692
693 <div className="dark:bg-muted/20 border-border m-2 flex flex-1 flex-col overflow-hidden rounded-md border bg-white">
694 {renderContent ? (
695 renderContent(currentUrl, activeTab?.isLoading || false)
696 ) : currentUrl === "about:blank" || currentUrl === "" ? (
697 <div className="flex flex-1 items-center justify-center">
698 <div className="space-y-4 text-center">
699 <Search className="text-muted-foreground mx-auto h-16 w-16" />
700 <h2 className="text-2xl font-semibold">New Tab</h2>
701 <p className="text-muted-foreground">
702 Start by searching or entering a web address
703 </p>
704 <div className="mt-8 grid grid-cols-2 gap-4">
705 {bookmarks.slice(0, 4).map((bookmark) => (
706 <Card
707 key={bookmark.id}
708 className="hover:bg-muted/50 cursor-pointer p-4 transition-colors"
709 onClick={() => navigateToUrl(bookmark.url)}
710 >
711 <div className="space-y-2 text-center">
712 <div className="text-2xl">{bookmark.favicon}</div>
713 <div className="text-sm font-medium">
714 {bookmark.title}
715 </div>
716 </div>
717 </Card>
718 ))}
719 </div>
720 </div>
721 </div>
722 ) : (
723 <div className="flex h-full items-center justify-center">
724 <Image
725 src={image}
726 alt={image}
727 width={320}
728 height={320}
729 className="h-full w-full rounded-md object-cover"
730 />
731 </div>
732 )}
733 </div>
734 </div>
735
736 {showStatusBar && (
737 <div className="bg-muted/30 border-border text-muted-foreground flex items-center justify-between border-t px-4 py-1 text-xs">
738 <div className="flex items-center gap-4">
739 <span>Ready</span>
740 {isSecure && (
741 <span className="flex items-center gap-1">
742 <Lock className="h-3 w-3" /> Secure
743 </span>
744 )}
745 </div>
746 <div className="flex items-center gap-4">
747 <span>Zoom: 100%</span>
748 <span>
749 {tabs.length} tab{tabs.length !== 1 ? "s" : ""}
750 </span>
751 </div>
752 </div>
753 )}
754 </div>
755 )
756}
757