Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | 'use client'; |
| 2 | |
| 3 | import { useMemo, useState } from 'react'; |
| 4 | import Link from 'next/link'; |
| 5 | import { ArrowUpRight, Search } from 'lucide-react'; |
| 6 | import type { KnowledgeLink } from '@/lib/knowledge/catalog'; |
| 7 | import type { KnowledgeSourceType } from '@/lib/knowledge/types'; |
| 8 | import { getKnowledgeDisplayDescription } from '@/lib/knowledge/display'; |
| 9 | |
| 10 | const GROUPS: Array<{ title: string; sourceTypes: KnowledgeSourceType[] }> = [ |
| 11 | { title: 'Docs', sourceTypes: ['docs'] }, |
| 12 | { title: 'Knowledge Base answers', sourceTypes: ['kb'] }, |
| 13 | { title: 'OAuth guides', sourceTypes: ['oauth-guide'] }, |
| 14 | { title: 'Toolkits', sourceTypes: ['toolkit'] }, |
| 15 | { title: 'Examples', sourceTypes: ['example'] }, |
| 16 | { title: 'Reference', sourceTypes: ['reference', 'legacy'] }, |
| 17 | { title: 'Changelog', sourceTypes: ['changelog'] }, |
| 18 | ]; |
| 19 | |
| 20 | const INITIAL_GROUP_LIMIT = 12; |
| 21 | |
| 22 | const TOOLKIT_SOURCE_LABELS: Partial<Record<KnowledgeSourceType, string>> = { |
| 23 | kb: 'Support answer', |
| 24 | 'oauth-guide': 'OAuth guide', |
| 25 | toolkit: 'Toolkit', |
| 26 | }; |
| 27 | |
| 28 | function KnowledgeResultCard({ |
| 29 | link, |
| 30 | showSourceLabel = false, |
| 31 | displayTitle, |
| 32 | }: { |
| 33 | link: KnowledgeLink; |
| 34 | showSourceLabel?: boolean; |
| 35 | displayTitle?: string; |
| 36 | }) { |
| 37 | const external = /^https?:\/\//.test(link.href); |
| 38 | const content = <> |
| 39 | {showSourceLabel && ( |
| 40 | <p className="mb-3 text-xs font-medium text-fd-muted-foreground"> |
| 41 | {TOOLKIT_SOURCE_LABELS[link.sourceType] ?? link.sourceLabel} |
| 42 | </p> |
| 43 | )} |
| 44 | <div className="flex items-start justify-between gap-4"> |
| 45 | <h3 className="font-semibold group-hover:text-fd-primary">{displayTitle ?? link.title}</h3> |
| 46 | <span className="mt-1 shrink-0"> |
| 47 | <ArrowUpRight className="size-4 text-fd-muted-foreground" aria-hidden="true" /> |
| 48 | {external && <span className="sr-only">opens in a new tab</span>} |
| 49 | </span> |
| 50 | </div> |
| 51 | <p className="mt-1.5 max-w-3xl text-sm leading-6 text-fd-muted-foreground"> |
| 52 | {getKnowledgeDisplayDescription(link.description)} |
| 53 | </p> |
| 54 | </>; |
| 55 | const className = 'group block h-full border border-fd-border bg-fd-background p-5 transition-colors hover:border-fd-primary/40 hover:bg-fd-accent/30 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring'; |
| 56 | |
| 57 | return external ? ( |
| 58 | <a href={link.href} target="_blank" rel="noopener noreferrer" className={className}> |
| 59 | {content} |
| 60 | </a> |
| 61 | ) : ( |
| 62 | <Link href={link.href} className={className}> |
| 63 | {content} |
| 64 | </Link> |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | export function filterKnowledgeLinks(links: KnowledgeLink[], query: string): KnowledgeLink[] { |
| 69 | const terms = query.toLowerCase().trim().split(/\s+/).filter(Boolean); |
| 70 | if (terms.length === 0) return links; |
| 71 | return links.filter((link) => { |
| 72 | const searchable = [ |
| 73 | link.title, |
| 74 | link.description, |
| 75 | link.sourceLabel, |
| 76 | ...link.productAreas, |
| 77 | ...link.toolkitSlugs, |
| 78 | ].join(' ').toLowerCase(); |
| 79 | return terms.every((term) => searchable.includes(term)); |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | interface BrowseResultsProps { |
| 84 | links: KnowledgeLink[]; |
| 85 | variant?: 'default' | 'topic' | 'toolkit'; |
| 86 | toolkitName?: string; |
| 87 | } |
| 88 | |
| 89 | export function BrowseResults({ links, variant = 'default', toolkitName }: BrowseResultsProps) { |
| 90 | const [query, setQuery] = useState(''); |
| 91 | const [expandedGroups, setExpandedGroups] = useState<string[]>([]); |
| 92 | const isTopic = variant === 'topic'; |
| 93 | const isToolkit = variant === 'toolkit'; |
| 94 | const filteredLinks = useMemo( |
| 95 | () => filterKnowledgeLinks(links, query), |
| 96 | [links, query], |
| 97 | ); |
| 98 | const normalizedQuery = query.trim(); |
| 99 | |
| 100 | if (links.length === 0) { |
| 101 | return ( |
| 102 | <div className="border border-fd-border p-6 text-sm text-fd-muted-foreground"> |
| 103 | No resources are mapped here yet. Try the unified search or another product area. |
| 104 | </div> |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | if (isToolkit) { |
| 109 | return ( |
| 110 | <ul |
| 111 | aria-label="Toolkit knowledge sources" |
| 112 | className="grid gap-3 md:grid-cols-2 lg:grid-cols-3" |
| 113 | > |
| 114 | {links.map((link) => { |
| 115 | const displayTitle = toolkitName && link.sourceType === 'kb' |
| 116 | ? `${toolkitName} support & troubleshooting` |
| 117 | : toolkitName && link.sourceType === 'toolkit' |
| 118 | ? `${toolkitName} tools reference` |
| 119 | : undefined; |
| 120 | |
| 121 | return ( |
| 122 | <li key={link.href}> |
| 123 | <KnowledgeResultCard link={link} showSourceLabel displayTitle={displayTitle} /> |
| 124 | </li> |
| 125 | ); |
| 126 | })} |
| 127 | </ul> |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | return ( |
| 132 | <div> |
| 133 | {!isTopic && links.length > INITIAL_GROUP_LIMIT && ( |
| 134 | <div className="mb-8 border border-fd-border bg-fd-muted/20 p-4 sm:flex sm:items-end sm:justify-between sm:gap-6"> |
| 135 | <div className="w-full max-w-xl"> |
| 136 | <label htmlFor="knowledge-browse-search" className="mb-2 block text-sm font-medium"> |
| 137 | Filter answers on this page |
| 138 | </label> |
| 139 | <div className="relative"> |
| 140 | <Search className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-fd-muted-foreground" aria-hidden="true" /> |
| 141 | <input |
| 142 | id="knowledge-browse-search" |
| 143 | name="knowledge-browse-search" |
| 144 | type="search" |
| 145 | value={query} |
| 146 | onChange={(event) => setQuery(event.target.value)} |
| 147 | placeholder="Search titles, topics, or toolkits" |
| 148 | autoComplete="off" |
| 149 | className="h-11 w-full border border-fd-border bg-fd-background pl-10 pr-4 text-sm outline-none placeholder:text-fd-muted-foreground focus-visible:border-fd-primary focus-visible:ring-2 focus-visible:ring-fd-primary/20" |
| 150 | /> |
| 151 | </div> |
| 152 | </div> |
| 153 | <p className="mt-3 shrink-0 text-sm text-fd-muted-foreground sm:mb-3" aria-live="polite"> |
| 154 | {filteredLinks.length} of {links.length} answers |
| 155 | </p> |
| 156 | </div> |
| 157 | )} |
| 158 | |
| 159 | {filteredLinks.length === 0 ? ( |
| 160 | <div className="border border-fd-border bg-fd-muted/20 p-8 text-center text-sm text-fd-muted-foreground"> |
| 161 | No answers match “{normalizedQuery}”. |
| 162 | </div> |
| 163 | ) : ( |
| 164 | <div className="space-y-12"> |
| 165 | {GROUPS.map((group) => { |
| 166 | const matches = filteredLinks.filter((link) => group.sourceTypes.includes(link.sourceType)); |
| 167 | if (matches.length === 0) return null; |
| 168 | const groupKey = group.title.toLowerCase().replace(/[^a-z]+/g, '-'); |
| 169 | const expanded = normalizedQuery.length > 0 || expandedGroups.includes(groupKey); |
| 170 | const visibleMatches = expanded ? matches : matches.slice(0, INITIAL_GROUP_LIMIT); |
| 171 | const remaining = matches.length - visibleMatches.length; |
| 172 | const title = isTopic && group.sourceTypes.includes('kb') |
| 173 | ? 'Support answers' |
| 174 | : group.title; |
| 175 | |
| 176 | return ( |
| 177 | <section key={group.title} aria-labelledby={`group-${groupKey}`}> |
| 178 | <div className="flex items-baseline justify-between gap-4 border-b border-fd-border pb-3"> |
| 179 | <h2 id={`group-${groupKey}`} className="text-xl font-semibold"> |
| 180 | {title} |
| 181 | </h2> |
| 182 | <span className="text-sm text-fd-muted-foreground">{matches.length}</span> |
| 183 | </div> |
| 184 | <ul className="mt-3 grid gap-3 md:grid-cols-2"> |
| 185 | {visibleMatches.map((link) => ( |
| 186 | <li key={link.href}> |
| 187 | <KnowledgeResultCard link={link} /> |
| 188 | </li> |
| 189 | ))} |
| 190 | </ul> |
| 191 | {remaining > 0 && ( |
| 192 | <button |
| 193 | type="button" |
| 194 | onClick={() => setExpandedGroups((current) => [...current, groupKey])} |
| 195 | className="mt-4 border border-fd-border px-4 py-2 text-sm font-medium transition-colors hover:bg-fd-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring" |
| 196 | > |
| 197 | Show {remaining} more |
| 198 | </button> |
| 199 | )} |
| 200 | </section> |
| 201 | ); |
| 202 | })} |
| 203 | </div> |
| 204 | )} |
| 205 | </div> |
| 206 | ); |
| 207 | } |