|
| 1 | +import React, { useEffect, useContext, useState, useRef } from "react"; |
| 2 | +import DialogTitle from "@mui/material/DialogTitle"; |
| 3 | +import Dialog from "@mui/material/Dialog"; |
| 4 | +import DialogActions from "@mui/material/DialogActions"; |
| 5 | +import DialogContent from "@mui/material/DialogContent"; |
| 6 | +import DialogContentText from "@mui/material/DialogContentText"; |
| 7 | +import FormGroup from "@mui/material/FormGroup"; |
| 8 | +import FormControlLabel from "@mui/material/FormControlLabel"; |
| 9 | +import TextField from "@mui/material/TextField"; |
| 10 | +import Switch from "@mui/material/Switch"; |
| 11 | +import InputAdornment from "@mui/material/InputAdornment"; |
| 12 | +import Chip from "@mui/material/Chip"; |
| 13 | +import { useStore } from "zustand"; |
| 14 | +import { RepoContext } from "../lib/store"; |
| 15 | +import { Link, Stack } from "@mui/material"; |
| 16 | +import LaunchIcon from "@mui/icons-material/Launch"; |
| 17 | +import { useApolloClient } from "@apollo/client"; |
| 18 | +import Button from "@mui/material/Button"; |
| 19 | +import DoneIcon from "@mui/icons-material/Done"; |
| 20 | +import CloseIcon from "@mui/icons-material/Close"; |
| 21 | +import Snackbar from "@mui/material/Snackbar"; |
| 22 | +import Alert, { AlertColor } from "@mui/material/Alert"; |
| 23 | +import { openTokenPage, registerUser } from "../lib/monacoCompletionProvider"; |
| 24 | + |
| 25 | +interface SettingDiagProps { |
| 26 | + open: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +export function SettingDialog({ open = false }: SettingDiagProps) { |
| 30 | + const store = useContext(RepoContext); |
| 31 | + if (!store) throw new Error("Missing BearContext.Provider in the tree"); |
| 32 | + const setSettingOpen = useStore(store, (state) => state.setSettingOpen); |
| 33 | + const apiKey = useStore(store, (state) => state.user.codeiumAPIKey); |
| 34 | + const user = useStore(store, (state) => state.user); |
| 35 | + const isCustomToken = useStore(store, (state) => state.isCustomToken); |
| 36 | + const setIsCustomToken = useStore(store, (state) => state.setIsCustomToken); |
| 37 | + const updateAPIKey = useStore(store, (state) => state.updateAPIKey); |
| 38 | + const client = useApolloClient(); |
| 39 | + const inputRef = useRef<HTMLInputElement>(null); |
| 40 | + const [infoShow, setInfoShow] = useState(false); |
| 41 | + const [status, setStatus] = useState<"success" | "error" | "warning">( |
| 42 | + "success" |
| 43 | + ); |
| 44 | + const [message, setMessage] = useState(""); |
| 45 | + |
| 46 | + const onAlertClose = ( |
| 47 | + event: React.SyntheticEvent | Event, |
| 48 | + reason?: string |
| 49 | + ) => { |
| 50 | + if (reason === "clickaway") { |
| 51 | + return; |
| 52 | + } |
| 53 | + setInfoShow(false); |
| 54 | + }; |
| 55 | + |
| 56 | + const updateToken = async () => { |
| 57 | + const token = inputRef.current?.value.trim(); |
| 58 | + if (!token) { |
| 59 | + setStatus("error"); |
| 60 | + setMessage("Token cannot be empty"); |
| 61 | + setInfoShow(true); |
| 62 | + return; |
| 63 | + } |
| 64 | + try { |
| 65 | + const { api_key, name } = await registerUser(token); |
| 66 | + if (api_key === "" || api_key === undefined) { |
| 67 | + throw new Error("Invalid token"); |
| 68 | + } |
| 69 | + if (await updateAPIKey(client, api_key)) { |
| 70 | + setStatus("success"); |
| 71 | + setMessage(`${name}, welcome. Token updated`); |
| 72 | + setInfoShow(true); |
| 73 | + } else { |
| 74 | + throw new Error("Update failed"); |
| 75 | + } |
| 76 | + } catch (e) { |
| 77 | + setStatus("error"); |
| 78 | + setMessage((e as Error).message || "Unknown error"); |
| 79 | + console.log( |
| 80 | + (e as Error).message === undefined, |
| 81 | + (e as Error).message === "" |
| 82 | + ); |
| 83 | + setInfoShow(true); |
| 84 | + return; |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <Dialog open={open}> |
| 90 | + <DialogTitle>Auto Completion</DialogTitle> |
| 91 | + <DialogContent> |
| 92 | + <DialogContentText> |
| 93 | + The AI code auto completion is powered by{" "} |
| 94 | + <Link href="https://codeium.com/" target="_blank" rel="noreferrer"> |
| 95 | + Codeium <LaunchIcon fontSize="small" /> |
| 96 | + </Link>{" "} |
| 97 | + You can also use your own token instead of our default API keys, which |
| 98 | + records your own activities of using Codeium. |
| 99 | + </DialogContentText> |
| 100 | + |
| 101 | + <Stack |
| 102 | + direction="row" |
| 103 | + flexWrap="wrap" |
| 104 | + spacing={{ xs: 1, sm: 2 }} |
| 105 | + sx={{ justifyContent: "space-between" }} |
| 106 | + > |
| 107 | + {apiKey ? ( |
| 108 | + <Chip |
| 109 | + label="Token Verified" |
| 110 | + color="success" |
| 111 | + size="small" |
| 112 | + variant="outlined" |
| 113 | + icon={<DoneIcon />} |
| 114 | + /> |
| 115 | + ) : ( |
| 116 | + <Chip |
| 117 | + label="No Stored Token" |
| 118 | + color="error" |
| 119 | + size="small" |
| 120 | + variant="outlined" |
| 121 | + icon={<CloseIcon />} |
| 122 | + /> |
| 123 | + )} |
| 124 | + <Button endIcon={<LaunchIcon />} onClick={() => openTokenPage()}> |
| 125 | + Get Token |
| 126 | + </Button> |
| 127 | + </Stack> |
| 128 | + |
| 129 | + <FormGroup> |
| 130 | + <FormControlLabel |
| 131 | + sx={{ marginRight: 0 }} |
| 132 | + control={ |
| 133 | + <Switch |
| 134 | + checked={isCustomToken} |
| 135 | + onChange={(e) => setIsCustomToken(e.target.checked)} |
| 136 | + /> |
| 137 | + } |
| 138 | + label="Use my own token" |
| 139 | + /> |
| 140 | + |
| 141 | + {isCustomToken && ( |
| 142 | + <TextField |
| 143 | + fullWidth |
| 144 | + label="Token" |
| 145 | + variant="standard" |
| 146 | + inputRef={inputRef} |
| 147 | + placeholder="Paste your token here" |
| 148 | + InputProps={{ |
| 149 | + endAdornment: ( |
| 150 | + <InputAdornment position="end"> |
| 151 | + <Button onClick={() => updateToken()}>Update</Button> |
| 152 | + </InputAdornment> |
| 153 | + ), |
| 154 | + }} |
| 155 | + /> |
| 156 | + )} |
| 157 | + </FormGroup> |
| 158 | + <Snackbar |
| 159 | + open={infoShow} |
| 160 | + autoHideDuration={3000} |
| 161 | + anchorOrigin={{ vertical: "top", horizontal: "center" }} |
| 162 | + onClose={onAlertClose} |
| 163 | + > |
| 164 | + <Alert severity={status as AlertColor} onClose={onAlertClose}> |
| 165 | + {message} |
| 166 | + </Alert> |
| 167 | + </Snackbar> |
| 168 | + </DialogContent> |
| 169 | + <DialogActions> |
| 170 | + <Button onClick={() => setSettingOpen(false)}>Close</Button> |
| 171 | + </DialogActions> |
| 172 | + </Dialog> |
| 173 | + ); |
| 174 | +} |
0 commit comments