Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 2x 2x 3x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 24x 24x 24x 24x 24x 24x 24x 24x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x | import {JSX, useEffect, useState} from "react";
import {getCurrentUser, signInWithRedirect, signOut} from "aws-amplify/auth";
import {CircleUser, Coffee, Contrast, Menu, Monitor, Moon, Palette, Sparkles, Sun, ZoomIn, ZoomOut} from "lucide-react";
import {ThemeName, useTheme} from "../hooks/use-theme";
import {ProfileIcon} from "../assets/icons";
import Alert from "./Alert";
import Help from "./Help.tsx";
interface ThemeOption {
name: ThemeName;
label: string;
icon: JSX.Element;
}
// Define available themes with their icons
const themeOptions: ThemeOption[] = [
{
name: "system",
label: "System",
icon: <Monitor className="w-4 h-4 text-gray-500"/>,
},
{
name: "light",
label: "Light",
icon: <Sun className="w-4 h-4 text-yellow-500"/>,
},
{
name: "dark",
label: "Dark",
icon: <Moon className="w-4 h-4 text-blue-200"/>,
},
{
name: "cupcake",
label: "Cupcake",
icon: <Coffee className="w-4 h-4 text-pink-300"/>,
},
{
name: "cyberpunk",
label: "Cyberpunk",
icon: <Sparkles className="w-4 h-4 text-purple-400"/>,
},
{
name: "corporate",
label: "Corporate",
icon: <CircleUser className="w-4 h-4 text-blue-400"/>,
},
{
name: "forest",
label: "Forest",
icon: <Contrast className="w-4 h-4 text-green-600"/>,
},
{
name: "aqua",
label: "Aqua",
icon: <Palette className="w-4 h-4 text-cyan-500"/>,
},
];
export interface ToolbarProps {
/**
* For test mocking purposes.
*/
authenticated?: boolean;
}
function Toolbar(props: ToolbarProps) {
const [isAuthenticated, setIsAuthenticated] = useState(props.authenticated ?? false);
const {theme, setTheme, textSize, setTextSize} = useTheme();
const [showSignInAlert, setShowSignInAlert] = useState(false);
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
const [isMobileView, setIsMobileView] = useState(false);
// Check screen size and set mobile view state
useEffect(() => {
function checkScreenSize() {
setIsMobileView(window.innerWidth < 640); // Use sm breakpoint (640px)
}
// Check on mount
checkScreenSize();
// Add resize listener
window.addEventListener("resize", checkScreenSize);
// Clean up
return () => window.removeEventListener("resize", checkScreenSize);
}, []);
useEffect(() => {
async function handleRedirect() {
try {
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get("code");
if (code) {
await checkAuthStatus();
}
} catch (error) {
console.error("Error handling redirect:", error);
}
}
// noinspection JSIgnoredPromiseFromCall
handleRedirect();
// noinspection JSIgnoredPromiseFromCall
checkAuthStatus();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
async function checkAuthStatus() {
if (props.authenticated) {
return;
}
try {
const user = await getCurrentUser();
const hasUser = !!user;
if (hasUser
&& !isAuthenticated
&& localStorage.shownSignInAlert !== "true"
) {
setShowSignInAlert(true);
localStorage.shownSignInAlert = "true";
}
setIsAuthenticated(hasUser);
} catch (error) {
setIsAuthenticated(false);
}
}
async function handleSignIn() {
try {
await signInWithRedirect({
provider: {
custom: "MicrosoftEntraID",
},
});
} catch (error) {
console.error("Error signing in:", error);
}
}
async function handleSignOut() {
try {
const savedTheme = localStorage.getItem("app-theme");
const savedTextSize = localStorage.getItem("app-text-size");
await signOut({
global: true,
});
localStorage.clear();
sessionStorage.clear();
if (savedTheme) {
localStorage.setItem("app-theme", savedTheme);
}
if (savedTextSize) {
localStorage.setItem("app-text-size", savedTextSize);
}
setIsAuthenticated(false);
} catch (error) {
console.error("Error signing out:", error);
}
}
// Handle theme selection
function handleThemeChange(newTheme: ThemeName) {
setTheme(newTheme);
}
// Function to increase text size by clicking on the ZoomIn icon
function increaseTextSize() {
const newSize = Math.min(24, textSize + 1);
setTextSize(newSize);
}
// Function to decrease text size by clicking on the ZoomOut icon
function decreaseTextSize() {
const newSize = Math.max(10, textSize - 1);
setTextSize(newSize);
}
// Toggle mobile menu
function toggleMobileMenu() {
setMobileMenuOpen(!mobileMenuOpen);
}
// Desktop toolbar component
function DesktopToolbar() {
return (
<div className="navbar-end flex space-x-2">
{/* Text Size Controls with Tooltip */}
<div className="dropdown dropdown-end flex items-center">
<div className="tooltip tooltip-primary tooltip-bottom" data-tip={`Text Size`}>
<div className="flex items-center gap-2 btn btn-ghost">
<button
className="btn btn-ghost btn-sm p-1"
onClick={decreaseTextSize}
>
<ZoomOut className="w-5 h-5"/>
</button>
<span className="text-sm">{textSize}px</span>
<button
className="btn btn-ghost btn-sm p-1"
onClick={increaseTextSize}
>
<ZoomIn className="w-5 h-5"/>
</button>
</div>
</div>
</div>
{/* Theme Dropdown */}
<div className="dropdown dropdown-end">
<label tabIndex={0} className="btn btn-ghost btn-circle">
<Palette className="w-5 h-5"/>
</label>
<ul
tabIndex={0}
className="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-52 mt-1"
>
{themeOptions.map((option) => (
<li key={option.name}>
<a
className={theme === option.name ? "active font-bold" : ""}
onClick={() => handleThemeChange(option.name)}
>
{option.icon}
{option.label}
</a>
</li>
))}
</ul>
</div>
{/* Help Modal Button */}
<Help/>
{/* Log In or Sign Out Button */}
{isAuthenticated ? (
<button
className="btn btn-error"
aria-label="Sign out button"
onClick={handleSignOut}
>
<ProfileIcon className="mr-2 h-4 w-4"/>
Sign out
</button>
) : (
<button
className="btn btn-primary"
aria-label="Login button"
onClick={handleSignIn}
>
<ProfileIcon className="mr-2 h-4 w-4"/>
Login
</button>
)}
</div>
);
}
// Mobile toolbar component
function MobileToolbarButton() {
return (
<div className="navbar-end">
<button className="btn btn-ghost" onClick={toggleMobileMenu}>
<Menu className="w-6 h-6"/>
</button>
</div>
);
}
// Mobile menu component with buttons for text size
function MobileMenu() {
return (
<div className="bg-base-100 shadow-md p-4 flex flex-col gap-4">
{/* Text Size Controls for Mobile */}
<div className="flex items-center justify-between">
<span>
Text Size
</span>
<div className="flex items-center">
<button className="btn btn-ghost btn-sm" onClick={decreaseTextSize}>
<ZoomOut className="w-4 h-4"/>
</button>
<span className="mx-2">
{textSize}px
</span>
<button className="btn btn-ghost btn-sm" onClick={increaseTextSize}>
<ZoomIn className="w-4 h-4"/>
</button>
</div>
</div>
{/* Theme Selection for Mobile */}
<div className="flex flex-col gap-2">
<span>
Theme
</span>
<div className="grid grid-cols-3 gap-2">
{themeOptions.map((option) => (
<button
key={option.name}
className={`btn btn-sm ${theme === option.name ? "btn-primary" : "btn-ghost"}`}
onClick={() => handleThemeChange(option.name)}
>
{option.icon}
<span className="text-xs">
{option.label}
</span>
</button>
))}
</div>
</div>
{/* Help Button for Mobile */}
<div className="flex justify-center">
<Help/>
</div>
{/* Auth Button for Mobile */}
<div className="flex justify-center">
{isAuthenticated ? (
<button
className="btn btn-error w-full"
aria-label="Sign out button"
onClick={handleSignOut}
>
<ProfileIcon className="mr-2 h-4 w-4"/>
Sign out
</button>
) : (
<button
className="btn btn-primary w-full"
aria-label="Login button"
onClick={handleSignIn}
>
<ProfileIcon className="mr-2 h-4 w-4"/>
Login
</button>
)}
</div>
</div>
);
}
return (
<>
<div className="navbar bg-base-100 shadow-md p-4">
<div className="navbar-start">
<h1
className="normal-case text-3xl font-bold"
aria-label="App title"
>
Conversate.
</h1>
</div>
{/* Render either mobile or desktop toolbar based on screen size */}
{isMobileView ? <MobileToolbarButton/> : <DesktopToolbar/>}
</div>
{/* Mobile menu dropdown - only appears when mobileMenuOpen is true AND in mobile view */}
{mobileMenuOpen && isMobileView && <MobileMenu/>}
{/* Success Alert */}
<Alert
message="Successfully signed in!"
isVisible={showSignInAlert}
onDismiss={() => setShowSignInAlert(false)}
autoDismissTime={3000}
/>
</>
);
}
export default Toolbar;
|