Skip to content

Commit

Permalink
Fix: use modal to rename threads
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Aug 22, 2024
1 parent a53bb6e commit e6d4944
Showing 1 changed file with 87 additions and 45 deletions.
132 changes: 87 additions & 45 deletions components/threads/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ import {
Button,
Menu,
MenuItem,
useDisclosure,
Modal,
ModalContent,
ModalHeader,
ModalFooter,
ModalBody,
} from '@nextui-org/react';
import { deleteThread, renameThread, Thread } from '@/actions/threads';
import { GoPencil, GoTrash, GoKebabHorizontal } from 'react-icons/go';
import { ScriptContext } from '@/contexts/script';
import { Input } from '@nextui-org/input';

interface NewThreadProps {
className?: string;
thread: string;
}

const NewThread = ({ className, thread }: NewThreadProps) => {
const [isOpen, setIsOpen] = useState(false);
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
const { isOpen, onOpen, onClose } = useDisclosure();
const { setThreads } = useContext(ScriptContext);
const [threadNameInput, setThreadNameInput] = useState('');

const handleDeleteThread = () => {
deleteThread(thread).then(() => {
Expand All @@ -28,8 +37,7 @@ const NewThread = ({ className, thread }: NewThreadProps) => {
});
};

const handleRenameThread = () => {
const newName = prompt('Enter a new name for the thread');
const handleRenameThread = (newName: string) => {
if (newName) {
renameThread(thread, newName).then(() => {
setThreads((threads: Thread[]) =>
Expand All @@ -45,48 +53,82 @@ const NewThread = ({ className, thread }: NewThreadProps) => {
};

return (
<Popover
placement="right"
isOpen={isOpen}
onOpenChange={(open) => setIsOpen(open)}
>
<PopoverTrigger>
<Button
variant="light"
radius="full"
className={`${className}`}
isIconOnly
startContent={<GoKebabHorizontal />}
/>
</PopoverTrigger>
<PopoverContent className="">
<Menu aria-label="options">
<MenuItem
className="py-2"
content="Rename"
startContent={<GoPencil />}
onClick={() => {
setIsOpen(false);
handleRenameThread();
}}
>
Rename
</MenuItem>
<MenuItem
aria-label="delete"
className="py-2"
content="Delete"
startContent={<GoTrash />}
onClick={() => {
setIsOpen(false);
handleDeleteThread();
}}
>
Delete
</MenuItem>
</Menu>
</PopoverContent>
</Popover>
<>
<Popover
placement="right-start"
isOpen={isPopoverOpen}
onOpenChange={(open) => setIsPopoverOpen(open)}
>
<PopoverTrigger>
<Button
variant="light"
radius="full"
className={`${className}`}
isIconOnly
startContent={<GoKebabHorizontal />}
/>
</PopoverTrigger>
<PopoverContent className="">
<Menu aria-label="options">
<MenuItem
className="py-2"
content="Rename"
startContent={<GoPencil />}
onClick={() => {
setIsPopoverOpen(false);
onOpen();
}}
>
Rename
</MenuItem>
<MenuItem
aria-label="delete"
className="py-2"
content="Delete"
startContent={<GoTrash />}
onClick={() => {
setIsPopoverOpen(false);
handleDeleteThread();
}}
>
Delete
</MenuItem>
</Menu>
</PopoverContent>
</Popover>
<Modal
backdrop={'opaque'}
isOpen={isOpen}
onClose={onClose}
placement="top-center"
>
<ModalContent>
<ModalHeader className="flex flex-col gap-1">
Rename thread
</ModalHeader>
<ModalBody>
<Input
aria-label="rename"
label="New Name"
value={threadNameInput}
onChange={(e) => setThreadNameInput(e.target.value)}
/>
</ModalBody>
<ModalFooter>
<Button
aria-label="rename"
color="primary"
onPress={() => {
handleRenameThread(threadNameInput);
onClose();
}}
>
Rename
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
};

Expand Down

0 comments on commit e6d4944

Please sign in to comment.