AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

design-chat.tsx 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { Form, router } from '@inertiajs/react';
  2. import { CircleAlert, CircleCheck, Send } from 'lucide-react';
  3. import { useState } from 'react';
  4. import StoreGenerationController from '@/actions/App/Http/Controllers/StoreGenerationController';
  5. import InputError from '@/components/input-error';
  6. import { Button } from '@/components/ui/button';
  7. import { Spinner } from '@/components/ui/spinner';
  8. import type { Store, StoreGeneration, UsageCount } from '@/types';
  9. const fieldClass =
  10. 'w-full rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-xs outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50';
  11. /**
  12. * Ask the AI for a small change in plain words, and see the last few requests.
  13. */
  14. export default function DesignChat({
  15. store,
  16. edits,
  17. usage,
  18. busy,
  19. }: {
  20. store: Store;
  21. edits: StoreGeneration[];
  22. usage: UsageCount;
  23. busy: boolean;
  24. }) {
  25. const left = Math.max(0, usage.limit - usage.used);
  26. const disabled = busy || left === 0;
  27. return (
  28. <div className="space-y-4 rounded-xl border p-4">
  29. <div className="flex flex-wrap items-baseline justify-between gap-2">
  30. <h3 className="font-medium">Ask the AI</h3>
  31. <p className="text-xs text-muted-foreground">
  32. {left} of {usage.limit} AI edits left
  33. </p>
  34. </div>
  35. <Form
  36. {...StoreGenerationController.edit.form(store.slug)}
  37. resetOnSuccess
  38. options={{ preserveScroll: true }}
  39. className="space-y-2"
  40. >
  41. {({ processing, errors }) => (
  42. <>
  43. <label htmlFor="instruction" className="sr-only">
  44. Describe the change you want
  45. </label>
  46. <textarea
  47. id="instruction"
  48. name="instruction"
  49. required
  50. rows={2}
  51. minLength={3}
  52. maxLength={500}
  53. disabled={disabled}
  54. className={fieldClass}
  55. placeholder="e.g. Make the main colour dark green. Or: Create a 10% code RAYA10 until the end of the month. Or: Set the 100ml moisturiser to RM 52."
  56. />
  57. <InputError message={errors.instruction} />
  58. <div className="flex flex-wrap items-center justify-between gap-2">
  59. <p className="text-xs text-muted-foreground">
  60. {left === 0
  61. ? 'You have used all the AI edits in your plan.'
  62. : 'One change at a time works best.'}
  63. </p>
  64. <Button
  65. size="sm"
  66. disabled={disabled || processing}
  67. data-test="send-edit-button"
  68. >
  69. <Send />
  70. Send
  71. </Button>
  72. </div>
  73. </>
  74. )}
  75. </Form>
  76. {edits.length > 0 && (
  77. <ul
  78. className="space-y-3 border-t pt-4"
  79. data-test="edit-history"
  80. >
  81. {edits.map((edit) => (
  82. <li key={edit.id} className="flex gap-3 text-sm">
  83. <span className="mt-0.5 shrink-0">
  84. {edit.status === 'completed' ? (
  85. <CircleCheck className="size-4 text-green-600 dark:text-green-400" />
  86. ) : edit.status === 'failed' ? (
  87. <CircleAlert className="size-4 text-red-600 dark:text-red-400" />
  88. ) : (
  89. <Spinner />
  90. )}
  91. </span>
  92. <div className="min-w-0">
  93. <p className="break-words">
  94. {edit.instruction}
  95. </p>
  96. <p className="mt-0.5 break-words text-muted-foreground">
  97. {edit.status === 'completed'
  98. ? (edit.reply ?? 'Done.')
  99. : edit.status === 'failed'
  100. ? (edit.error ??
  101. 'This change failed.')
  102. : 'Working on it…'}
  103. </p>
  104. {edit.status === 'completed' &&
  105. edit.actions.length > 0 && (
  106. <ProposedActions
  107. store={store}
  108. edit={edit}
  109. />
  110. )}
  111. {edit.status === 'completed' &&
  112. edit.changed === false &&
  113. edit.actions.length === 0 && (
  114. <p
  115. className="mt-1 text-xs font-medium text-amber-700 dark:text-amber-400"
  116. data-test="edit-no-change"
  117. >
  118. No change was made to your design.
  119. </p>
  120. )}
  121. </div>
  122. </li>
  123. ))}
  124. </ul>
  125. )}
  126. </div>
  127. );
  128. }
  129. /**
  130. * Store changes the AI prepared from the chat. Nothing happens until the merchant confirms.
  131. */
  132. function ProposedActions({
  133. store,
  134. edit,
  135. }: {
  136. store: Store;
  137. edit: StoreGeneration;
  138. }) {
  139. const [busy, setBusy] = useState(false);
  140. const pending = edit.actions_status === 'pending';
  141. function send(action: 'confirm' | 'cancel') {
  142. const route =
  143. action === 'confirm'
  144. ? StoreGenerationController.confirm
  145. : StoreGenerationController.cancel;
  146. router.post(
  147. route.url({ slug: store.slug, generation: edit.id }),
  148. {},
  149. {
  150. preserveScroll: true,
  151. onStart: () => setBusy(true),
  152. onFinish: () => setBusy(false),
  153. },
  154. );
  155. }
  156. return (
  157. <div
  158. className="mt-2 space-y-2 rounded-lg border bg-muted/40 p-3"
  159. data-test="proposed-actions"
  160. >
  161. <p className="text-xs font-medium text-muted-foreground">
  162. {pending
  163. ? 'Waiting for your OK'
  164. : edit.actions_status === 'confirmed'
  165. ? 'Done'
  166. : 'Cancelled, nothing was changed'}
  167. </p>
  168. <ul className="list-disc space-y-1 pl-4">
  169. {edit.actions.map((summary) => (
  170. <li
  171. key={summary}
  172. className={
  173. edit.actions_status === 'cancelled'
  174. ? 'text-muted-foreground line-through'
  175. : undefined
  176. }
  177. >
  178. {summary}
  179. </li>
  180. ))}
  181. </ul>
  182. {pending && (
  183. <div className="flex gap-2 pt-1">
  184. <Button
  185. size="sm"
  186. disabled={busy}
  187. onClick={() => send('confirm')}
  188. data-test="confirm-actions-button"
  189. >
  190. Confirm
  191. </Button>
  192. <Button
  193. size="sm"
  194. variant="outline"
  195. disabled={busy}
  196. onClick={() => send('cancel')}
  197. >
  198. Cancel
  199. </Button>
  200. </div>
  201. )}
  202. </div>
  203. );
  204. }