feat: add project logo field
This commit is contained in:
parent
1ba3d33a61
commit
3a44c02099
79
app/components/projects/form/logo-field.tsx
Normal file
79
app/components/projects/form/logo-field.tsx
Normal file
@ -0,0 +1,79 @@
|
||||
import { useState } from 'react';
|
||||
import { useFormContext, useWatch } from 'react-hook-form';
|
||||
import { Upload } from 'lucide-react';
|
||||
import { Button } from '~/components/ui/button';
|
||||
import { DropZone } from '~/components/ui/drop-zone';
|
||||
import { Field } from '~/components/ui/field';
|
||||
import { LogoTile } from '~/components/ui/logo-tile';
|
||||
import { Text } from '~/components/ui/text';
|
||||
import { TextPair } from '~/components/ui/text-pair';
|
||||
import { useAdminData } from '~/hooks/admin/use-admin-data';
|
||||
import { fileTypeLabel, formatFileSize } from '~/lib/format';
|
||||
import { logoAccept, logoFormatsLabel, prepareLogoFile } from '~/lib/projects/logo-file';
|
||||
import type { ProjectFormValues } from '~/schemas/projects/project-form';
|
||||
import type { BrandIdentity } from '~/types/project';
|
||||
|
||||
export function LogoField({ brand }: { brand: BrandIdentity }) {
|
||||
const { settings } = useAdminData();
|
||||
const { control, setValue, trigger, formState } = useFormContext<ProjectFormValues>();
|
||||
const logo = useWatch({ control, name: 'logo' });
|
||||
const [fileError, setFileError] = useState<string>();
|
||||
const hasLogo = !!logo.file || !!logo.existing;
|
||||
|
||||
const update = (next: ProjectFormValues['logo']) => {
|
||||
setValue('logo', next, { shouldDirty: true });
|
||||
if (formState.isSubmitted) trigger('logo');
|
||||
};
|
||||
|
||||
const onFile = async (file: File) => {
|
||||
setFileError(undefined);
|
||||
const result = await prepareLogoFile(file, settings.allow_svg_logos);
|
||||
if ('error' in result) setFileError(result.error);
|
||||
else update({ file: result.file, existing: logo.existing });
|
||||
};
|
||||
|
||||
return (
|
||||
<Field
|
||||
label='Company logo'
|
||||
requirement={settings.logo_required ? 'required' : 'optional'}
|
||||
error={fileError ?? formState.errors.logo?.message}
|
||||
hint={!hasLogo && !settings.logo_required ? 'No logo yet? The company name is shown as text instead.' : undefined}
|
||||
>
|
||||
<DropZone accept={logoAccept(settings.allow_svg_logos)} layout={hasLogo ? 'row' : 'stack'} filled={hasLogo} invalid={!!fileError} onFile={onFile}>
|
||||
{hasLogo ? (
|
||||
<div className='flex items-center gap-3.5'>
|
||||
<LogoTile brand={brand} size='field' />
|
||||
<TextPair
|
||||
className='flex-1'
|
||||
title={logo.file?.name ?? logo.existing}
|
||||
meta={logo.file ? `${formatFileSize(logo.file.size)} · ${fileTypeLabel(logo.file)}` : 'Current logo'}
|
||||
/>
|
||||
<Button type='button' variant='ghost' size='sm' className='text-text-emphasis'>
|
||||
Replace
|
||||
</Button>
|
||||
<Button
|
||||
type='button'
|
||||
variant='ghost'
|
||||
size='sm'
|
||||
className='text-danger hover:text-danger'
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
update({ file: null, existing: '' });
|
||||
}}
|
||||
>
|
||||
Remove
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={18} className='text-text-secondary' />
|
||||
<Text>
|
||||
<Text as='span' variant='label' tone='primary'>Click to upload</Text> or drag a file here
|
||||
</Text>
|
||||
<Text variant='meta'>{logoFormatsLabel(settings.allow_svg_logos)} · up to 2 MB · transparent background recommended</Text>
|
||||
</>
|
||||
)}
|
||||
</DropZone>
|
||||
</Field>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user