mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
opencode chat frontend implementation
This commit is contained in:
102
packages/frontend/src/components/chat.tsx
Normal file
102
packages/frontend/src/components/chat.tsx
Normal file
@@ -0,0 +1,102 @@
|
||||
import { useState } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import Icon from "@/components/ui/icon";
|
||||
import { useChatMutation, useSelectedOrganisation, useSelectedProject } from "@/lib/query/hooks";
|
||||
import { parseError } from "@/lib/server";
|
||||
import { IconButton } from "./ui/icon-button";
|
||||
import { Input } from "./ui/input";
|
||||
|
||||
export function Chat() {
|
||||
const selectedOrganisation = useSelectedOrganisation();
|
||||
const selectedProject = useSelectedProject();
|
||||
const chatMutation = useChatMutation();
|
||||
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [message, setMessage] = useState("");
|
||||
const [response, setResponse] = useState<string>("");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!message.trim()) return;
|
||||
if (!selectedOrganisation || !selectedProject) {
|
||||
setError("Please select an organisation and project first");
|
||||
return;
|
||||
}
|
||||
|
||||
setError(null);
|
||||
setResponse("");
|
||||
|
||||
try {
|
||||
const data = await chatMutation.mutateAsync({
|
||||
orgId: selectedOrganisation.Organisation.id,
|
||||
projectId: selectedProject.Project.id,
|
||||
message: message.trim(),
|
||||
});
|
||||
setResponse(data.text);
|
||||
setMessage("");
|
||||
} catch (err) {
|
||||
const errorMessage = parseError(err as Error);
|
||||
setError(errorMessage);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<IconButton
|
||||
onClick={() => setIsOpen(!isOpen)}
|
||||
className="fixed bottom-4 left-1/2 -translate-x-1/2 z-50 rounded-full"
|
||||
size="lg"
|
||||
variant="outline"
|
||||
>
|
||||
<Icon icon={isOpen ? "x" : "handsUp"} className="size-5" />
|
||||
</IconButton>
|
||||
|
||||
{isOpen && (
|
||||
<div className="fixed bottom-20 left-1/2 -translate-x-1/2 z-40 w-full max-w-2xl mx-4 bg-background border shadow-xl">
|
||||
<div className="flex flex-col p-2">
|
||||
<form onSubmit={handleSubmit} className="flex flex-col gap-2">
|
||||
{response && (
|
||||
<div className="p-4 border max-h-60 overflow-y-auto">
|
||||
<p className="text-sm whitespace-pre-wrap">{response}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{chatMutation.isPending && (
|
||||
<div className="flex justify-center py-4">
|
||||
<Icon icon="loader" size={32} className="animate-spin" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
value={message}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setMessage(e.target.value)}
|
||||
placeholder={`Ask me anything about the ${selectedProject?.Project.name || "..."} project...`}
|
||||
disabled={chatMutation.isPending}
|
||||
showCounter={false}
|
||||
/>
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={
|
||||
chatMutation.isPending || !message.trim() || !selectedOrganisation || !selectedProject
|
||||
}
|
||||
>
|
||||
{chatMutation.isPending ? "Sending..." : "Send"}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{error && (
|
||||
<div className="p-4 bg-destructive/10 border border-destructive">
|
||||
<p className="text-destructive text-sm">{error}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -15,6 +15,7 @@ export function Field({
|
||||
spellcheck,
|
||||
maxLength,
|
||||
showCounter = true,
|
||||
disabled = false,
|
||||
}: {
|
||||
label: string;
|
||||
value?: string;
|
||||
@@ -28,6 +29,7 @@ export function Field({
|
||||
spellcheck?: boolean;
|
||||
maxLength?: number;
|
||||
showCounter?: boolean;
|
||||
disabled?: boolean;
|
||||
}) {
|
||||
const [internalTouched, setInternalTouched] = useState(false);
|
||||
const isTouched = submitAttempted || internalTouched;
|
||||
@@ -62,6 +64,7 @@ export function Field({
|
||||
spellCheck={spellcheck}
|
||||
maxLength={maxLength}
|
||||
showCounter={showCounter}
|
||||
disabled={disabled}
|
||||
/>
|
||||
<div className="flex items-end justify-end w-full text-xs mb-0 -mt-1">
|
||||
{error || invalidMessage !== "" ? (
|
||||
|
||||
@@ -22,6 +22,11 @@ const iconButtonVariants = cva(
|
||||
sm: "w-5 h-5",
|
||||
md: "w-9 h-9",
|
||||
lg: "w-10 h-10",
|
||||
xl: "w-12 h-12",
|
||||
"2xl": "w-14 h-14",
|
||||
"3xl": "w-16 h-16",
|
||||
"4xl": "w-18 h-18",
|
||||
"5xl": "w-20 h-20",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
Edit as PixelEdit,
|
||||
EyeClosed as PixelEyeClosed,
|
||||
AddGrid as PixelGridAdd,
|
||||
HumanHandsup as PixelHandsUp,
|
||||
Home as PixelHome,
|
||||
InfoBox as PixelInfo,
|
||||
Link as PixelLink,
|
||||
@@ -61,6 +62,7 @@ import {
|
||||
DotsThreeVerticalIcon as PhosphorDotsThreeVertical,
|
||||
PencilSimpleIcon as PhosphorEdit,
|
||||
EyeClosedIcon as PhosphorEyeClosed,
|
||||
PersonArmsSpreadIcon as PhosphorHandsUp,
|
||||
HashIcon as PhosphorHash,
|
||||
HashStraightIcon as PhosphorHashStraight,
|
||||
HouseIcon as PhosphorHome,
|
||||
@@ -123,6 +125,7 @@ import {
|
||||
Moon,
|
||||
OctagonXIcon,
|
||||
Pause,
|
||||
PersonStanding,
|
||||
Play,
|
||||
Plus,
|
||||
Rocket,
|
||||
@@ -181,6 +184,7 @@ const icons = {
|
||||
phosphor: PhosphorDotsSixVertical,
|
||||
},
|
||||
hash: { lucide: Hash, pixel: PhosphorHashStraight, phosphor: PhosphorHash },
|
||||
handsUp: { lucide: PersonStanding, pixel: PixelHandsUp, phosphor: PhosphorHandsUp },
|
||||
home: { lucide: LucideHome, pixel: PixelHome, phosphor: PhosphorHome },
|
||||
info: { lucide: InfoIcon, pixel: PixelInfo, phosphor: PhosphorInfo },
|
||||
layoutDashboard: { lucide: LayoutDashboard, pixel: PixelDashboard, phosphor: PhosphorLayout },
|
||||
|
||||
Reference in New Issue
Block a user