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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | 52x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 8x 11x 11x 11x 11x 8x 3x 8x 8x 8x 8x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 1x 1x 1x 1x 1x 11x 11x 13x 1x 1x 1x 1x 1x 1x 1x 1x 13x | import { useState, useRef, useEffect, useCallback } from "react";
import {
X,
MoreVertical,
Wand2,
Pen,
Trash2,
Plus,
Download,
Upload,
FileCode2,
Code2,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useToast } from "@/hooks/use-toast";
import { TabBar } from "@/components/ui/tab-bar";
import { AppTab, AppTabMain } from "@/components/ui/app-tab";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
DropdownMenuSeparator,
} from "@/components/ui/dropdown-menu";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogFooter,
} from "@/components/ui/dialog";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { UnifiedScrollArea } from "@/components/ui/unified-scroll-area";
interface Tab {
id: string;
name: string;
path?: string;
content: string;
}
interface SketchTabsProps {
readonly tabs: Tab[];
readonly activeTabId: string | null;
readonly modifiedTabId: string | null;
readonly onTabClick: (tabId: string) => void;
readonly onTabClose: (tabId: string) => void;
readonly onTabRename: (tabId: string, newName: string) => void;
readonly onTabAdd: () => void;
readonly onFilesLoaded?: (
files: Array<{ name: string; path?: string; content: string }>,
replaceAll: boolean,
) => void;
readonly onFormatCode?: () => void;
readonly onDownloadAllFiles?: () => void;
readonly examplesMenu?: React.ReactNode;
readonly loadFilesTriggerRef?: React.MutableRefObject<(() => void) | null>;
readonly renameTriggerRef?: React.MutableRefObject<(() => void) | null>;
}
function getDisplayFileName(path: string): string {
return path.split(/[\\/]/).pop() || path;
}
export function SketchTabs({
tabs,
activeTabId,
modifiedTabId,
onTabClick,
onTabClose,
onTabRename,
onTabAdd,
onFilesLoaded,
onFormatCode,
onDownloadAllFiles,
examplesMenu,
loadFilesTriggerRef,
renameTriggerRef,
}: SketchTabsProps) {
const { toast } = useToast();
const [renamingTabId, setRenamingTabId] = useState<string | null>(null);
const [newName, setNewName] = useState("");
const [isRenameDialogOpen, setIsRenameDialogOpen] = useState(false);
const [deleteConfirmTabId, setDeleteConfirmTabId] = useState<string | null>(
null,
);
const [isReplaceConfirmOpen, setIsReplaceConfirmOpen] = useState(false);
const [pendingFilesToLoad, setPendingFilesToLoad] = useState<Array<{
name: string;
content: string;
}> | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
// Focus and select text in input when renaming starts
Iif (renamingTabId && inputRef.current) {
inputRef.current.focus();
inputRef.current.select();
}
}, [renamingTabId]);
const handleRenameStart = (tabId: string, currentName: string) => {
setRenamingTabId(tabId);
// Remove file extension for display
const nameWithoutExtension = currentName.slice(
0,
Math.max(0, currentName.lastIndexOf(".")),
);
setNewName(nameWithoutExtension);
};
const handleRenameStartDialog = useCallback(
(tabId: string, currentName: string) => {
setRenamingTabId(tabId);
// Remove file extension for display
const nameWithoutExtension = currentName.slice(
0,
Math.max(0, currentName.lastIndexOf(".")),
);
setNewName(nameWithoutExtension);
setIsRenameDialogOpen(true);
},
[],
);
const openRenameDialog = useCallback(() => {
if (!activeTabId) {
toast({
title: "No file selected",
description: "Open a file/tab first to rename.",
});
return;
}
const activeTab = tabs.find((tab) => tab.id === activeTabId);
if (activeTab) handleRenameStartDialog(activeTabId, activeTab.name);
}, [activeTabId, handleRenameStartDialog, tabs, toast]);
useEffect(() => {
if (loadFilesTriggerRef) {
loadFilesTriggerRef.current = () => fileInputRef.current?.click();
}
if (renameTriggerRef) renameTriggerRef.current = openRenameDialog;
return () => {
if (loadFilesTriggerRef) loadFilesTriggerRef.current = null;
if (renameTriggerRef) renameTriggerRef.current = null;
};
}, [loadFilesTriggerRef, openRenameDialog, renameTriggerRef]);
const handleRenameSave = () => {
if (newName.trim() && renamingTabId) {
const currentTab = tabs.find((t) => t.id === renamingTabId);
if (currentTab) {
// Extract the file extension
const extension = currentTab.name.slice(
Math.max(0, currentTab.name.lastIndexOf(".")),
);
// Remove extension from new name if user included it
let baseName = newName.trim();
if (baseName.endsWith(extension)) {
baseName = baseName.slice(0, Math.max(0, baseName.length - extension.length));
}
// Combine base name with original extension
const finalName = baseName + extension;
onTabRename(renamingTabId, finalName);
}
setIsRenameDialogOpen(false);
}
setRenamingTabId(null);
setNewName("");
};
const handleRenameCancel = () => {
setIsRenameDialogOpen(false);
setRenamingTabId(null);
setNewName("");
};
const handleDeleteConfirm = () => {
if (deleteConfirmTabId) {
onTabClose(deleteConfirmTabId);
setDeleteConfirmTabId(null);
}
};
const handleFileUpload = async (
event: React.ChangeEvent<HTMLInputElement>,
) => {
const files = event.target.files;
Iif (!files || files.length === 0) return;
try {
const loadedFiles: Array<{ name: string; path?: string; content: string }> = [];
let inoCount = 0;
// Read all files
for (const file of files) {
const extension = file.name.slice(Math.max(0, file.name.lastIndexOf(".")));
// Only allow .ino and .h files
Iif (![".ino", ".h"].includes(extension)) {
toast({
title: "Unsupported File Type",
description: `"${file.name}" is not supported. Please only upload .ino and .h files.`,
variant: "destructive",
});
return;
}
Eif (extension === ".ino") {
inoCount++;
}
// Read file content
const content = await file.text();
loadedFiles.push({
name: file.name,
path: file.webkitRelativePath || file.name,
content: content,
});
}
// Multiple .ino files not allowed
Iif (inoCount > 1) {
toast({
title: "Too many .ino files",
description: "Only one .ino file can be loaded.",
variant: "destructive",
});
return;
}
// If .ino file is present, show confirmation dialog
if (inoCount === 1) {
setPendingFilesToLoad(loadedFiles);
setIsReplaceConfirmOpen(true);
} else E{
// Only .h files: add them directly
if (onFilesLoaded) {
onFilesLoaded(loadedFiles, false); // false = don't replace all
}
toast({
title: "Header files loaded",
description: `${loadedFiles.length} header file(s) added`,
});
}
} catch (error) {
toast({
title: "Error Loading",
description: error instanceof Error ? error.message : "Unknown error",
variant: "destructive",
});
}
// Reset file input
Eif (fileInputRef.current) {
fileInputRef.current.value = "";
}
};
const handleReplaceConfirmYes = () => {
Eif (pendingFilesToLoad && onFilesLoaded) {
onFilesLoaded(pendingFilesToLoad, true); // true = replace all
toast({
title: "Sketch replaced",
description: `${pendingFilesToLoad.length} file(s) loaded`,
});
}
setIsReplaceConfirmOpen(false);
setPendingFilesToLoad(null);
};
const handleReplaceConfirmNo = () => {
// Only add .h files, skip the .ino
if (pendingFilesToLoad && onFilesLoaded) {
const hFiles = pendingFilesToLoad.filter((f) => f.name.endsWith(".h"));
if (hFiles.length > 0) {
onFilesLoaded(hFiles, false); // false = don't replace all
toast({
title: "Header files loaded",
description: `${hFiles.length} header file(s) added`,
});
} else {
toast({
title: "No header files",
description: "Only .ino files were present.",
});
}
}
setIsReplaceConfirmOpen(false);
setPendingFilesToLoad(null);
};
return (
<TabBar
className="relative flex items-center unified-tab-bar--panel border-b px-0"
style={{ height: "var(--ui-header-height)" }}
>
{/* Tabs container with overflow */}
<UnifiedScrollArea
orientation="horizontal"
className="flex-1 h-full"
viewportClassName="flex items-center overflow-y-hidden"
viewportProps={{ style: { scrollBehavior: "smooth", touchAction: "pan-x" } }}
>
{tabs.map((tab) => (
<AppTab
key={tab.id}
active={activeTabId === tab.id}
style={{
maxWidth: "200px",
}}
>
{renamingTabId === tab.id ? (
<div className="px-3 flex items-center" style={{ height: "var(--ui-header-height)" }}>
<Input
ref={inputRef}
value={newName}
onChange={(e) => setNewName(e.target.value)}
onKeyDown={(e) => {
e.stopPropagation();
if (e.key === "Enter") {
handleRenameSave();
} else if (e.key === "Escape") {
handleRenameCancel();
}
}}
onBlur={handleRenameSave}
className="ui-type-tab w-24 px-2 py-1"
style={{
height: "var(--ui-button-height)",
}}
/>
</div>
) : (
<>
<AppTabMain
className={
activeTabId === tab.id
? "text-foreground"
: "text-muted-foreground"
}
style={{
flex: "1",
minWidth: "0",
}}
onClick={() => onTabClick(tab.id)}
tabIndex={0}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
onTabClick(tab.id);
} else Eif (e.key === "F2") {
e.preventDefault();
handleRenameStart(tab.id, tab.name);
}
}}
onDoubleClick={() => handleRenameStart(tab.id, tab.name)}
aria-label={`${getDisplayFileName(tab.name)}${modifiedTabId === tab.id ? ", modified" : ""}`}
title={`${getDisplayFileName(tab.name)}${modifiedTabId === tab.id ? ", modified" : ""}`}
>
{getDisplayFileName(tab.name).toLowerCase().endsWith(".ino") ? (
<Code2 className="h-3 w-3 flex-shrink-0 text-teal-600" strokeWidth={2} />
) : (
<FileCode2 className="h-3 w-3 flex-shrink-0 text-purple-400" strokeWidth={2} />
)}
<span
className="whitespace-nowrap overflow-hidden text-ellipsis"
style={{
height: "var(--ui-button-height)",
display: "flex",
alignItems: "center",
minWidth: "0",
flex: "1",
}}
>
{getDisplayFileName(tab.name)}
{modifiedTabId === tab.id && <span className="ml-1 flex-shrink-0">•</span>}
</span>
</AppTabMain>
{tabs[0]?.id !== tab.id && (
<Button
variant="ghost"
size="icon"
className="opacity-0 group-hover:opacity-100 data-[active=true]:opacity-100 transition-opacity"
data-active={activeTabId === tab.id}
onClick={(e) => {
e.stopPropagation();
setDeleteConfirmTabId(tab.id);
}}
title="Close file"
>
<X className="h-3 w-3" />
</Button>
)}
</>
)}
</AppTab>
))}
{/* Context Menu Button */}
<div className="flex items-center px-2 flex-shrink-0">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="icon"
title="Options"
>
<MoreVertical className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={onTabAdd}>
<Plus className="h-4 w-4 mr-2" />
New File
</DropdownMenuItem>
{onFormatCode && (
<DropdownMenuItem onClick={onFormatCode}>
<Wand2 className="h-4 w-4 mr-2" />
Format Code
</DropdownMenuItem>
)}
{activeTabId && (
<DropdownMenuItem
onClick={() => {
if (renameTriggerRef?.current) {
renameTriggerRef.current();
} else E{
openRenameDialog();
}
}}
>
<Pen className="h-4 w-4 mr-2" />
Rename
</DropdownMenuItem>
)}
{activeTabId && tabs[0]?.id !== activeTabId && (
<DropdownMenuItem
onClick={() => setDeleteConfirmTabId(activeTabId)}
className="text-destructive"
>
<Trash2 className="h-4 w-4 mr-2" />
Delete File
</DropdownMenuItem>
)}
<DropdownMenuSeparator />
<DropdownMenuItem onClick={() => fileInputRef.current?.click()}>
<Upload className="h-4 w-4 mr-2" />
Load Files
</DropdownMenuItem>
<DropdownMenuItem onClick={onDownloadAllFiles}>
<Download className="h-4 w-4 mr-2" />
Save All Files
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
{/* Hidden file input */}
<input
ref={fileInputRef}
type="file"
multiple
accept=".ino,.h"
onChange={handleFileUpload}
style={{ display: "none" }}
/>
{/* Examples Menu - Right-aligned */}
<div className="ml-auto flex items-center px-2 flex-shrink-0">
{examplesMenu}
</div>
</UnifiedScrollArea>
{/* Delete Confirmation Dialog */}
<AlertDialog
open={deleteConfirmTabId !== null}
onOpenChange={(open) => {
if (!open) setDeleteConfirmTabId(null);
}}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete File?</AlertDialogTitle>
<AlertDialogDescription>
Are you sure you want to delete "
{tabs.find((t) => t.id === deleteConfirmTabId)?.name}"? This
action cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={handleDeleteConfirm}
className="bg-destructive text-destructive-foreground"
>
Delete
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{/* Rename Dialog */}
<Dialog open={isRenameDialogOpen} onOpenChange={setIsRenameDialogOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Rename File</DialogTitle>
</DialogHeader>
<Input
autoFocus
value={newName}
onChange={(e) => setNewName(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
handleRenameSave();
} else if (e.key === "Escape") {
handleRenameCancel();
}
}}
placeholder="Enter new name..."
/>
<DialogFooter>
<Button variant="outline" onClick={handleRenameCancel}>
Cancel
</Button>
<Button onClick={handleRenameSave}>Rename</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{/* Replace Sketch Confirmation Dialog */}
<AlertDialog
open={isReplaceConfirmOpen}
onOpenChange={setIsReplaceConfirmOpen}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Sketch ersetzen?</AlertDialogTitle>
<AlertDialogDescription>
Es wurde eine .ino-Datei zum Laden ausgewählt. Möchtest du den
aktuellen Sketch ersetzen?
<br />
<br />
<strong>Ja:</strong> Der aktuelle Sketch wird vollständig ersetzt
<br />
<strong>Nein:</strong> Nur die Header-Dateien (.h) werden
hinzugefügt
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={handleReplaceConfirmNo}>Nein (nur .h Dateien)</AlertDialogCancel>
<AlertDialogAction
onClick={handleReplaceConfirmYes}
className="bg-blue-600 hover:bg-blue-700"
>
Ja (Sketch ersetzen)
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</TabBar>
);
}
|