All files / src/components/projects ProjectDetailContent.tsx

95.12% Statements 39/41
66.66% Branches 16/24
100% Functions 15/15
94.87% Lines 37/39

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                                                                          163x 163x 163x 163x 163x     163x 72x 72x       163x               163x 2x 2x     163x 2x 2x 4x       163x 4x 12x 8x 4x   4x 4x   2x         163x 5x       163x     163x                 163x                               163x       652x                                                                         163x 6x           1x                                                           1x       24x                               157x                                                            
// Main presentational component for project detail page — tabbed layout
import { useState, useEffect } from "react";
import { Link, useNavigate } from "@tanstack/react-router";
import { ArrowLeft } from "lucide-react";
import { Button } from "../ui/button";
import { Breadcrumb } from "../ui/breadcrumb";
import { Tabs, TabsList, TabsTrigger, TabsContent } from "../ui/tabs";
import { ProjectHeader } from "./ProjectHeader";
import { useIsMobile } from "../../hooks/useIsMobile";
 
import { DetailsTab } from "./tabs/DetailsTab";
import { SpecificsTab } from "./tabs/SpecificsTab";
import { PhotosTab } from "./tabs/PhotosTab";
import { SeoTab } from "./tabs/SeoTab";
import type { ProjectDetailContentProps } from "./types";
import type { Project } from "../../lib/api";
 
