|
| 1 | +import { |
| 2 | + Cross2Icon, |
| 3 | + EnterFullScreenIcon, |
| 4 | + ExitFullScreenIcon, |
| 5 | +} from "@radix-ui/react-icons"; |
| 6 | +import { |
| 7 | + Button, |
| 8 | + Dialog, |
| 9 | + Flex, |
| 10 | + IconButton, |
| 11 | + Switch, |
| 12 | + Text, |
| 13 | + TextArea, |
| 14 | +} from "@radix-ui/themes"; |
| 15 | +import { useRef, useState } from "react"; |
| 16 | +import { useHotkeys } from "react-hotkeys-hook"; |
| 17 | +import { useTabStore } from "../stores/tabStore"; |
| 18 | +import { useTaskStore } from "../stores/taskStore"; |
| 19 | + |
| 20 | +interface TaskCreateProps { |
| 21 | + open: boolean; |
| 22 | + onOpenChange: (open: boolean) => void; |
| 23 | +} |
| 24 | + |
| 25 | +export function TaskCreate({ open, onOpenChange }: TaskCreateProps) { |
| 26 | + const { createTask, isLoading } = useTaskStore(); |
| 27 | + const { createTab } = useTabStore(); |
| 28 | + const [title, setTitle] = useState(""); |
| 29 | + const [description, setDescription] = useState(""); |
| 30 | + const [isExpanded, setIsExpanded] = useState(false); |
| 31 | + const [createMore, setCreateMore] = useState(false); |
| 32 | + const titleRef = useRef<HTMLTextAreaElement>(null); |
| 33 | + const descriptionRef = useRef<HTMLTextAreaElement>(null); |
| 34 | + |
| 35 | + const handleCreate = async () => { |
| 36 | + if (!title.trim() || !description.trim()) return; |
| 37 | + |
| 38 | + const newTask = await createTask(title, description); |
| 39 | + if (newTask) { |
| 40 | + createTab({ |
| 41 | + type: "task-detail", |
| 42 | + title: newTask.title, |
| 43 | + data: newTask, |
| 44 | + }); |
| 45 | + setTitle(""); |
| 46 | + setDescription(""); |
| 47 | + if (!createMore) { |
| 48 | + onOpenChange(false); |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + useHotkeys("mod+enter", handleCreate, { |
| 54 | + enabled: open, |
| 55 | + enableOnFormTags: true, |
| 56 | + }); |
| 57 | + |
| 58 | + return ( |
| 59 | + <Dialog.Root open={open} onOpenChange={onOpenChange}> |
| 60 | + <Dialog.Content |
| 61 | + maxHeight={isExpanded ? "90vh" : "600px"} |
| 62 | + height={isExpanded ? "90vh" : "auto"} |
| 63 | + > |
| 64 | + <Flex direction="column" height="100%"> |
| 65 | + <Flex justify="between" align="center"> |
| 66 | + <Dialog.Title className="mb-0" size="2"> |
| 67 | + New Task |
| 68 | + </Dialog.Title> |
| 69 | + <Flex gap="2"> |
| 70 | + <IconButton |
| 71 | + size="1" |
| 72 | + variant="ghost" |
| 73 | + color="gray" |
| 74 | + onClick={() => setIsExpanded(!isExpanded)} |
| 75 | + > |
| 76 | + {isExpanded ? <ExitFullScreenIcon /> : <EnterFullScreenIcon />} |
| 77 | + </IconButton> |
| 78 | + <Dialog.Close> |
| 79 | + <IconButton size="1" variant="ghost" color="gray"> |
| 80 | + <Cross2Icon /> |
| 81 | + </IconButton> |
| 82 | + </Dialog.Close> |
| 83 | + </Flex> |
| 84 | + </Flex> |
| 85 | + |
| 86 | + <Flex direction="column" gap="4" mt="4" flexGrow="1"> |
| 87 | + <Flex direction="column" gap="2"> |
| 88 | + <TextArea |
| 89 | + ref={titleRef} |
| 90 | + value={title} |
| 91 | + onChange={(e) => { |
| 92 | + setTitle(e.target.value); |
| 93 | + if (titleRef.current && !isExpanded) { |
| 94 | + titleRef.current.style.height = "auto"; |
| 95 | + titleRef.current.style.height = `${titleRef.current.scrollHeight}px`; |
| 96 | + } |
| 97 | + }} |
| 98 | + placeholder="Task title..." |
| 99 | + size="3" |
| 100 | + autoFocus |
| 101 | + rows={1} |
| 102 | + style={{ |
| 103 | + resize: "none", |
| 104 | + overflow: "hidden", |
| 105 | + minHeight: "auto", |
| 106 | + }} |
| 107 | + /> |
| 108 | + </Flex> |
| 109 | + |
| 110 | + <Flex |
| 111 | + direction="column" |
| 112 | + gap="2" |
| 113 | + flexGrow="1" |
| 114 | + style={{ minHeight: 0 }} |
| 115 | + > |
| 116 | + <TextArea |
| 117 | + ref={descriptionRef} |
| 118 | + value={description} |
| 119 | + onChange={(e) => { |
| 120 | + setDescription(e.target.value); |
| 121 | + if (descriptionRef.current && !isExpanded) { |
| 122 | + descriptionRef.current.style.height = "auto"; |
| 123 | + descriptionRef.current.style.height = `${descriptionRef.current.scrollHeight}px`; |
| 124 | + } |
| 125 | + }} |
| 126 | + placeholder="Add description..." |
| 127 | + size="3" |
| 128 | + rows={3} |
| 129 | + style={{ |
| 130 | + resize: "none", |
| 131 | + overflow: isExpanded ? "auto" : "hidden", |
| 132 | + minHeight: "auto", |
| 133 | + height: isExpanded ? "100%" : "auto", |
| 134 | + }} |
| 135 | + /> |
| 136 | + </Flex> |
| 137 | + |
| 138 | + <Flex gap="3" justify="end" align="end"> |
| 139 | + <Text as="label" size="1" style={{ cursor: "pointer" }}> |
| 140 | + <Flex gap="2" align="center" mb="2"> |
| 141 | + <Switch |
| 142 | + checked={createMore} |
| 143 | + onCheckedChange={setCreateMore} |
| 144 | + size="1" |
| 145 | + /> |
| 146 | + <Text size="1" color="gray" className="select-none"> |
| 147 | + Create more |
| 148 | + </Text> |
| 149 | + </Flex> |
| 150 | + </Text> |
| 151 | + <Button |
| 152 | + variant="classic" |
| 153 | + onClick={handleCreate} |
| 154 | + disabled={!title.trim() || !description.trim() || isLoading} |
| 155 | + > |
| 156 | + {isLoading ? "Creating..." : "Create task"} |
| 157 | + </Button> |
| 158 | + </Flex> |
| 159 | + </Flex> |
| 160 | + </Flex> |
| 161 | + </Dialog.Content> |
| 162 | + </Dialog.Root> |
| 163 | + ); |
| 164 | +} |
0 commit comments