All files / src/pages profile.tsx

92.59% Statements 25/27
79.06% Branches 34/43
60% Functions 3/5
92.59% Lines 25/27

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                                                  1x   1x                   13x           13x                 13x                   13x   13x 13x 13x 13x 13x 13x   13x 13x 13x     13x       13x   13x       13x 2x             11x 1x             10x                                                                       60x                                                         60x                                                                                                                              
import { useState } from "react";
import { useSearch } from "@tanstack/react-router";
import { usePro } from "../lib/pro-context";
import {
	useTaxonomyData,
	useLocalitiesByCity,
} from "../hooks/queries/useTaxonomyQueries";
import { useProjects } from "../hooks/queries/useProjectQueries";
import type { MaterialTag, Brand } from "../lib/api";
import { User } from "lucide-react";
import { StickyPageHeader } from "../components/ui/sticky-page-header";
import {
	Tabs,
	TabsList,
	TabsTrigger,
	TabsContent,
} from "../components/ui/tabs";
import { ProfileProgressBar } from "../components/profile/ProfileProgressBar";
import { BusinessBasicsTab } from "../components/profile/tabs/BusinessBasicsTab";
import { ServicesTab } from "../components/profile/tabs/ServicesTab";
import { DeliveryPricingTab } from "../components/profile/tabs/DeliveryPricingTab";
import { SocialContactTab } from "../components/profile/tabs/SocialContactTab";
import { TeamCredentialsTab } from "../components/profile/tabs/TeamCredentialsTab";
import { SeoTab } from "../components/profile/tabs/SeoTab";
 
const VALID_TABS = ["basics", "services", "delivery", "social", "team", "seo"];
 
const profileTabs = [
	{ value: "basics", label: "Business Info" },
	{ value: "services", label: "Services & Expertise" },
	{ value: "delivery", label: "Pricing & Delivery" },
	{ value: "social", label: "Contact & Media" },
	{ value: "team", label: "Team & Credentials" },
	{ value: "seo", label: "SEO" },
];
 
