mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-13 04:45:19 +00:00
refactor demo cards to their own component
This commit is contained in:
parent
bfb9352d24
commit
648df845f0
3 changed files with 233 additions and 103 deletions
|
|
@ -6,15 +6,12 @@ import { ChatBoxToolbar } from "@/features/chat/components/chatBox/chatBoxToolba
|
|||
import { LanguageModelInfo } from "@/features/chat/types";
|
||||
import { useCreateNewChatThread } from "@/features/chat/useCreateNewChatThread";
|
||||
import { RepositoryQuery, SearchContextQuery } from "@/lib/types";
|
||||
import { Layers, Search } from "lucide-react";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { useState } from "react";
|
||||
import { SearchModeSelector, SearchModeSelectorProps } from "./toolbar";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { CardContent } from "@/components/ui/card";
|
||||
import { useLocalStorage } from "usehooks-ts";
|
||||
import { ContextItem } from "@/features/chat/components/chatBox/contextSelector";
|
||||
import { DemoExamples, DemoSearchExample, DemoSearchContextExample } from "@/types";
|
||||
import { DemoExamples } from "@/types";
|
||||
import { AskSourcebotDemoCards } from "./askSourcebotDemoCards";
|
||||
|
||||
interface AgenticSearchProps {
|
||||
searchModeSelectorProps: SearchModeSelectorProps;
|
||||
|
|
@ -40,14 +37,6 @@ export const AgenticSearch = ({
|
|||
const [selectedItems, setSelectedItems] = useLocalStorage<ContextItem[]>("selectedContextItems", [], { initializeWithValue: false });
|
||||
const [isContextSelectorOpen, setIsContextSelectorOpen] = useState(false);
|
||||
|
||||
const handleExampleClick = (example: DemoSearchExample) => {
|
||||
console.log(example);
|
||||
}
|
||||
|
||||
const handleContextClick = (context: DemoSearchContextExample) => {
|
||||
console.log(context);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center w-full">
|
||||
<div className="mt-4 w-full border rounded-md shadow-sm max-w-[800px]">
|
||||
|
|
@ -83,88 +72,13 @@ export const AgenticSearch = ({
|
|||
</div>
|
||||
|
||||
{demoExamples && (
|
||||
<div className="w-full mt-8 grid grid-cols-1 lg:grid-cols-2 gap-8 px-4 max-w-[1200px]">
|
||||
{/* Example Searches Column */}
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center gap-2 mb-6">
|
||||
<Search className="h-5 w-5 text-muted-foreground" />
|
||||
<h3 className="text-lg font-semibold">Example Searches</h3>
|
||||
</div>
|
||||
<div className="space-y-3">
|
||||
{demoExamples.searchExamples.map((example) => (
|
||||
<Card
|
||||
key={example.id}
|
||||
className="cursor-pointer transition-all duration-200 hover:shadow-md hover:border-primary/50 group"
|
||||
onClick={() => handleExampleClick(example)}
|
||||
>
|
||||
<CardContent className="p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="flex-shrink-0 p-2 rounded-lg bg-primary/10 text-primary group-hover:bg-primary group-hover:text-primary-foreground transition-colors">
|
||||
{example.icon}
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<h4 className="font-medium text-sm mb-1 group-hover:text-primary transition-colors">
|
||||
{example.title}
|
||||
</h4>
|
||||
<p className="text-xs text-muted-foreground mb-2">{example.description}</p>
|
||||
<Badge className="text-xs">
|
||||
{example.category}
|
||||
</Badge>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Search Contexts Column */}
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center gap-2 mb-6">
|
||||
<Layers className="h-5 w-5 text-muted-foreground" />
|
||||
<h3 className="text-lg font-semibold">Search Contexts</h3>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-1 xl:grid-cols-2 gap-3">
|
||||
{demoExamples.searchContextExamples.map((context) => {
|
||||
const searchContext = searchContexts.find((item) => item.name === context.name);
|
||||
if (!searchContext) return null;
|
||||
const isSelected = false; //selectedItems.some((item) => item.id === context.id)
|
||||
|
||||
const numRepos = searchContext.repoNames.length;
|
||||
return (
|
||||
<Card
|
||||
key={context.id}
|
||||
className={`cursor-pointer transition-all duration-200 hover:shadow-md group ${isSelected ? "border-primary bg-primary/5 shadow-sm" : "hover:border-primary/50"
|
||||
}`}
|
||||
onClick={() => handleContextClick(context)}
|
||||
>
|
||||
<CardContent className="p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<div
|
||||
className={`flex-shrink-0 p-2 rounded-lg ${context.color} transition-transform group-hover:scale-105`}
|
||||
>
|
||||
{context.icon}
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<h4
|
||||
className={`font-medium text-sm mb-1 transition-colors ${isSelected ? "text-primary" : "group-hover:text-primary"
|
||||
}`}
|
||||
>
|
||||
{context.displayName}
|
||||
</h4>
|
||||
<p className="text-xs text-muted-foreground mb-2">{context.description}</p>
|
||||
<Badge className="text-xs">
|
||||
{numRepos} repos
|
||||
</Badge>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<AskSourcebotDemoCards
|
||||
demoExamples={demoExamples}
|
||||
selectedItems={selectedItems}
|
||||
setSelectedItems={setSelectedItems}
|
||||
searchContexts={searchContexts}
|
||||
repos={repos}
|
||||
/>
|
||||
)}
|
||||
</div >
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,211 @@
|
|||
'use client';
|
||||
|
||||
import Image from "next/image";
|
||||
import { Search, LibraryBigIcon, Code, Layers } from "lucide-react";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { CardContent } from "@/components/ui/card";
|
||||
import { ContextItem, RepoContextItem, SearchContextItem } from "@/features/chat/components/chatBox/contextSelector";
|
||||
import { DemoExamples, DemoSearchExample, DemoSearchContextExample, DemoSearchContext } from "@/types";
|
||||
import { cn, getCodeHostIcon } from "@/lib/utils";
|
||||
import { RepositoryQuery, SearchContextQuery } from "@/lib/types";
|
||||
|
||||
interface AskSourcebotDemoCardsProps {
|
||||
demoExamples: DemoExamples;
|
||||
selectedItems: ContextItem[];
|
||||
setSelectedItems: (items: ContextItem[]) => void;
|
||||
searchContexts: SearchContextQuery[];
|
||||
repos: RepositoryQuery[];
|
||||
}
|
||||
|
||||
export const AskSourcebotDemoCards = ({
|
||||
demoExamples,
|
||||
selectedItems,
|
||||
setSelectedItems,
|
||||
searchContexts,
|
||||
repos,
|
||||
}: AskSourcebotDemoCardsProps) => {
|
||||
const handleExampleClick = (example: DemoSearchExample) => {
|
||||
if (example.url) {
|
||||
window.open(example.url, '_blank');
|
||||
}
|
||||
}
|
||||
|
||||
const getContextIcon = (context: DemoSearchContext, size: number = 20) => {
|
||||
const sizeClass = size === 12 ? "h-3 w-3" : "h-5 w-5";
|
||||
|
||||
if (context.type === "set") {
|
||||
return <LibraryBigIcon className={cn(sizeClass, "text-muted-foreground")} />;
|
||||
}
|
||||
|
||||
if (context.codeHostType) {
|
||||
const codeHostIcon = getCodeHostIcon(context.codeHostType);
|
||||
if (codeHostIcon) {
|
||||
return (
|
||||
<Image
|
||||
src={codeHostIcon.src}
|
||||
alt={`${context.codeHostType} icon`}
|
||||
width={size}
|
||||
height={size}
|
||||
className={cn(sizeClass, codeHostIcon.className)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return <Code className={cn(sizeClass, "text-muted-foreground")} />;
|
||||
}
|
||||
|
||||
const handleContextClick = (demoSearchContexts: DemoSearchContext[], contextExample: DemoSearchContextExample) => {
|
||||
const context = demoSearchContexts.find((context) => context.id === contextExample.searchContext)
|
||||
if (!context) {
|
||||
console.error(`Search context ${contextExample.searchContext} not found on handleContextClick`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.type === "set") {
|
||||
const searchContext = searchContexts.find((item) => item.name === context.value);
|
||||
if (!searchContext) {
|
||||
console.error(`Search context ${context.value} not found on handleContextClick`);
|
||||
return;
|
||||
}
|
||||
|
||||
const isSelected = selectedItems.some(
|
||||
(selected) => selected.type === 'context' && selected.value === context.value
|
||||
);
|
||||
const newSelectedItems = isSelected
|
||||
? selectedItems.filter(
|
||||
(selected) => !(selected.type === 'context' && selected.value === context.value)
|
||||
)
|
||||
: [...selectedItems, { type: 'context', value: context.value, name: context.displayName, repoCount: searchContext.repoNames.length } as SearchContextItem];
|
||||
|
||||
setSelectedItems(newSelectedItems);
|
||||
} else {
|
||||
const repo = repos.find((repo) => repo.repoName === context.value);
|
||||
if (!repo) {
|
||||
console.error(`Repo ${context.value} not found on handleContextClick`);
|
||||
return;
|
||||
}
|
||||
|
||||
const isSelected = selectedItems.some(
|
||||
(selected) => selected.type === 'repo' && selected.value === context.value
|
||||
);
|
||||
const newSelectedItems = isSelected
|
||||
? selectedItems.filter(
|
||||
(selected) => !(selected.type === 'repo' && selected.value === context.value)
|
||||
)
|
||||
: [...selectedItems, { type: 'repo', value: context.value, name: context.displayName, codeHostType: repo.codeHostType } as RepoContextItem];
|
||||
|
||||
setSelectedItems(newSelectedItems);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full mt-8 space-y-12 px-4 max-w-[1200px]">
|
||||
{/* Search Context Row */}
|
||||
<div className="space-y-4">
|
||||
<div className="text-center mb-8">
|
||||
<div className="flex items-center justify-center gap-2 mb-2">
|
||||
<Layers className="h-5 w-5 text-muted-foreground" />
|
||||
<h3 className="text-lg font-semibold">Search Context</h3>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">Select the context you want to ask questions about</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap justify-center gap-3">
|
||||
{demoExamples.searchContextExamples.map((contextExample) => {
|
||||
const context = demoExamples.searchContexts.find((context) => context.id === contextExample.searchContext)
|
||||
if (!context) {
|
||||
console.error(`Search context ${contextExample.searchContext} not found on handleContextClick`);
|
||||
return;
|
||||
}
|
||||
|
||||
const isSelected = selectedItems.some(
|
||||
(selected) => (selected.type === 'context' && selected.value === context.value) ||
|
||||
(selected.type === 'repo' && selected.value === context.value)
|
||||
);
|
||||
|
||||
const searchContext = searchContexts.find((item) => item.name === context.value);
|
||||
const numRepos = searchContext ? searchContext.repoNames.length : undefined;
|
||||
return (
|
||||
<Card
|
||||
key={context.value}
|
||||
className={`cursor-pointer transition-all duration-200 hover:shadow-md hover:scale-105 group w-full max-w-[280px] ${isSelected ? "border-primary bg-primary/5 shadow-sm" : "hover:border-primary/50"
|
||||
}`}
|
||||
onClick={() => handleContextClick(demoExamples.searchContexts, contextExample)}
|
||||
>
|
||||
<CardContent className="p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<div
|
||||
className={`flex-shrink-0 p-2 rounded-lg transition-transform group-hover:scale-105`}
|
||||
>
|
||||
{getContextIcon(context)}
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<h4
|
||||
className={`font-medium text-sm transition-colors ${isSelected ? "text-primary" : "group-hover:text-primary"
|
||||
}`}
|
||||
>
|
||||
{context.displayName}
|
||||
</h4>
|
||||
{numRepos && (
|
||||
<Badge className="text-[10px] px-1.5 py-0.5 h-4">
|
||||
{numRepos} repos
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">{contextExample.description}</p>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Example Searches Row */}
|
||||
<div className="space-y-4">
|
||||
<div className="text-center mb-8">
|
||||
<div className="flex items-center justify-center gap-2 mb-2">
|
||||
<Search className="h-5 w-5 text-muted-foreground" />
|
||||
<h3 className="text-lg font-semibold">Community Ask Results</h3>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">Check out these featured ask results from the community</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap justify-center gap-3">
|
||||
{demoExamples.searchExamples.map((example) => {
|
||||
const searchContexts = demoExamples.searchContexts.filter((context) => example.searchContext.includes(context.id))
|
||||
return (
|
||||
<Card
|
||||
key={example.url}
|
||||
className="cursor-pointer transition-all duration-200 hover:shadow-md hover:scale-105 hover:border-primary/50 group w-full max-w-[350px]"
|
||||
onClick={() => handleExampleClick(example)}
|
||||
>
|
||||
<CardContent className="p-4">
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-center justify-between">
|
||||
{searchContexts.map((context) => (
|
||||
<Badge key={context.value} variant="secondary" className="text-[10px] px-1.5 py-0.5 h-4 flex items-center gap-1">
|
||||
{getContextIcon(context, 12)}
|
||||
{context.displayName}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<h4 className="font-semibold text-sm group-hover:text-primary transition-colors line-clamp-2">
|
||||
{example.title}
|
||||
</h4>
|
||||
<p className="text-xs text-muted-foreground line-clamp-3 leading-relaxed">
|
||||
{example.description}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
|
@ -4,29 +4,34 @@ export const orgMetadataSchema = z.object({
|
|||
anonymousAccessEnabled: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export const demoSearchContextSchema = z.object({
|
||||
id: z.number(),
|
||||
displayName: z.string(),
|
||||
value: z.string(),
|
||||
type: z.enum(["repo", "set"]),
|
||||
codeHostType: z.string().optional(),
|
||||
})
|
||||
|
||||
export const demoSearchExampleSchema = z.object({
|
||||
id: z.string(),
|
||||
title: z.string(),
|
||||
description: z.string(),
|
||||
icon: z.string(),
|
||||
category: z.string(),
|
||||
url: z.string(),
|
||||
searchContext: z.array(z.number())
|
||||
})
|
||||
|
||||
export const demoSearchContextExampleSchema = z.object({
|
||||
id: z.string(),
|
||||
displayName: z.string(),
|
||||
name: z.string(),
|
||||
searchContext: z.number(),
|
||||
description: z.string(),
|
||||
icon: z.string(),
|
||||
color: z.string(),
|
||||
})
|
||||
|
||||
export const demoExamplesSchema = z.object({
|
||||
searchContexts: demoSearchContextSchema.array(),
|
||||
searchExamples: demoSearchExampleSchema.array(),
|
||||
searchContextExamples: demoSearchContextExampleSchema.array(),
|
||||
})
|
||||
|
||||
export type OrgMetadata = z.infer<typeof orgMetadataSchema>;
|
||||
export type DemoExamples = z.infer<typeof demoExamplesSchema>;
|
||||
export type DemoSearchContext = z.infer<typeof demoSearchContextSchema>;
|
||||
export type DemoSearchExample = z.infer<typeof demoSearchExampleSchema>;
|
||||
export type DemoSearchContextExample = z.infer<typeof demoSearchContextExampleSchema>;
|
||||
Loading…
Reference in a new issue