"use client" import type React from "react" import { useState, useEffect } from "react" import { useAuth } from "@/context/AuthContext" import { LocationStorageService, type LocationData, type LocationShare } from "@/lib/location/locationStorage" import { LocationMap } from "./LocationMap" interface ShareWithLocation { share: LocationShare location: LocationData } export const LocationDashboard: React.FC = () => { const { session, fileSystem } = useAuth() const [shares, setShares] = useState([]) const [loading, setLoading] = useState(true) const [selectedShare, setSelectedShare] = useState(null) const [error, setError] = useState(null) const loadShares = async () => { if (!fileSystem) { setError("File system not available") setLoading(false) return } try { const storageService = new LocationStorageService(fileSystem) await storageService.initialize() // Get all shares const allShares = await storageService.getAllShares() // Get locations for each share const sharesWithLocations: ShareWithLocation[] = [] for (const share of allShares) { const location = await storageService.getLocation(share.locationId) if (location) { sharesWithLocations.push({ share, location }) } } // Sort by creation date (newest first) sharesWithLocations.sort((a, b) => b.share.createdAt - a.share.createdAt) setShares(sharesWithLocations) setLoading(false) } catch (err) { console.error("Error loading shares:", err) setError("Failed to load location shares") setLoading(false) } } useEffect(() => { if (session.authed && fileSystem) { loadShares() } }, [session.authed, fileSystem]) const handleCopyLink = async (shareToken: string) => { const baseUrl = window.location.origin const link = `${baseUrl}/location/${shareToken}` try { await navigator.clipboard.writeText(link) alert("Link copied to clipboard!") } catch (err) { console.error("Failed to copy link:", err) alert("Failed to copy link") } } const isExpired = (share: LocationShare): boolean => { return share.expiresAt ? share.expiresAt < Date.now() : false } const isMaxViewsReached = (share: LocationShare): boolean => { return share.maxViews ? share.viewCount >= share.maxViews : false } const getShareStatus = (share: LocationShare): { label: string; color: string } => { if (isExpired(share)) { return { label: "Expired", color: "text-destructive" } } if (isMaxViewsReached(share)) { return { label: "Max Views Reached", color: "text-destructive" } } return { label: "Active", color: "text-green-600" } } if (!session.authed) { return (
🔒

Authentication Required

Please log in to view your location shares

) } if (loading) { return (

Loading your shares...

) } if (error) { return (
⚠️

Error Loading Dashboard

{error}

) } return (

Location Shares

Manage your shared locations and privacy settings

{shares.length === 0 ? (
📍

No Location Shares Yet

You haven't shared any locations yet. Create your first share to get started.

Share Your Location
) : (
{/* Stats Overview */}
Total Shares
{shares.length}
Active Shares
{shares.filter((s) => !isExpired(s.share) && !isMaxViewsReached(s.share)).length}
Total Views
{shares.reduce((sum, s) => sum + s.share.viewCount, 0)}
{/* Shares List */}
{shares.map(({ share, location }) => { const status = getShareStatus(share) const isSelected = selectedShare?.share.id === share.id return (

Location Share

{status.label}

Created: {new Date(share.createdAt).toLocaleString()}

{share.expiresAt &&

Expires: {new Date(share.expiresAt).toLocaleString()}

}

Views: {share.viewCount} {share.maxViews && ` / ${share.maxViews}`}

Precision: {share.precision}

{isSelected && (
)}
) })}
)}
) }