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.

layout-picker.tsx 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { router } from '@inertiajs/react';
  2. import { ChevronDown, LayoutTemplate } from 'lucide-react';
  3. import { useState } from 'react';
  4. import SectionLayoutController from '@/actions/App/Http/Controllers/SectionLayoutController';
  5. import type { LayoutSection, Store } from '@/types';
  6. const fieldClass =
  7. 'h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:opacity-50 md:text-sm';
  8. /** Section types and layouts in plain words. Unknown ones fall back to their code name. */
  9. const typeNames: Record<string, string> = {
  10. header: 'Header',
  11. trust_bar: 'Trust row',
  12. shop_by: 'Shop by collection',
  13. bento: 'Bento tiles',
  14. video: 'Video',
  15. product_grid: 'Products',
  16. featured_product: 'Featured product',
  17. about: 'About',
  18. testimonials: 'Reviews',
  19. faq: 'FAQ',
  20. newsletter: 'Newsletter',
  21. contact: 'Contact',
  22. footer: 'Footer',
  23. };
  24. const layoutNames: Record<string, Record<string, string>> = {
  25. header: { simple: 'Logo on the left', centered: 'Logo in the middle' },
  26. trust_bar: { row: 'Icon row', boxed: 'Small cards' },
  27. shop_by: { circles: 'Round photos', tiles: 'Photo tiles' },
  28. bento: { mosaic: 'One big tile and smaller ones', even: 'Equal tiles' },
  29. video: { wide: 'Wide video', split: 'Words beside the video' },
  30. product_grid: {
  31. grid: 'Grid',
  32. minimal: 'Centred grid',
  33. carousel: 'Sliding row',
  34. large: 'Large cards',
  35. },
  36. featured_product: {
  37. 'image-left': 'Photo on the left',
  38. 'image-right': 'Photo on the right',
  39. stacked: 'Wide photo on top',
  40. },
  41. about: {
  42. simple: 'Centred text',
  43. 'with-stats': 'Text with numbers',
  44. statement: 'One big statement',
  45. },
  46. testimonials: {
  47. cards: 'Cards',
  48. single: 'One big quote',
  49. wall: 'Wall of quotes',
  50. },
  51. faq: {
  52. list: 'Fold-open list',
  53. 'two-column': 'Two columns',
  54. cards: 'All answers in cards',
  55. },
  56. newsletter: {
  57. banner: 'Colour banner',
  58. card: 'Colour card',
  59. split: 'Heading beside the form',
  60. },
  61. contact: { simple: 'Simple', card: 'Card' },
  62. footer: { simple: 'One line', centered: 'Centred', columns: 'Columns' },
  63. };
  64. /**
  65. * The Design tab's Layouts box: the layout of every section, header and footer included.
  66. * Heroes have their own box.
  67. */
  68. export default function LayoutPicker({
  69. store,
  70. sections,
  71. options,
  72. busy,
  73. }: {
  74. store: Store;
  75. sections: LayoutSection[];
  76. options: Record<string, string[]>;
  77. busy: boolean;
  78. }) {
  79. const [saving, setSaving] = useState<string | null>(null);
  80. if (sections.length === 0) {
  81. return null;
  82. }
  83. function choose(section: LayoutSection, variant: string) {
  84. router.put(
  85. SectionLayoutController.update.url({
  86. slug: store.slug,
  87. sectionId: section.section_id,
  88. }),
  89. { variant },
  90. {
  91. preserveScroll: true,
  92. onStart: () => setSaving(section.section_id),
  93. onFinish: () => setSaving(null),
  94. },
  95. );
  96. }
  97. return (
  98. <details
  99. className="group rounded-xl border p-4"
  100. data-test="layout-picker"
  101. >
  102. <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
  103. <LayoutTemplate className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
  104. <div>
  105. <h3 className="font-medium">Layouts</h3>
  106. <p className="mt-0.5 text-sm text-muted-foreground">
  107. How each part of your store is arranged. Open to change
  108. one.
  109. </p>
  110. </div>
  111. <ChevronDown
  112. className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
  113. aria-hidden="true"
  114. />
  115. </summary>
  116. <ul className="mt-4 divide-y">
  117. {sections.map((section) => {
  118. const id = `layout-${section.section_id}`;
  119. const names = layoutNames[section.type] ?? {};
  120. return (
  121. <li
  122. key={section.section_id}
  123. className="grid gap-2 py-3 sm:grid-cols-[1fr_220px] sm:items-center"
  124. >
  125. <label htmlFor={id} className="min-w-0 text-sm">
  126. <span className="font-medium">
  127. {typeNames[section.type] ?? section.type}
  128. </span>
  129. {section.label &&
  130. !['header', 'footer'].includes(
  131. section.type,
  132. ) && (
  133. <span className="text-muted-foreground">
  134. {' '}
  135. · “{section.label}”
  136. </span>
  137. )}
  138. {section.page_id &&
  139. section.page_id !== 'home' && (
  140. <span className="text-muted-foreground">
  141. {' '}
  142. on {section.page_title}
  143. </span>
  144. )}
  145. </label>
  146. <select
  147. id={id}
  148. value={section.variant}
  149. disabled={busy || saving === section.section_id}
  150. onChange={(event) =>
  151. choose(section, event.target.value)
  152. }
  153. className={fieldClass}
  154. >
  155. {(options[section.type] ?? []).map(
  156. (variant) => (
  157. <option key={variant} value={variant}>
  158. {names[variant] ?? variant}
  159. </option>
  160. ),
  161. )}
  162. </select>
  163. </li>
  164. );
  165. })}
  166. </ul>
  167. </details>
  168. );
  169. }