"use client" import { useState, useEffect } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Store, Search } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; export default function StoreSelector() { const [storeId, setStoreId] = useState(''); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); const searchParams = useSearchParams(); const { toast } = useToast(); // Initialize with current storeId from URL useEffect(() => { const currentStoreId = searchParams.get('storeId'); if (currentStoreId) { setStoreId(currentStoreId); } }, [searchParams]); const handleStoreSelect = async () => { if (!storeId.trim()) { toast({ title: "Error", description: "Please enter a store ID", variant: "destructive", }); return; } setIsLoading(true); try { // Navigate to analytics with the selected storeId router.push(`/dashboard/analytics?storeId=${encodeURIComponent(storeId.trim())}`); toast({ title: "Store Selected", description: `Analytics will now show data for store: ${storeId}`, }); } catch (error) { toast({ title: "Error", description: "Failed to switch store", variant: "destructive", }); } finally { setIsLoading(false); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleStoreSelect(); } }; return ( Select Store Enter the store ID to view analytics for that store
setStoreId(e.target.value)} onKeyPress={handleKeyPress} className="flex-1" />
{searchParams.get('storeId') && (
Currently viewing: {searchParams.get('storeId')}
)}
); }