feat: add hook for the project form's unsaved brand
This commit is contained in:
parent
a72f76f814
commit
f9ff79c88d
32
app/hooks/projects/use-project-form-brand.ts
Normal file
32
app/hooks/projects/use-project-form-brand.ts
Normal file
@ -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<ProjectFormValues>, 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 };
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user