|
| 1 | +import React, { FC, Fragment, useCallback } from 'react'; |
| 2 | +import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; |
| 3 | +import useSWR from 'swr'; |
| 4 | +import { Button } from '@gitroom/react/form/button'; |
| 5 | +import clsx from 'clsx'; |
| 6 | +import { useModals } from '@mantine/modals'; |
| 7 | +import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component'; |
| 8 | +import { array, boolean, object, string } from 'yup'; |
| 9 | +import { FormProvider, useForm } from 'react-hook-form'; |
| 10 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 11 | +import { CopilotTextarea } from '@copilotkit/react-textarea'; |
| 12 | +import { Select } from '@gitroom/react/form/select'; |
| 13 | +import { useToaster } from '@gitroom/react/toaster/toaster'; |
| 14 | + |
| 15 | +export const SignaturesComponent: FC<{ |
| 16 | + appendSignature?: (value: string) => void; |
| 17 | +}> = (props) => { |
| 18 | + const { appendSignature } = props; |
| 19 | + const fetch = useFetch(); |
| 20 | + const modal = useModals(); |
| 21 | + const load = useCallback(async () => { |
| 22 | + return (await fetch('/signatures')).json(); |
| 23 | + }, []); |
| 24 | + |
| 25 | + const { data, mutate } = useSWR('signatures', load); |
| 26 | + |
| 27 | + const addSignature = useCallback( |
| 28 | + (data?: any) => () => { |
| 29 | + modal.openModal({ |
| 30 | + title: '', |
| 31 | + withCloseButton: false, |
| 32 | + classNames: { |
| 33 | + modal: 'bg-transparent text-textColor', |
| 34 | + }, |
| 35 | + children: <AddOrRemoveSignature data={data} reload={mutate} />, |
| 36 | + }); |
| 37 | + }, |
| 38 | + [mutate] |
| 39 | + ); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="flex flex-col"> |
| 43 | + <h3 className="text-[20px]">Signatures</h3> |
| 44 | + <div className="text-customColor18 mt-[4px]"> |
| 45 | + You can add signatures to your account to be used in your posts. |
| 46 | + </div> |
| 47 | + <div className="my-[16px] mt-[16px] bg-sixth border-fifth items-center border rounded-[4px] p-[24px] flex gap-[24px]"> |
| 48 | + <div className="flex flex-col w-full"> |
| 49 | + {!!data?.length && ( |
| 50 | + <div className={`grid ${!!appendSignature ? 'grid-cols-[1fr,1fr,1fr,1fr,1fr]': 'grid-cols-[1fr,1fr,1fr,1fr]'} w-full gap-y-[10px]`}> |
| 51 | + <div>Content</div> |
| 52 | + <div className="text-center">Auto Add?</div> |
| 53 | + {!!appendSignature && <div className="text-center">Actions</div>} |
| 54 | + <div className="text-center">Edit</div> |
| 55 | + <div className="text-center">Delete</div> |
| 56 | + {data?.map((p: any) => ( |
| 57 | + <Fragment key={p.id}> |
| 58 | + <div className="relative flex-1 mr-[20px] overflow-x-hidden"> |
| 59 | + <div className="absolute left-0 line-clamp-1 top-[50%] -translate-y-[50%] text-ellipsis"> |
| 60 | + {p.content.slice(0,15) + '...'} |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + <div className="flex flex-col justify-center relative mr-[20px]"> |
| 64 | + <div className="text-center w-full absolute left-0 line-clamp-1 top-[50%] -translate-y-[50%]"> |
| 65 | + {p.autoAdd ? 'Yes' : 'No'} |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + {!!appendSignature && ( |
| 69 | + <div className="flex justify-center"> |
| 70 | + <Button onClick={() => appendSignature(p.content)}>Use Signature</Button> |
| 71 | + </div> |
| 72 | + )} |
| 73 | + <div className="flex justify-center"> |
| 74 | + <div> |
| 75 | + <Button onClick={addSignature(p)}>Edit</Button> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + <div className="flex justify-center"> |
| 79 | + <div> |
| 80 | + <Button onClick={addSignature(p)}>Delete</Button> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + </Fragment> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + )} |
| 87 | + <div> |
| 88 | + <Button |
| 89 | + onClick={addSignature()} |
| 90 | + className={clsx((data?.length || 0) > 0 && 'my-[16px]')} |
| 91 | + > |
| 92 | + Add a signature |
| 93 | + </Button> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +const details = object().shape({ |
| 102 | + content: string().required(), |
| 103 | + autoAdd: boolean().required(), |
| 104 | +}); |
| 105 | + |
| 106 | +const AddOrRemoveSignature: FC<{ data?: any; reload: () => void }> = ( |
| 107 | + props |
| 108 | +) => { |
| 109 | + const { data, reload } = props; |
| 110 | + const toast = useToaster(); |
| 111 | + const fetch = useFetch(); |
| 112 | + |
| 113 | + const form = useForm({ |
| 114 | + resolver: yupResolver(details), |
| 115 | + values: { |
| 116 | + content: data?.content || '', |
| 117 | + autoAdd: data?.autoAdd || false, |
| 118 | + }, |
| 119 | + }); |
| 120 | + |
| 121 | + const text = form.watch('content'); |
| 122 | + const autoAdd = form.watch('autoAdd'); |
| 123 | + const modal = useModals(); |
| 124 | + |
| 125 | + const callBack = useCallback( |
| 126 | + async (values: any) => { |
| 127 | + await fetch(data?.id ? `/signatures/${data.id}` : '/signatures', { |
| 128 | + method: data?.id ? 'PUT' : 'POST', |
| 129 | + body: JSON.stringify(values), |
| 130 | + }); |
| 131 | + |
| 132 | + toast.show( |
| 133 | + data?.id |
| 134 | + ? 'Webhook updated successfully' |
| 135 | + : 'Webhook added successfully', |
| 136 | + 'success' |
| 137 | + ); |
| 138 | + |
| 139 | + modal.closeModal(modal.modals[modal.modals.length - 1].id); |
| 140 | + reload(); |
| 141 | + }, |
| 142 | + [data, modal] |
| 143 | + ); |
| 144 | + |
| 145 | + return ( |
| 146 | + <FormProvider {...form}> |
| 147 | + <form onSubmit={form.handleSubmit(callBack)}> |
| 148 | + <div className="relative flex gap-[20px] flex-col flex-1 rounded-[4px] border border-customColor6 bg-sixth p-[16px] pt-0 w-[500px]"> |
| 149 | + <TopTitle title={data ? 'Edit Signature' : 'Add Signature'} /> |
| 150 | + <button |
| 151 | + className="outline-none absolute right-[20px] top-[15px] mantine-UnstyledButton-root mantine-ActionIcon-root hover:bg-tableBorder cursor-pointer mantine-Modal-close mantine-1dcetaa" |
| 152 | + type="button" |
| 153 | + onClick={() => modal.closeModal(modal.modals[modal.modals.length - 1].id)} |
| 154 | + > |
| 155 | + <svg |
| 156 | + viewBox="0 0 15 15" |
| 157 | + fill="none" |
| 158 | + xmlns="http://www.w3.org/2000/svg" |
| 159 | + width="16" |
| 160 | + height="16" |
| 161 | + > |
| 162 | + <path |
| 163 | + d="M11.7816 4.03157C12.0062 3.80702 12.0062 3.44295 11.7816 3.2184C11.5571 2.99385 11.193 2.99385 10.9685 3.2184L7.50005 6.68682L4.03164 3.2184C3.80708 2.99385 3.44301 2.99385 3.21846 3.2184C2.99391 3.44295 2.99391 3.80702 3.21846 4.03157L6.68688 7.49999L3.21846 10.9684C2.99391 11.193 2.99391 11.557 3.21846 11.7816C3.44301 12.0061 3.80708 12.0061 4.03164 11.7816L7.50005 8.31316L10.9685 11.7816C11.193 12.0061 11.5571 12.0061 11.7816 11.7816C12.0062 11.557 12.0062 11.193 11.7816 10.9684L8.31322 7.49999L11.7816 4.03157Z" |
| 164 | + fill="currentColor" |
| 165 | + fillRule="evenodd" |
| 166 | + clipRule="evenodd" |
| 167 | + ></path> |
| 168 | + </svg> |
| 169 | + </button> |
| 170 | + |
| 171 | + <div className="relative bg-customColor2"> |
| 172 | + <CopilotTextarea |
| 173 | + disableBranding={true} |
| 174 | + className={clsx( |
| 175 | + '!min-h-40 !max-h-80 p-2 overflow-x-hidden scrollbar scrollbar-thumb-[#612AD5] bg-customColor2 outline-none' |
| 176 | + )} |
| 177 | + value={text} |
| 178 | + onChange={(e) => { |
| 179 | + form.setValue('content', e.target.value); |
| 180 | + }} |
| 181 | + placeholder="Write your signature..." |
| 182 | + autosuggestionsConfig={{ |
| 183 | + textareaPurpose: `Assist me in writing social media signature`, |
| 184 | + chatApiConfigs: {}, |
| 185 | + }} |
| 186 | + /> |
| 187 | + </div> |
| 188 | + |
| 189 | + <Select |
| 190 | + label="Auto add signature?" |
| 191 | + {...form.register('autoAdd', { |
| 192 | + setValueAs: (value) => value === 'true', |
| 193 | + })} |
| 194 | + > |
| 195 | + <option value="false" selected={!autoAdd}> |
| 196 | + No |
| 197 | + </option> |
| 198 | + <option value="true" selected={autoAdd}> |
| 199 | + Yes |
| 200 | + </option> |
| 201 | + </Select> |
| 202 | + |
| 203 | + <Button type="submit">Save</Button> |
| 204 | + </div> |
| 205 | + </form> |
| 206 | + </FormProvider> |
| 207 | + ); |
| 208 | +}; |
0 commit comments