| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- import { Form, router } from '@inertiajs/react';
- import { CircleAlert, CircleCheck, Send } from 'lucide-react';
- import { useState } from 'react';
- import StoreGenerationController from '@/actions/App/Http/Controllers/StoreGenerationController';
- import InputError from '@/components/input-error';
- import { Button } from '@/components/ui/button';
- import { Spinner } from '@/components/ui/spinner';
- import type { Store, StoreGeneration, UsageCount } from '@/types';
-
- const fieldClass =
- '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';
-
- /**
- * Ask the AI for a small change in plain words, and see the last few requests.
- */
- export default function DesignChat({
- store,
- edits,
- usage,
- busy,
- }: {
- store: Store;
- edits: StoreGeneration[];
- usage: UsageCount;
- busy: boolean;
- }) {
- const left = Math.max(0, usage.limit - usage.used);
- const disabled = busy || left === 0;
-
- return (
- <div className="space-y-4 rounded-xl border p-4">
- <div className="flex flex-wrap items-baseline justify-between gap-2">
- <h3 className="font-medium">Ask the AI</h3>
- <p className="text-xs text-muted-foreground">
- {left} of {usage.limit} AI edits left
- </p>
- </div>
-
- <Form
- {...StoreGenerationController.edit.form(store.slug)}
- resetOnSuccess
- options={{ preserveScroll: true }}
- className="space-y-2"
- >
- {({ processing, errors }) => (
- <>
- <label htmlFor="instruction" className="sr-only">
- Describe the change you want
- </label>
- <textarea
- id="instruction"
- name="instruction"
- required
- rows={2}
- minLength={3}
- maxLength={500}
- disabled={disabled}
- className={fieldClass}
- 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."
- />
- <InputError message={errors.instruction} />
- <div className="flex flex-wrap items-center justify-between gap-2">
- <p className="text-xs text-muted-foreground">
- {left === 0
- ? 'You have used all the AI edits in your plan.'
- : 'One change at a time works best.'}
- </p>
- <Button
- size="sm"
- disabled={disabled || processing}
- data-test="send-edit-button"
- >
- <Send />
- Send
- </Button>
- </div>
- </>
- )}
- </Form>
-
- {edits.length > 0 && (
- <ul
- className="space-y-3 border-t pt-4"
- data-test="edit-history"
- >
- {edits.map((edit) => (
- <li key={edit.id} className="flex gap-3 text-sm">
- <span className="mt-0.5 shrink-0">
- {edit.status === 'completed' ? (
- <CircleCheck className="size-4 text-green-600 dark:text-green-400" />
- ) : edit.status === 'failed' ? (
- <CircleAlert className="size-4 text-red-600 dark:text-red-400" />
- ) : (
- <Spinner />
- )}
- </span>
- <div className="min-w-0">
- <p className="break-words">
- {edit.instruction}
- </p>
- <p className="mt-0.5 break-words text-muted-foreground">
- {edit.status === 'completed'
- ? (edit.reply ?? 'Done.')
- : edit.status === 'failed'
- ? (edit.error ??
- 'This change failed.')
- : 'Working on it…'}
- </p>
- {edit.status === 'completed' &&
- edit.actions.length > 0 && (
- <ProposedActions
- store={store}
- edit={edit}
- />
- )}
- {edit.status === 'completed' &&
- edit.changed === false &&
- edit.actions.length === 0 && (
- <p
- className="mt-1 text-xs font-medium text-amber-700 dark:text-amber-400"
- data-test="edit-no-change"
- >
- No change was made to your design.
- </p>
- )}
- </div>
- </li>
- ))}
- </ul>
- )}
- </div>
- );
- }
-
- /**
- * Store changes the AI prepared from the chat. Nothing happens until the merchant confirms.
- */
- function ProposedActions({
- store,
- edit,
- }: {
- store: Store;
- edit: StoreGeneration;
- }) {
- const [busy, setBusy] = useState(false);
- const pending = edit.actions_status === 'pending';
-
- function send(action: 'confirm' | 'cancel') {
- const route =
- action === 'confirm'
- ? StoreGenerationController.confirm
- : StoreGenerationController.cancel;
-
- router.post(
- route.url({ slug: store.slug, generation: edit.id }),
- {},
- {
- preserveScroll: true,
- onStart: () => setBusy(true),
- onFinish: () => setBusy(false),
- },
- );
- }
-
- return (
- <div
- className="mt-2 space-y-2 rounded-lg border bg-muted/40 p-3"
- data-test="proposed-actions"
- >
- <p className="text-xs font-medium text-muted-foreground">
- {pending
- ? 'Waiting for your OK'
- : edit.actions_status === 'confirmed'
- ? 'Done'
- : 'Cancelled, nothing was changed'}
- </p>
- <ul className="list-disc space-y-1 pl-4">
- {edit.actions.map((summary) => (
- <li
- key={summary}
- className={
- edit.actions_status === 'cancelled'
- ? 'text-muted-foreground line-through'
- : undefined
- }
- >
- {summary}
- </li>
- ))}
- </ul>
- {pending && (
- <div className="flex gap-2 pt-1">
- <Button
- size="sm"
- disabled={busy}
- onClick={() => send('confirm')}
- data-test="confirm-actions-button"
- >
- Confirm
- </Button>
- <Button
- size="sm"
- variant="outline"
- disabled={busy}
- onClick={() => send('cancel')}
- >
- Cancel
- </Button>
- </div>
- )}
- </div>
- );
- }
|