| 1 | import { SlashCommandDescWithSource } from "core"; |
| 2 | import { useState } from "react"; |
| 3 | import { useBookmarkedSlashCommands } from "../../hooks/useBookmarkedSlashCommands"; |
| 4 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 5 | import { useMainEditor } from "../mainInput/TipTapEditor"; |
| 6 | import { ConversationStarterCard } from "./ConversationStarterCard"; |
| 7 | |
| 8 | const NUM_CARDS_TO_RENDER = 5; |
| 9 | |
| 10 | /** |
| 11 | * Displays a grid of conversation starter cards for bookmarked slash commands |
| 12 | */ |
| 13 | export function ConversationStarterCards() { |
| 14 | const dispatch = useAppDispatch(); |
| 15 | const { mainEditor } = useMainEditor(); |
| 16 | const [showAll, setShowAll] = useState(false); |
| 17 | const { isCommandBookmarked } = useBookmarkedSlashCommands(); |
| 18 | const slashCommands = useAppSelector( |
| 19 | (state) => state.config.config.slashCommands ?? [], |
| 20 | ); |
| 21 | |
| 22 | const bookmarkedSlashCommands = slashCommands.filter((command) => |
| 23 | isCommandBookmarked(command.name), |
| 24 | ); |
| 25 | |
| 26 | function onClick(command: SlashCommandDescWithSource) { |
| 27 | mainEditor?.commands.insertPrompt({ |
| 28 | description: command.description, |
| 29 | title: command.name, |
| 30 | content: command.prompt, |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | if (bookmarkedSlashCommands.length === 0) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | const visibleCommands = showAll |
| 39 | ? bookmarkedSlashCommands |
| 40 | : bookmarkedSlashCommands.slice(0, NUM_CARDS_TO_RENDER); |
| 41 | |
| 42 | const remainingCount = bookmarkedSlashCommands.length - NUM_CARDS_TO_RENDER; |
| 43 | |
| 44 | return ( |
| 45 | <div className="flex w-full max-w-full flex-col"> |
| 46 | <div className="lg:grid lg:grid-cols-3 lg:gap-4"> |
| 47 | {visibleCommands.map((command, i) => ( |
| 48 | <ConversationStarterCard |
| 49 | key={command.name + i} |
| 50 | command={command} |
| 51 | onClick={onClick} |
| 52 | /> |
| 53 | ))} |
| 54 | </div> |
| 55 | {bookmarkedSlashCommands.length > NUM_CARDS_TO_RENDER && ( |
| 56 | <span |
| 57 | className="text-lightgray mt-1 cursor-pointer text-xs hover:underline" |
| 58 | onClick={() => setShowAll(!showAll)} |
| 59 | > |
| 60 | {showAll ? "Show less" : `Show ${remainingCount} more...`} |
| 61 | </span> |
| 62 | )} |
| 63 | </div> |
| 64 | ); |
| 65 | } |