'use client'
import { Badge } from '@thedaviddias/design-system/badge'
import { ToggleGroup, ToggleGroupItem } from '@thedaviddias/design-system/toggle-group'
import { Clock, SortAsc } from 'lucide-react'
import Link from 'next/link'
import { useEffect, useMemo, useState } from 'react'
import { useAnalyticsEvents } from '@/components/analytics-tracker'
import { EmptyState } from '@/components/empty-state'
import { Card } from '@/components/ui/card'
import { FaviconWithFallback } from '@/components/ui/favicon-with-fallback'
import type { WebsiteMetadata } from '@/lib/content-loader'
import { getRoute } from '@/lib/routes'
import { stripHtmlTags } from '@/lib/utils'
interface WebsitesListWithSortProps {
initialWebsites: WebsiteMetadata[]
emptyTitle?: string
emptyDescription?: string
categoryType?: 'tool' | 'non-tool'
}
/**
* Client component that handles sorting on the client side
* Initial data is server-side sorted, then client can re-sort
*/
export function WebsitesListWithSort({
initialWebsites,
emptyTitle = 'No websites found',
emptyDescription = 'There are no websites available. Try checking back later or submit a new website.',
categoryType = 'non-tool'
}: WebsitesListWithSortProps) {
const [sortBy, setSortBy] = useState<'name' | 'latest'>('name')
const [isClient, setIsClient] = useState(false)
const { trackSortChange } = useAnalyticsEvents()
// Load saved sort preference
useEffect(() => {
setIsClient(true)
const savedSortBy = localStorage.getItem('category-sort-by')
if (savedSortBy && (savedSortBy === 'name' || savedSortBy === 'latest')) {
setSortBy(savedSortBy)
}
}, [])
// Save sort preference
useEffect(() => {
if (isClient) {
localStorage.setItem('category-sort-by', sortBy)
}
}, [sortBy, isClient])
const sortedWebsites = useMemo(() => {
const websites = [...initialWebsites]
if (sortBy === 'latest') {
// Sort by publishedAt date, most recent first
return websites.sort((a, b) => {
const dateA = new Date(a.publishedAt).getTime()
const dateB = new Date(b.publishedAt).getTime()
return dateB - dateA // Newer dates first
})
} else {
// Sort alphabetically by name
return websites.sort((a, b) => a.name.localeCompare(b.name))
}
}, [initialWebsites, sortBy])
/**
* Renders a single website card
*/
const renderWebsite = (website: WebsiteMetadata) => (
{stripHtmlTags(website.description)}
{website.name}
{website.isUnofficial && (