use thunks for cycling profile id

This commit is contained in:
Nate 2025-01-07 17:42:08 -08:00
parent 185f0c5c79
commit 80f1c11d8d
6 changed files with 44 additions and 26 deletions

View file

@ -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<HTMLButtonElement>(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;
}
}

View file

@ -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))}
>
<div className="flex flex-grow items-center">
{option.id === "local" ? (

View file

@ -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();

View file

@ -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<ProfileDescription[]>,
@ -688,7 +679,6 @@ export const {
removeCodeToEdit,
setCalling,
cancelToolCall,
cycleSelectedProfileId,
setAvailableProfiles,
acceptToolCall,
setToolGenerated,

View file

@ -0,0 +1,21 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { ThunkApiType } from "../store";
import { setProfileId } from "./setProfileId";
export const cycleProfile = createAsyncThunk<void, undefined, ThunkApiType>(
"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,
});
},
);

View file

@ -0,0 +1,13 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { setSelectedProfileId } from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
export const setProfileId = createAsyncThunk<void, string, ThunkApiType>(
"profile/setId",
async (id, { dispatch, extra }) => {
dispatch(setSelectedProfileId(id));
extra.ideMessenger.post("didChangeSelectedProfile", {
id,
});
},
);