xonecas.io
556
Bn

bees-nest / commit 22255f9

feat: Implement Search UI and integrate with backend

xonecas2026-01-27 22255f9c218760b78ea683956b8a20beacd78b80 parent 00f1e00

6 files changed

scripts/seed-search.ts+96−0
@@ -0,0 +1,96 @@
1+import { eq } from "drizzle-orm";
2+import { domains, pages, urls } from "@/db/schema";
3+import { db } from "@/lib/db";
4+
5+async function seed() {
6+ console.log("Seeding test data...");
7+
8+ const domainName = "test.com";
9+ const url = "https://test.com/hello-world";
10+
11+ try {
12+ // 1. Insert Domain
13+ let domainId: number;
14+ const existingDomain = await db.query.domains.findFirst({
15+ where: eq(domains.domain, domainName),
16+ });
17+
18+ if (existingDomain) {
19+ domainId = existingDomain.id;
20+ console.log(`Domain ${domainName} already exists (ID: ${domainId})`);
21+ } else {
22+ const [newDomain] = await db
23+ .insert(domains)
24+ .values({
25+ domain: domainName,
26+ })
27+ .returning();
28+ domainId = newDomain.id;
29+ console.log(`Created domain ${domainName} (ID: ${domainId})`);
30+ }
31+
32+ // 2. Insert URL
33+ let urlId: number;
34+ const existingUrl = await db.query.urls.findFirst({
35+ where: eq(urls.url, url),
36+ });
37+
38+ if (existingUrl) {
39+ urlId = existingUrl.id;
40+ console.log(`URL ${url} already exists (ID: ${urlId})`);
41+ } else {
42+ const [newUrl] = await db
43+ .insert(urls)
44+ .values({
45+ url: url,
46+ domainId: domainId,
47+ status: "completed",
48+ })
49+ .returning();
50+ urlId = newUrl.id;
51+ console.log(`Created URL ${url} (ID: ${urlId})`);
52+ }
53+
54+ // 3. Insert Page
55+ const existingPage = await db.query.pages.findFirst({
56+ where: eq(pages.urlId, urlId),
57+ });
58+
59+ if (existingPage) {
60+ console.log(`Page for URL ID ${urlId} already exists. Updating...`);
61+ await db
62+ .update(pages)
63+ .set({
64+ title: "Hello World Test Page",
65+ description:
66+ "This is a mock page injected for testing the search UI and API integration.",
67+ content:
68+ "Welcome to the test page. We are strictly testing the search functionality here. The quick brown fox jumps over the lazy dog.",
69+ siteName: "Test Site",
70+ image: "/window.svg", // Use one of the public assets
71+ })
72+ .where(eq(pages.id, existingPage.id));
73+ console.log("Updated existing page.");
74+ } else {
75+ await db.insert(pages).values({
76+ urlId: urlId,
77+ title: "Hello World Test Page",
78+ description:
79+ "This is a mock page injected for testing the search UI and API integration.",
80+ content:
81+ "Welcome to the test page. We are strictly testing the search functionality here. The quick brown fox jumps over the lazy dog.",
82+ siteName: "Test Site",
83+ image: "/window.svg",
84+ });
85+ console.log("Created new page.");
86+ }
87+
88+ console.log("Seeding complete!");
89+ process.exit(0);
90+ } catch (error) {
91+ console.error("Error seeding data:", error);
92+ process.exit(1);
93+ }
94+}
95+
96+seed();
src/app/page.tsx+13−144
@@ -1,152 +1,21 @@
1+import { CrawlInterface } from "@/components/crawl-interface";
2+import { SearchInterface } from "@/components/search-interface";
13 import { ThemeToggle } from "@/components/theme-toggle";
2-import { Badge } from "@/components/ui/badge";
3-import { Button } from "@/components/ui/button";
4-import {
5- Card,
6- CardContent,
7- CardDescription,
8- CardFooter,
9- CardHeader,
10- CardTitle,
11-} from "@/components/ui/card";
12-import { Input } from "@/components/ui/input";
13-import { Separator } from "@/components/ui/separator";
14-
15-const crawlSteps = [
16- "Review the crawl scope and rate limits.",
17- "Connect a site or repository to index.",
18- "Start a private crawl session.",
19-];
20-
21-const searchChips = ["docs", "product", "issues", "release notes"];
22-
23-const searchResults = [
24- {
25- title: "Overview of indexing signals",
26- description: "How ranking signals are evaluated during retrieval.",
27- source: "Search docs",
28- },
29- {
30- title: "Crawler queue status",
31- description: "See how prioritization affects scheduled recrawls.",
32- source: "Operations",
33- },
34- {
35- title: "Glossary: recall vs precision",
36- description: "Understand how result quality is balanced.",
37- source: "Knowledge base",
38- },
39-];
404
415 export default function Page() {
426 return (
43- <main className="flex min-h-screen flex-col bg-background text-foreground">
44- <div className="grid flex-1 grid-cols-1 gap-4 md:grid-cols-2 md:gap-6">
45- <section className="flex items-center justify-center bg-[radial-gradient(circle_at_bottom,_var(--tw-gradient-stops))] from-sky-100/70 via-background to-background px-6 py-12">
46- <div className="flex w-full max-w-2xl flex-col gap-6">
47- <div className="flex items-center justify-between gap-4">
48- <div>
49- <p className="text-xs uppercase tracking-[0.2em] text-muted-foreground">
50- Search preview
51- </p>
52- <h2 className="text-2xl font-semibold">Find what matters.</h2>
53- </div>
54- <Badge>Beta</Badge>
55- </div>
56- <Card className="shadow-sm">
57- <CardHeader>
58- <CardTitle>Search</CardTitle>
59- <CardDescription>
60- Placeholder search UI for indexed content.
61- </CardDescription>
62- </CardHeader>
63- <CardContent className="flex flex-col gap-4">
64- <div className="flex flex-col gap-3 sm:flex-row">
65- <Input placeholder="Search docs, tickets, or notes" />
66- <Button className="sm:w-32">Search</Button>
67- </div>
68- <div className="flex flex-wrap gap-2">
69- {searchChips.map((chip) => (
70- <Badge key={chip} variant="secondary">
71- {chip}
72- </Badge>
73- ))}
74- </div>
75- <Separator />
76- <div className="flex flex-col gap-3">
77- {searchResults.map((result) => (
78- <div
79- key={result.title}
80- className="rounded-2xl border border-border/60 bg-background/60 px-4 py-3"
81- >
82- <div className="flex items-center justify-between">
83- <span className="font-medium">{result.title}</span>
84- <Badge variant="outline">{result.source}</Badge>
85- </div>
86- <p className="text-sm text-muted-foreground">
87- {result.description}
88- </p>
89- </div>
90- ))}
91- </div>
92- </CardContent>
93- <CardFooter className="justify-between text-xs text-muted-foreground">
94- <span>Showing 3 placeholder results</span>
95- <span>Last indexed: just now</span>
96- </CardFooter>
97- </Card>
98- </div>
99- </section>
100- <section className="flex items-center justify-center bg-[radial-gradient(circle_at_top,_var(--tw-gradient-stops))] from-amber-100/70 via-background to-background px-6 py-12">
101- <div className="flex w-full max-w-xl flex-col gap-6">
102- <div className="flex items-center gap-3">
103- <Badge variant="secondary">Crawl opt-in</Badge>
104- <span className="text-xs uppercase tracking-[0.2em] text-muted-foreground">
105- Private by default
106- </span>
107- </div>
108- <h1 className="text-balance text-3xl font-semibold leading-tight">
109- Bring your sources online with a secure crawl session.
110- </h1>
111- <p className="text-pretty text-sm text-muted-foreground">
112- Opt in once to allow the crawler to fetch the sources you choose.
113- You stay in control of what gets indexed and when it refreshes.
114- </p>
115- <Card className="shadow-sm">
116- <CardHeader>
117- <CardTitle>Start a crawl</CardTitle>
118- <CardDescription>
119- We will prepare a private crawl environment for your data.
120- </CardDescription>
121- </CardHeader>
122- <CardContent className="flex flex-col gap-4">
123- <div className="flex flex-col gap-3 text-sm">
124- {crawlSteps.map((step) => (
125- <div key={step} className="flex items-start gap-3">
126- <span className="mt-1 h-2.5 w-2.5 rounded-full bg-primary/70" />
127- <span className="text-muted-foreground">{step}</span>
128- </div>
129- ))}
130- </div>
131- <Separator />
132- <div className="rounded-2xl border border-dashed border-border/70 bg-muted/40 px-4 py-3 text-xs text-muted-foreground">
133- Placeholder: Crawl configuration and target selection will
134- live here.
135- </div>
136- </CardContent>
137- <CardFooter className="gap-3">
138- <Button className="w-full">Enable crawling</Button>
139- <Button variant="outline" className="w-full">
140- Review permissions
141- </Button>
142- </CardFooter>
143- </Card>
144- </div>
145- </section>
146- </div>
147- <footer className="flex items-center justify-end px-6 py-4">
7+ <main className="min-h-screen bg-background text-foreground relative flex flex-col">
8+ <div className="absolute top-4 right-6 z-10">
1489 <ThemeToggle />
149- </footer>
10+ </div>
11+ <div className="grid flex-1 grid-cols-1 md:grid-cols-2">
12+ <div className="border-r border-border/40">
13+ <SearchInterface />
14+ </div>
15+ <div className="h-full">
16+ <CrawlInterface />
17+ </div>
18+ </div>
15019 </main>
15120 );
15221 }
src/components/crawl-interface.tsx+45−0
@@ -0,0 +1,45 @@
1+"use client";
2+
3+import * as React from "react";
4+import { Badge } from "@/components/ui/badge";
5+import { Button } from "@/components/ui/button";
6+import {
7+ Card,
8+ CardContent,
9+ CardDescription,
10+ CardFooter,
11+ CardHeader,
12+ CardTitle,
13+} from "@/components/ui/card";
14+
15+export function CrawlInterface() {
16+ return (
17+ <div className="flex h-full flex-col items-center justify-center bg-muted/10 p-6">
18+ <div className="flex w-full max-w-md flex-col gap-6">
19+ <div className="flex items-center gap-3">
20+ <Badge variant="secondary">Crawl opt-in</Badge>
21+ <span className="text-xs uppercase tracking-[0.2em] text-muted-foreground">
22+ Private by default
23+ </span>
24+ </div>
25+
26+ <Card className="shadow-sm">
27+ <CardHeader>
28+ <CardTitle>Start a crawl</CardTitle>
29+ <CardDescription>
30+ Opt in to crawl URLs and bring your sources online.
31+ </CardDescription>
32+ </CardHeader>
33+ <CardContent>
34+ <div className="rounded-2xl border border-dashed border-border/70 bg-muted/40 px-4 py-8 text-center text-sm text-muted-foreground">
35+ Opt in to crawl URLs
36+ </div>
37+ </CardContent>
38+ <CardFooter>
39+ <Button className="w-full">Enable crawling</Button>
40+ </CardFooter>
41+ </Card>
42+ </div>
43+ </div>
44+ );
45+}
src/components/result-card.tsx+92−0
@@ -0,0 +1,92 @@
1+"use client";
2+
3+import { ArrowRight01Icon } from "@hugeicons/core-free-icons";
4+import { HugeiconsIcon } from "@hugeicons/react";
5+import Image from "next/image";
6+import Link from "next/link";
7+import type * as React from "react";
8+
9+import { Badge } from "@/components/ui/badge";
10+import { Button } from "@/components/ui/button";
11+import {
12+ Card,
13+ CardContent,
14+ CardDescription,
15+ CardFooter,
16+ CardHeader,
17+ CardTitle,
18+} from "@/components/ui/card";
19+import { cn } from "@/lib/utils";
20+
21+export interface SearchResult {
22+ id: string;
23+ title: string;
24+ description: string;
25+ source: string;
26+ url: string;
27+ imageUrl?: string;
28+ date?: string;
29+}
30+
31+interface ResultCardProps extends React.ComponentProps<typeof Card> {
32+ result: SearchResult;
33+}
34+
35+export function ResultCard({ result, className, ...props }: ResultCardProps) {
36+ return (
37+ <Link
38+ href={result.url}
39+ target="_blank"
40+ rel="noopener noreferrer"
41+ className="block"
42+ >
43+ <Card
44+ className={cn(
45+ "overflow-hidden transition-colors hover:bg-muted/40",
46+ className,
47+ )}
48+ {...props}
49+ >
50+ <div className="flex flex-col sm:flex-row">
51+ {result.imageUrl && (
52+ <div className="relative h-48 w-full sm:h-auto sm:w-48 shrink-0 bg-muted">
53+ <Image
54+ src={result.imageUrl}
55+ alt={result.title}
56+ fill
57+ className="object-cover"
58+ />
59+ </div>
60+ )}
61+ <div className="flex flex-1 flex-col">
62+ <CardHeader>
63+ <div className="flex items-start justify-between gap-4">
64+ <div className="space-y-1">
65+ <CardTitle className="line-clamp-1">{result.title}</CardTitle>
66+ <CardDescription className="line-clamp-2">
67+ {result.description}
68+ </CardDescription>
69+ </div>
70+ <Badge variant="secondary" className="shrink-0">
71+ {result.source}
72+ </Badge>
73+ </div>
74+ </CardHeader>
75+ <CardContent className="flex-1">
76+ {/* Additional content could go here */}
77+ </CardContent>
78+ <CardFooter className="justify-between">
79+ <span className="text-xs text-muted-foreground">
80+ {result.date || "Recently indexed"}
81+ </span>
82+ <Button variant="ghost" size="sm" className="gap-1 text-xs">
83+ Read more
84+ <HugeiconsIcon icon={ArrowRight01Icon} className="size-3" />
85+ </Button>
86+ </CardFooter>
87+ </div>
88+ </div>
89+ </Card>
90+ </Link>
91+ );
92+}
src/components/search-form.tsx+54−0
@@ -0,0 +1,54 @@
1+"use client";
2+
3+import { Search01Icon } from "@hugeicons/core-free-icons";
4+import { HugeiconsIcon } from "@hugeicons/react";
5+import * as React from "react";
6+
7+import { Button } from "@/components/ui/button";
8+import { Input } from "@/components/ui/input";
9+import { cn } from "@/lib/utils";
10+
11+interface SearchFormProps extends React.ComponentProps<"form"> {
12+ onSearch: (query: string) => void;
13+ initialQuery?: string;
14+}
15+
16+export function SearchForm({
17+ className,
18+ onSearch,
19+ initialQuery = "",
20+ ...props
21+}: SearchFormProps) {
22+ const [query, setQuery] = React.useState(initialQuery);
23+
24+ const handleSubmit = (e: React.FormEvent) => {
25+ e.preventDefault();
26+ if (query.trim()) {
27+ onSearch(query);
28+ }
29+ };
30+
31+ return (
32+ <form
33+ onSubmit={handleSubmit}
34+ className={cn("flex w-full max-w-2xl items-center gap-2", className)}
35+ {...props}
36+ >
37+ <div className="relative flex-1">
38+ <HugeiconsIcon
39+ icon={Search01Icon}
40+ strokeWidth={2}
41+ className="text-muted-foreground absolute left-3 top-1/2 size-4 -translate-y-1/2"
42+ />
43+ <Input
44+ type="search"
45+ placeholder="Search for documentation, issues, or notes..."
46+ className="pl-9"
47+ value={query}
48+ onChange={(e) => setQuery(e.target.value)}
49+ />
50+ </div>
51+ <Button type="submit">Search</Button>
52+ </form>
53+ );
54+}
src/components/search-interface.tsx+137−0
@@ -0,0 +1,137 @@
1+"use client";
2+
3+import * as React from "react";
4+import { ResultCard, type SearchResult } from "@/components/result-card";
5+import { SearchForm } from "@/components/search-form";
6+import { cn } from "@/lib/utils";
7+
8+interface ApiSearchResult {
9+ id: number;
10+ title: string | null;
11+ description: string | null;
12+ url: string;
13+ image: string | null;
14+ siteName: string | null;
15+ rank: number;
16+}
17+
18+export function SearchInterface() {
19+ const [hasSearched, setHasSearched] = React.useState(false);
20+ const [results, setResults] = React.useState<SearchResult[]>([]);
21+ const [isSearching, setIsSearching] = React.useState(false);
22+ const [error, setError] = React.useState<string | null>(null);
23+
24+ const getHostnameSafely = (url: string) => {
25+ try {
26+ return new URL(url).hostname;
27+ } catch {
28+ return url;
29+ }
30+ };
31+
32+ const handleSearch = async (query: string) => {
33+ setIsSearching(true);
34+ setError(null);
35+ setHasSearched(true); // Move to top immediately for better UX
36+
37+ try {
38+ const response = await fetch(
39+ `/api/search?q=${encodeURIComponent(query)}`,
40+ );
41+
42+ if (!response.ok) {
43+ throw new Error("Failed to fetch search results");
44+ }
45+
46+ const data = await response.json();
47+
48+ const mappedResults: SearchResult[] = (data.results || []).map(
49+ (item: ApiSearchResult) => ({
50+ id: item.id.toString(),
51+ title: item.title || "Untitled",
52+ description: item.description || "No description available",
53+ source: item.siteName || getHostnameSafely(item.url),
54+ url: item.url,
55+ imageUrl: item.image || undefined,
56+ date: undefined, // API doesn't return date yet
57+ }),
58+ );
59+
60+ setResults(mappedResults);
61+ } catch (err) {
62+ console.error(err);
63+ setError("An error occurred while searching. Please try again.");
64+ setResults([]);
65+ } finally {
66+ setIsSearching(false);
67+ }
68+ };
69+
70+ return (
71+ <div
72+ className={cn(
73+ "flex flex-col transition-all duration-500 ease-in-out w-full max-w-4xl mx-auto px-4",
74+ hasSearched
75+ ? "pt-8 gap-8"
76+ : "min-h-[80vh] justify-center items-center gap-6",
77+ )}
78+ >
79+ <div
80+ className={cn(
81+ "flex flex-col items-center gap-6 w-full transition-all duration-500",
82+ hasSearched ? "items-start" : "items-center text-center",
83+ )}
84+ >
85+ <SearchForm
86+ onSearch={handleSearch}
87+ className={cn(
88+ "transition-all duration-500",
89+ hasSearched ? "max-w-full" : "max-w-2xl",
90+ )}
91+ />
92+ </div>
93+
94+ {hasSearched && (
95+ <div className="w-full space-y-4 animate-in slide-in-from-bottom-4 duration-500 fade-in">
96+ {isSearching ? (
97+ <div className="space-y-4">
98+ <div className="text-sm text-muted-foreground">Searching...</div>
99+ {/* Skeleton Loading */}
100+ {[1, 2, 3].map((i) => (
101+ <div
102+ key={i}
103+ className="h-32 w-full animate-pulse rounded-2xl bg-muted/40"
104+ />
105+ ))}
106+ </div>
107+ ) : error ? (
108+ <div className="rounded-xl border border-destructive/20 bg-destructive/10 p-4 text-sm text-destructive">
109+ {error}
110+ </div>
111+ ) : results.length > 0 ? (
112+ <>
113+ <div className="text-sm text-muted-foreground">
114+ Found {results.length} results
115+ </div>
116+ <div className="grid gap-4">
117+ {results.map((result) => (
118+ <ResultCard key={result.id} result={result} />
119+ ))}
120+ </div>
121+ </>
122+ ) : (
123+ <div className="flex flex-col items-center justify-center gap-2 py-12 text-center text-muted-foreground">
124+ <p className="text-lg font-medium text-foreground">
125+ No results found
126+ </p>
127+ <p>
128+ We couldn't find anything matching your search. Try different
129+ keywords.
130+ </p>
131+ </div>
132+ )}
133+ </div>
134+ )}
135+ </div>
136+ );
137+}