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

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