import useSWR from "swr"; import { useSearch } from "wouter"; import type { Document } from "dbtype/api"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { GalleryCard } from "../components/gallery/GalleryCard"; import TagBadge from "@/components/gallery/TagBadge"; async function fetcher(url: string) { const res = await fetch(url); return res.json(); } interface SearchParams { word?: string; tags?: string; limit?: number; cursor?: number; } function useSearchGallery({ word, tags, limit, cursor, }: SearchParams) { const search = new URLSearchParams(); if (word) search.set("word", word); if (tags) search.set("allow_tag", tags); if (limit) search.set("limit", limit.toString()); if (cursor) search.set("cursor", cursor.toString()); return useSWR< Document[] >(`/api/doc/search?${search.toString()}`, fetcher); } export default function Gallery() { const search = useSearch(); const searchParams = new URLSearchParams(search); const word = searchParams.get("word") ?? undefined; const tags = searchParams.get("allow_tag") ?? undefined; const limit = searchParams.get("limit"); const cursor = searchParams.get("cursor"); const { data, error, isLoading } = useSearchGallery({ word, tags, limit: limit ? Number.parseInt(limit) : undefined, cursor: cursor ? Number.parseInt(cursor) : undefined }); if (isLoading) { return
Loading...
} if (error) { return
Error: {String(error)}
} return (
{(word || tags) &&
{word && Search: {word}} {tags && Tags:
    {tags.split(",").map(x=> )}
}
} { data?.length === 0 &&
No results
} { data?.map((x) => { return ( ); }) }
); }