AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logo-box.tsx 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import { router, useForm, usePage } from '@inertiajs/react';
  2. import { Check, Sparkles, Trash2, Upload } from 'lucide-react';
  3. import type { FormEvent } from 'react';
  4. import { useState } from 'react';
  5. import StoreLogoController from '@/actions/App/Http/Controllers/StoreLogoController';
  6. import InputError from '@/components/input-error';
  7. import { Button } from '@/components/ui/button';
  8. import { Spinner } from '@/components/ui/spinner';
  9. import { cn } from '@/lib/utils';
  10. import { LogoDesign, StoreLogo } from '@/storefront/logo';
  11. import { fontStylesheetUrl, themeStyle } from '@/storefront/theme';
  12. import type {
  13. FontMap,
  14. LogoDesignKey,
  15. StoreIcon,
  16. StoreLogoData,
  17. Theme,
  18. } from '@/storefront/types';
  19. import type { Store } from '@/types';
  20. export type LogoSettings = {
  21. current: StoreLogoData | null;
  22. /** The browser tab icon made from the logo, or from the store's initials. */
  23. icon: StoreIcon;
  24. /** The words the logo shows: the store header's words. */
  25. name: string;
  26. designs: LogoDesignKey[];
  27. /** Logos the AI drew for this store, cleaned on the server. */
  28. drafts: string[];
  29. tries_left: number;
  30. tries_total: number;
  31. per_try: number;
  32. theme: Theme | null;
  33. fonts: FontMap;
  34. };
  35. /**
  36. * The Logo box in the Design tab: upload a logo, pick a ready design, or have the AI draw some.
  37. * The logo belongs to the store, so it shows on the live store right away.
  38. */
  39. export function LogoBox({
  40. store,
  41. logo,
  42. busy,
  43. }: {
  44. store: Store;
  45. logo: LogoSettings;
  46. busy: boolean;
  47. }) {
  48. const [saving, setSaving] = useState(false);
  49. const [drawing, setDrawing] = useState(false);
  50. const upload = useForm<{ logo: File | null }>({ logo: null });
  51. const errors = usePage().props.errors as Record<string, string | undefined>;
  52. const disabled = busy || saving || drawing || upload.processing;
  53. // The store's colours and fonts, so every design looks as it will in the store.
  54. const look = logo.theme ? themeStyle(logo.theme, logo.fonts) : undefined;
  55. const fontUrl = logo.theme
  56. ? fontStylesheetUrl(logo.theme, logo.fonts)
  57. : null;
  58. const choose = (data: { design: LogoDesignKey } | { drawn: number }) => {
  59. setSaving(true);
  60. router.put(StoreLogoController.choose.url(store.slug), data, {
  61. preserveScroll: true,
  62. onFinish: () => setSaving(false),
  63. });
  64. };
  65. const remove = () => {
  66. if (
  67. !window.confirm(
  68. 'Remove the logo? Your store will show its name instead.',
  69. )
  70. ) {
  71. return;
  72. }
  73. setSaving(true);
  74. router.delete(StoreLogoController.destroy.url(store.slug), {
  75. preserveScroll: true,
  76. onFinish: () => setSaving(false),
  77. });
  78. };
  79. const send = (event: FormEvent) => {
  80. event.preventDefault();
  81. upload.post(StoreLogoController.upload.url(store.slug), {
  82. preserveScroll: true,
  83. forceFormData: true,
  84. onSuccess: () => upload.reset(),
  85. });
  86. };
  87. const draw = () => {
  88. setDrawing(true);
  89. router.post(
  90. StoreLogoController.draw.url(store.slug),
  91. {},
  92. { preserveScroll: true, onFinish: () => setDrawing(false) },
  93. );
  94. };
  95. const isCurrent = (data: { design: LogoDesignKey } | { drawn: string }) =>
  96. 'design' in data
  97. ? logo.current?.type === 'design' &&
  98. logo.current.key === data.design
  99. : logo.current?.type === 'drawn' && logo.current.svg === data.drawn;
  100. return (
  101. <div data-test="logo-box">
  102. {fontUrl && <link rel="stylesheet" href={fontUrl} />}
  103. <p className="text-sm text-muted-foreground">
  104. Upload your logo, pick a ready design, or let the AI draw some.
  105. It shows on your store right away.
  106. </p>
  107. <div className="mt-5 space-y-6">
  108. {/* What the store shows now */}
  109. <div className="space-y-2">
  110. <p className="text-sm font-medium">Your logo now</p>
  111. <div className="flex flex-wrap items-center gap-3">
  112. <div
  113. style={look}
  114. className="flex min-h-20 min-w-56 items-center rounded-lg border bg-(--sf-bg,white) px-5 py-4"
  115. data-test="current-logo"
  116. >
  117. {logo.current ? (
  118. <StoreLogo
  119. logo={logo.current}
  120. name={logo.name}
  121. />
  122. ) : (
  123. <span className="text-sm text-muted-foreground">
  124. No logo yet. Your store shows its name.
  125. </span>
  126. )}
  127. </div>
  128. {logo.current && (
  129. <Button
  130. type="button"
  131. variant="ghost"
  132. size="sm"
  133. onClick={remove}
  134. disabled={disabled}
  135. >
  136. <Trash2 />
  137. Remove
  138. </Button>
  139. )}
  140. </div>
  141. <InputError message={errors.logo} />
  142. </div>
  143. {/* The browser tab */}
  144. <div className="space-y-2">
  145. <p className="text-sm font-medium">In the browser tab</p>
  146. <div
  147. className="inline-flex max-w-full items-center gap-2 rounded-t-lg border border-b-0 bg-muted/60 px-3 py-2 text-xs"
  148. data-test="tab-icon"
  149. >
  150. <img
  151. src={logo.icon.href}
  152. alt=""
  153. width={16}
  154. height={16}
  155. className="size-4 shrink-0"
  156. />
  157. <span className="truncate">{logo.name}</span>
  158. </div>
  159. <p className="text-xs text-muted-foreground">
  160. {logo.current?.type === 'upload' &&
  161. logo.icon.type === 'image/png'
  162. ? 'Made from your uploaded logo.'
  163. : logo.current?.type === 'upload'
  164. ? 'Your logo is too wide to read this small, so the tab shows your initials in your main colour. Upload a squarer logo to use it here.'
  165. : 'Your initials in your main colour. Upload a squarish logo to use it here instead.'}
  166. </p>
  167. </div>
  168. {/* Upload */}
  169. <form onSubmit={send} className="space-y-2" noValidate>
  170. <p className="text-sm font-medium">Upload your own</p>
  171. <div className="flex flex-wrap items-center gap-2">
  172. <label htmlFor="logo-file" className="sr-only">
  173. Choose a logo picture
  174. </label>
  175. <input
  176. id="logo-file"
  177. type="file"
  178. accept="image/png,image/jpeg,image/webp"
  179. onChange={(event) =>
  180. upload.setData(
  181. 'logo',
  182. event.target.files?.[0] ?? null,
  183. )
  184. }
  185. 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"
  186. />
  187. <Button
  188. size="sm"
  189. variant="outline"
  190. disabled={disabled || upload.data.logo === null}
  191. >
  192. {upload.processing ? <Spinner /> : <Upload />}
  193. Upload logo
  194. </Button>
  195. </div>
  196. <p className="text-xs text-muted-foreground">
  197. PNG, JPG or WebP, up to 2 MB. A PNG with a see-through
  198. background looks best. A wide logo suits the top of your
  199. store.
  200. </p>
  201. <InputError message={upload.errors.logo} />
  202. </form>
  203. {/* Ready designs */}
  204. {logo.theme && (
  205. <div className="space-y-2">
  206. <p className="text-sm font-medium">Ready designs</p>
  207. <p className="text-xs text-muted-foreground">
  208. Made from your store name, fonts and colours. They
  209. change with your store&apos;s look.
  210. </p>
  211. <div
  212. style={look}
  213. className="grid gap-2 sm:grid-cols-2"
  214. data-test="logo-designs"
  215. >
  216. {logo.designs.map((design) => (
  217. <LogoChoice
  218. key={design}
  219. current={isCurrent({ design })}
  220. disabled={disabled}
  221. onChoose={() => choose({ design })}
  222. >
  223. <LogoDesign
  224. design={design}
  225. name={logo.name}
  226. />
  227. </LogoChoice>
  228. ))}
  229. </div>
  230. </div>
  231. )}
  232. {/* Drawn by AI */}
  233. <div className="space-y-2">
  234. <p className="text-sm font-medium">Draw with AI</p>
  235. <p className="text-xs text-muted-foreground">
  236. The AI draws {logo.per_try} new logos for your brand
  237. each time, in your colours. You have{' '}
  238. <span
  239. className="font-medium text-foreground"
  240. data-test="logo-tries-left"
  241. >
  242. {logo.tries_left} of {logo.tries_total}
  243. </span>{' '}
  244. tries left for this store. A try that fails is not
  245. counted.
  246. </p>
  247. <Button
  248. type="button"
  249. size="sm"
  250. onClick={draw}
  251. disabled={disabled || logo.tries_left === 0}
  252. data-test="logo-draw"
  253. >
  254. {drawing ? <Spinner /> : <Sparkles />}
  255. {drawing
  256. ? 'Drawing… about 30 seconds'
  257. : logo.tries_left === 0
  258. ? 'No tries left'
  259. : `Draw ${logo.per_try} logos`}
  260. </Button>
  261. {logo.drafts.length > 0 && (
  262. <div
  263. style={look}
  264. className="grid gap-2 pt-2 sm:grid-cols-2"
  265. data-test="logo-drafts"
  266. >
  267. {logo.drafts.map((svg, index) => (
  268. <LogoChoice
  269. key={svg}
  270. current={isCurrent({ drawn: svg })}
  271. disabled={disabled}
  272. onChoose={() => choose({ drawn: index })}
  273. >
  274. <StoreLogo
  275. logo={{ type: 'drawn', svg }}
  276. name={logo.name}
  277. />
  278. </LogoChoice>
  279. ))}
  280. </div>
  281. )}
  282. </div>
  283. </div>
  284. </div>
  285. );
  286. }
  287. /** One logo to pick, on the store's background colour. */
  288. function LogoChoice({
  289. current,
  290. disabled,
  291. onChoose,
  292. children,
  293. }: {
  294. current: boolean;
  295. disabled: boolean;
  296. onChoose: () => void;
  297. children: React.ReactNode;
  298. }) {
  299. return (
  300. <button
  301. type="button"
  302. onClick={onChoose}
  303. disabled={disabled || current}
  304. aria-pressed={current}
  305. className={cn(
  306. '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',
  307. current && 'border-emerald-600 ring-2 ring-emerald-600/30',
  308. )}
  309. >
  310. {children}
  311. {current && (
  312. <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">
  313. <Check className="size-3" />
  314. In use
  315. </span>
  316. )}
  317. </button>
  318. );
  319. }