refactor: Introduce useBoardBackgroundSettings hook for managing board background settings with persistence

- Refactored BoardBackgroundModal to utilize the new useBoardBackgroundSettings hook, improving code organization and reusability.
- Updated methods for setting board background, card opacity, column opacity, and other settings to include server persistence.
- Enhanced error handling and user feedback with toast notifications for successful and failed operations.
- Added keyboard shortcut support for selecting folders in FileBrowserDialog, improving user experience.
- Improved KanbanCard component layout and added dropdown menu for editing and viewing model information.
This commit is contained in:
Test User
2025-12-20 11:27:39 -05:00
parent f367db741a
commit 532d03c231
4 changed files with 294 additions and 55 deletions

View File

@@ -15,6 +15,7 @@ import { Checkbox } from "@/components/ui/checkbox";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { useAppStore, defaultBackgroundSettings } from "@/store/app-store"; import { useAppStore, defaultBackgroundSettings } from "@/store/app-store";
import { getHttpApiClient } from "@/lib/http-api-client"; import { getHttpApiClient } from "@/lib/http-api-client";
import { useBoardBackgroundSettings } from "@/hooks/use-board-background-settings";
import { toast } from "sonner"; import { toast } from "sonner";
const ACCEPTED_IMAGE_TYPES = [ const ACCEPTED_IMAGE_TYPES = [
@@ -35,9 +36,8 @@ export function BoardBackgroundModal({
open, open,
onOpenChange, onOpenChange,
}: BoardBackgroundModalProps) { }: BoardBackgroundModalProps) {
const { currentProject, boardBackgroundByProject } = useAppStore();
const { const {
currentProject,
boardBackgroundByProject,
setBoardBackground, setBoardBackground,
setCardOpacity, setCardOpacity,
setColumnOpacity, setColumnOpacity,
@@ -47,7 +47,7 @@ export function BoardBackgroundModal({
setCardBorderOpacity, setCardBorderOpacity,
setHideScrollbar, setHideScrollbar,
clearBoardBackground, clearBoardBackground,
} = useAppStore(); } = useBoardBackgroundSettings();
const [isDragOver, setIsDragOver] = useState(false); const [isDragOver, setIsDragOver] = useState(false);
const [isProcessing, setIsProcessing] = useState(false); const [isProcessing, setIsProcessing] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);
@@ -139,8 +139,8 @@ export function BoardBackgroundModal({
); );
if (result.success && result.path) { if (result.success && result.path) {
// Update store with the relative path (live update) // Update store and persist to server
setBoardBackground(currentProject.path, result.path); await setBoardBackground(currentProject.path, result.path);
toast.success("Background image saved"); toast.success("Background image saved");
} else { } else {
toast.error(result.error || "Failed to save background image"); toast.error(result.error || "Failed to save background image");
@@ -214,7 +214,7 @@ export function BoardBackgroundModal({
); );
if (result.success) { if (result.success) {
clearBoardBackground(currentProject.path); await clearBoardBackground(currentProject.path);
setPreviewImage(null); setPreviewImage(null);
toast.success("Background image cleared"); toast.success("Background image cleared");
} else { } else {
@@ -228,59 +228,59 @@ export function BoardBackgroundModal({
} }
}, [currentProject, clearBoardBackground]); }, [currentProject, clearBoardBackground]);
// Live update opacity when sliders change // Live update opacity when sliders change (with persistence)
const handleCardOpacityChange = useCallback( const handleCardOpacityChange = useCallback(
(value: number[]) => { async (value: number[]) => {
if (!currentProject) return; if (!currentProject) return;
setCardOpacity(currentProject.path, value[0]); await setCardOpacity(currentProject.path, value[0]);
}, },
[currentProject, setCardOpacity] [currentProject, setCardOpacity]
); );
const handleColumnOpacityChange = useCallback( const handleColumnOpacityChange = useCallback(
(value: number[]) => { async (value: number[]) => {
if (!currentProject) return; if (!currentProject) return;
setColumnOpacity(currentProject.path, value[0]); await setColumnOpacity(currentProject.path, value[0]);
}, },
[currentProject, setColumnOpacity] [currentProject, setColumnOpacity]
); );
const handleColumnBorderToggle = useCallback( const handleColumnBorderToggle = useCallback(
(checked: boolean) => { async (checked: boolean) => {
if (!currentProject) return; if (!currentProject) return;
setColumnBorderEnabled(currentProject.path, checked); await setColumnBorderEnabled(currentProject.path, checked);
}, },
[currentProject, setColumnBorderEnabled] [currentProject, setColumnBorderEnabled]
); );
const handleCardGlassmorphismToggle = useCallback( const handleCardGlassmorphismToggle = useCallback(
(checked: boolean) => { async (checked: boolean) => {
if (!currentProject) return; if (!currentProject) return;
setCardGlassmorphism(currentProject.path, checked); await setCardGlassmorphism(currentProject.path, checked);
}, },
[currentProject, setCardGlassmorphism] [currentProject, setCardGlassmorphism]
); );
const handleCardBorderToggle = useCallback( const handleCardBorderToggle = useCallback(
(checked: boolean) => { async (checked: boolean) => {
if (!currentProject) return; if (!currentProject) return;
setCardBorderEnabled(currentProject.path, checked); await setCardBorderEnabled(currentProject.path, checked);
}, },
[currentProject, setCardBorderEnabled] [currentProject, setCardBorderEnabled]
); );
const handleCardBorderOpacityChange = useCallback( const handleCardBorderOpacityChange = useCallback(
(value: number[]) => { async (value: number[]) => {
if (!currentProject) return; if (!currentProject) return;
setCardBorderOpacity(currentProject.path, value[0]); await setCardBorderOpacity(currentProject.path, value[0]);
}, },
[currentProject, setCardBorderOpacity] [currentProject, setCardBorderOpacity]
); );
const handleHideScrollbarToggle = useCallback( const handleHideScrollbarToggle = useCallback(
(checked: boolean) => { async (checked: boolean) => {
if (!currentProject) return; if (!currentProject) return;
setHideScrollbar(currentProject.path, checked); await setHideScrollbar(currentProject.path, checked);
}, },
[currentProject, setHideScrollbar] [currentProject, setHideScrollbar]
); );

View File

@@ -208,13 +208,31 @@ export function FileBrowserDialog({
} }
}; };
const handleSelect = () => { const handleSelect = useCallback(() => {
if (currentPath) { if (currentPath) {
addRecentFolder(currentPath); addRecentFolder(currentPath);
onSelect(currentPath); onSelect(currentPath);
onOpenChange(false); onOpenChange(false);
} }
}; }, [currentPath, onSelect, onOpenChange]);
// Handle Command/Ctrl+Enter keyboard shortcut to select current folder
useEffect(() => {
if (!open) return;
const handleKeyDown = (e: KeyboardEvent) => {
// Check for Command+Enter (Mac) or Ctrl+Enter (Windows/Linux)
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
if (currentPath && !loading) {
handleSelect();
}
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [open, currentPath, loading, handleSelect]);
// Helper to get folder name from path // Helper to get folder name from path
const getFolderName = (path: string) => { const getFolderName = (path: string) => {
@@ -399,9 +417,12 @@ export function FileBrowserDialog({
<Button variant="ghost" size="sm" onClick={() => onOpenChange(false)}> <Button variant="ghost" size="sm" onClick={() => onOpenChange(false)}>
Cancel Cancel
</Button> </Button>
<Button size="sm" onClick={handleSelect} disabled={!currentPath || loading}> <Button size="sm" onClick={handleSelect} disabled={!currentPath || loading} title="Select current folder (Cmd+Enter / Ctrl+Enter)">
<FolderOpen className="w-3.5 h-3.5 mr-1.5" /> <FolderOpen className="w-3.5 h-3.5 mr-1.5" />
Select Current Folder Select Current Folder
<kbd className="ml-2 px-1.5 py-0.5 text-[10px] bg-background/50 rounded border border-border">
{typeof navigator !== "undefined" && navigator.platform?.includes("Mac") ? "⌘" : "Ctrl"}+
</kbd>
</Button> </Button>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>

View File

@@ -483,21 +483,54 @@ export const KanbanCard = memo(function KanbanCard({
)} )}
> >
{isCurrentAutoTask && ( {isCurrentAutoTask && (
<div className="absolute top-2 right-2 flex items-center justify-center gap-2 bg-[var(--status-in-progress)]/15 border border-[var(--status-in-progress)]/50 rounded-md px-2 py-0.5"> <div className="absolute top-2 right-2 flex items-center gap-1">
<Loader2 className="w-3.5 h-3.5 text-[var(--status-in-progress)] animate-spin" /> <div className="flex items-center justify-center gap-2 bg-[var(--status-in-progress)]/15 border border-[var(--status-in-progress)]/50 rounded-md px-2 py-0.5">
<span className="text-[10px] text-[var(--status-in-progress)] font-medium"> <Loader2 className="w-3.5 h-3.5 text-[var(--status-in-progress)] animate-spin" />
{formatModelName(feature.model ?? DEFAULT_MODEL)} {feature.startedAt && (
</span> <CountUpTimer
{feature.startedAt && ( startedAt={feature.startedAt}
<CountUpTimer className="text-[var(--status-in-progress)] text-[10px]"
startedAt={feature.startedAt} />
className="text-[var(--status-in-progress)] text-[10px]" )}
/> </div>
)} <DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
size="sm"
className="h-6 w-6 p-0 hover:bg-muted/80 rounded-md"
onClick={(e) => e.stopPropagation()}
onPointerDown={(e) => e.stopPropagation()}
data-testid={`menu-running-${feature.id}`}
>
<MoreVertical className="w-3.5 h-3.5 text-muted-foreground" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-36">
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
onEdit();
}}
data-testid={`edit-running-${feature.id}`}
className="text-xs"
>
<Edit className="w-3 h-3 mr-2" />
Edit
</DropdownMenuItem>
{/* Model info in dropdown */}
<div className="px-2 py-1.5 text-[10px] text-muted-foreground border-t mt-1 pt-1.5">
<div className="flex items-center gap-1">
<Cpu className="w-3 h-3" />
<span>{formatModelName(feature.model ?? DEFAULT_MODEL)}</span>
</div>
</div>
</DropdownMenuContent>
</DropdownMenu>
</div> </div>
)} )}
{!isCurrentAutoTask && feature.status === "backlog" && ( {!isCurrentAutoTask && feature.status === "backlog" && (
<div className="absolute bottom-1 right-2"> <div className="absolute top-2 right-2">
<Button <Button
variant="ghost" variant="ghost"
size="sm" size="sm"
@@ -554,8 +587,6 @@ export const KanbanCard = memo(function KanbanCard({
<FileText className="w-4 h-4" /> <FileText className="w-4 h-4" />
</Button> </Button>
)} )}
</div>
<div className="absolute bottom-1 right-2">
<Button <Button
variant="ghost" variant="ghost"
size="sm" size="sm"
@@ -577,7 +608,21 @@ export const KanbanCard = memo(function KanbanCard({
)} )}
{!isCurrentAutoTask && feature.status === "in_progress" && ( {!isCurrentAutoTask && feature.status === "in_progress" && (
<> <>
<div className="absolute top-2 right-2"> <div className="absolute top-2 right-2 flex items-center gap-1">
<Button
variant="ghost"
size="sm"
className="h-6 w-6 p-0 hover:bg-white/10 text-muted-foreground hover:text-destructive"
onClick={(e) => {
e.stopPropagation();
handleDeleteClick(e);
}}
onPointerDown={(e) => e.stopPropagation()}
data-testid={`delete-feature-${feature.id}`}
title="Delete"
>
<Trash2 className="w-4 h-4" />
</Button>
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
<Button <Button
@@ -616,25 +661,16 @@ export const KanbanCard = memo(function KanbanCard({
View Logs View Logs
</DropdownMenuItem> </DropdownMenuItem>
)} )}
{/* Model info in dropdown */}
<div className="px-2 py-1.5 text-[10px] text-muted-foreground border-t mt-1 pt-1.5">
<div className="flex items-center gap-1">
<Cpu className="w-3 h-3" />
<span>{formatModelName(feature.model ?? DEFAULT_MODEL)}</span>
</div>
</div>
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>
</div> </div>
<div className="absolute bottom-1 right-2">
<Button
variant="ghost"
size="sm"
className="h-6 w-6 p-0 hover:bg-white/10 text-muted-foreground hover:text-destructive"
onClick={(e) => {
e.stopPropagation();
handleDeleteClick(e);
}}
onPointerDown={(e) => e.stopPropagation()}
data-testid={`delete-feature-${feature.id}`}
title="Delete"
>
<Trash2 className="w-4 h-4" />
</Button>
</div>
</> </>
)} )}
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">

View File

@@ -0,0 +1,182 @@
import { useCallback } from "react";
import { useAppStore } from "@/store/app-store";
import { getHttpApiClient } from "@/lib/http-api-client";
import { toast } from "sonner";
/**
* Hook for managing board background settings with automatic persistence to server
*/
export function useBoardBackgroundSettings() {
const store = useAppStore();
const httpClient = getHttpApiClient();
// Helper to persist settings to server
const persistSettings = useCallback(
async (projectPath: string, settingsToUpdate: Record<string, unknown>) => {
try {
const result = await httpClient.settings.updateProject(
projectPath,
{
boardBackground: settingsToUpdate,
}
);
if (!result.success) {
console.error("Failed to persist settings:", result.error);
toast.error("Failed to save settings");
}
} catch (error) {
console.error("Failed to persist settings:", error);
toast.error("Failed to save settings");
}
},
[httpClient]
);
// Get current background settings for a project
const getCurrentSettings = useCallback(
(projectPath: string) => {
const current = store.boardBackgroundByProject[projectPath];
return current || {
imagePath: null,
cardOpacity: 100,
columnOpacity: 100,
columnBorderEnabled: true,
cardGlassmorphism: true,
cardBorderEnabled: true,
cardBorderOpacity: 100,
hideScrollbar: false,
};
},
[store.boardBackgroundByProject]
);
// Persisting wrappers for store actions
const setBoardBackground = useCallback(
async (projectPath: string, imagePath: string | null) => {
// Get current settings first
const current = getCurrentSettings(projectPath);
// Prepare the updated settings
const toUpdate = {
...current,
imagePath,
imageVersion: imagePath ? Date.now() : undefined,
};
// Update local store
store.setBoardBackground(projectPath, imagePath);
// Persist to server
await persistSettings(projectPath, toUpdate);
},
[store, persistSettings, getCurrentSettings]
);
const setCardOpacity = useCallback(
async (projectPath: string, opacity: number) => {
const current = getCurrentSettings(projectPath);
store.setCardOpacity(projectPath, opacity);
await persistSettings(projectPath, { ...current, cardOpacity: opacity });
},
[store, persistSettings, getCurrentSettings]
);
const setColumnOpacity = useCallback(
async (projectPath: string, opacity: number) => {
const current = getCurrentSettings(projectPath);
store.setColumnOpacity(projectPath, opacity);
await persistSettings(projectPath, { ...current, columnOpacity: opacity });
},
[store, persistSettings, getCurrentSettings]
);
const setColumnBorderEnabled = useCallback(
async (projectPath: string, enabled: boolean) => {
const current = getCurrentSettings(projectPath);
store.setColumnBorderEnabled(projectPath, enabled);
await persistSettings(projectPath, {
...current,
columnBorderEnabled: enabled,
});
},
[store, persistSettings, getCurrentSettings]
);
const setCardGlassmorphism = useCallback(
async (projectPath: string, enabled: boolean) => {
const current = getCurrentSettings(projectPath);
store.setCardGlassmorphism(projectPath, enabled);
await persistSettings(projectPath, {
...current,
cardGlassmorphism: enabled,
});
},
[store, persistSettings, getCurrentSettings]
);
const setCardBorderEnabled = useCallback(
async (projectPath: string, enabled: boolean) => {
const current = getCurrentSettings(projectPath);
store.setCardBorderEnabled(projectPath, enabled);
await persistSettings(projectPath, {
...current,
cardBorderEnabled: enabled,
});
},
[store, persistSettings, getCurrentSettings]
);
const setCardBorderOpacity = useCallback(
async (projectPath: string, opacity: number) => {
const current = getCurrentSettings(projectPath);
store.setCardBorderOpacity(projectPath, opacity);
await persistSettings(projectPath, {
...current,
cardBorderOpacity: opacity,
});
},
[store, persistSettings, getCurrentSettings]
);
const setHideScrollbar = useCallback(
async (projectPath: string, hide: boolean) => {
const current = getCurrentSettings(projectPath);
store.setHideScrollbar(projectPath, hide);
await persistSettings(projectPath, { ...current, hideScrollbar: hide });
},
[store, persistSettings, getCurrentSettings]
);
const clearBoardBackground = useCallback(
async (projectPath: string) => {
store.clearBoardBackground(projectPath);
// Clear the boardBackground settings
await persistSettings(projectPath, {
imagePath: null,
imageVersion: undefined,
cardOpacity: 100,
columnOpacity: 100,
columnBorderEnabled: true,
cardGlassmorphism: true,
cardBorderEnabled: true,
cardBorderOpacity: 100,
hideScrollbar: false,
});
},
[store, persistSettings]
);
return {
setBoardBackground,
setCardOpacity,
setColumnOpacity,
setColumnBorderEnabled,
setCardGlassmorphism,
setCardBorderEnabled,
setCardBorderOpacity,
setHideScrollbar,
clearBoardBackground,
getCurrentSettings,
};
}