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.8KB

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