From d6ad3e2071ca1ee3f0b5cd4d8e93852a84fd138a Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Fri, 21 Mar 2025 15:42:05 -0700 Subject: [PATCH] feat: init profiles prefs --- .../ConversationStarterCards.tsx | 22 ++++++++-------- gui/src/redux/slices/profiles/slice.ts | 25 +++++++++++-------- gui/src/redux/slices/profiles/utils.ts | 12 +++++++++ 3 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 gui/src/redux/slices/profiles/utils.ts diff --git a/gui/src/components/ConversationStarters/ConversationStarterCards.tsx b/gui/src/components/ConversationStarters/ConversationStarterCards.tsx index f69d39974..921d47d6e 100644 --- a/gui/src/components/ConversationStarters/ConversationStarterCards.tsx +++ b/gui/src/components/ConversationStarters/ConversationStarterCards.tsx @@ -5,7 +5,7 @@ import { getParagraphNodeFromString } from "../mainInput/utils"; import { ConversationStarterCard } from "./ConversationStarterCard"; import { useBookmarkedSlashCommands } from "./useBookmarkedSlashCommands"; -const NUM_CARDS_TO_RENDER_COLLAPSED = 3; +const NUM_CARDS_TO_RENDER = 3; export function ConversationStarterCards() { const dispatch = useAppDispatch(); @@ -26,17 +26,15 @@ export function ConversationStarterCards() { return (
- {cmdsSortedByBookmark - .slice(0, NUM_CARDS_TO_RENDER_COLLAPSED) - .map((command, i) => ( - toggleBookmark(command)} - isBookmarked={bookmarkStatuses[command.name]} - /> - ))} + {cmdsSortedByBookmark.slice(0, NUM_CARDS_TO_RENDER).map((command, i) => ( + toggleBookmark(command)} + isBookmarked={bookmarkStatuses[command.name]} + /> + ))}
); } diff --git a/gui/src/redux/slices/profiles/slice.ts b/gui/src/redux/slices/profiles/slice.ts index c63a5c0ae..85152e455 100644 --- a/gui/src/redux/slices/profiles/slice.ts +++ b/gui/src/redux/slices/profiles/slice.ts @@ -1,12 +1,13 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { ProfileDescription } from "core/config/ConfigHandler"; import { OrganizationDescription } from "core/config/ProfileLifecycleManager"; +import { ensureProfilePreferences } from "./utils"; -interface PreferencesState { +export interface PreferencesState { bookmarksByName: string[]; } -interface ProfilesState { +export interface ProfilesState { availableProfiles: ProfileDescription[] | null; selectedProfileId: string | null; organizations: OrganizationDescription[]; @@ -28,12 +29,22 @@ export const profilesSlice = createSlice({ reducers: { setSelectedProfile: (state, { payload }: PayloadAction) => { state.selectedProfileId = payload; + + if (payload) { + ensureProfilePreferences(state, payload); + } }, setAvailableProfiles: ( state, { payload }: PayloadAction, ) => { state.availableProfiles = payload; + + if (payload) { + for (const profile of payload) { + ensureProfilePreferences(state, profile.id); + } + } }, setOrganizations: ( state, @@ -56,14 +67,6 @@ export const profilesSlice = createSlice({ if (!profileId) return; - // Initialize preferences for this profile if needed - if (!state.preferencesByProfileId[profileId]) { - state.preferencesByProfileId[profileId] = { - bookmarksByName: [], - }; - } - - // Only add if not already bookmarked const bookmarks = state.preferencesByProfileId[profileId].bookmarksByName; if (!bookmarks.includes(commandName)) { bookmarks.push(commandName); @@ -77,7 +80,7 @@ export const profilesSlice = createSlice({ const { commandName } = action.payload; const profileId = state.selectedProfileId; - if (!profileId || !state.preferencesByProfileId[profileId]) return; + if (!profileId) return; const preferences = state.preferencesByProfileId[profileId]; preferences.bookmarksByName = preferences.bookmarksByName.filter( diff --git a/gui/src/redux/slices/profiles/utils.ts b/gui/src/redux/slices/profiles/utils.ts new file mode 100644 index 000000000..58da69942 --- /dev/null +++ b/gui/src/redux/slices/profiles/utils.ts @@ -0,0 +1,12 @@ +import { ProfilesState } from "./slice"; + +export function ensureProfilePreferences( + state: ProfilesState, + profileId: string, +) { + if (!state.preferencesByProfileId[profileId]) { + state.preferencesByProfileId[profileId] = { + bookmarksByName: [], + }; + } +}