Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): integrate search into the home page #2405

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 38 additions & 28 deletions ee/tabby-ui/app/(home)/components/completion-charts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,25 @@ export function CompletionCharts({
})
return (
<div>
<div className="flex w-full flex-col items-center justify-center space-y-5 md:flex-row md:space-x-6 md:space-y-0 xl:justify-start">
<Card className="flex flex-1 flex-col justify-between self-stretch bg-transparent pb-6 md:block">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-1">
<CardTitle className="text-base font-normal tracking-tight">
<div className="flex w-full flex-col items-center justify-center space-y-5 md:flex-row md:space-x-4 md:space-y-0 xl:justify-start">
<Card
className="flex flex-1 flex-col justify-between self-stretch bg-transparent pb-3 md:block"
data-aos="fade-up"
data-aos-delay="200"
>
<CardHeader className="flex flex-row items-center justify-between space-y-0 px-4 pb-1 pt-4">
<CardTitle className="text-sm font-normal leading-none tracking-tight">
Acceptance Rate
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{averageAcceptance}%</div>
<CardContent className="px-4 pb-2">
<div className="font-bold">{averageAcceptance}%</div>
</CardContent>

<ResponsiveContainer width="100%" height={60}>
<ResponsiveContainer width="100%" height={40}>
<LineChart
data={acceptRateData}
margin={{ top: 15, right: 30, left: 20, bottom: 5 }}
margin={{ top: 10, right: 20, left: 15, bottom: 5 }}
>
<Line
type="monotone"
Expand All @@ -183,26 +187,28 @@ export function CompletionCharts({
</ResponsiveContainer>
</Card>

<Card className="flex flex-1 flex-col justify-between self-stretch bg-transparent pb-6 md:block">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-1">
<CardTitle className="text-base font-normal tracking-tight">
<Card
className="flex flex-1 flex-col justify-between self-stretch bg-transparent pb-3 md:block"
data-aos="fade-up"
data-aos-delay="250"
>
<CardHeader className="flex flex-row items-center justify-between space-y-0 px-4 pb-1 pt-4">
<CardTitle className="text-sm font-normal leading-none tracking-tight">
Completions
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{numeral(totalViews).format('0,0')}
</div>
<CardContent className="px-4 pb-2">
<div className="font-bold">{numeral(totalViews).format('0,0')}</div>
</CardContent>

<ResponsiveContainer width="100%" height={60}>
<ResponsiveContainer width="100%" height={40}>
<BarChart
data={viewData}
margin={{
top: totalViews === 0 ? 40 : 5,
right: 20,
left: 20,
bottom: 5
right: 15,
left: 15,
bottom: 0
}}
>
<Bar
Expand All @@ -225,26 +231,30 @@ export function CompletionCharts({
</ResponsiveContainer>
</Card>

<Card className="flex flex-1 flex-col justify-between self-stretch bg-transparent pb-6 md:block">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-1">
<CardTitle className="text-base font-normal tracking-tight">
<Card
className="flex flex-1 flex-col justify-between self-stretch bg-transparent pb-3 md:block"
data-aos="fade-up"
data-aos-delay="300"
>
<CardHeader className="flex flex-row items-center justify-between space-y-0 px-4 pb-1 pt-4">
<CardTitle className="text-sm font-normal leading-none tracking-tight">
Acceptances
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
<CardContent className="px-4 pb-2">
<div className="font-bold">
{numeral(totalAccepts).format('0,0')}
</div>
</CardContent>

<ResponsiveContainer width="100%" height={60}>
<ResponsiveContainer width="100%" height={40}>
<BarChart
data={viewData}
margin={{
top: totalViews === 0 ? 40 : 5,
right: 20,
left: 20,
bottom: 5
right: 15,
left: 15,
bottom: 0
}}
>
<Bar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@

.sparkle-animation {
animation: sparkle 2s infinite;
}

.text-area-autosize::-webkit-scrollbar {
display: none;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

import {
createContext,
ForwardedRef,
forwardRef,
useContext,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState
} from 'react'
import Image from 'next/image'
import Link from 'next/link'
import logoDarkUrl from '@/assets/logo-dark.png'
import logoUrl from '@/assets/logo.png'
import tabbyUrl from '@/assets/tabby.png'
import { Message } from 'ai'
import { nanoid } from 'nanoid'
import { useTheme } from 'next-themes'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'

import { SESSION_STORAGE_KEY } from '@/lib/constants'
import { useEnableSearch } from '@/lib/experiment-flags'
import { useIsChatEnabled } from '@/lib/hooks/use-server-info'
import { useTabbyAnswer } from '@/lib/hooks/use-tabby-answer'
Expand All @@ -46,14 +42,9 @@ import { ScrollArea } from '@/components/ui/scroll-area'
import { Separator } from '@/components/ui/separator'
import { Skeleton } from '@/components/ui/skeleton'
import { ButtonScrollToBottom } from '@/components/button-scroll-to-bottom'
import { ClientOnly } from '@/components/client-only'
import { CopyButton } from '@/components/copy-button'
import { BANNER_HEIGHT, useShowDemoBanner } from '@/components/demo-banner'
import { MemoizedReactMarkdown } from '@/components/markdown'
import TextAreaSearch from '@/components/textarea-search'
import { ThemeToggle } from '@/components/theme-toggle'
import { UserAvatar } from '@/components/user-avatar'
import UserPanel from '@/components/user-panel'

import './search.css'

Expand All @@ -80,6 +71,11 @@ type SearchContextValue = {
onSubmitSearch: (question: string) => void
}

export interface SearchRef {
onSubmitSearch: (question: string) => void
stop: () => void
}

export const SearchContext = createContext<SearchContextValue>(
{} as SearchContextValue
)
Expand All @@ -101,11 +97,9 @@ const SOURCE_CARD_STYLE = {
expand: 7
}

export function Search() {
export function SearchRenderer({}, ref: ForwardedRef<SearchRef>) {
const isChatEnabled = useIsChatEnabled()
const [searchFlag] = useEnableSearch()
const [isShowDemoBanner] = useShowDemoBanner()
const { theme } = useTheme()
const [conversation, setConversation] = useState<ConversationMessage[]>([])
const [showStop, setShowStop] = useState(false)
const [container, setContainer] = useState<HTMLDivElement | null>(null)
Expand All @@ -117,15 +111,16 @@ export function Search() {
fetcher: tabbyFetcher
})

useEffect(() => {
const initialQuestion = sessionStorage.getItem(
SESSION_STORAGE_KEY.SEARCH_INITIAL_MSG
)
if (initialQuestion) {
onSubmitSearch(initialQuestion)
sessionStorage.removeItem(SESSION_STORAGE_KEY.SEARCH_INITIAL_MSG)
}
}, [])
useImperativeHandle(
ref,
() => {
return {
onSubmitSearch,
stop
}
},
[]
)

useEffect(() => {
if (title) document.title = title
Expand Down Expand Up @@ -269,10 +264,6 @@ export function Search() {
return <></>
}

const noConversation = conversation.length === 0
const style = isShowDemoBanner
? { height: `calc(100vh - ${BANNER_HEIGHT})` }
: { height: '100vh' }
return (
<SearchContext.Provider
value={{
Expand All @@ -281,26 +272,8 @@ export function Search() {
onSubmitSearch: onSubmitSearch
}}
>
<div className="flex flex-col transition-all" style={style}>
<div className="flex w-full items-center justify-between border-b px-10 py-3">
<Link href="/">
<Image
src={theme === 'dark' ? logoDarkUrl : logoUrl}
alt="logo"
width={80}
/>
</Link>

<div className="flex items-center justify-center gap-4">
<ClientOnly>
<ThemeToggle />
</ClientOnly>
<UserPanel>
<UserAvatar className="h-8 w-8 border" />
</UserPanel>
</div>
</div>
<ScrollArea className="flex-1" ref={contentContainerRef}>
<>
<ScrollArea className="h-full" ref={contentContainerRef}>
<div className="mx-auto px-10 pb-24 lg:max-w-4xl lg:px-0">
<div className="flex flex-col">
{conversation.map((item, idx) => {
Expand All @@ -318,7 +291,6 @@ export function Search() {
return (
<div key={item.id + idx} className="pb-8 pt-2">
<AnswerBlock
question="todo"
answer={item}
showRelatedQuestion={idx === conversation.length - 1}
/>
Expand All @@ -341,35 +313,19 @@ export function Search() {

<div
className={cn(
'fixed left-0 flex w-full flex-col items-center transition-all',
{
'bottom-1/2 -mt-48': noConversation,
'bottom-5 min-h-[5rem]': !noConversation
}
'fixed bottom-5 left-0 flex min-h-[5rem] w-full flex-col items-center transition-all'
)}
>
{noConversation && (
<>
<Image src={tabbyUrl} alt="logo" width={42} />
<h4 className="mb-6 scroll-m-20 text-xl font-semibold tracking-tight text-secondary-foreground">
The Private Search Assistant
</h4>
</>
)}
{!isLoading && (
<div className="relative z-20 flex justify-center self-stretch px-10">
<TextAreaSearch
className={cn({
'lg:max-w-2xl': noConversation,
'lg:max-w-4xl': !noConversation
})}
onSearch={onSubmitSearch}
placeholder={
(!noConversation && 'Ask a follow up question') || undefined
}
className="lg:max-w-4xl"
placeholder="Ask a follow up question"
/>
</div>
)}

<Button
className={cn(
'absolute top-2 z-0 flex items-center gap-x-2 px-8 py-4',
Expand All @@ -388,17 +344,17 @@ export function Search() {
<p>Stop</p>
</Button>
</div>
</div>
</>
</SearchContext.Provider>
)
}

export const Search = forwardRef<SearchRef>(SearchRenderer)

function AnswerBlock({
question,
answer,
showRelatedQuestion
}: {
question: string
answer: ConversationMessage
showRelatedQuestion: boolean
}) {
Expand All @@ -423,9 +379,9 @@ function AnswerBlock({
}

const totalHeightInRem = answer.relevant_documents
? Math.ceil(answer.relevant_documents.length / 3) *
? Math.ceil(answer.relevant_documents.length / 4) *
SOURCE_CARD_STYLE.expand +
0.5 * Math.floor(answer.relevant_documents.length / 3)
0.5 * Math.floor(answer.relevant_documents.length / 4)
: 0
return (
<div className="flex flex-col gap-y-5">
Expand All @@ -434,10 +390,10 @@ function AnswerBlock({
<div>
<div className="mb-1 flex items-center gap-x-2">
<IconBlocks className="relative" style={{ top: '-0.04rem' }} />
<p className="text-sm font-bold leading-normal">Source</p>
<p className="text-sm font-bold leading-normal">Sources</p>
</div>
<div
className="gap-sm grid grid-cols-3 gap-2 overflow-hidden"
className="gap-sm grid grid-cols-3 gap-2 overflow-hidden md:grid-cols-4"
style={{
transition: 'height 0.25s ease-out',
height: showMore
Expand Down Expand Up @@ -519,7 +475,7 @@ function AnswerBlock({
<div>
<div className="flex items-center gap-x-1.5">
<IconLayers />
<p className="text-sm font-bold leading-none">Related</p>
<p className="text-sm font-bold leading-none">Suggestions</p>
</div>
<div className="mt-3 flex flex-col gap-y-3">
{answer.relevant_questions?.map((related, index) => (
Expand Down Expand Up @@ -553,7 +509,7 @@ function SourceCard({
const { hostname } = new URL(source.link)
return (
<div
className="flex cursor-pointer flex-col justify-between gap-y-1 rounded-lg border bg-card px-4 py-2 transition-all hover:bg-card/60"
className="flex cursor-pointer flex-col justify-between gap-y-1 rounded-lg border bg-card px-3 py-2 transition-all hover:bg-card/60"
style={{
height: showMore
? `${SOURCE_CARD_STYLE.expand}rem`
Expand All @@ -569,16 +525,16 @@ function SourceCard({
{source.snippet}
</p>
)}
<div className="flex items-center">
<SiteFavicon hostname={hostname} className="mr-1" />

<div className="flex items-center gap-x-0.5 text-xs text-muted-foreground">
<p className="flex-1 overflow-hidden text-ellipsis">
<div className="flex items-center justify-between text-xs text-muted-foreground">
<div className="flex flex-1 items-center">
<SiteFavicon hostname={hostname} />
<p className="ml-1 flex-1 overflow-hidden text-ellipsis">
{hostname.replace('www.', '').split('.')[0]}
</p>
<span className="relative -top-1.5 text-xl leading-none">.</span>
<p>{index}</p>
</div>
<p className="h-4 w-4 rounded bg-primary/50 text-center text-primary-foreground">
{index}
</p>
</div>
</div>
)
Expand Down
Loading