account dialog p2

This commit is contained in:
Dallin Romney 2024-11-20 16:48:29 -08:00
parent 9249ed8e00
commit 44f4ce5c92
3 changed files with 362 additions and 101 deletions

View file

@ -5,10 +5,11 @@ import {
} from "@heroicons/react/24/outline";
import { UserCircleIcon as UserCircleIconSolid } from "@heroicons/react/24/solid";
import { ProfileDescription } from "core/config/ConfigHandler";
import { Fragment, useContext, useState } from "react";
import { Fragment, useContext, useRef, useState } from "react";
import ReactDOM from "react-dom";
import styled from "styled-components";
import {
Button,
defaultBorderRadius,
lightGray,
vscBackground,
@ -118,102 +119,97 @@ function AccountDialog() {
controlServerBetaEnabled,
} = useAuth();
const topDiv = document.getElementById("profile-select-top-div");
// These shouldn't usually show but just to be safe
if (!session?.account?.id) {
return (
<div className="p-4">
<h1>Account</h1>
<p>Not signed in</p>
<Button onClick={login}>Login</Button>
</div>
);
}
if (!controlServerBetaEnabled) {
return (
<div className="p-4">
<h1>Account</h1>
<p>
Continue for teams is not enabled. You can enable it in your IDE
settings
</p>
<p>Using local config.</p>
<Button>Close</Button>
</div>
);
}
const topDiv = useRef<HTMLDivElement>(null);
return (
<div className="p-4">
<div className="mb-8">
<h1>Profiles</h1>
{/*
<p>
Continue pre-indexes many common documentation sites, but if there's
one you don't see in the dropdown, enter the URL here.
</p>
<p>
Continue's indexing engine will crawl the site and generate embeddings
so that you can ask questions.
</p> */}
</div>
{controlServerBetaEnabled && session?.account?.id && (
<StyledListbox
value={"GPT-4"}
onChange={(id: string) => {
ideMessenger.post("didChangeSelectedProfile", { id });
}}
>
<div className="relative">
<StyledListboxButton>
<span className="truncate">{selectedProfile?.title}</span>
<div className="pointer-events-none flex items-center">
<ChevronUpDownIcon
className="h-4 w-4 text-gray-400"
aria-hidden="true"
<h1>Account</h1>
{!!topDiv.current &&
ReactDOM.createPortal(
<Transition
as={Fragment}
leave="transition ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
show={true}
>
<StyledListboxOptions>
{profiles.map((option, idx) => (
<ListBoxOption
selected={option.id === selectedProfile.id}
option={option}
idx={idx}
key={idx}
showDelete={profiles.length > 1}
/>
))}
<div
className="px-2 py-1"
style={{
color: lightGray,
fontSize: getFontSize() - 2,
}}
>
{profiles.length === 0 ? (
<i>No workspaces found</i>
) : (
"Select workspace"
)}
</div>
</StyledListboxButton>
{topDiv &&
ReactDOM.createPortal(
<Transition
as={Fragment}
leave="transition ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<StyledListboxOptions>
{profiles.map((option, idx) => (
<ListBoxOption
selected={option.id === selectedProfile?.id}
option={option}
idx={idx}
key={idx}
showDelete={profiles.length > 1}
/>
))}
<div
className="px-2 py-1"
style={{
color: lightGray,
fontSize: getFontSize() - 2,
}}
>
{profiles.length === 0 ? (
<i>No workspaces found</i>
) : (
"Select workspace"
)}
</div>
</StyledListboxOptions>
</Transition>,
topDiv,
)}
</div>
</StyledListbox>
)}
{controlServerBetaEnabled &&
(session?.account ? (
<HeaderButtonWithToolTip
tooltipPlacement="top-end"
text={
session.account.label === ""
? "Logged in"
: `Logged in as ${session.account.label}`
}
onClick={logout}
>
<UserCircleIconSolid className="h-4 w-4" />
</HeaderButtonWithToolTip>
) : (
<HeaderButtonWithToolTip
tooltipPlacement="top-end"
text="Click to login to Continue"
onClick={login}
>
<UserCircleIconOutline className="h-4 w-4" />
</HeaderButtonWithToolTip>
))}
</StyledListboxOptions>
</Transition>,
topDiv.current,
)}
<StyledListbox
ref={topDiv}
value={"GPT-4"}
onChange={(id: string) => {
ideMessenger.post("didChangeSelectedProfile", { id });
}}
>
<div className="relative">
<StyledListboxButton>
<span className="truncate">{selectedProfile?.title}</span>
<div className="pointer-events-none flex items-center">
<ChevronUpDownIcon
className="h-4 w-4 text-gray-400"
aria-hidden="true"
/>
</div>
</StyledListboxButton>
</div>
</StyledListbox>
<div className="mt-4 flex flex-col items-start gap-2">
{session.account.label === ""
? "Signed in"
: `Signed in as ${session.account.label}`}
<Button onClick={logout}>Sign out</Button>
</div>
</div>
);
}

