Skip to content

Commit

Permalink
Merge pull request #1 from smallcloudai/docker-authorization
Browse files Browse the repository at this point in the history
WIP: api_key cookies
  • Loading branch information
MarcMcIntosh authored Feb 2, 2024
2 parents fbc9b50 + 54c369c commit de39178
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 11 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
[ ] export the chat history component
[ ] add vscode specific button for opening the history in a tab
[ ] should be monotype font on tooltip (will require adding a custom tooltip)
[ ] update readme with the new features/options
[x] uninstall react-cookie and delete code comments

### EVENTS TODO FOR IDEs

Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,11 @@
"types": "tsc --noEmit",
"prepublishOnly": "npm run build"
},
"dependencies": {},
"devDependencies": {
"@ariakit/react": "^0.3.13",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-toolbar": "^1.0.4",
"@radix-ui/themes": "^2.0.2",
"classnames": "^2.3.2",
"match-sorter": "^6.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"remark-breaks": "^4.0.0",
"textarea-caret": "^3.1.0",
"usehooks-ts": "^2.9.1",
"uuid": "^9.0.1",
"@storybook/addon-essentials": "^7.6.4",
"@storybook/addon-interactions": "^7.6.4",
"@storybook/addon-links": "^7.6.4",
Expand All @@ -70,6 +59,7 @@
"@storybook/test": "^7.6.4",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.1",
"@types/js-cookie": "^3.0.6",
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@types/react-syntax-highlighter": "^15.5.11",
Expand All @@ -80,19 +70,30 @@
"@vitejs/plugin-react-swc": "^3.5.0",
"@vitest/coverage-v8": "^1.1.0",
"@vitest/ui": "^1.1.0",
"classnames": "^2.3.2",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"eslint-plugin-storybook": "^0.6.15",
"husky": ">=6",
"js-cookie": "^3.0.5",
"jsdom": "^23.0.1",
"lint-staged": ">=10",
"match-sorter": "^6.3.1",
"prettier": "3.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"remark-breaks": "^4.0.0",
"storybook": "^7.6.4",
"textarea-caret": "^3.1.0",
"typescript": "^5.2.2",
"typescript-plugin-css-modules": "^5.0.2",
"usehooks-ts": "^2.9.1",
"uuid": "^9.0.1",
"vite": "^5.0.8",
"vite-plugin-dts": "^3.7.0",
"vite-plugin-eslint": "^1.8.1",
Expand Down
78 changes: 78 additions & 0 deletions src/components/Sidebar/Settings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, { useEffect } from "react";
import {
Flex,
IconButton,
Dialog,
Text,
TextField,
Button,
// DialogRoot,
// DialogTrigger,
// DialogContent,
// DialogTitle,
// DialogDescription,
// DialogClose,
} from "@radix-ui/themes";
import { GearIcon } from "@radix-ui/react-icons";
import { useApiKey } from "../../hooks";

export const Settings: React.FC = () => {
const [apiKey, setApiKey] = useApiKey();
const [keyValue, setValue] = React.useState(apiKey);
const [open, setOpen] = React.useState(false);

useEffect(() => {
setValue(apiKey);
}, [apiKey, open]);

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setApiKey(keyValue);
setOpen(false);
};

return (
<Flex p="4">
<Dialog.Root open={open} onOpenChange={setOpen}>
<Dialog.Trigger>
<IconButton variant="outline" title="Settings">
<GearIcon />
</IconButton>
</Dialog.Trigger>

<Dialog.Content onOpenAutoFocus={(event) => event.preventDefault()}>
<Dialog.Title>Settings</Dialog.Title>
<Dialog.Description>Change chat settings</Dialog.Description>
<Flex asChild pt="4">
<form onSubmit={handleSubmit}>
<label style={{ width: "100%" }}>
<Text as="div" size="2" mb="1" weight="bold">
API Key
</Text>
<TextField.Input
name="apiKey"
type="text"
value={keyValue}
onChange={(event) => setValue(event.target.value)}
placeholder="Enter your refact api key"
/>
</label>
</form>
</Flex>
<Flex gap="3" mt="4" justify="end">
<Dialog.Close>
<Button variant="soft" color="gray">
Cancel
</Button>
</Dialog.Close>
<Dialog.Close>
<Button onClick={() => setApiKey(keyValue)} type="submit">
Save
</Button>
</Dialog.Close>
</Flex>
</Dialog.Content>
</Dialog.Root>
</Flex>
);
};
2 changes: 2 additions & 0 deletions src/components/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { Box, Flex, Button } from "@radix-ui/themes";
import styles from "./sidebar.module.css";
import { ChatHistory, type ChatHistoryProps } from "../ChatHistory";
import { Settings } from "./Settings";

export const Sidebar: React.FC<
{
Expand Down Expand Up @@ -35,6 +36,7 @@ export const Sidebar: React.FC<
onHistoryItemClick={onHistoryItemClick}
onDeleteHistoryItem={onDeleteHistoryItem}
/>
<Settings />
</Flex>
</Box>
);
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./useEventBusForHost";
export * from "./useIsOnline";
export * from "./useOnPressedEnter";
export * from "./useEventBusForSidebar";
export * from "./useApiKey";
18 changes: 18 additions & 0 deletions src/hooks/useApiKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useState, useEffect } from "react";
import * as ApiKey from "../utils/ApiKey";

export function useApiKey(): [string, (value: string) => void] {
const maybeKey = ApiKey.getApiKey();
const [key, setKey] = useState(maybeKey);
useEffect(() => {
const apiKey = ApiKey.getApiKey();
setKey(apiKey);
}, []);

const setApiKey = (value: string) => {
ApiKey.setApiKey(value);
setKey(value);
};

return [key, setApiKey];
}
3 changes: 3 additions & 0 deletions src/services/refact.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getApiKey } from "../utils/ApiKey";
const CHAT_URL = `/v1/chat`;
const CAPS_URL = `/v1/caps`;

Expand Down Expand Up @@ -106,8 +107,10 @@ export function sendChat(
stream: true,
});

const apiKey = getApiKey();
const headers = {
"Content-Type": "application/json",
...(apiKey ? { Authorization: "Bearer " + apiKey } : {}),
};
const chatEndpoint = lspUrl
? `${lspUrl.replace(/\/*$/, "")}${CHAT_URL}`
Expand Down
6 changes: 6 additions & 0 deletions src/utils/ApiKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Cookies from "js-cookie";
export const getApiKey = () => Cookies.get("api_key") ?? "";

export const setApiKey = (value: string) => {
Cookies.set("api_key", value);
};

0 comments on commit de39178

Please sign in to comment.