From 80f1c11d8df2cfbd057a882302484fcebcad7484 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 7 Jan 2025 17:42:08 -0800 Subject: [PATCH] use thunks for cycling profile id --- .../platform/AssistantAndModelSelect.tsx | 11 +++++----- .../platform/AssistantSelect.tsx | 13 ++++-------- .../modelSelection/platform/shared.tsx | 2 +- gui/src/redux/slices/sessionSlice.ts | 10 --------- gui/src/redux/thunks/cycleProfile.ts | 21 +++++++++++++++++++ gui/src/redux/thunks/setProfileId.ts | 13 ++++++++++++ 6 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 gui/src/redux/thunks/cycleProfile.ts create mode 100644 gui/src/redux/thunks/setProfileId.ts diff --git a/gui/src/components/modelSelection/platform/AssistantAndModelSelect.tsx b/gui/src/components/modelSelection/platform/AssistantAndModelSelect.tsx index 955f770f9..7c1767f83 100644 --- a/gui/src/components/modelSelection/platform/AssistantAndModelSelect.tsx +++ b/gui/src/components/modelSelection/platform/AssistantAndModelSelect.tsx @@ -1,17 +1,16 @@ import { Popover } from "@headlessui/react"; import { ChevronDownIcon } from "@heroicons/react/24/outline"; import { useEffect, useRef, useState } from "react"; -import { useDispatch } from "react-redux"; import styled from "styled-components"; import { defaultBorderRadius, lightGray, vscInputBackground } from "../.."; import AddModelForm from "../../../forms/AddModelForm"; -import { useAppSelector } from "../../../redux/hooks"; +import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; import { cycleDefaultModel, selectDefaultModel, } from "../../../redux/slices/configSlice"; -import { cycleSelectedProfileId } from "../../../redux/slices/sessionSlice"; import { setDialogMessage, setShowDialog } from "../../../redux/slices/uiSlice"; +import { cycleProfile } from "../../../redux/thunks/cycleProfile"; import { getFontSize, isMetaEquivalentKeyPressed } from "../../../util"; import PopoverTransition from "../../mainInput/InputToolbar/PopoverTransition"; import { AssistantSelect } from "./AssistantSelect"; @@ -50,7 +49,7 @@ const StyledPopoverPanel = styled(Popover.Panel)<{ $showabove: boolean }>` `; function AssistantAndModelSelect() { - const dispatch = useDispatch(); + const dispatch = useAppDispatch(); const defaultModel = useAppSelector(selectDefaultModel); const [showAbove, setShowAbove] = useState(false); const buttonRef = useRef(null); @@ -76,7 +75,7 @@ function AssistantAndModelSelect() { setShowAbove(spaceBelow < dropdownHeight && spaceAbove > spaceBelow); } - function onClickAddModel(e) { + function onClickAddModel(e: any) { e.stopPropagation(); e.preventDefault(); @@ -108,7 +107,7 @@ function AssistantAndModelSelect() { dispatch(cycleDefaultModel("next")); } else { if (now - lastToggleTime >= DEBOUNCE_MS) { - dispatch(cycleSelectedProfileId()); + dispatch(cycleProfile()); lastToggleTime = now; } } diff --git a/gui/src/components/modelSelection/platform/AssistantSelect.tsx b/gui/src/components/modelSelection/platform/AssistantSelect.tsx index cde44669a..292b71309 100644 --- a/gui/src/components/modelSelection/platform/AssistantSelect.tsx +++ b/gui/src/components/modelSelection/platform/AssistantSelect.tsx @@ -4,11 +4,11 @@ import { SparklesIcon, } from "@heroicons/react/24/outline"; import { useContext } from "react"; -import { useDispatch } from "react-redux"; import { lightGray } from "../.."; import { useAuth } from "../../../context/Auth"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; -import { setSelectedProfileId } from "../../../redux/slices/sessionSlice"; +import { useAppDispatch } from "../../../redux/hooks"; +import { setProfileId } from "../../../redux/thunks/setProfileId"; import { getFontSize, getMetaKeyLabel } from "../../../util"; import { Divider, Option, OptionDiv } from "./shared"; @@ -18,12 +18,7 @@ export function AssistantSelect(props: AssistantSelectProps) { const ideMessenger = useContext(IdeMessengerContext); const { profiles, selectedProfile } = useAuth(); - const dispatch = useDispatch(); - - const changeProfileId = (id: string) => { - ideMessenger.post("didChangeSelectedProfile", { id }); - dispatch(setSelectedProfileId(id)); - }; + const dispatch = useAppDispatch(); function onNewAssistant() { ideMessenger.post("openUrl", "https://app.continue.dev/new"); @@ -45,7 +40,7 @@ export function AssistantSelect(props: AssistantSelectProps) { profileId: option.id, }); }} - onClick={() => changeProfileId(option.id)} + onClick={() => dispatch(setProfileId(option.id))} >
{option.id === "local" ? ( diff --git a/gui/src/components/modelSelection/platform/shared.tsx b/gui/src/components/modelSelection/platform/shared.tsx index 1d45fafd9..7b2bcbf75 100644 --- a/gui/src/components/modelSelection/platform/shared.tsx +++ b/gui/src/components/modelSelection/platform/shared.tsx @@ -81,7 +81,7 @@ export function Option({ }: ModelOptionProps) { const [hovered, setHovered] = useState(false); - function handleOptionClick(e) { + function handleOptionClick(e: any) { if (disabled) { e.preventDefault(); e.stopPropagation(); diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 64d75a9ef..3943d1b8f 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -485,15 +485,6 @@ export const sessionSlice = createSlice({ selectedProfileId: payload, }; }, - cycleSelectedProfileId: (state) => { - const profileIds = state.availableProfiles.map((profile) => profile.id); - const curIndex = profileIds.indexOf(state.selectedProfileId); - const nextIndex = (curIndex + 1) % profileIds.length; - return { - ...state, - selectedProfileId: profileIds[nextIndex], - }; - }, setAvailableProfiles: ( state, { payload }: PayloadAction, @@ -688,7 +679,6 @@ export const { removeCodeToEdit, setCalling, cancelToolCall, - cycleSelectedProfileId, setAvailableProfiles, acceptToolCall, setToolGenerated, diff --git a/gui/src/redux/thunks/cycleProfile.ts b/gui/src/redux/thunks/cycleProfile.ts new file mode 100644 index 000000000..0ee548c31 --- /dev/null +++ b/gui/src/redux/thunks/cycleProfile.ts @@ -0,0 +1,21 @@ +import { createAsyncThunk } from "@reduxjs/toolkit"; +import { ThunkApiType } from "../store"; +import { setProfileId } from "./setProfileId"; + +export const cycleProfile = createAsyncThunk( + "profile/cycle", + async (_, { dispatch, getState, extra }) => { + const state = getState(); + const profileIds = state.session.availableProfiles.map( + (profile) => profile.id, + ); + const curIndex = profileIds.indexOf(state.session.selectedProfileId); + const nextIndex = (curIndex + 1) % profileIds.length; + const nextId = profileIds[nextIndex]; + dispatch(setProfileId(nextId)); + + extra.ideMessenger.post("didChangeSelectedProfile", { + id: nextId, + }); + }, +); diff --git a/gui/src/redux/thunks/setProfileId.ts b/gui/src/redux/thunks/setProfileId.ts new file mode 100644 index 000000000..6ec1020ce --- /dev/null +++ b/gui/src/redux/thunks/setProfileId.ts @@ -0,0 +1,13 @@ +import { createAsyncThunk } from "@reduxjs/toolkit"; +import { setSelectedProfileId } from "../slices/sessionSlice"; +import { ThunkApiType } from "../store"; + +export const setProfileId = createAsyncThunk( + "profile/setId", + async (id, { dispatch, extra }) => { + dispatch(setSelectedProfileId(id)); + extra.ideMessenger.post("didChangeSelectedProfile", { + id, + }); + }, +);