|
| 1 | +import { useTheme } from "@emotion/react"; |
| 2 | +import SendIcon from "@mui/icons-material/Send"; |
| 3 | +import Button from "@mui/material/Button"; |
| 4 | +import IconButton from "@mui/material/IconButton"; |
| 5 | +import Paper from "@mui/material/Paper"; |
| 6 | +import Stack from "@mui/material/Stack"; |
| 7 | +import TextField from "@mui/material/TextField"; |
| 8 | +import { createChat } from "api/queries/chats"; |
| 9 | +import type { Chat } from "api/typesGenerated"; |
| 10 | +import { Margins } from "components/Margins/Margins"; |
| 11 | +import { useAuthenticated } from "hooks"; |
| 12 | +import { type FC, type FormEvent, useState } from "react"; |
| 13 | +import { useMutation, useQueryClient } from "react-query"; |
| 14 | +import { useNavigate } from "react-router-dom"; |
| 15 | +import { LanguageModelSelector } from "./LanguageModelSelector"; |
| 16 | + |
| 17 | +export interface ChatLandingLocationState { |
| 18 | + chat: Chat; |
| 19 | + message: string; |
| 20 | +} |
| 21 | + |
| 22 | +const ChatLanding: FC = () => { |
| 23 | + const { user } = useAuthenticated(); |
| 24 | + const theme = useTheme(); |
| 25 | + const [input, setInput] = useState(""); |
| 26 | + const navigate = useNavigate(); |
| 27 | + const queryClient = useQueryClient(); |
| 28 | + const createChatMutation = useMutation(createChat(queryClient)); |
| 29 | + |
| 30 | + return ( |
| 31 | + <Margins> |
| 32 | + <div |
| 33 | + css={{ |
| 34 | + display: "flex", |
| 35 | + flexDirection: "column", |
| 36 | + marginTop: theme.spacing(24), |
| 37 | + alignItems: "center", |
| 38 | + paddingBottom: theme.spacing(4), |
| 39 | + }} |
| 40 | + > |
| 41 | + {/* Initial Welcome Message Area */} |
| 42 | + <div |
| 43 | + css={{ |
| 44 | + flexGrow: 1, |
| 45 | + display: "flex", |
| 46 | + flexDirection: "column", |
| 47 | + justifyContent: "center", |
| 48 | + alignItems: "center", |
| 49 | + gap: theme.spacing(1), |
| 50 | + padding: theme.spacing(1), |
| 51 | + width: "100%", |
| 52 | + maxWidth: "700px", |
| 53 | + marginBottom: theme.spacing(4), |
| 54 | + }} |
| 55 | + > |
| 56 | + <h1 |
| 57 | + css={{ |
| 58 | + fontSize: theme.typography.h4.fontSize, |
| 59 | + fontWeight: theme.typography.h4.fontWeight, |
| 60 | + lineHeight: theme.typography.h4.lineHeight, |
| 61 | + marginBottom: theme.spacing(1), |
| 62 | + textAlign: "center", |
| 63 | + }} |
| 64 | + > |
| 65 | + Good evening, {user?.name.split(" ")[0]} |
| 66 | + </h1> |
| 67 | + <p |
| 68 | + css={{ |
| 69 | + fontSize: theme.typography.h6.fontSize, |
| 70 | + fontWeight: theme.typography.h6.fontWeight, |
| 71 | + lineHeight: theme.typography.h6.lineHeight, |
| 72 | + color: theme.palette.text.secondary, |
| 73 | + textAlign: "center", |
| 74 | + margin: 0, |
| 75 | + maxWidth: "500px", |
| 76 | + marginInline: "auto", |
| 77 | + }} |
| 78 | + > |
| 79 | + How can I help you today? |
| 80 | + </p> |
| 81 | + </div> |
| 82 | + |
| 83 | + {/* Input Form and Suggestions - Always Visible */} |
| 84 | + <div css={{ width: "100%", maxWidth: "700px", marginTop: "auto" }}> |
| 85 | + <Stack |
| 86 | + direction="row" |
| 87 | + spacing={2} |
| 88 | + justifyContent="center" |
| 89 | + sx={{ mb: 2 }} |
| 90 | + > |
| 91 | + <Button |
| 92 | + variant="outlined" |
| 93 | + onClick={() => setInput("Help me work on issue #...")} |
| 94 | + > |
| 95 | + Work on Issue |
| 96 | + </Button> |
| 97 | + <Button |
| 98 | + variant="outlined" |
| 99 | + onClick={() => setInput("Help me build a template for...")} |
| 100 | + > |
| 101 | + Build a Template |
| 102 | + </Button> |
| 103 | + <Button |
| 104 | + variant="outlined" |
| 105 | + onClick={() => setInput("Help me start a new project using...")} |
| 106 | + > |
| 107 | + Start a Project |
| 108 | + </Button> |
| 109 | + </Stack> |
| 110 | + <LanguageModelSelector /> |
| 111 | + <Paper |
| 112 | + component="form" |
| 113 | + onSubmit={async (e: FormEvent<HTMLFormElement>) => { |
| 114 | + e.preventDefault(); |
| 115 | + setInput(""); |
| 116 | + const chat = await createChatMutation.mutateAsync(); |
| 117 | + navigate(`/chat/${chat.id}`, { |
| 118 | + state: { |
| 119 | + chat, |
| 120 | + message: input, |
| 121 | + }, |
| 122 | + }); |
| 123 | + }} |
| 124 | + elevation={2} |
| 125 | + css={{ |
| 126 | + padding: "16px", |
| 127 | + display: "flex", |
| 128 | + alignItems: "center", |
| 129 | + width: "100%", |
| 130 | + borderRadius: "12px", |
| 131 | + border: `1px solid ${theme.palette.divider}`, |
| 132 | + }} |
| 133 | + > |
| 134 | + <TextField |
| 135 | + value={input} |
| 136 | + onChange={(event: React.ChangeEvent<HTMLInputElement>) => { |
| 137 | + setInput(event.target.value); |
| 138 | + }} |
| 139 | + placeholder="Ask Coder..." |
| 140 | + required |
| 141 | + fullWidth |
| 142 | + variant="outlined" |
| 143 | + multiline |
| 144 | + maxRows={5} |
| 145 | + css={{ |
| 146 | + marginRight: theme.spacing(1), |
| 147 | + "& .MuiOutlinedInput-root": { |
| 148 | + borderRadius: "8px", |
| 149 | + padding: "10px 14px", |
| 150 | + }, |
| 151 | + }} |
| 152 | + autoFocus |
| 153 | + /> |
| 154 | + <IconButton type="submit" color="primary" disabled={!input.trim()}> |
| 155 | + <SendIcon /> |
| 156 | + </IconButton> |
| 157 | + </Paper> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + </Margins> |
| 161 | + ); |
| 162 | +}; |
| 163 | + |
| 164 | +export default ChatLanding; |
0 commit comments