| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- import { router, useForm, usePage } from '@inertiajs/react';
- import {
- Check,
- ChevronDown,
- Sparkles,
- Stamp,
- Trash2,
- Upload,
- } from 'lucide-react';
- import type { FormEvent } from 'react';
- import { useState } from 'react';
- import StoreLogoController from '@/actions/App/Http/Controllers/StoreLogoController';
- import InputError from '@/components/input-error';
- import { Button } from '@/components/ui/button';
- import { Spinner } from '@/components/ui/spinner';
- import { cn } from '@/lib/utils';
- import { LogoDesign, StoreLogo } from '@/storefront/logo';
- import { fontStylesheetUrl, themeStyle } from '@/storefront/theme';
- import type {
- FontMap,
- LogoDesignKey,
- StoreLogoData,
- Theme,
- } from '@/storefront/types';
- import type { Store } from '@/types';
-
- export type LogoSettings = {
- current: StoreLogoData | null;
- /** The words the logo shows: the store header's words. */
- name: string;
- designs: LogoDesignKey[];
- /** Logos the AI drew for this store, cleaned on the server. */
- drafts: string[];
- tries_left: number;
- tries_total: number;
- per_try: number;
- theme: Theme | null;
- fonts: FontMap;
- };
-
- /**
- * The Logo box in the Design tab: upload a logo, pick a ready design, or have the AI draw some.
- * The logo belongs to the store, so it shows on the live store right away.
- */
- export function LogoBox({
- store,
- logo,
- busy,
- }: {
- store: Store;
- logo: LogoSettings;
- busy: boolean;
- }) {
- const [saving, setSaving] = useState(false);
- const [drawing, setDrawing] = useState(false);
- const upload = useForm<{ logo: File | null }>({ logo: null });
- const errors = usePage().props.errors as Record<string, string | undefined>;
- const disabled = busy || saving || drawing || upload.processing;
-
- // The store's colours and fonts, so every design looks as it will in the store.
- const look = logo.theme ? themeStyle(logo.theme, logo.fonts) : undefined;
- const fontUrl = logo.theme
- ? fontStylesheetUrl(logo.theme, logo.fonts)
- : null;
-
- const choose = (data: { design: LogoDesignKey } | { drawn: number }) => {
- setSaving(true);
- router.put(StoreLogoController.choose.url(store.slug), data, {
- preserveScroll: true,
- onFinish: () => setSaving(false),
- });
- };
-
- const remove = () => {
- if (
- !window.confirm(
- 'Remove the logo? Your store will show its name instead.',
- )
- ) {
- return;
- }
-
- setSaving(true);
- router.delete(StoreLogoController.destroy.url(store.slug), {
- preserveScroll: true,
- onFinish: () => setSaving(false),
- });
- };
-
- const send = (event: FormEvent) => {
- event.preventDefault();
- upload.post(StoreLogoController.upload.url(store.slug), {
- preserveScroll: true,
- forceFormData: true,
- onSuccess: () => upload.reset(),
- });
- };
-
- const draw = () => {
- setDrawing(true);
- router.post(
- StoreLogoController.draw.url(store.slug),
- {},
- { preserveScroll: true, onFinish: () => setDrawing(false) },
- );
- };
-
- const isCurrent = (data: { design: LogoDesignKey } | { drawn: string }) =>
- 'design' in data
- ? logo.current?.type === 'design' &&
- logo.current.key === data.design
- : logo.current?.type === 'drawn' && logo.current.svg === data.drawn;
-
- return (
- <details className="group rounded-xl border p-4" data-test="logo-box">
- {fontUrl && <link rel="stylesheet" href={fontUrl} />}
- <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
- <Stamp className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
- <div>
- <h3 className="font-medium">Logo</h3>
- <p className="mt-0.5 text-sm text-muted-foreground">
- Upload your logo, pick a ready design, or let the AI
- draw some. It shows on your store right away.
- </p>
- </div>
- <ChevronDown
- className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
- aria-hidden="true"
- />
- </summary>
-
- <div className="mt-5 space-y-6">
- {/* What the store shows now */}
- <div className="space-y-2">
- <p className="text-sm font-medium">Your logo now</p>
- <div className="flex flex-wrap items-center gap-3">
- <div
- style={look}
- className="flex min-h-20 min-w-56 items-center rounded-lg border bg-(--sf-bg,white) px-5 py-4"
- data-test="current-logo"
- >
- {logo.current ? (
- <StoreLogo
- logo={logo.current}
- name={logo.name}
- />
- ) : (
- <span className="text-sm text-muted-foreground">
- No logo yet. Your store shows its name.
- </span>
- )}
- </div>
- {logo.current && (
- <Button
- type="button"
- variant="ghost"
- size="sm"
- onClick={remove}
- disabled={disabled}
- >
- <Trash2 />
- Remove
- </Button>
- )}
- </div>
- <InputError message={errors.logo} />
- </div>
-
- {/* Upload */}
- <form onSubmit={send} className="space-y-2" noValidate>
- <p className="text-sm font-medium">Upload your own</p>
- <div className="flex flex-wrap items-center gap-2">
- <label htmlFor="logo-file" className="sr-only">
- Choose a logo picture
- </label>
- <input
- id="logo-file"
- type="file"
- accept="image/png,image/jpeg,image/webp"
- onChange={(event) =>
- upload.setData(
- 'logo',
- event.target.files?.[0] ?? null,
- )
- }
- className="max-w-full text-sm file:mr-3 file:rounded-md file:border file:bg-transparent file:px-3 file:py-1.5 file:text-sm file:font-medium"
- />
- <Button
- size="sm"
- variant="outline"
- disabled={disabled || upload.data.logo === null}
- >
- {upload.processing ? <Spinner /> : <Upload />}
- Upload logo
- </Button>
- </div>
- <p className="text-xs text-muted-foreground">
- PNG, JPG or WebP, up to 2 MB. A PNG with a see-through
- background looks best. A wide logo suits the top of your
- store.
- </p>
- <InputError message={upload.errors.logo} />
- </form>
-
- {/* Ready designs */}
- {logo.theme && (
- <div className="space-y-2">
- <p className="text-sm font-medium">Ready designs</p>
- <p className="text-xs text-muted-foreground">
- Made from your store name, fonts and colours. They
- change with your store's look.
- </p>
- <div
- style={look}
- className="grid gap-2 sm:grid-cols-2"
- data-test="logo-designs"
- >
- {logo.designs.map((design) => (
- <LogoChoice
- key={design}
- current={isCurrent({ design })}
- disabled={disabled}
- onChoose={() => choose({ design })}
- >
- <LogoDesign
- design={design}
- name={logo.name}
- />
- </LogoChoice>
- ))}
- </div>
- </div>
- )}
-
- {/* Drawn by AI */}
- <div className="space-y-2">
- <p className="text-sm font-medium">Draw with AI</p>
- <p className="text-xs text-muted-foreground">
- The AI draws {logo.per_try} new logos for your brand
- each time, in your colours. You have{' '}
- <span
- className="font-medium text-foreground"
- data-test="logo-tries-left"
- >
- {logo.tries_left} of {logo.tries_total}
- </span>{' '}
- tries left for this store. A try that fails is not
- counted.
- </p>
- <Button
- type="button"
- size="sm"
- onClick={draw}
- disabled={disabled || logo.tries_left === 0}
- data-test="logo-draw"
- >
- {drawing ? <Spinner /> : <Sparkles />}
- {drawing
- ? 'Drawing… about 30 seconds'
- : logo.tries_left === 0
- ? 'No tries left'
- : `Draw ${logo.per_try} logos`}
- </Button>
-
- {logo.drafts.length > 0 && (
- <div
- style={look}
- className="grid gap-2 pt-2 sm:grid-cols-2"
- data-test="logo-drafts"
- >
- {logo.drafts.map((svg, index) => (
- <LogoChoice
- key={svg}
- current={isCurrent({ drawn: svg })}
- disabled={disabled}
- onChoose={() => choose({ drawn: index })}
- >
- <StoreLogo
- logo={{ type: 'drawn', svg }}
- name={logo.name}
- />
- </LogoChoice>
- ))}
- </div>
- )}
- </div>
- </div>
- </details>
- );
- }
-
- /** One logo to pick, on the store's background colour. */
- function LogoChoice({
- current,
- disabled,
- onChoose,
- children,
- }: {
- current: boolean;
- disabled: boolean;
- onChoose: () => void;
- children: React.ReactNode;
- }) {
- return (
- <button
- type="button"
- onClick={onChoose}
- disabled={disabled || current}
- aria-pressed={current}
- className={cn(
- 'relative flex min-h-20 items-center justify-center overflow-hidden rounded-lg border bg-(--sf-bg) px-4 py-4 transition hover:border-foreground/40 disabled:cursor-default',
- current && 'border-emerald-600 ring-2 ring-emerald-600/30',
- )}
- >
- {children}
- {current && (
- <span className="absolute top-1.5 right-1.5 flex items-center gap-1 rounded-full bg-emerald-600 px-1.5 py-0.5 text-[10px] font-medium text-white">
- <Check className="size-3" />
- In use
- </span>
- )}
- </button>
- );
- }
|