From af54ffebec1641be100c9526aab8f8b81e2aa85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E4=B9=90=E4=B9=90?= Date: Fri, 31 Mar 2023 15:59:24 +0800 Subject: [PATCH] feat: add custom prompt lib --- .../{searchInput => Input}/index.tsx | 10 +- .../promptLibModal/CustomPrompt.tsx | 152 ++++++++++++++++++ .../promptLibModal/PromptContent.tsx | 28 ++-- src/components/promptLibModal/PromptItem.tsx | 10 +- .../promptLibModal/PromptLibModal.tsx | 4 + src/i18n/en/common.json | 11 +- src/i18n/zh/common.json | 11 +- src/styles/tailwind.css | 4 + typing.d.ts | 2 + 9 files changed, 201 insertions(+), 31 deletions(-) rename src/components/{searchInput => Input}/index.tsx (59%) create mode 100644 src/components/promptLibModal/CustomPrompt.tsx diff --git a/src/components/searchInput/index.tsx b/src/components/Input/index.tsx similarity index 59% rename from src/components/searchInput/index.tsx rename to src/components/Input/index.tsx index db46bbf..f0808d7 100644 --- a/src/components/searchInput/index.tsx +++ b/src/components/Input/index.tsx @@ -1,23 +1,21 @@ -import { useI18n } from '@/hook/useI18n'; import { Dispatch, SetStateAction } from 'react'; type SearchInputProps = { setData: Dispatch>; + placeholder: string; }; -function SearchInput({ setData }: SearchInputProps) { - const { t } = useI18n(); - +function BasicInput({ setData, placeholder }: SearchInputProps) { return ( <> setData(e.target.value)} /> ); } -export default SearchInput; +export default BasicInput; diff --git a/src/components/promptLibModal/CustomPrompt.tsx b/src/components/promptLibModal/CustomPrompt.tsx new file mode 100644 index 0000000..e9e4805 --- /dev/null +++ b/src/components/promptLibModal/CustomPrompt.tsx @@ -0,0 +1,152 @@ +'use client'; + +import { useEffect, useState } from 'react'; +import { useI18n } from '@/hook/useI18n'; +import BasicInput from '../Input'; +import { PlusIcon, PlusCircleIcon } from '@heroicons/react/24/outline'; +import { + addDoc, + collection, + serverTimestamp, + query, + orderBy +} from 'firebase/firestore'; +import { useSession } from 'next-auth/react'; +import { db } from '@/service/firebase/firebase'; +import { useCollection } from 'react-firebase-hooks/firestore'; +import PromptItem from './PromptItem'; + +function CustomPrompt() { + const [isAdding, setIsAdding] = useState(false); + const [titleValue, setTitleValue] = useState(''); + const [desValue, setDesValue] = useState(''); + const [promptValue, setPromptValue] = useState(''); + const [searchValue, setSearchValue] = useState(''); + const [addBtnDis, setAddBtnDis] = useState(false); + + const { data: session } = useSession(); + + const { t } = useI18n(); + + const [customPromptData] = useCollection( + query( + collection(db, 'users', session?.user?.email!, 'customPrompt'), + orderBy('createAt', 'asc') + ) + ); + + const addPrompt = () => { + if (titleValue === '' || promptValue === '') { + return; + } + + const newPrompt = { + title: titleValue, + des: desValue, + source: Date.now(), + prompt: promptValue, + type: 'custom', + createAt: serverTimestamp() + }; + + setAddBtnDis(true); + + addDoc( + collection(db, 'users', session?.user?.email!, 'customPrompt'), + newPrompt + ) + .then((res) => { + if (res && res.id) { + setIsAdding(false); + setTitleValue(''); + setDesValue(''); + setPromptValue(''); + } + }) + .finally(() => { + setAddBtnDis(false); + }); + }; + + return ( + <> + {isAdding ? ( +
+
+ + +
+
+ + +
+
+ +