| 1 | import { |
| 2 | CheckCircleIcon, |
| 3 | ExclamationCircleIcon, |
| 4 | ExclamationTriangleIcon, |
| 5 | } from "@heroicons/react/24/outline"; |
| 6 | import { useContext, useEffect, useMemo, useState } from "react"; |
| 7 | import { useNavigate } from "react-router-dom"; |
| 8 | import { Button } from "../components"; |
| 9 | import { IdeMessengerContext } from "../context/IdeMessenger"; |
| 10 | import { isJetBrains } from "../util"; |
| 11 | import { ROUTES } from "../util/navigation"; |
| 12 | import { |
| 13 | clearThemeLocalCache, |
| 14 | setDocumentStylesFromTheme, |
| 15 | THEME_COLORS, |
| 16 | varWithFallback, |
| 17 | } from "./theme"; |
| 18 | |
| 19 | const ThemeTailwindClassExample = ({ |
| 20 | colorName, |
| 21 | varNames, |
| 22 | defaultColor, |
| 23 | isMissing, |
| 24 | }: { |
| 25 | colorName: string; |
| 26 | varNames: string[]; |
| 27 | defaultColor: string; |
| 28 | isMissing?: boolean; |
| 29 | }) => { |
| 30 | return ( |
| 31 | <> |
| 32 | <div className={`line-clamp-1 break-all px-2 text-right`}> |
| 33 | {colorName} |
| 34 | </div> |
| 35 | <div |
| 36 | className={`line-clamp-1 break-all p-1 text-[9px] ${isMissing ? "text-error" : ""}`} |
| 37 | style={{ |
| 38 | backgroundColor: varWithFallback( |
| 39 | colorName as keyof typeof THEME_COLORS, |
| 40 | ), |
| 41 | }} |
| 42 | > |
| 43 | {varNames.join(", ")} |
| 44 | </div> |
| 45 | <div |
| 46 | className={`line-clamp-1 break-all p-1 text-[9px]`} |
| 47 | style={{ |
| 48 | backgroundColor: defaultColor, |
| 49 | }} |
| 50 | > |
| 51 | {defaultColor} |
| 52 | </div> |
| 53 | </> |
| 54 | ); |
| 55 | }; |
| 56 | |
| 57 | function ThemePage() { |
| 58 | const navigate = useNavigate(); |
| 59 | const [listToggled, setListToggled] = useState(false); |
| 60 | const ideMessenger = useContext(IdeMessengerContext); |
| 61 | const jetbrains = useMemo(() => { |
| 62 | return isJetBrains(); |
| 63 | }, []); |
| 64 | |
| 65 | const [missingVars, setMissingVars] = useState<string[]>([]); |
| 66 | const refreshColors = () => { |
| 67 | if (jetbrains) { |
| 68 | // Jetbrains: get colors and check which ones are missing from theme |
| 69 | void ideMessenger |
| 70 | .request("jetbrains/getColors", undefined) |
| 71 | .then((result) => { |
| 72 | console.log(result); |
| 73 | if (result.status === "success") { |
| 74 | const missingColors = setDocumentStylesFromTheme(result.content); |
| 75 | setMissingVars(missingColors); |
| 76 | } |
| 77 | }); |
| 78 | } else { |
| 79 | // VS Code: find actual missing CSS variables |
| 80 | const notFound: string[] = []; |
| 81 | Object.entries(THEME_COLORS).forEach(([colorName, themeVals], idx) => { |
| 82 | let found = false; |
| 83 | themeVals.vars.forEach((cssVar) => { |
| 84 | const value = getComputedStyle( |
| 85 | document.documentElement, |
| 86 | ).getPropertyValue(cssVar); |
| 87 | if (!value) { |
| 88 | notFound.push(`cssVar (choice #${idx + 1} for color ${colorName}`); |
| 89 | } |
| 90 | }); |
| 91 | }); |
| 92 | setMissingVars(notFound); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | useEffect(() => { |
| 97 | refreshColors(); |
| 98 | }, []); |
| 99 | |
| 100 | return ( |
| 101 | <div className="flex flex-1 flex-col gap-3 px-2 py-2"> |
| 102 | <span |
| 103 | className="flex cursor-pointer flex-row items-center gap-2" |
| 104 | onClick={() => navigate(ROUTES.HOME)} |
| 105 | > |
| 106 | <span className="">←</span> |
| 107 | Back to Chat |
| 108 | </span> |
| 109 | <h2 className="mb-2 mt-0 p-0 text-center text-xl font-semibold"> |
| 110 | Theme Tester |
| 111 | </h2> |
| 112 | <div className="bg-background border-border grid grid-cols-1 gap-4 rounded-md border border-solid p-4 md:grid-cols-2"> |
| 113 | <div className="col-span-2 flex gap-4"> |
| 114 | <p className="">Normal text</p> |
| 115 | <p className="text-description">Description text</p> |
| 116 | <p className="text-description-muted">Muted text</p> |
| 117 | </div> |
| 118 | |
| 119 | <button className="bg-primary text-primary-foreground hover:bg-primary-hover cursor-pointer rounded border-none px-4 py-2 disabled:cursor-not-allowed disabled:brightness-75"> |
| 120 | Primary Button |
| 121 | </button> |
| 122 | <button className="bg-secondary text-secondary-foreground hover:bg-secondary-hover cursor-pointer rounded border-none px-4 py-2 disabled:cursor-not-allowed disabled:brightness-75"> |
| 123 | Secondary Button |
| 124 | </button> |
| 125 | |
| 126 | <div className="border-border rounded-md border border-solid p-3"> |
| 127 | Normal border |
| 128 | </div> |
| 129 | |
| 130 | <div className="border-border-focus rounded-md border border-solid p-3"> |
| 131 | Focus border |
| 132 | </div> |
| 133 | |
| 134 | <input |
| 135 | className="bg-input text-input-foreground border-input-border placeholder:text-input-placeholder focus:border-border-focus rounded-md border border-solid p-2 focus:outline-none" |
| 136 | placeholder="Input example" |
| 137 | /> |
| 138 | |
| 139 | <div className="bg-badge text-badge-foreground border-border inline-block rounded-full border border-solid p-2"> |
| 140 | <span className="px-2">Badge</span> |
| 141 | </div> |
| 142 | |
| 143 | <div className="flex items-center gap-2"> |
| 144 | <CheckCircleIcon className="text-success h-4 w-4" /> |
| 145 | <span className="text-success">Success</span> |
| 146 | </div> |
| 147 | <div className="flex items-center gap-2"> |
| 148 | <ExclamationCircleIcon className="text-warning h-4 w-4" /> |
| 149 | <span className="text-warning">Warning</span> |
| 150 | </div> |
| 151 | <div className="flex items-center gap-2"> |
| 152 | <ExclamationTriangleIcon className="text-error h-4 w-4" /> |
| 153 | <span className="text-error">Error</span> |
| 154 | </div> |
| 155 | |
| 156 | <div |
| 157 | onClick={() => setListToggled(!listToggled)} |
| 158 | className={`cursor-pointer p-2 ${listToggled ? "bg-list-active text-list-active-foreground" : "bg-background text-foreground hover:bg-list-hover"}`} |
| 159 | > |
| 160 | List item (clickable) |
| 161 | </div> |
| 162 | <div className="bg-editor text-editor-foreground border-border rounded-sm border border-solid p-2"> |
| 163 | <span>Editor</span> |
| 164 | </div> |
| 165 | <table> |
| 166 | <tbody> |
| 167 | <tr> |
| 168 | <td className="border-border border-b border-solid p-2"> |
| 169 | <span>even table row</span> |
| 170 | </td> |
| 171 | </tr> |
| 172 | <tr className="bg-table-oddRow"> |
| 173 | <td className="border-border border-b border-solid p-2"> |
| 174 | <span>odd table row</span> |
| 175 | </td> |
| 176 | </tr> |
| 177 | </tbody> |
| 178 | </table> |
| 179 | <div> |
| 180 | <span className="text-link">Link</span> |
| 181 | </div> |
| 182 | <div className="flex items-center gap-2"> |
| 183 | <span className="bg-findMatch">find match</span> |
| 184 | <span className="bg-findMatch-selected">current match</span> |
| 185 | </div> |
| 186 | <div className="flex items-center gap-2"> |
| 187 | <span className="">Accent</span> |
| 188 | <div className="bg-accent h-3 w-3" /> |
| 189 | </div> |
| 190 | </div> |
| 191 | <h2 className="mb-2 mt-6 text-xl font-semibold">Manage Theme</h2> |
| 192 | <h2 className="mb-2 text-lg font-semibold">Missing Theme Colors</h2> |
| 193 | {missingVars.length > 0 ? ( |
| 194 | <div className="flex flex-col gap-1"> |
| 195 | {missingVars.map((varName) => ( |
| 196 | <div key={varName} className=""> |
| 197 | {varName} |
| 198 | </div> |
| 199 | ))} |
| 200 | </div> |
| 201 | ) : ( |
| 202 | <div className="">No missing variables</div> |
| 203 | )} |
| 204 | <Button onClick={refreshColors}>Refresh Missing Colors</Button> |
| 205 | {jetbrains ? ( |
| 206 | <Button onClick={clearThemeLocalCache}>Clear Theme Cache</Button> |
| 207 | ) : null} |
| 208 | <h2 className="mb-2 mt-6 text-xl font-semibold">All Theme Colors</h2> |
| 209 | <div className="grid grid-cols-3"> |
| 210 | <div className="p-1"> |
| 211 | <span className="font-bold">Theme Color</span> |
| 212 | </div> |
| 213 | <div className="p-1"> |
| 214 | <span className="font-bold">CSS Var</span> |
| 215 | </div> |
| 216 | <div className="p-1"> |
| 217 | <span className="font-bold">Default</span> |
| 218 | </div> |
| 219 | {Object.entries(THEME_COLORS).map(([colorName, val]) => ( |
| 220 | <ThemeTailwindClassExample |
| 221 | isMissing={missingVars.includes(colorName)} |
| 222 | key={colorName} |
| 223 | colorName={colorName} |
| 224 | defaultColor={val.default} |
| 225 | varNames={val.vars} |
| 226 | /> |
| 227 | ))} |
| 228 | </div> |
| 229 | </div> |
| 230 | ); |
| 231 | } |
| 232 | export default ThemePage; |