export function ProjectDetailContent({
	project,
	pro,
	context,
	isLoading,
	isSaving,
	isUploading,
	onSave,
	onPublish,
	onArchive,
	onDelete,
	onPhotoUpload,
	onPhotoDelete,
	onPhotoReorder,
	onRefresh,
	supportsRooms,
	apiAdapter,
	backLink,
	initialTab,
}: ProjectDetailContentProps) {
	const [photos, setPhotos] = useState(project.photos || []);
	const [roomMediaCount, setRoomMediaCount] = useState(0);
	const [activeTab, setActiveTab] = useState(initialTab || "details");
	const isMobile = useIsMobile();
	const navigate = useNavigate();
 
	// Sync photos when project data changes
	useEffect(() => {
		Eif (project.photos) {
			setPhotos(project.photos);
		}
	}, [project.photos]);
 
	const PROJECT_TABS = [
		{ value: "details", label: "Details" },
		{ value: "specifics", label: "Specifics" },
		{ value: "photos", label: "Photos" },
		{ value: "seo", label: "SEO" },
	];
 
	// Photo handlers (passed to PhotosTab)
	const handlePhotoUpload = async (file: File, caption?: string) => {
		await onPhotoUpload(file, caption);
		await onRefresh();
	};
 
	const handlePhotoDelete = async (photoId: number) => {
		await onPhotoDelete(photoId);
		setPhotos((prev) =>
			prev.filter((p) => (p as { id: number }).id !== photoId),
		);
	};
 
	const handlePhotoReorder = async (photoIds: number[]) => {
		const newPhotos = photoIds
			.map((id) => photos.find((p) => (p as { id: number }).id === id))
			.filter((p): p is NonNullable<typeof p> => p !== undefined);
		setPhotos(newPhotos);
 
		try {
			await onPhotoReorder(photoIds);
		} catch {
			await onRefresh();
		}
	};
 
	// Save handler for tab modals
	const handleSave = async (data: Partial<Project>) => {
		await onSave(data);
	};
 
	// Determine proId for room operations
	const proId = project.proId;
 
	// Loading state
	Iif (isLoading) {
		return (
			<div className="flex items-center justify-center h-64">
				<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600" />
			</div>
		);
	}
 
	// Error state (no project)
	Iif (!project) {
		return (
			<div className="text-center py-12">
				<p className="text-error mb-4">Project not found</p>
				<Link to={backLink}>
					<Button variant="outline">
						<ArrowLeft className="h-4 w-4 mr-2" />
						Back to Projects
					</Button>
				</Link>
			</div>
		);
	}
 
	// Shared tab content
	const tabContent = (
		<Tabs value={activeTab} onValueChange={setActiveTab}>
			{/* Desktop: tab bar */}
			<TabsList className="hidden sm:inline-flex w-full gap-1">
				{PROJECT_TABS.map((tab) => (
					<TabsTrigger key={tab.value} value={tab.value}>
						{tab.label}
					</TabsTrigger>
				))}
			</TabsList>
 
			<TabsContent value="details">
				<DetailsTab project={project} onSave={handleSave} />
			</TabsContent>
 
			<TabsContent value="specifics">
				<SpecificsTab project={project} onSave={handleSave} />
			</TabsContent>
 
			<TabsContent value="photos">
				<PhotosTab
					project={project}
					photos={photos}
					isUploading={isUploading}
					supportsRooms={supportsRooms}
					apiAdapter={apiAdapter}
					onPhotoUpload={handlePhotoUpload}
					onPhotoDelete={handlePhotoDelete}
					onPhotoReorder={handlePhotoReorder}
					onSave={handleSave}
					proId={String(proId)}
					onTotalMediaChange={setRoomMediaCount}
				/>
			</TabsContent>
 
			<TabsContent value="seo">
				<SeoTab project={project} onSave={handleSave} />
			</TabsContent>
		</Tabs>
	);
 
	// ─── Mobile: full-screen page ───────────────────────────────────────
	if (isMobile) {
		return (
			<div className="fixed inset-0 z-[60] flex flex-col bg-background-base">
				{/* Header */}
				<div className="flex h-14 items-center gap-3 border-b border-border-default bg-background-elevated px-4 flex-shrink-0">
					<button
						type="button"
						onClick={() => navigate({ to: backLink })}
						className="p-2 -ml-2 text-foreground-muted hover:text-foreground-default rounded-md"
						aria-label="Go back"
					>
						<ArrowLeft className="h-5 w-5" />
					</button>
					<h2 className="text-lg font-semibold text-foreground-default truncate">
						{project.title}
					</h2>
					<div className="flex items-center gap-2 ml-auto">
						<ProjectHeader
							project={project}
							pro={pro}
							context={context}
							backLink={backLink}
							isSaving={isSaving}
							photoCount={photos.length + roomMediaCount > 0 ? photos.length + roomMediaCount : (project.coverImage ? 1 : 0)}
							onPublish={onPublish}
							onArchive={onArchive}
							onDelete={context === "admin" ? onDelete : undefined}
							mobileActionsOnly
						/>
					</div>
				</div>
 
				{/* Tab selector */}
				<div className="px-4 pt-3 pb-2 bg-background-base flex-shrink-0">
					<select
						aria-label="Project section"
						value={activeTab}
						onChange={(e) => setActiveTab(e.target.value)}
						className="w-full h-10 px-3 rounded-md border border-border-default bg-background-elevated text-foreground-default text-sm focus:ring-2 focus:ring-primary-500"
					>
						{PROJECT_TABS.map((tab) => (
							<option key={tab.value} value={tab.value}>
								{tab.label}
							</option>
						))}
					</select>
				</div>
 
				{/* Scrollable content */}
				<div className="flex-1 overflow-y-auto p-4">
					{tabContent}
				</div>
			</div>
		);
	}
 
	// ─── Desktop: standard layout ───────────────────────────────────────
	return (
		<div className="space-y-4 sm:space-y-6">
			{/* Breadcrumb */}
			<Breadcrumb
				items={[
					{ label: "Projects", href: backLink },
					{ label: project.title },
				]}
			/>
 
			{/* Header */}
			<ProjectHeader
				project={project}
				pro={pro}
				context={context}
				backLink={backLink}
				isSaving={isSaving}
				photoCount={photos.length + roomMediaCount > 0 ? photos.length + roomMediaCount : (project.coverImage ? 1 : 0)}
				onPublish={onPublish}
				onArchive={onArchive}
				onDelete={context === "admin" ? onDelete : undefined}
			/>
 
			{/* Tabbed content */}
			<div className="sm:max-w-4xl">
				{tabContent}
			</div>
		</div>
	);
}