import React, { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { Header } from '@/components/layout/Header'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Search, Filter, Building2 } from 'lucide-react'; import { supabase } from '@/integrations/supabase/client'; import ParkOwnerCard from '@/components/park-owners/ParkOwnerCard'; import { Company } from '@/types/database'; const ParkOwners = () => { const [searchTerm, setSearchTerm] = useState(''); const [sortBy, setSortBy] = useState('name'); const [filterBy, setFilterBy] = useState('all'); const { data: parkOwners, isLoading } = useQuery({ queryKey: ['park-owners'], queryFn: async () => { // Get companies that are property owners with park counts const { data, error } = await supabase .from('companies') .select(` *, parks:parks!property_owner_id(count) `) .in('id', await supabase .from('parks') .select('property_owner_id') .not('property_owner_id', 'is', null) .then(({ data }) => data?.map(park => park.property_owner_id) || []) ) .order('name'); if (error) throw error; // Transform the data to include park_count const transformedData = data?.map(company => ({ ...company, park_count: company.parks?.[0]?.count || 0 })) || []; return transformedData as (Company & { park_count: number })[]; }, }); const filteredAndSortedOwners = React.useMemo(() => { if (!parkOwners) return []; let filtered = parkOwners.filter(owner => { const matchesSearch = owner.name.toLowerCase().includes(searchTerm.toLowerCase()) || (owner.description && owner.description.toLowerCase().includes(searchTerm.toLowerCase())); if (filterBy === 'all') return matchesSearch; if (filterBy === 'with-rating') return matchesSearch && owner.average_rating > 0; if (filterBy === 'established') return matchesSearch && owner.founded_year; return matchesSearch; }); // Sort filtered.sort((a, b) => { switch (sortBy) { case 'name': return a.name.localeCompare(b.name); case 'rating': return (b.average_rating || 0) - (a.average_rating || 0); case 'founded': return (b.founded_year || 0) - (a.founded_year || 0); case 'reviews': return (b.review_count || 0) - (a.review_count || 0); default: return 0; } }); return filtered; }, [parkOwners, searchTerm, sortBy, filterBy]); return (

Property Owners

Discover companies that own and manage theme parks

{/* Search and Filters */}
setSearchTerm(e.target.value)} className="pl-10" />
{/* Results Count */}
{filteredAndSortedOwners?.length || 0} property owners {searchTerm && ( Searching: "{searchTerm}" )}
{/* Loading State */} {isLoading && (

Loading property owners...

)} {/* Property Owners Grid */} {!isLoading && filteredAndSortedOwners && (
{filteredAndSortedOwners.map((owner) => ( ))}
)} {/* Empty State */} {!isLoading && filteredAndSortedOwners?.length === 0 && (

No property owners found

{searchTerm ? "Try adjusting your search terms or filters" : "No property owners are currently available" }

)}
); }; export default ParkOwners;