All files / src/components/projects PublishChecklist.tsx

96.77% Statements 30/31
89.28% Branches 25/28
100% Functions 13/13
95.83% Lines 23/24

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                                                  9x 9x   9x                                                                     45x 45x   9x   9x     9x 9x   3x 1x               3x 3x     9x 4x   1x   3x       9x 1x 1x     9x                                     3x                                                     12x                                                                                                            
// Publish checklist popover — intercepts publish action and validates required/recommended fields
import { useState, useRef, useEffect } from "react";
import { Eye, CheckCircle2, Circle, AlertTriangle } from "lucide-react";
import { Button } from "../ui/button";
import { Card } from "../ui/card";
import type { Project } from "../../lib/api";
 
interface PublishChecklistProps {
	project: Project;
	photoCount: number;
	onPublish: () => void;
}
 
interface ChecklistItem {
	key: string;
	label: string;
	passed: boolean;
	required: boolean;
}
 
export function PublishChecklist({
	project,
	photoCount,
	onPublish,
}: PublishChecklistProps) {
	const [isOpen, setIsOpen] = useState(false);
	const containerRef = useRef<HTMLDivElement>(null);
 
	const checklistItems: ChecklistItem[] = [
		{
			key: "hasPhotos",
			label: "At least one photo",
			passed: photoCount > 0,
			required: true,
		},
		{
			key: "hasDescription",
			label: "Project description",
			passed: Boolean(project.description),
			required: false,
		},
		{
			key: "hasCategory",
			label: "Scope or worked area",
			passed:
				(project.scope?.length ?? 0) > 0 ||
				(project.workedAreaIds?.length ?? 0) > 0,
			required: false,
		},
		{
			key: "hasLocation",
			label: "Location",
			passed: Boolean(project.localityId),
			required: false,
		},
		{
			key: "hasBudgetRange",
			label: "Budget range",
			passed: Boolean(project.budgetRange),
			required: false,
		},
	];
 
	const requiredItems = checklistItems.filter((item) => item.required);
	const recommendedItems = checklistItems.filter((item) => !item.required);
 
	const requiredPassed = requiredItems.every((item) => item.passed);
	const allPassed =
		requiredPassed && recommendedItems.every((item) => item.passed);
 
	// Close popover when clicking outside
	useEffect(() => {
		if (!isOpen) return;
 
		const handleClickOutside = (event: MouseEvent) => {
			Iif (
				containerRef.current &&
				!containerRef.current.contains(event.target as Node)
			) {
				setIsOpen(false);
			}
		};
 
		document.addEventListener("mousedown", handleClickOutside);
		return () => document.removeEventListener("mousedown", handleClickOutside);
	}, [isOpen]);
 
	const handleButtonClick = () => {
		if (allPassed) {
			// Everything passes — publish immediately without showing popover
			onPublish();
		} else {
			setIsOpen((prev) => !prev);
		}
	};
 
	const handlePublishAnyway = () => {
		setIsOpen(false);
		onPublish();
	};
 
	return (
		<div ref={containerRef} className="relative">
			<Button
				onClick={handleButtonClick}
				className="flex-1 sm:flex-none"
			>
				<Eye className="h-4 w-4 mr-2" />
				Publish
			</Button>
 
			{isOpen && (
				<Card className="absolute right-0 top-full mt-2 w-72 z-50 p-4 shadow-lg">
					{/* Required section */}
					<div className="mb-3">
						<p className="text-xs font-semibold text-foreground-muted uppercase tracking-wider mb-2">
							Required
						</p>
						<ul className="space-y-1.5">
							{requiredItems.map((item) => (
								<li key={item.key} className="flex items-center gap-2 text-sm">
									{item.passed ? (
										<CheckCircle2 className="h-4 w-4 text-success shrink-0" />
									) : (
										<Circle className="h-4 w-4 text-foreground-muted shrink-0" />
									)}
									<span
										className={
											item.passed
												? "text-foreground-default"
												: "text-foreground-muted"
										}
									>
										{item.label}
									</span>
								</li>
							))}
						</ul>
					</div>
 
					{/* Recommended section */}
					<div className="mb-3">
						<p className="text-xs font-semibold text-foreground-muted uppercase tracking-wider mb-2">
							Recommended
						</p>
						<ul className="space-y-1.5">
							{recommendedItems.map((item) => (
								<li key={item.key} className="flex items-center gap-2 text-sm">
									{item.passed ? (
										<CheckCircle2 className="h-4 w-4 text-success shrink-0" />
									) : (
										<Circle className="h-4 w-4 text-foreground-muted shrink-0" />
									)}
									<span
										className={
											item.passed
												? "text-foreground-default"
												: "text-foreground-muted"
										}
									>
										{item.label}
									</span>
								</li>
							))}
						</ul>
					</div>
 
					<div className="border-t border-border-default pt-3">
						{!requiredPassed ? (
							// Blocker: required items failing
							<div className="flex items-start gap-2">
								<AlertTriangle className="h-4 w-4 text-warning shrink-0 mt-0.5" />
								<p className="text-sm text-foreground-muted">
									Add at least one photo before publishing.
								</p>
							</div>
						) : (
							// Required items pass but some recommended items are missing
							<div className="space-y-2">
								<div className="flex items-start gap-2">
									<AlertTriangle className="h-4 w-4 text-warning shrink-0 mt-0.5" />
									<p className="text-sm text-foreground-muted">
										Some recommended fields are missing. Complete them to
										improve visibility.
									</p>
								</div>
								<Button
									size="sm"
									className="w-full"
									onClick={handlePublishAnyway}
								>
									Publish Anyway
								</Button>
							</div>
						)}
					</div>
				</Card>
			)}
		</div>
	);
}