diff --git a/apps/web/app/(dash)/home/filterSpaces.tsx b/apps/web/app/(dash)/home/filterSpaces.tsx new file mode 100644 index 00000000..6a8ad9ec --- /dev/null +++ b/apps/web/app/(dash)/home/filterSpaces.tsx @@ -0,0 +1,89 @@ +import { ChevronUpDownIcon } from "@heroicons/react/24/outline"; +import { + Command, + CommandGroup, + CommandInput, + CommandItem, + CommandList, +} from "@repo/ui/shadcn/command"; +import { Check } from "lucide-react"; +import React, { useState } from "react"; + +type space = { + id: number; + name: string; +}; + +export function FilterSpaces({ + initialSpaces, + selectedSpaces, + setSelectedSpaces +}: { + initialSpaces: space[]; + selectedSpaces: space[]; + setSelectedSpaces: React.Dispatch> +}) { + const [input, setInput] = useState(""); + + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key === "Backspace" && input === "") { + setSelectedSpaces((prevValue) => prevValue.slice(0, -1)); + } + }; + + const handleSelect = (selectedSpace: space) => { + setSelectedSpaces((current) => + current.some((space) => space.id === selectedSpace.id) + ? current.filter((space) => space.id !== selectedSpace.id) + : [...current, selectedSpace] + ); + }; + + return ( +
+
+ {selectedSpaces.map((v) => ( + + ))} +
+ +
+ setInput(e.currentTarget.value)} + value={input} + /> + +
+ + + {initialSpaces.map((space) => ( + handleSelect(space)} + > + v.id === space.id) ? "opacity-100" : "opacity-0"}`} + /> + {space.name} + + ))} + + +
+
+ ); +} diff --git a/apps/web/app/(dash)/home/page.tsx b/apps/web/app/(dash)/home/page.tsx index 68a14d53..43a4faa1 100644 --- a/apps/web/app/(dash)/home/page.tsx +++ b/apps/web/app/(dash)/home/page.tsx @@ -83,7 +83,6 @@ function Page({ ); }} initialSpaces={spaces} - setInitialSpaces={setSpaces} /> diff --git a/apps/web/app/(dash)/home/queryinput.tsx b/apps/web/app/(dash)/home/queryinput.tsx index c7267298..f402b11b 100644 --- a/apps/web/app/(dash)/home/queryinput.tsx +++ b/apps/web/app/(dash)/home/queryinput.tsx @@ -2,23 +2,17 @@ import { ArrowRightIcon } from "@repo/ui/icons"; import Image from "next/image"; -import React, { useEffect, useMemo, useState } from "react"; +import React, { useState } from "react"; import Divider from "@repo/ui/shadcn/divider"; -import { useRouter } from "next/navigation"; -import { getSpaces } from "@/app/actions/fetchers"; -import Combobox from "@repo/ui/shadcn/combobox"; -import { MinusIcon } from "lucide-react"; -import { toast } from "sonner"; -import { createSpace } from "@/app/actions/doers"; +import { FilterSpaces } from "./filterSpaces"; function QueryInput({ + initialSpaces, initialQuery = "", - initialSpaces = [], disabled = false, className, mini = false, handleSubmit, - setInitialSpaces, }: { initialQuery?: string; initialSpaces?: { @@ -29,45 +23,25 @@ function QueryInput({ className?: string; mini?: boolean; handleSubmit: (q: string, spaces: { id: number; name: string }[]) => void; - setInitialSpaces?: React.Dispatch< - React.SetStateAction<{ id: number; name: string }[]> - >; }) { const [q, setQ] = useState(initialQuery); - const [selectedSpaces, setSelectedSpaces] = useState([]); - - const options = useMemo( - () => - initialSpaces.map((x) => ({ - label: x.name, - value: x.id.toString(), - })), - [initialSpaces], - ); - - const preparedSpaces = useMemo( - () => - initialSpaces - .filter((x) => selectedSpaces.includes(x.id)) - .map((x) => { - return { - id: x.id, - name: x.name, - }; - }), - [selectedSpaces, initialSpaces], - ); + const [selectedSpaces, setSelectedSpaces] = useState< + { id: number; name: string }[] + >([]); return (
{/* input and action button */}
{ - handleSubmit(q, preparedSpaces); + if (q.trim().length === 0) { + return; + } + handleSubmit(q, selectedSpaces); setQ(""); }} className="flex gap-4 p-3" @@ -85,7 +59,7 @@ function QueryInput({ if (q.trim().length === 0) { return; } - handleSubmit(q, preparedSpaces); + handleSubmit(q, selectedSpaces); setQ(""); } }} @@ -96,84 +70,24 @@ function QueryInput({ -
+ {" "} + {!mini && ( + <> + + + + )}
{/* selected sources */} - {!mini && ( - <> - -
- - setSelectedSpaces((prev) => { - if (v === "") { - return []; - } - return [...prev, parseInt(v)]; - }) - } - onSubmit={async (spaceName) => { - const space = options.find((x) => x.label === spaceName); - toast.info("Creating space..."); - - if (space) { - toast.error("A space with that name already exists."); - } - - const creationTask = await createSpace(spaceName); - if (creationTask.success && creationTask.data) { - toast.success("Space created " + creationTask.data); - setInitialSpaces?.((prev) => [ - ...prev, - { - name: spaceName, - id: creationTask.data!, - }, - ]); - setSelectedSpaces((prev) => [...prev, creationTask.data!]); - } else { - toast.error( - "Space creation failed: " + creationTask.error ?? - "Unknown error", - ); - } - }} - placeholder="Chat with a space..." - /> - -
- {preparedSpaces.map((x, idx) => ( - - ))} -
-
- - )}
); } diff --git a/packages/ui/shadcn/command.tsx b/packages/ui/shadcn/command.tsx index ed929aa2..94c4434f 100644 --- a/packages/ui/shadcn/command.tsx +++ b/packages/ui/shadcn/command.tsx @@ -37,9 +37,8 @@ const CommandDialog = ({ children, ...props }: CommandDialogProps) => { const CommandInput = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef ->(({ className, ...props }, ref) => ( +>(({ className , ...props }, ref) => (
-