diff --git a/gui/src/components/ConversationStarters/ConversationStarterCards.tsx b/gui/src/components/ConversationStarters/ConversationStarterCards.tsx index 4176271ba..f69d39974 100644 --- a/gui/src/components/ConversationStarters/ConversationStarterCards.tsx +++ b/gui/src/components/ConversationStarters/ConversationStarterCards.tsx @@ -1,37 +1,16 @@ import { SlashCommandDescription } from "core"; -import { useAppDispatch, useAppSelector } from "../../redux/hooks"; -import { - bookmarkSlashCommand, - selectBookmarkedSlashCommands, - selectSelectedProfileId, - unbookmarkSlashCommand, -} from "../../redux/slices/profiles/slice"; +import { useAppDispatch } from "../../redux/hooks"; import { setMainEditorContentTrigger } from "../../redux/slices/sessionSlice"; import { getParagraphNodeFromString } from "../mainInput/utils"; import { ConversationStarterCard } from "./ConversationStarterCard"; -import { isDeprecatedCommandName } from "./utils"; +import { useBookmarkedSlashCommands } from "./useBookmarkedSlashCommands"; const NUM_CARDS_TO_RENDER_COLLAPSED = 3; export function ConversationStarterCards() { const dispatch = useAppDispatch(); - - const slashCommands = - useAppSelector((state) => state.config.config.slashCommands) ?? []; - - const selectedProfileId = useAppSelector(selectSelectedProfileId); - const bookmarkedCommands = useAppSelector(selectBookmarkedSlashCommands); - - const filteredSlashCommands = slashCommands.filter(isDeprecatedCommandName); - - const bookmarkStatuses: Record = {}; - if (selectedProfileId) { - filteredSlashCommands.forEach((command) => { - bookmarkStatuses[command.name] = bookmarkedCommands.includes( - command.name, - ); - }); - } + const { cmdsSortedByBookmark, bookmarkStatuses, toggleBookmark } = + useBookmarkedSlashCommands(); function onClick(command: SlashCommandDescription) { if (command.prompt) { @@ -41,37 +20,20 @@ export function ConversationStarterCards() { } } - function handleBookmarkCommand(command: SlashCommandDescription) { - const isBookmarked = bookmarkStatuses[command.name]; - - if (isBookmarked) { - dispatch( - unbookmarkSlashCommand({ - commandName: command.name, - }), - ); - } else { - dispatch( - bookmarkSlashCommand({ - commandName: command.name, - }), - ); - } - } - if (!filteredSlashCommands || filteredSlashCommands.length === 0) { + if (!cmdsSortedByBookmark || cmdsSortedByBookmark.length === 0) { return null; } return (
- {filteredSlashCommands + {cmdsSortedByBookmark .slice(0, NUM_CARDS_TO_RENDER_COLLAPSED) .map((command, i) => ( toggleBookmark(command)} isBookmarked={bookmarkStatuses[command.name]} /> ))} diff --git a/gui/src/components/ConversationStarters/__tests__/utils.test.ts b/gui/src/components/ConversationStarters/__tests__/utils.test.ts new file mode 100644 index 000000000..3e7050985 --- /dev/null +++ b/gui/src/components/ConversationStarters/__tests__/utils.test.ts @@ -0,0 +1,45 @@ +import { SlashCommandDescription } from "core"; +import { sortCommandsByBookmarkStatus } from "../utils"; + +describe("sortCommandsByBookmarkStatus", () => { + const mockCommands: SlashCommandDescription[] = [ + { + name: "command1", + description: "First command", + prompt: "This is command 1", + }, + { + name: "command2", + description: "Second command", + prompt: "This is command 2", + }, + { + name: "command3", + description: "Third command", + prompt: "This is command 3", + }, + { + name: "command4", + description: "Fourth command", + prompt: "This is command 4", + }, + ]; + + it("should return commands in the same order when no bookmarks exist", () => { + const result = sortCommandsByBookmarkStatus(mockCommands, []); + expect(result).toEqual(mockCommands); + // Ensure it's a new array, not the original + expect(result).not.toBe(mockCommands); + }); + + it("should put bookmarked commands first", () => { + const bookmarkedCommands = ["command3", "command1"]; + const result = sortCommandsByBookmarkStatus( + mockCommands, + bookmarkedCommands, + ); + + expect(result[0].name).toBe("command1"); + expect(result[1].name).toBe("command3"); + }); +}); diff --git a/gui/src/components/ConversationStarters/useBookmarkedSlashCommands.ts b/gui/src/components/ConversationStarters/useBookmarkedSlashCommands.ts new file mode 100644 index 000000000..732c2beb1 --- /dev/null +++ b/gui/src/components/ConversationStarters/useBookmarkedSlashCommands.ts @@ -0,0 +1,64 @@ +import { SlashCommandDescription } from "core"; +import { useMemo } from "react"; +import { + bookmarkSlashCommand, + selectBookmarkedSlashCommands, + selectSelectedProfileId, + unbookmarkSlashCommand, +} from "../../redux"; +import { useAppDispatch, useAppSelector } from "../../redux/hooks"; +import { isDeprecatedCommandName, sortCommandsByBookmarkStatus } from "./utils"; + +export function useBookmarkedSlashCommands() { + const dispatch = useAppDispatch(); + + const slashCommands = + useAppSelector((state) => state.config.config.slashCommands) ?? []; + const selectedProfileId = useAppSelector(selectSelectedProfileId); + const bookmarkedCommands = useAppSelector(selectBookmarkedSlashCommands); + + const filteredSlashCommands = slashCommands.filter(isDeprecatedCommandName); + + // Create a map of command names to bookmark status + const bookmarkStatuses = useMemo(() => { + const statuses: Record = {}; + if (selectedProfileId) { + filteredSlashCommands.forEach((command) => { + statuses[command.name] = bookmarkedCommands.includes(command.name); + }); + } + return statuses; + }, [filteredSlashCommands, bookmarkedCommands, selectedProfileId]); + + // Sort commands by bookmark status + const cmdsSortedByBookmark = useMemo( + () => + sortCommandsByBookmarkStatus(filteredSlashCommands, bookmarkedCommands), + [filteredSlashCommands, bookmarkedCommands], + ); + + // Function to toggle bookmark status + const toggleBookmark = (command: SlashCommandDescription) => { + const isBookmarked = bookmarkStatuses[command.name]; + + if (isBookmarked) { + dispatch( + unbookmarkSlashCommand({ + commandName: command.name, + }), + ); + } else { + dispatch( + bookmarkSlashCommand({ + commandName: command.name, + }), + ); + } + }; + + return { + cmdsSortedByBookmark, + bookmarkStatuses, + toggleBookmark, + }; +} diff --git a/gui/src/components/ConversationStarters/utils.ts b/gui/src/components/ConversationStarters/utils.ts index aab6cb414..d37b45416 100644 --- a/gui/src/components/ConversationStarters/utils.ts +++ b/gui/src/components/ConversationStarters/utils.ts @@ -27,3 +27,23 @@ export function isDeprecatedCommandName(command: SlashCommandDescription) { (defaultCommand) => defaultCommand.name === command.name, ); } + +/** + * Sorts commands with bookmarked ones first + * @param commands The list of commands to sort + * @param bookmarkedCommands An array of bookmarked command names + * @returns A new sorted array with bookmarked commands first + */ +export function sortCommandsByBookmarkStatus( + commands: SlashCommandDescription[], + bookmarkedCommands: string[], +): SlashCommandDescription[] { + return [...commands].sort((a, b) => { + const aIsBookmarked = bookmarkedCommands.includes(a.name); + const bIsBookmarked = bookmarkedCommands.includes(b.name); + + if (aIsBookmarked && !bIsBookmarked) return -1; + if (!aIsBookmarked && bIsBookmarked) return 1; + return 0; + }); +}