Skip to content

Commit

Permalink
feat: impl shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
hlhr202 committed Mar 25, 2024
1 parent cc27490 commit cd97614
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
18 changes: 16 additions & 2 deletions crates/openconnect-gui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ToastContainer } from "react-toastify";
import connected from "./assets/connected-animate.json";
import Lottie from "lottie-react";
import { AboutModal } from "./About";
import { useKey } from "react-use";

enum EStatus {
Initialized = "INITIALIZED",
Expand All @@ -46,6 +47,19 @@ function App() {
const { selectedServer } = useStoredConfigs();
const [isAboutOpened, setIsAboutOpened] = useState(false);

useKey(
"Enter",
() => {
if (vpnStatus.status === EStatus.Connected) {
handleDisconnect();
} else if (selectedServer) {
handleConnect();
}
},
undefined,
[vpnStatus, selectedServer]
);

const handleConnect = useCallback(async () => {
if (selectedServer) {
try {
Expand Down Expand Up @@ -160,7 +174,7 @@ function App() {
className="m-3 w-[50%]"
onClick={handleDisconnect}
>
Disconnect
Disconnect [Enter]
</Button>
)}
{vpnStatus.status === EStatus.Disconnected ||
Expand All @@ -171,7 +185,7 @@ function App() {
className="m-3 w-full"
onClick={handleConnect}
>
Connect
Connect [Enter]
</Button>
) : null}
</CardFooter>
Expand Down
32 changes: 31 additions & 1 deletion crates/openconnect-gui/src/ServerSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { FC, PropsWithChildren, useEffect } from "react";
import { ServerEditorModal } from "./ServerEditorModal";
import { useStoredConfigs } from "./state";
import { useKey } from "react-use";

export const ServerSelector = () => {
const {
Expand All @@ -20,6 +21,35 @@ export const ServerSelector = () => {

const { isOpen, onOpen, onOpenChange } = useDisclosure();

useKey(
"Tab",
(evt) => {
evt.preventDefault();
if (serverList.length) {
const currentIndex = serverList.findIndex(
({ name }) => name === selectedName
);
if (currentIndex !== -1) {
const nextIndex =
currentIndex === serverList.length - 1 ? 0 : currentIndex + 1;
const next = serverList[nextIndex].name;
setSelectedName(next);
}
}
},
undefined,
[serverList, selectedName, setSelectedName]
);

useKey(
"m",
() => {
onOpen();
},
undefined,
[onOpen]
);

useEffect(() => {
getStoredConfigs();
}, [getStoredConfigs]);
Expand Down Expand Up @@ -49,7 +79,7 @@ export const ServerSelector = () => {
</Select>
)}
<Button size="md" color="primary" className="ml-2" onClick={onOpen}>
Manage Server
Manage Server [M]
</Button>
</div>
<Divider className="mt-3 mb-3"></Divider>
Expand Down

0 comments on commit cd97614

Please sign in to comment.