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.

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