| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import { Form } from '@inertiajs/react';
- import { CircleAlert, CircleCheck, Send } from 'lucide-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">Change something</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="Describe a change, e.g. Make the main colour dark green, or Rewrite the headline to sound more premium."
- />
- <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>
- </div>
- </li>
- ))}
- </ul>
- )}
- </div>
- );
- }
|