export function ProfilePage() {
	const search = useSearch({ from: "/_proLayout/profile" });
	const {
		pro,
		proId,
		proRole,
		isLoading: proLoading,
	} = usePro();
 
	// #362: staff could see Edit buttons but the PUT /api/pro/profile route
	// requires `requireProManager` so saves 403'd. The backend is correct;
	// the frontend should not show an affordance it can't honor. Owner,
	// manager, and platform admin (the middleware sets proRole="admin" when
	// a platform admin acts on a pro — see auth.middleware.ts:180) all pass
	// the API gate.
	const canEditProfile =
		proRole === "owner" || proRole === "manager" || proRole === "admin";
 
	const {
		isLoading: isLoadingTaxonomy,
		businessTypes: businessTypesRaw,
		customerSegments: customerSegmentsRaw,
		serviceCategories: serviceCategoriesRaw,
		materialTags: materialTagsRaw,
		brands: brandsRaw,
		cities: citiesRaw,
	} = useTaxonomyData();
 
	const businessTypes = businessTypesRaw ?? [];
	const customerSegments = customerSegmentsRaw ?? [];
	const serviceCategories = serviceCategoriesRaw ?? [];
	const materialTags: Record<string, MaterialTag[]> = materialTagsRaw ?? {};
	const brands: Record<string, Brand[]> = brandsRaw ?? {};
	const cities = citiesRaw ?? [];
 
	const initialTab = VALID_TABS.includes(search.tab) ? search.tab : "basics";
	const [activeTab, setActiveTab] = useState(initialTab);
	const [cityIdForLocalities, setCityIdForLocalities] = useState(
		pro?.cityId || "",
	);
	const { data: localities = [] } = useLocalitiesByCity(
		cityIdForLocalities || pro?.cityId || null,
	);
 
	const { data: projects } = useProjects(proId);
 
	const handleTabChange = (newTab: string) => {
		setActiveTab(newTab);
	};
 
	if (proLoading || isLoadingTaxonomy) {
		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>
		);
	}
 
	if (!pro || !proId) {
		return (
			<div className="p-4 text-sm text-notification-error-text bg-notification-error-bg border border-notification-error-border rounded-lg">
				No pro profile found.
			</div>
		);
	}
 
	return (
		<div className="space-y-6">
				<StickyPageHeader>
					<div className="flex flex-col gap-2 w-full">
						<div className="flex items-center gap-2 min-w-0">
							<h1 className="text-xl sm:text-2xl font-bold text-foreground-default flex items-center gap-2 shrink-0">
								<User className="h-5 w-5 sm:h-6 sm:w-6" />
								Profile
							</h1>
							{pro.status === "draft" && (
								<span className="inline-flex items-center px-2 py-0.5 text-xs font-semibold rounded-full bg-amber-100 text-amber-800 border border-amber-300 shrink-0">
									Draft
								</span>
							)}
							{pro.status === "published" && (
								<span className="inline-flex items-center px-2 py-0.5 text-xs font-semibold rounded-full bg-green-100 text-green-800 border border-green-300 shrink-0">
									Published
								</span>
							)}
							{pro.status === "archived" && (
								<span className="inline-flex items-center px-2 py-0.5 text-xs font-semibold rounded-full bg-gray-100 text-gray-600 border border-gray-300 shrink-0">
									Archived
								</span>
							)}
							<div className="ml-auto">
								<ProfileProgressBar pro={pro} projectCount={projects?.length ?? 0} />
							</div>
						</div>
						{/* Mobile: sticky dropdown in header */}
						<select
							aria-label="Profile section"
							value={activeTab}
							onChange={(e) => handleTabChange(e.target.value)}
							className="sm:hidden w-full h-9 px-3 rounded-md border border-border-default bg-background-elevated text-foreground-default text-sm focus:ring-2 focus:ring-primary-500"
						>
							{profileTabs.map((tab) => (
								<option key={tab.value} value={tab.value}>
									{tab.label}
								</option>
							))}
						</select>
					</div>
				</StickyPageHeader>
 
				<div className="max-w-6xl mx-auto space-y-6">
				{!canEditProfile && (
					<p
						role="note"
						className="text-xs text-amber-700 bg-amber-50 border border-amber-200 rounded-lg px-3 py-2"
					>
						Profile editing is limited to owners and managers. You can view
						everything here; ask an owner to update any details that need changing.
					</p>
				)}
				{/* Draft: mobile-only email message (hidden on desktop where it's in header) */}
				{pro.status === "draft" && (
					<p className="md:hidden text-xs text-amber-700 bg-amber-50 border border-amber-200 rounded-lg px-3 py-2">
						Email <a href="mailto:srini@interioring.com" className="text-primary-600 hover:underline font-medium">srini@interioring.com</a> to get your profile approved
					</p>
				)}
 
				<Tabs value={activeTab} onValueChange={handleTabChange}>
					{/* Desktop: tab bar (mobile uses sticky header dropdown) */}
					<TabsList className="hidden sm:inline-flex w-full gap-1">
						{profileTabs.map((tab) => (
							<TabsTrigger key={tab.value} value={tab.value}>
								{tab.label}
							</TabsTrigger>
						))}
					</TabsList>
 
					<TabsContent value="basics">
						<BusinessBasicsTab
							pro={pro}
							proId={proId}
							cities={cities}
							localities={localities}
							onCityChange={setCityIdForLocalities}
							canEdit={canEditProfile}
						/>
					</TabsContent>
 
					<TabsContent value="services">
						<ServicesTab
							pro={pro}
							proId={proId}
							businessTypes={businessTypes}
							serviceCategories={serviceCategories}
							materialTags={materialTags}
							brands={brands}
							canEdit={canEditProfile}
						/>
					</TabsContent>
 
					<TabsContent value="delivery">
						<DeliveryPricingTab
							pro={pro}
							proId={proId}
							customerSegments={customerSegments}
							serviceCategories={serviceCategories}
							canEdit={canEditProfile}
						/>
					</TabsContent>
 
					<TabsContent value="social">
						<SocialContactTab
							pro={pro}
							proId={proId}
							canEdit={canEditProfile}
						/>
					</TabsContent>
 
					<TabsContent value="team">
						<TeamCredentialsTab proId={proId} canEdit={canEditProfile} />
					</TabsContent>
 
					<TabsContent value="seo">
						<SeoTab
							pro={pro}
							proId={proId}
							canEdit={canEditProfile}
						/>
					</TabsContent>
				</Tabs>
				</div>
		</div>
	);
}