From 121becd9cf52c735e2d33032873fca8fc6e3db54 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Sun, 5 Nov 2023 22:22:37 +0200 Subject: Added all the main features of the plugin and also added setting and made the plugin respect themeing --- src/components/CommandPallete.tsx | 96 -------------------- src/components/OllamaCommandPallete.tsx | 54 +++++++++++ src/components/PromptAI.tsx | 23 ++--- src/components/ui/command.tsx | 153 ++++++++++++++++++++++++++++++++ src/components/ui/dialog.tsx | 120 +++++++++++++++++++++++++ src/components/ui/input.tsx | 25 ++++++ 6 files changed, 365 insertions(+), 106 deletions(-) delete mode 100644 src/components/CommandPallete.tsx create mode 100644 src/components/OllamaCommandPallete.tsx create mode 100644 src/components/ui/command.tsx create mode 100644 src/components/ui/dialog.tsx create mode 100644 src/components/ui/input.tsx (limited to 'src/components') diff --git a/src/components/CommandPallete.tsx b/src/components/CommandPallete.tsx deleted file mode 100644 index e4f5924..0000000 --- a/src/components/CommandPallete.tsx +++ /dev/null @@ -1,96 +0,0 @@ -import React, { useState, useEffect, useRef } from 'react'; -import { DivideTaskIntoSubTasks, summarize } from '../ollama'; -import { PromptAI } from './PromptAI'; - - -function CommandPalette({ options }) { - console.log("rendered commana pallate") - const [inputValue, setInputValue] = useState(''); - const [selectedOption, setSelectedOption] = useState<{ label: string }>({ label: "Ask Ai" }); - const [filteredOptions, setFilteredOptions] = useState(options); - const [isExecute, setIsExecute] = useState(false) - const inputRef = useRef(null); - - useEffect(() => { - // Initially, select the first option. - if (filteredOptions.length > 0) { - setSelectedOption(filteredOptions[0]); - } - }, [filteredOptions]); - - - const handleInputChange = (e) => { - const query = e.target.value; - setInputValue(query); - - // Filter options based on the input. - const results = options.filter((option: { label: string }) => - option.label.toLowerCase().includes(query.toLowerCase()) - ); - setFilteredOptions(results); - }; - - const handleKeyDown = (e: KeyboardEvent) => { - if (e.key === 'ArrowUp' || e.key === 'ArrowDown' || e.key === 'Tab') { - e.preventDefault(); - - const currentIndex = filteredOptions.indexOf(selectedOption); - let newIndex = currentIndex; - - if (e.key === 'ArrowUp' || (e.shiftKey && e.key == 'Tab')) { - newIndex = (currentIndex - 1 + filteredOptions.length) % filteredOptions.length; - } else if (e.key === 'ArrowDown' || e.key === 'Tab') { - newIndex = (currentIndex + 1) % filteredOptions.length; - } - - setSelectedOption(filteredOptions[newIndex]); - } else if (e.key === 'Enter') { - if (selectedOption) { - setIsExecute(true) - setInputValue(selectedOption.label); - if (selectedOption.label === "Divide into subtasks") { - DivideTaskIntoSubTasks() - } else if (selectedOption.label === "Summarize") { - summarize() - } - } - } - }; - - return ( - isExecute && inputValue == "Ask Ai" ? ( - - ) : isExecute && inputValue === "Define" ? ( - - ) : !isExecute ? ( -
-
- -
    - {filteredOptions.map((option: { label: string }, index: number) => ( -
  • setSelectedOption(option)} - className={`p-2 cursor-pointer ${selectedOption === option ? 'bg-blue-600 text-white border-2 border-blue-500' : '' - } hover:bg-gray-600`} - > - {option.label} -
  • - ))} -
-
-
- ) : null - ); -} - -export default CommandPalette; diff --git a/src/components/OllamaCommandPallete.tsx b/src/components/OllamaCommandPallete.tsx new file mode 100644 index 0000000..d48d392 --- /dev/null +++ b/src/components/OllamaCommandPallete.tsx @@ -0,0 +1,54 @@ +import React, { KeyboardEventHandler, useEffect, useState } from "react" +import { + Command, + CommandEmpty, + CommandInput, + CommandItem, + CommandList, +} from "@/components/ui/command" +import { convertToFlashCard, DivideTaskIntoSubTasks, summarize } from "@/ollama"; +import { PromptAI } from "./PromptAI"; + +export function OllamaCommandPallete({ options, theme }: { options: string[], theme: string }) { + const [selection, setSelection] = useState('') + const [isEnterPressed, setIsEnterPressed] = useState(false); + const handleSelection = (selection: string) => { + setSelection(selection) + setIsEnterPressed(true); + switch (selection) { + case "divide into subtasks": + DivideTaskIntoSubTasks() + break; + case "summarize": + summarize() + break; + case "convert to flash card": + convertToFlashCard() + break; + default: + break; + } + } + + if (isEnterPressed && (selection !== 'ask ai' && selection !== 'define' && selection !== 'ask with context')) { + return null + } + + return ( + selection === 'ask with context' || selection === 'ask ai' || selection === 'define' ? () : ( + + + + No results found. + { + options.map((option) => ( + + {option} + + )) + } + + + ) + ) +} diff --git a/src/components/PromptAI.tsx b/src/components/PromptAI.tsx index 7a6b361..0b87917 100644 --- a/src/components/PromptAI.tsx +++ b/src/components/PromptAI.tsx @@ -1,28 +1,31 @@ -import React, { useEffect, useRef, useState } from 'react' -import { askAI, defineWord, DivideTaskIntoSubTasks } from '../ollama'; +import React, { KeyboardEventHandler, useEffect, useState } from 'react' +import { askAI, askWithContext, defineWord } from '../ollama'; +import { Input } from '@/components/ui/input'; -export const PromptAI = ({ type }) => { +export const PromptAI = ({ type, theme }: { type: string, theme: string }) => { - const placeholder = type === 'prompt' ? "Prompt..." : "Define..." + const placeholder = type === 'ask ai' ? "Prompt..." : "Define..." const [inputValue, setInputValue] = useState(''); const [hitEnter, setHitEnter] = useState(false) useEffect(() => { if (hitEnter) { - if (type === 'prompt') { + if (type === 'ask ai') { askAI(inputValue) - } else { + } else if (type === 'define') { defineWord(inputValue) + } else if (type === 'ask with context') { + askWithContext(inputValue) } } }, [hitEnter]) - const handleInputChange = (e) => { + const handleInputChange = (e: any) => { const query = e.target.value; setInputValue(query); }; - const handleKeyDown = (e) => { + const handleKeyDown: KeyboardEventHandler = (e) => { if (e.key === 'Enter') { setHitEnter(true) } @@ -30,14 +33,14 @@ export const PromptAI = ({ type }) => { return ( !hitEnter ? (
-
) : null diff --git a/src/components/ui/command.tsx b/src/components/ui/command.tsx new file mode 100644 index 0000000..c283b7b --- /dev/null +++ b/src/components/ui/command.tsx @@ -0,0 +1,153 @@ +import * as React from "react" +import { DialogProps } from "@radix-ui/react-dialog" +import { Command as CommandPrimitive } from "cmdk" +import { Search } from "lucide-react" + +import { cn } from "@/lib/utils" +import { Dialog, DialogContent } from "@/components/ui/dialog" + +const Command = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)) +Command.displayName = CommandPrimitive.displayName + +interface CommandDialogProps extends DialogProps {} + +const CommandDialog = ({ children, ...props }: CommandDialogProps) => { + return ( + + + + {children} + + + + ) +} + +const CommandInput = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( +
+ + +
+)) + +CommandInput.displayName = CommandPrimitive.Input.displayName + +const CommandList = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)) + +CommandList.displayName = CommandPrimitive.List.displayName + +const CommandEmpty = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>((props, ref) => ( + +)) + +CommandEmpty.displayName = CommandPrimitive.Empty.displayName + +const CommandGroup = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)) + +CommandGroup.displayName = CommandPrimitive.Group.displayName + +const CommandSeparator = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)) +CommandSeparator.displayName = CommandPrimitive.Separator.displayName + +const CommandItem = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)) + +CommandItem.displayName = CommandPrimitive.Item.displayName + +const CommandShortcut = ({ + className, + ...props +}: React.HTMLAttributes) => { + return ( + + ) +} +CommandShortcut.displayName = "CommandShortcut" + +export { + Command, + CommandDialog, + CommandInput, + CommandList, + CommandEmpty, + CommandGroup, + CommandItem, + CommandShortcut, + CommandSeparator, +} diff --git a/src/components/ui/dialog.tsx b/src/components/ui/dialog.tsx new file mode 100644 index 0000000..097fe47 --- /dev/null +++ b/src/components/ui/dialog.tsx @@ -0,0 +1,120 @@ +import * as React from "react" +import * as DialogPrimitive from "@radix-ui/react-dialog" +import { X } from "lucide-react" + +import { cn } from "@/lib/utils" + +const Dialog = DialogPrimitive.Root + +const DialogTrigger = DialogPrimitive.Trigger + +const DialogPortal = DialogPrimitive.Portal + +const DialogClose = DialogPrimitive.Close + +const DialogOverlay = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)) +DialogOverlay.displayName = DialogPrimitive.Overlay.displayName + +const DialogContent = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, children, ...props }, ref) => ( + + + + {children} + + + Close + + + +)) +DialogContent.displayName = DialogPrimitive.Content.displayName + +const DialogHeader = ({ + className, + ...props +}: React.HTMLAttributes) => ( +
+) +DialogHeader.displayName = "DialogHeader" + +const DialogFooter = ({ + className, + ...props +}: React.HTMLAttributes) => ( +
+) +DialogFooter.displayName = "DialogFooter" + +const DialogTitle = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)) +DialogTitle.displayName = DialogPrimitive.Title.displayName + +const DialogDescription = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)) +DialogDescription.displayName = DialogPrimitive.Description.displayName + +export { + Dialog, + DialogPortal, + DialogOverlay, + DialogClose, + DialogTrigger, + DialogContent, + DialogHeader, + DialogFooter, + DialogTitle, + DialogDescription, +} diff --git a/src/components/ui/input.tsx b/src/components/ui/input.tsx new file mode 100644 index 0000000..677d05f --- /dev/null +++ b/src/components/ui/input.tsx @@ -0,0 +1,25 @@ +import * as React from "react" + +import { cn } from "@/lib/utils" + +export interface InputProps + extends React.InputHTMLAttributes {} + +const Input = React.forwardRef( + ({ className, type, ...props }, ref) => { + return ( + + ) + } +) +Input.displayName = "Input" + +export { Input } -- cgit v1.2.3