|
| 1 | +import React, { useState } from 'react' |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + DialogTrigger, |
| 5 | + Button, |
| 6 | + DialogContent, |
| 7 | + DialogHeader, |
| 8 | + DialogTitle, |
| 9 | + DialogDescription, |
| 10 | + DialogFooter, |
| 11 | + InputWithLabel, |
| 12 | + TextareaWithLabel, |
| 13 | +} from '@magickml/client-ui' |
| 14 | +import toast from 'react-hot-toast' |
| 15 | +import { createEmbedderReactClient } from '@magickml/embedder-client-react' |
| 16 | + |
| 17 | +interface CreateKnowledgePackFormProps { |
| 18 | + client: ReturnType<typeof createEmbedderReactClient> |
| 19 | +} |
| 20 | + |
| 21 | +const CreateKnowledgePackForm: React.FC<CreateKnowledgePackFormProps> = ({ |
| 22 | + client, |
| 23 | +}) => { |
| 24 | + const { invalidate } = client.useGetPacksByEntityAndOwner() |
| 25 | + |
| 26 | + const { mutateAsync: createPack } = client.useCreatePack( |
| 27 | + {}, |
| 28 | + { |
| 29 | + onSuccess: async () => { |
| 30 | + toast.success('Knowledge pack created successfully!') |
| 31 | + await invalidate() |
| 32 | + }, |
| 33 | + onError: () => { |
| 34 | + toast.error('Failed to create knowledge pack.') |
| 35 | + }, |
| 36 | + } |
| 37 | + ) |
| 38 | + |
| 39 | + const handleCreatePack = async () => { |
| 40 | + await createPack({ |
| 41 | + name, |
| 42 | + description, |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + const [name, setName] = useState('') |
| 47 | + const [description, setDescription] = useState('') |
| 48 | + |
| 49 | + return ( |
| 50 | + <Dialog> |
| 51 | + <DialogTrigger asChild> |
| 52 | + <Button variant="outline">Create New</Button> |
| 53 | + </DialogTrigger> |
| 54 | + <DialogContent className="sm:max-w-[800px]"> |
| 55 | + <DialogHeader> |
| 56 | + <DialogTitle>Create New Knowledge Pack</DialogTitle> |
| 57 | + <DialogDescription> |
| 58 | + Fill out the details for your new knowledge pack. |
| 59 | + </DialogDescription> |
| 60 | + </DialogHeader> |
| 61 | + |
| 62 | + <InputWithLabel |
| 63 | + id="name" |
| 64 | + label="Name" |
| 65 | + value={name} |
| 66 | + onChange={e => setName(e.target.value)} |
| 67 | + placeholder="Enter the name of the knowledge pack" |
| 68 | + /> |
| 69 | + |
| 70 | + <TextareaWithLabel |
| 71 | + id="description" |
| 72 | + label="Description" |
| 73 | + value={description} |
| 74 | + onChange={e => setDescription(e.target.value)} |
| 75 | + placeholder="Enter a description for the knowledge pack" |
| 76 | + /> |
| 77 | + |
| 78 | + <DialogFooter> |
| 79 | + <Button onClick={handleCreatePack} type="submit"> |
| 80 | + Create Pack |
| 81 | + </Button> |
| 82 | + </DialogFooter> |
| 83 | + </DialogContent> |
| 84 | + </Dialog> |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +export default CreateKnowledgePackForm |
0 commit comments