-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
506 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { auth } from '@/auth'; | ||
import { API_TOKEN, VECTOR_HOST } from '@/lib/env'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
export async function GET(request: Request) { | ||
const { searchParams } = new URL(request.url); | ||
const query = searchParams.get('q'); | ||
|
||
const session = await auth(); | ||
if (!session?.user) { | ||
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 }); | ||
} | ||
const searchUrl = `${VECTOR_HOST}/api/vector/search`; | ||
const response = await fetch(searchUrl, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: API_TOKEN!, | ||
}, | ||
body: JSON.stringify({ | ||
userId: session?.user.id, | ||
query, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Error! status: ${response.status}`); | ||
} | ||
|
||
const result = await response.json(); | ||
console.log(result); | ||
|
||
return NextResponse.json(result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// components/SearchDialog.tsx | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { Input } from '@/components/ui/input'; | ||
import { Button } from '@/components/ui/button'; | ||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; | ||
import { Loader2, MessageCircle } from 'lucide-react'; | ||
import { ScrollArea } from '@/components/ui/scroll-area'; | ||
|
||
interface SearchResult { | ||
id: string; | ||
title: string; | ||
url: string; | ||
} | ||
|
||
interface SearchDialogProps { | ||
openSearch: boolean; | ||
onOpenModelChange: (open: boolean) => void; | ||
} | ||
|
||
interface SearchResult { | ||
id: string; | ||
title: string; | ||
url: string; | ||
text: string; | ||
} | ||
|
||
export function SearchDialog({ openSearch: open, onOpenModelChange: onOpenChange }: SearchDialogProps) { | ||
const router = useRouter(); | ||
const [query, setQuery] = useState(''); | ||
const [results, setResults] = useState<SearchResult[]>([]); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const handleSearch = async (searchQuery: string) => { | ||
if (!searchQuery.trim()) { | ||
setResults([]); | ||
return; | ||
} | ||
|
||
setIsLoading(true); | ||
try { | ||
const response = await fetch(`/api/history-search?q=${encodeURIComponent(searchQuery)}`); | ||
const data = await response.json(); | ||
console.log(data); | ||
setResults(data); | ||
} catch (error) { | ||
console.error('search error:', error); | ||
setResults([]); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
const handleResultClick = (url: string) => { | ||
router.push('/search/' + url); | ||
onOpenChange(false); | ||
}; | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={onOpenChange}> | ||
<DialogContent className="max-w-3xl"> | ||
<DialogHeader> | ||
<DialogTitle>AI-Powered Search History</DialogTitle> | ||
</DialogHeader> | ||
|
||
<div className="space-y-4"> | ||
<div className="flex gap-2"> | ||
<Input | ||
type="text" | ||
placeholder="Search your search history" | ||
value={query} | ||
onChange={(e) => setQuery(e.target.value)} | ||
className="flex-1" | ||
autoFocus | ||
/> | ||
<Button onClick={() => handleSearch(query)} disabled={isLoading}> | ||
{isLoading ? <Loader2 className="h-4 w-4 animate-spin" /> : 'Search'} | ||
</Button> | ||
</div> | ||
|
||
<ScrollArea className="h-[400px] rounded-md border"> | ||
{isLoading ? ( | ||
<div className="flex items-center justify-center h-full"> | ||
<div className="text-sm text-muted-foreground">Searching ...</div> | ||
</div> | ||
) : results.length === 0 ? ( | ||
<div className="flex items-center justify-center h-full"> | ||
<div className="text-sm text-muted-foreground">No Result</div> | ||
</div> | ||
) : ( | ||
<div className="divide-y"> | ||
{results.map((result) => ( | ||
<div | ||
key={result.id} | ||
className="flex items-start gap-3 p-4 hover:bg-muted/50 cursor-pointer transition-colors" | ||
onClick={() => handleResultClick(result.url)} | ||
> | ||
<div className="mt-1"> | ||
<MessageCircle className="h-5 w-5 text-muted-foreground" /> | ||
</div> | ||
<div className="flex-1 min-w-0"> | ||
<h4 className="text-sm font-medium leading-none mb-1 truncate">{result.title}</h4> | ||
<p className="text-sm text-muted-foreground line-clamp-2">{result.text}</p> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</ScrollArea> | ||
</div> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
|
||
import Link from 'next/link'; | ||
import Image from 'next/image'; | ||
|
||
import { siteConfig } from '@/config'; | ||
import { SidebarClose } from '@/components/sidebar/sidebar-close'; | ||
import { Button } from '@/components/ui/button'; | ||
import { SearchDialog } from '@/components/modal/search-model'; | ||
import { Search } from 'lucide-react'; | ||
|
||
export async function SidebarHeader() { | ||
const [open, setOpen] = React.useState(false); | ||
return ( | ||
<div className="flex items-center mt-4 md:col-span-1 mx-4"> | ||
<Link href="/" prefetch={false} className="items-center space-x-2 flex"> | ||
<Image src={'/logo.png'} width="24" height="24" alt="MemFree Logo"></Image> | ||
<span className=" mx-2 font-urban text-xl font-bold">{siteConfig.name}</span> | ||
</Link> | ||
<div className="flex ml-auto space-x-2"> | ||
<Button | ||
variant="ghost" | ||
className="hidden border-solid shadow-sm border-gray-200 dark:text-white dark:hover:bg-gray-700 rounded-full size-9 p-0 lg:flex" | ||
onClick={() => setOpen(true)} | ||
> | ||
<Search className="size-4" /> | ||
</Button> | ||
<SidebarClose /> | ||
</div> | ||
<SearchDialog openSearch={open} onOpenModelChange={setOpen} /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.