|
1 | 1 | import { |
2 | | - Body1, |
3 | | - Body1Strong, |
4 | | - Button, |
5 | | - Caption1, |
6 | | - Card, |
7 | | - Title2, |
| 2 | + Body1Strong, |
| 3 | + Button, |
| 4 | + Caption1, |
| 5 | + Title2, |
8 | 6 | } from "@fluentui/react-components"; |
9 | | -import { FoodToast20Regular, Send20Regular } from "@fluentui/react-icons"; |
| 7 | +import { Send20Regular } from "@fluentui/react-icons"; |
10 | 8 | import React, { useRef, useEffect, useState } from "react"; |
11 | | -import { useNavigate } from "react-router-dom"; |
| 9 | +import { useNavigate, useLocation } from "react-router-dom"; |
12 | 10 |
|
13 | 11 | import "./../../styles/Chat.css"; |
14 | 12 | import "../../styles/prism-material-oceanic.css"; |
15 | 13 | import "./../../styles/HomeInput.css"; |
| 14 | + |
16 | 15 | import { HomeInputProps, quickTasks, QuickTask } from "../../models/homeInput"; |
17 | 16 | import { TaskService } from "../../services/TaskService"; |
18 | 17 | import { NewTaskService } from "../../services/NewTaskService"; |
| 18 | + |
19 | 19 | import ChatInput from "@/coral/modules/ChatInput"; |
20 | 20 | import InlineToaster, { useInlineToaster } from "../toast/InlineToaster"; |
21 | 21 | import PromptCard from "@/coral/components/PromptCard"; |
| 22 | +import { Send } from "@/coral/imports/bundleicons"; |
22 | 23 |
|
23 | 24 | const HomeInput: React.FC<HomeInputProps> = ({ |
24 | | - onInputSubmit, |
25 | | - onQuickTaskSelect, |
| 25 | + onInputSubmit, |
| 26 | + onQuickTaskSelect, |
26 | 27 | }) => { |
27 | | - const [submitting, setSubmitting] = useState(false); |
28 | | - const [input, setInput] = useState(""); |
29 | | - const textareaRef = useRef<HTMLTextAreaElement>(null); |
30 | | - const navigate = useNavigate(); |
31 | | - const { showToast, dismissToast } = useInlineToaster(); |
32 | | - |
33 | | - const resetTextarea = () => { |
| 28 | + const [submitting, setSubmitting] = useState(false); |
| 29 | + const [input, setInput] = useState(""); |
| 30 | + |
| 31 | + const textareaRef = useRef<HTMLTextAreaElement>(null); |
| 32 | + const navigate = useNavigate(); |
| 33 | + const location = useLocation(); // ✅ location.state used to control focus |
| 34 | + const { showToast, dismissToast } = useInlineToaster(); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (location.state?.focusInput) { |
| 38 | + textareaRef.current?.focus(); |
| 39 | + } |
| 40 | + }, [location]); |
| 41 | + |
| 42 | + const resetTextarea = () => { |
| 43 | + setInput(""); |
| 44 | + if (textareaRef.current) { |
| 45 | + textareaRef.current.style.height = "auto"; |
| 46 | + textareaRef.current.focus(); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + const cleanup = NewTaskService.addResetListener(resetTextarea); |
| 52 | + return cleanup; |
| 53 | + }, []); |
| 54 | + |
| 55 | + const handleSubmit = async () => { |
| 56 | + if (input.trim()) { |
| 57 | + setSubmitting(true); |
| 58 | + let id = showToast("Creating a plan", "progress"); |
| 59 | + |
| 60 | + try { |
| 61 | + const response = await TaskService.submitInputTask(input.trim()); |
34 | 62 | setInput(""); |
35 | | - if (textareaRef.current) { |
36 | | - textareaRef.current.style.height = "auto"; |
37 | | - textareaRef.current.focus(); |
38 | | - } |
39 | | - }; |
40 | | - |
41 | | - useEffect(() => { |
42 | | - const cleanup = NewTaskService.addResetListener(resetTextarea); |
43 | | - return cleanup; |
44 | | - }, []); |
45 | | - |
46 | | - const handleSubmit = async () => { |
47 | | - if (input.trim()) { |
48 | | - setSubmitting(true); |
49 | | - let id = showToast("Creating a plan", "progress"); |
50 | | - try { |
51 | | - const response = await TaskService.submitInputTask(input.trim()); |
52 | | - |
53 | | - setInput(""); |
54 | | - if (textareaRef.current) { |
55 | | - textareaRef.current.style.height = "auto"; |
56 | | - } |
57 | | - |
58 | | - |
59 | | - console.log('Task response', response); |
60 | | - if (response.plan_id && response.plan_id !== null) { |
61 | | - // plan_id is valid (not null or undefined) |
62 | | - showToast("Plan created!", "success"); |
63 | | - dismissToast(id); |
64 | | - navigate(`/plan/${response.plan_id}`); |
65 | | - } else { |
66 | | - // plan_id is not valid, handle accordingly |
67 | | - console.log('Invalid plan:', response.status); |
68 | | - |
69 | | - showToast("Failed to create plan", "error"); |
70 | | - dismissToast(id); |
71 | | - } |
72 | | - } catch (error) { |
73 | | - console.error("Failed to create plan:", error); |
74 | | - dismissToast(id); |
75 | | - showToast("Something went wrong", "error"); |
76 | | - } finally { |
77 | | - setInput(""); |
78 | | - setSubmitting(false); |
79 | | - } |
80 | | - } |
81 | | - }; |
82 | 63 |
|
83 | | - const handleQuickTaskClick = (task: QuickTask) => { |
84 | | - setInput(task.description); |
85 | 64 | if (textareaRef.current) { |
86 | | - textareaRef.current.focus(); |
| 65 | + textareaRef.current.style.height = "auto"; |
87 | 66 | } |
88 | | - onQuickTaskSelect(task.description); |
89 | | - }; |
90 | 67 |
|
91 | | - useEffect(() => { |
92 | | - if (textareaRef.current) { |
93 | | - textareaRef.current.style.height = "auto"; |
94 | | - textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`; |
| 68 | + if (response.plan_id && response.plan_id !== null) { |
| 69 | + showToast("Plan created!", "success"); |
| 70 | + dismissToast(id); |
| 71 | + navigate(`/plan/${response.plan_id}`); |
| 72 | + } else { |
| 73 | + console.log("Invalid plan:", response.status); |
| 74 | + showToast("Failed to create plan", "error"); |
| 75 | + dismissToast(id); |
95 | 76 | } |
96 | | - }, [input]); |
97 | | - |
98 | | - |
99 | | - |
100 | | - return ( |
101 | | - <div className="home-input-container"> |
102 | | - <div className="home-input-content"> |
103 | | - <div className="home-input-center-content"> |
104 | | - <div className="home-input-title-wrapper"> |
105 | | - <Title2>How can I help?</Title2> |
106 | | - </div> |
107 | | - |
108 | | - <ChatInput |
109 | | - value={input} |
110 | | - placeholder="Tell us what needs planning, building, or connecting—we'll handle the rest." |
111 | | - onChange={setInput} |
112 | | - onEnter={handleSubmit} |
113 | | - disabledChat={submitting} |
114 | | - > |
115 | | - <Button |
116 | | - appearance="subtle" |
117 | | - className="home-input-send-button" |
118 | | - onClick={handleSubmit} |
119 | | - disabled={submitting} |
120 | | - icon={<Send20Regular />} |
121 | | - /> |
122 | | - |
123 | | - </ChatInput> |
124 | | - |
125 | | - {/* Inline Toaster lives right under chat input */} |
126 | | - <InlineToaster /> |
127 | | - |
128 | | - <div className="home-input-quick-tasks-section"> |
129 | | - <div className="home-input-quick-tasks-header"> |
130 | | - <Body1Strong>Quick tasks</Body1Strong> |
131 | | - </div> |
132 | | - <div className="home-input-quick-tasks"> |
133 | | - {quickTasks.map((task) => ( |
134 | | - <PromptCard |
135 | | - key={task.id} |
136 | | - title={task.title} |
137 | | - icon={task.icon} |
138 | | - description={task.description} |
139 | | - onClick={() => handleQuickTaskClick(task)} |
140 | | - disabled={submitting} |
141 | | - /> |
142 | | - ))} |
143 | | - </div> |
144 | | - </div> |
145 | | - </div> |
| 77 | + } catch (error) { |
| 78 | + console.error("Failed to create plan:", error); |
| 79 | + dismissToast(id); |
| 80 | + showToast("Something went wrong", "error"); |
| 81 | + } finally { |
| 82 | + setInput(""); |
| 83 | + setSubmitting(false); |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + const handleQuickTaskClick = (task: QuickTask) => { |
| 89 | + setInput(task.description); |
| 90 | + if (textareaRef.current) { |
| 91 | + textareaRef.current.focus(); |
| 92 | + } |
| 93 | + onQuickTaskSelect(task.description); |
| 94 | + }; |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + if (textareaRef.current) { |
| 98 | + textareaRef.current.style.height = "auto"; |
| 99 | + textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`; |
| 100 | + } |
| 101 | + }, [input]); |
| 102 | + |
| 103 | + return ( |
| 104 | + <div className="home-input-container"> |
| 105 | + <div className="home-input-content"> |
| 106 | + <div className="home-input-center-content"> |
| 107 | + <div className="home-input-title-wrapper"> |
| 108 | + <Title2>How can I help?</Title2> |
| 109 | + </div> |
| 110 | + |
| 111 | + <ChatInput |
| 112 | + ref={textareaRef} // forwarding |
| 113 | + value={input} |
| 114 | + placeholder="Tell us what needs planning, building, or connecting—we'll handle the rest." |
| 115 | + onChange={setInput} |
| 116 | + onEnter={handleSubmit} |
| 117 | + disabledChat={submitting} |
| 118 | + > |
| 119 | + <Button |
| 120 | + appearance="subtle" |
| 121 | + className="home-input-send-button" |
| 122 | + onClick={handleSubmit} |
| 123 | + disabled={submitting} |
| 124 | + icon={<Send />} |
| 125 | + /> |
| 126 | + </ChatInput> |
| 127 | + |
| 128 | + <InlineToaster /> |
| 129 | + |
| 130 | + <div className="home-input-quick-tasks-section"> |
| 131 | + <div className="home-input-quick-tasks-header"> |
| 132 | + <Body1Strong>Quick tasks</Body1Strong> |
146 | 133 | </div> |
| 134 | + |
| 135 | + <div className="home-input-quick-tasks"> |
| 136 | + {quickTasks.map((task) => ( |
| 137 | + <PromptCard |
| 138 | + key={task.id} |
| 139 | + title={task.title} |
| 140 | + icon={task.icon} |
| 141 | + description={task.description} |
| 142 | + onClick={() => handleQuickTaskClick(task)} |
| 143 | + disabled={submitting} |
| 144 | + /> |
| 145 | + ))} |
| 146 | + </div> |
| 147 | + </div> |
147 | 148 | </div> |
148 | | - ); |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + ); |
149 | 152 | }; |
150 | 153 |
|
151 | 154 | export default HomeInput; |
152 | | - |
|
0 commit comments