| 1 | import { SlashCommandDescription } from "core"; |
| 2 | import { usePostHog } from "posthog-js/react"; |
| 3 | import { |
| 4 | bookmarkSlashCommand, |
| 5 | selectBookmarkedSlashCommands, |
| 6 | unbookmarkSlashCommand, |
| 7 | } from "../redux"; |
| 8 | import { useAppDispatch, useAppSelector } from "../redux/hooks"; |
| 9 | |
| 10 | export function useBookmarkedSlashCommands() { |
| 11 | const dispatch = useAppDispatch(); |
| 12 | const posthog = usePostHog(); |
| 13 | const bookmarkedCommands = useAppSelector(selectBookmarkedSlashCommands); |
| 14 | |
| 15 | const isCommandBookmarked = (commandName: string): boolean => { |
| 16 | return bookmarkedCommands.includes(commandName); |
| 17 | }; |
| 18 | |
| 19 | const toggleBookmark = (command: SlashCommandDescription) => { |
| 20 | const isBookmarked = isCommandBookmarked(command.name); |
| 21 | |
| 22 | posthog.capture("toggle_bookmarked_slash_command", { |
| 23 | isBookmarked, |
| 24 | }); |
| 25 | |
| 26 | if (isBookmarked) { |
| 27 | dispatch( |
| 28 | unbookmarkSlashCommand({ |
| 29 | commandName: command.name, |
| 30 | }), |
| 31 | ); |
| 32 | } else { |
| 33 | dispatch( |
| 34 | bookmarkSlashCommand({ |
| 35 | commandName: command.name, |
| 36 | }), |
| 37 | ); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | return { |
| 42 | isCommandBookmarked, |
| 43 | toggleBookmark, |
| 44 | }; |
| 45 | } |