import * as React from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { Link, useNavigate, useSearch } from '@tanstack/react-router' import { getApprovedShowcasesQueryOptions, getMyShowcaseVotesQueryOptions, } from '~/queries/showcases' import { voteShowcase } from '~/utils/showcase.functions' import { ShowcaseCard, ShowcaseCardSkeleton } from './ShowcaseCard' import { CommunityProjectCard } from './CommunityProjectCard' import { SubmitShowcasePlaceholder } from './ShowcaseSection' import { PaginationControls } from './PaginationControls' import { ShowcaseTopBarFilters } from './ShowcaseTopBarFilters' import type { ShowcaseUseCase } from '~/db/types' import { PlusIcon } from '@phosphor-icons/react' import { Button } from '~/ui' import { useCurrentUser } from '~/hooks/useCurrentUser' import { useLoginModal } from '~/contexts/LoginModalContext' import type { LibraryId } from '~/libraries' import { PAGE_SIZE_OPTIONS } from '~/routes/showcase' export function ShowcaseGallery({ community = false, }: { community?: boolean }) { const gridClassName = community ? 'grid grid-cols-1 md:grid-cols-2 gap-x-10' : 'grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6' const from = community ? '/community-projects' : '/showcase/' const placement = community ? 'community' : 'showcase' const navigate = useNavigate({ from }) const search = useSearch({ from }) const queryClient = useQueryClient() const currentUser = useCurrentUser() const { openLoginModal } = useLoginModal() const pageSize = search.pageSize ?? 24 const { data, isLoading } = useQuery( getApprovedShowcasesQueryOptions({ pagination: { page: search.page, pageSize, }, filters: { placement, libraryIds: search.libraryIds, useCases: search.useCases, hasSourceCode: search.hasSourceCode, q: search.q, }, }), ) const showcaseIds = React.useMemo( () => data?.showcases.map((s) => s.showcase.id) ?? [], [data?.showcases], ) const { data: votesData } = useQuery({ ...getMyShowcaseVotesQueryOptions(showcaseIds), enabled: !community && !!currentUser && showcaseIds.length > 0, }) const votesMap = React.useMemo(() => { const map = new Map() votesData?.votes.forEach((v) => { if (v.value === 1 || v.value === -1) { map.set(v.showcaseId, v.value) } }) return map }, [votesData]) const voteMutation = useMutation({ mutationFn: (params: { showcaseId: string; value: 1 | -1 }) => voteShowcase({ data: params }), onMutate: async ({ showcaseId, value }) => { // Cancel outgoing refetches await queryClient.cancelQueries({ queryKey: ['showcases'] }) // Snapshot previous values const previousShowcases = queryClient.getQueryData( getApprovedShowcasesQueryOptions({ pagination: { page: search.page, pageSize }, filters: { placement, libraryIds: search.libraryIds, useCases: search.useCases, hasSourceCode: search.hasSourceCode, q: search.q, }, }).queryKey, ) const previousVotes = queryClient.getQueryData( getMyShowcaseVotesQueryOptions(showcaseIds).queryKey, ) // Optimistically update votes queryClient.setQueryData( getMyShowcaseVotesQueryOptions(showcaseIds).queryKey, (old: typeof votesData) => { if (!old) return { votes: [{ showcaseId, value }] } const existingVote = old.votes.find( (v) => v.showcaseId === showcaseId, ) if (existingVote) { if (existingVote.value === value) { // Toggle off return { votes: old.votes.filter((v) => v.showcaseId !== showcaseId), } } else { // Change vote return { votes: old.votes.map((v) => v.showcaseId === showcaseId ? { ...v, value } : v, ), } } } else { // New vote return { votes: [...old.votes, { showcaseId, value }] } } }, ) // Optimistically update showcase score queryClient.setQueryData( getApprovedShowcasesQueryOptions({ pagination: { page: search.page, pageSize }, filters: { placement, libraryIds: search.libraryIds, useCases: search.useCases, hasSourceCode: search.hasSourceCode, q: search.q, }, }).queryKey, (old: typeof data) => { if (!old) return old const currentVote = votesMap.get(showcaseId) return { ...old, showcases: old.showcases.map((s) => { if (s.showcase.id !== showcaseId) return s let scoreDelta: number = value if (currentVote === value) { // Toggling off scoreDelta = -value } else if (currentVote) { // Changing vote scoreDelta = value * 2 } return { ...s, showcase: { ...s.showcase, voteScore: s.showcase.voteScore + scoreDelta, }, } }), } }, ) return { previousShowcases, previousVotes } }, onError: (_err, _vars, context) => { // Rollback on error if (context?.previousShowcases) { queryClient.setQueryData( getApprovedShowcasesQueryOptions({ pagination: { page: search.page, pageSize }, filters: { placement, libraryIds: search.libraryIds, useCases: search.useCases, hasSourceCode: search.hasSourceCode, q: search.q, }, }).queryKey, context.previousShowcases, ) } if (context?.previousVotes) { queryClient.setQueryData( getMyShowcaseVotesQueryOptions(showcaseIds).queryKey, context.previousVotes, ) } }, onSettled: () => { // Refetch to sync with server queryClient.invalidateQueries({ queryKey: ['showcases'] }) }, }) const handleVote = (showcaseId: string, value: 1 | -1) => { if (!currentUser) { openLoginModal({ onSuccess: () => voteMutation.mutate({ showcaseId, value }), }) return } voteMutation.mutate({ showcaseId, value }) } const handleLibraryToggle = (libraryId: LibraryId) => { const current = search.libraryIds || [] const updated = current.includes(libraryId) ? current.filter((id) => id !== libraryId) : [...current, libraryId] navigate({ search: (prev) => ({ ...prev, libraryIds: updated.length > 0 ? updated : undefined, page: 1, }), }) } const clearLibraries = () => { navigate({ search: (prev: typeof search) => ({ ...prev, libraryIds: undefined, page: 1, }), }) } const handleUseCaseFilter = (useCase: ShowcaseUseCase) => { const current = search.useCases || [] const updated = current.includes(useCase) ? current.filter((c: ShowcaseUseCase) => c !== useCase) : [...current, useCase] navigate({ search: (prev: typeof search) => ({ ...prev, useCases: updated.length > 0 ? updated : undefined, page: 1, }), }) } const clearUseCases = () => { navigate({ search: (prev: typeof search) => ({ ...prev, useCases: undefined, page: 1, }), }) } const handleToggleOpenSource = () => { navigate({ search: (prev: typeof search) => ({ ...prev, hasSourceCode: prev.hasSourceCode ? undefined : true, page: 1, }), }) } const handlePageChange = (newPage: number) => { navigate({ search: (prev: typeof search) => ({ ...prev, page: newPage + 1 }), }) } const handlePageSizeChange = (newPageSize: number) => { navigate({ search: (prev: typeof search) => ({ ...prev, pageSize: newPageSize, page: 1, }), }) } const handleSearchChange = (q: string) => { navigate({ search: (prev: typeof search) => ({ ...prev, q: q || undefined, page: 1, }), }) } const clearFilters = () => { navigate({ search: { page: 1, libraryIds: undefined, useCases: undefined, hasSourceCode: undefined, q: undefined, }, }) } const hasFilters = (search.libraryIds && search.libraryIds.length > 0) || (search.useCases && search.useCases.length > 0) || search.hasSourceCode || search.q return (
{/* Header */}

{community ? 'Community projects' : 'Showcase'}

{!community && (

Selected by the TanStack team for their adoption, craft, and technical depth.

)}
{community ? 'Showcase' : 'Community projects'}
{/* Filters */}
{/* Grid */}
{isLoading ? (
{Array.from({ length: 6 }).map((_, i) => community ? (
) : ( ), )}
) : data?.showcases && data.showcases.length > 0 ? ( <>
{data.showcases.map(({ showcase, user }) => community ? ( ) : ( handleVote(showcase.id, value)} isVoting={ voteMutation.isPending && voteMutation.variables?.showcaseId === showcase.id } /> ), )}
{data.pagination.totalPages > 1 && (
1} canGoNext={search.page < data.pagination.totalPages} itemLabel="projects" showPageSizeSelector pageSizeOptions={[...PAGE_SIZE_OPTIONS]} />
)} ) : hasFilters ? (

No projects found matching your filters.

) : (

No projects found.

)}
) }