|
| 1 | +import { |
| 2 | + Box, |
| 3 | + Button, |
| 4 | + Card, |
| 5 | + Flex, |
| 6 | + Heading, |
| 7 | + Switch, |
| 8 | + Text, |
| 9 | + TextField, |
| 10 | +} from "@radix-ui/themes"; |
| 11 | +import { useMutation } from "@tanstack/react-query"; |
| 12 | +import { useState } from "react"; |
| 13 | +import { useAuthStore } from "../stores/authStore"; |
| 14 | +import { useThemeStore } from "../stores/themeStore"; |
| 15 | +import { AsciiArt } from "./AsciiArt"; |
| 16 | + |
| 17 | +export function SettingsView() { |
| 18 | + const { |
| 19 | + apiKey: currentApiKey, |
| 20 | + apiHost, |
| 21 | + isAuthenticated, |
| 22 | + setCredentials, |
| 23 | + logout, |
| 24 | + } = useAuthStore(); |
| 25 | + const isDarkMode = useThemeStore((state) => state.isDarkMode); |
| 26 | + const toggleDarkMode = useThemeStore((state) => state.toggleDarkMode); |
| 27 | + const [apiKey, setApiKey] = useState(""); |
| 28 | + const [host, setHost] = useState(apiHost); |
| 29 | + const [initialHost] = useState(apiHost); |
| 30 | + |
| 31 | + const updateCredentialsMutation = useMutation({ |
| 32 | + mutationFn: async ({ apiKey, host }: { apiKey: string; host: string }) => { |
| 33 | + await setCredentials(apiKey, host); |
| 34 | + }, |
| 35 | + onSuccess: () => { |
| 36 | + setApiKey(""); |
| 37 | + setTimeout(() => updateCredentialsMutation.reset(), 3000); |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const handleApiKeyBlur = () => { |
| 42 | + // Only update if apiKey was entered (not empty) |
| 43 | + if (apiKey.trim()) { |
| 44 | + updateCredentialsMutation.mutate({ apiKey, host }); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + const handleHostBlur = () => { |
| 49 | + // Only update if host changed and is not empty |
| 50 | + if (host.trim() && host !== initialHost) { |
| 51 | + // Need current API key or new one to update |
| 52 | + const keyToUse = apiKey.trim() || currentApiKey; |
| 53 | + if (keyToUse) { |
| 54 | + updateCredentialsMutation.mutate({ apiKey: keyToUse, host }); |
| 55 | + } |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const handleLogout = () => { |
| 60 | + logout(); |
| 61 | + setApiKey(""); |
| 62 | + setHost("https://us.posthog.com"); |
| 63 | + updateCredentialsMutation.reset(); |
| 64 | + }; |
| 65 | + |
| 66 | + return ( |
| 67 | + <Box height="100%"> |
| 68 | + <Flex height="100%"> |
| 69 | + {/* Left pane - Settings */} |
| 70 | + <Box |
| 71 | + width="50%" |
| 72 | + p="6" |
| 73 | + className="border-gray-6 border-r" |
| 74 | + overflowY="auto" |
| 75 | + > |
| 76 | + <Flex direction="column" gap="6"> |
| 77 | + <Flex direction="column" gap="2"> |
| 78 | + <Heading size="6">Settings</Heading> |
| 79 | + <Text size="2" color="gray"> |
| 80 | + Manage your PostHog connection and preferences |
| 81 | + </Text> |
| 82 | + </Flex> |
| 83 | + |
| 84 | + {/* Appearance Section */} |
| 85 | + <Flex direction="column" gap="3"> |
| 86 | + <Heading size="4">Appearance</Heading> |
| 87 | + <Card> |
| 88 | + <Flex align="center" justify="between"> |
| 89 | + <Text size="2" weight="medium"> |
| 90 | + Dark Mode |
| 91 | + </Text> |
| 92 | + <Switch |
| 93 | + checked={isDarkMode} |
| 94 | + onCheckedChange={toggleDarkMode} |
| 95 | + size="2" |
| 96 | + /> |
| 97 | + </Flex> |
| 98 | + </Card> |
| 99 | + </Flex> |
| 100 | + |
| 101 | + <Box className="border-gray-6 border-t" /> |
| 102 | + |
| 103 | + {/* Authentication Section */} |
| 104 | + <Flex direction="column" gap="3"> |
| 105 | + <Flex align="center" gap="3"> |
| 106 | + <Heading size="4">Authentication</Heading> |
| 107 | + <Flex align="center" gap="2"> |
| 108 | + <Box |
| 109 | + width="8px" |
| 110 | + height="8px" |
| 111 | + className={`rounded-full ${isAuthenticated ? "bg-green-9" : "bg-red-9"}`} |
| 112 | + /> |
| 113 | + <Text size="2" color="gray"> |
| 114 | + {isAuthenticated ? "Connected" : "Not connected"} |
| 115 | + </Text> |
| 116 | + </Flex> |
| 117 | + </Flex> |
| 118 | + |
| 119 | + <Card> |
| 120 | + <Flex direction="column" gap="3"> |
| 121 | + <Flex direction="column" gap="2"> |
| 122 | + <Text size="2" weight="medium"> |
| 123 | + API Key |
| 124 | + </Text> |
| 125 | + <TextField.Root |
| 126 | + type="password" |
| 127 | + placeholder="Enter your PostHog API key" |
| 128 | + value={apiKey} |
| 129 | + onChange={(e) => setApiKey(e.target.value)} |
| 130 | + onBlur={handleApiKeyBlur} |
| 131 | + disabled={updateCredentialsMutation.isPending} |
| 132 | + /> |
| 133 | + </Flex> |
| 134 | + |
| 135 | + <Flex direction="column" gap="2"> |
| 136 | + <Text size="2" weight="medium"> |
| 137 | + API Host |
| 138 | + </Text> |
| 139 | + <TextField.Root |
| 140 | + type="text" |
| 141 | + placeholder="https://us.posthog.com" |
| 142 | + value={host} |
| 143 | + onChange={(e) => setHost(e.target.value)} |
| 144 | + onBlur={handleHostBlur} |
| 145 | + disabled={updateCredentialsMutation.isPending} |
| 146 | + /> |
| 147 | + </Flex> |
| 148 | + |
| 149 | + {updateCredentialsMutation.isError && ( |
| 150 | + <Text size="2" color="red"> |
| 151 | + {updateCredentialsMutation.error instanceof Error |
| 152 | + ? updateCredentialsMutation.error.message |
| 153 | + : "Failed to update credentials"} |
| 154 | + </Text> |
| 155 | + )} |
| 156 | + |
| 157 | + {updateCredentialsMutation.isSuccess && ( |
| 158 | + <Text size="2" color="green"> |
| 159 | + Credentials updated successfully |
| 160 | + </Text> |
| 161 | + )} |
| 162 | + |
| 163 | + <Box> |
| 164 | + <Button |
| 165 | + variant="classic" |
| 166 | + size="2" |
| 167 | + onClick={() => { |
| 168 | + const keyToUse = apiKey.trim() || currentApiKey; |
| 169 | + if (keyToUse && host.trim()) { |
| 170 | + updateCredentialsMutation.mutate({ |
| 171 | + apiKey: keyToUse, |
| 172 | + host, |
| 173 | + }); |
| 174 | + } |
| 175 | + }} |
| 176 | + disabled={updateCredentialsMutation.isPending} |
| 177 | + loading={updateCredentialsMutation.isPending} |
| 178 | + > |
| 179 | + Save credentials |
| 180 | + </Button> |
| 181 | + </Box> |
| 182 | + </Flex> |
| 183 | + </Card> |
| 184 | + </Flex> |
| 185 | + |
| 186 | + {isAuthenticated && <Box className="border-gray-6 border-t" />} |
| 187 | + |
| 188 | + {/* Account Section */} |
| 189 | + {isAuthenticated && ( |
| 190 | + <Flex direction="column" gap="3"> |
| 191 | + <Heading size="4">Account</Heading> |
| 192 | + <Card> |
| 193 | + <Button |
| 194 | + variant="soft" |
| 195 | + color="red" |
| 196 | + onClick={handleLogout} |
| 197 | + disabled={updateCredentialsMutation.isPending} |
| 198 | + > |
| 199 | + Logout |
| 200 | + </Button> |
| 201 | + </Card> |
| 202 | + </Flex> |
| 203 | + )} |
| 204 | + </Flex> |
| 205 | + </Box> |
| 206 | + |
| 207 | + {/* Right pane - ASCII Art */} |
| 208 | + <Box |
| 209 | + width="50%" |
| 210 | + height="100%" |
| 211 | + className="bg-panel-solid" |
| 212 | + style={{ position: "relative" }} |
| 213 | + > |
| 214 | + <Box style={{ position: "absolute", inset: 0, zIndex: 0 }}> |
| 215 | + <AsciiArt scale={1} opacity={0.1} /> |
| 216 | + </Box> |
| 217 | + </Box> |
| 218 | + </Flex> |
| 219 | + </Box> |
| 220 | + ); |
| 221 | +} |
0 commit comments