View file

@ -52,14 +52,6 @@ const GridDiv = styled.div`
overflow-x: visible;
`;
const ModelDropdownPortalDiv = styled.div`
background-color: ${vscInputBackground};
position: relative;
margin-left: 8px;
z-index: 200;
font-size: ${getFontSize()};
`;
const ProfileDropdownPortalDiv = styled.div`
background-color: ${vscInputBackground};
position: relative;
@ -266,7 +258,6 @@ const Layout = () => {
</div>
)}
<ModelDropdownPortalDiv id="model-select-top-div"></ModelDropdownPortalDiv>
<ProfileDropdownPortalDiv id="profile-select-top-div"></ProfileDropdownPortalDiv>
<Footer />
</GridDiv>

View file

@ -0,0 +1,274 @@
import { Listbox, Transition } from "@headlessui/react";
import {
ChevronUpDownIcon,
UserCircleIcon as UserCircleIconOutline,
} from "@heroicons/react/24/outline";
import { UserCircleIcon as UserCircleIconSolid } from "@heroicons/react/24/solid";
import { ProfileDescription } from "core/config/ConfigHandler";
import { Fragment, useContext, useEffect, useState } from "react";
import ReactDOM from "react-dom";
import { useDispatch, useSelector } from "react-redux";
import styled from "styled-components";
import {
defaultBorderRadius,
lightGray,
vscBackground,
vscForeground,
vscInputBackground,
vscListActiveBackground,
vscListActiveForeground,
} from ".";
import { IdeMessengerContext } from "../context/IdeMessenger";
import { useAuth } from "../hooks/useAuth";
import { useWebviewListener } from "../hooks/useWebviewListener";
import { setLastControlServerBetaEnabledStatus } from "../redux/slices/miscSlice";
import { RootState } from "../redux/store";
import { getFontSize } from "../util";
import HeaderButtonWithToolTip from "./gui/HeaderButtonWithToolTip";
const StyledListbox = styled(Listbox)`
background-color: ${vscBackground};
min-width: 80px;
`;
const StyledListboxButton = styled(Listbox.Button)`
position: relative;
cursor: pointer;
background-color: ${vscBackground};
text-align: left;
border: none;
margin: 0;
height: 100%;
width: 100%;
max-width: 180px;
white-space: nowrap;
overflow: hidden;
border: 0.5px solid ${lightGray};
border-radius: ${defaultBorderRadius};
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
color: ${vscForeground};
padding: 3px 6px;
&:focus {
outline: none;
}
&:hover {
background-color: ${vscInputBackground};
}
font-size: ${getFontSize() - 2}px;
`;
const StyledListboxOptions = styled(Listbox.Options)`
background-color: ${vscInputBackground};
padding: 0;
position: absolute;
bottom: calc(100% - 16px);
max-width: 100%;
max-height: 80vh;
border-radius: ${defaultBorderRadius};
overflow-y: scroll;
`;
const StyledListboxOption = styled(Listbox.Option)<{ selected: boolean }>`
background-color: ${({ selected }) =>
selected ? vscListActiveBackground : vscInputBackground};
cursor: pointer;
padding: 6px 8px;
&:hover {
background-color: ${vscListActiveBackground};
color: ${vscListActiveForeground};
}
`;
function ListBoxOption({
option,
idx,
showDelete,
selected,
}: {
option: ProfileDescription;
idx: number;
showDelete?: boolean;
selected: boolean;
}) {
const [hovered, setHovered] = useState(false);
return (
<StyledListboxOption
key={idx}
selected={selected}
value={option.id}
onMouseEnter={() => {
setHovered(true);
}}
onMouseLeave={() => {
setHovered(false);
}}
>
<div className="relative flex h-5 items-center justify-between gap-3">
{option.title}
</div>
</StyledListboxOption>
);
}
function ProfileSwitcher() {
const ideMessenger = useContext(IdeMessengerContext);
const { session, logout, login } = useAuth();
const [profiles, setProfiles] = useState<ProfileDescription[]>([]);
const dispatch = useDispatch();
const lastControlServerBetaEnabledStatus = useSelector(
(state: RootState) => state.misc.lastControlServerBetaEnabledStatus,
);
const selectedProfileId = useSelector(
(store: RootState) => store.state.selectedProfileId,
);
const [controlServerBetaEnabled, setControlServerBetaEnabled] =
useState(false);
useEffect(() => {
ideMessenger.ide.getIdeSettings().then(({ enableControlServerBeta }) => {
setControlServerBetaEnabled(enableControlServerBeta);
dispatch(setLastControlServerBetaEnabledStatus(enableControlServerBeta));
const shouldShowPopup =
!lastControlServerBetaEnabledStatus && enableControlServerBeta;
if (shouldShowPopup) {
ideMessenger.ide.showToast("info", "Continue for Teams enabled");
}
});
}, []);
useWebviewListener(
"didChangeIdeSettings",
async (msg) => {
const { settings } = msg;
setControlServerBetaEnabled(settings.enableControlServerBeta);
dispatch(
setLastControlServerBetaEnabledStatus(settings.enableControlServerBeta),
);
},
[],
);
useEffect(() => {
ideMessenger
.request("config/listProfiles", undefined)
.then(
(result) => result.status === "success" && setProfiles(result.content),
);
}, []);
useWebviewListener(
"didChangeAvailableProfiles",
async (data) => {
setProfiles(data.profiles);
},
[],
);
const topDiv = document.getElementById("profile-select-top-div");
function selectedProfile() {
return profiles.find((p) => p.id === selectedProfileId);
}
return (
<>
{controlServerBetaEnabled && session?.account?.id && (
<StyledListbox
value={"GPT-4"}
onChange={(id: string) => {
ideMessenger.post("didChangeSelectedProfile", { id });
}}
>
<div className="relative">
<StyledListboxButton>
<span className="truncate">{selectedProfile()?.title}</span>
<div className="pointer-events-none flex items-center">
<ChevronUpDownIcon
className="h-4 w-4 text-gray-400"
aria-hidden="true"
/>
</div>
</StyledListboxButton>
{topDiv &&
ReactDOM.createPortal(
<Transition
as={Fragment}
leave="transition ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<StyledListboxOptions>
{profiles.map((option, idx) => (
<ListBoxOption
selected={option.id === selectedProfileId}
option={option}
idx={idx}
key={idx}
showDelete={profiles.length > 1}
/>
))}
<div
className="px-2 py-1"
style={{
color: lightGray,
fontSize: getFontSize() - 2,
}}
>
{profiles.length === 0 ? (
<i>No workspaces found</i>
) : (
"Select workspace"
)}
</div>
</StyledListboxOptions>
</Transition>,
topDiv,
)}
</div>
</StyledListbox>
)}
{controlServerBetaEnabled &&
(session?.account ? (
<HeaderButtonWithToolTip
tooltipPlacement="top-end"
text={
session.account.label === ""
? "Logged in"
: `Logged in as ${session.account.label}`
}
onClick={logout}
>
<UserCircleIconSolid className="h-4 w-4" />
</HeaderButtonWithToolTip>
) : (
<HeaderButtonWithToolTip
tooltipPlacement="top-end"
text="Click to login to Continue"
onClick={login}
>
<UserCircleIconOutline className="h-4 w-4" />
</HeaderButtonWithToolTip>
))}
</>
);
}
export default ProfileSwitcher;