import { useWatch, type Control } from 'react-hook-form'; import { DEFAULT_BRAND_COLOR, DEFAULT_BUTTON_TEXT_COLOR } from '~/data/projects/constants'; import { useAdminData } from '~/hooks/admin/use-admin-data'; import { useObjectUrl } from '~/hooks/use-object-url'; import { fitButtonCustom } from '~/lib/projects/button-size'; import type { ProjectFormValues } from '~/schemas/projects/project-form'; import { getProjectLogoUrl } from '~/services/asset-media'; import type { ArPageLayout, BrandIdentity, ButtonStyle, Project } from '~/types/project'; // The brand as it would look with the project form's current (unsaved) values export function useProjectFormBrand(control: Control, project: Project | null) { const { settings } = useAdminData(); const [name, logo, logoBg, logoBgColor, brandColor, buttonTextColor, buttonLabel, arPage] = useWatch({ control, name: ['name', 'logo', 'logoBg', 'logoBgColor', 'brandColor', 'buttonTextColor', 'buttonLabel', 'arPage'], }); const fileUrl = useObjectUrl(logo.file); const existingUrl = project && logo.existing ? getProjectLogoUrl(project, logo.existing) : undefined; const brand: BrandIdentity = { name: name.trim() || 'Company name', logoUrl: fileUrl ?? existingUrl, logoBg, logoBgColor: logoBgColor || undefined }; const button: ButtonStyle = settings.brand_button ? { color: brandColor === DEFAULT_BRAND_COLOR ? undefined : brandColor, textColor: buttonTextColor === DEFAULT_BUTTON_TEXT_COLOR ? undefined : buttonTextColor, label: buttonLabel.trim() || undefined, size: arPage.buttonSize, custom: fitButtonCustom(arPage.buttonCustom), } : {}; const layout: ArPageLayout = arPage; return { brand, button, layout }; }