From f9ff79c88d45efa94dc3a4e55e92b3c9092b9eb5 Mon Sep 17 00:00:00 2001 From: rajun Date: Tue, 27 Oct 2026 16:28:53 +0530 Subject: [PATCH] feat: add hook for the project form's unsaved brand --- app/hooks/projects/use-project-form-brand.ts | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/hooks/projects/use-project-form-brand.ts diff --git a/app/hooks/projects/use-project-form-brand.ts b/app/hooks/projects/use-project-form-brand.ts new file mode 100644 index 0000000..69f1388 --- /dev/null +++ b/app/hooks/projects/use-project-form-brand.ts @@ -0,0 +1,32 @@ +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 }; +}