"use client" import React, { useState } from "react" import { LocationCapture } from "./LocationCapture" import { ShareSettingsComponent } from "./ShareSettings" import { LocationMap } from "./LocationMap" import type { LocationData, LocationShare } from "@/lib/location/locationStorage" import { LocationStorageService, generateShareToken } from "@/lib/location/locationStorage" import type { ShareSettings } from "@/lib/location/types" import { useAuth } from "@/context/AuthContext" export const ShareLocation: React.FC = () => { const { session, fileSystem } = useAuth() const [step, setStep] = useState<"capture" | "settings" | "share">("capture") const [capturedLocation, setCapturedLocation] = useState(null) const [shareSettings, setShareSettings] = useState({ duration: 24 * 3600000, // 24 hours maxViews: null, precision: "street", }) const [shareLink, setShareLink] = useState(null) const [isCreatingShare, setIsCreatingShare] = useState(false) const [error, setError] = useState(null) // Show loading state while auth is initializing if (session.loading) { return (

Loading...

Initializing authentication

) } const handleLocationCaptured = (location: LocationData) => { setCapturedLocation(location) setStep("settings") } const handleCreateShare = async () => { if (!capturedLocation || !fileSystem) { setError("Location or filesystem not available") return } setIsCreatingShare(true) setError(null) try { const storageService = new LocationStorageService(fileSystem) await storageService.initialize() // Generate share token const shareToken = generateShareToken() // Calculate expiration const expiresAt = shareSettings.duration ? Date.now() + shareSettings.duration : null // Update location with expiration const updatedLocation: LocationData = { ...capturedLocation, expiresAt, precision: shareSettings.precision, } await storageService.saveLocation(updatedLocation) // Create share const share: LocationShare = { id: crypto.randomUUID(), locationId: capturedLocation.id, shareToken, createdAt: Date.now(), expiresAt, maxViews: shareSettings.maxViews, viewCount: 0, precision: shareSettings.precision, } await storageService.createShare(share) // Generate share link const baseUrl = window.location.origin const link = `${baseUrl}/location/${shareToken}` setShareLink(link) setStep("share") } catch (err) { console.error("Error creating share:", err) setError("Failed to create share link") } finally { setIsCreatingShare(false) } } const handleCopyLink = async () => { if (!shareLink) return try { await navigator.clipboard.writeText(shareLink) // Could add a toast notification here alert("Link copied to clipboard!") } catch (err) { console.error("Failed to copy link:", err) alert("Failed to copy link. Please copy manually.") } } const handleReset = () => { setStep("capture") setCapturedLocation(null) setShareLink(null) setError(null) } if (!session.authed) { return (
🔒

Authentication Required

Please log in to share your location securely

) } return (
{/* Progress Steps */}
{["capture", "settings", "share"].map((s, index) => (
{index + 1}
{s}
{index < 2 && (
)} ))}
{/* Error Display */} {error && (

{error}

)} {/* Step Content */}
{step === "capture" && } {step === "settings" && capturedLocation && (

Preview Your Location

)} {step === "share" && shareLink && capturedLocation && (

Share Link Created!

Your location is ready to share securely

e.currentTarget.select()} />

Location Preview

Share Settings

Precision: {shareSettings.precision}
Duration: {shareSettings.duration ? `${shareSettings.duration / 3600000} hours` : "No expiration"}
Max Views: {shareSettings.maxViews || "Unlimited"}
)}
) }