feat: add tests
This commit is contained in:
parent
b82d29c2df
commit
0819becfd9
4 changed files with 136 additions and 45 deletions
|
|
@ -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<string, boolean> = {};
|
||||
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 (
|
||||
<div className="flex w-full max-w-full flex-col lg:grid lg:grid-cols-3 lg:gap-4">
|
||||
{filteredSlashCommands
|
||||
{cmdsSortedByBookmark
|
||||
.slice(0, NUM_CARDS_TO_RENDER_COLLAPSED)
|
||||
.map((command, i) => (
|
||||
<ConversationStarterCard
|
||||
key={command.name + i}
|
||||
command={command}
|
||||
onClick={onClick}
|
||||
onBookmark={handleBookmarkCommand}
|
||||
onBookmark={() => toggleBookmark(command)}
|
||||
isBookmarked={bookmarkStatuses[command.name]}
|
||||
/>
|
||||
))}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
});
|
||||
});
|
||||
|
|
@ -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<string, boolean> = {};
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue