aboutsummaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-11-05 22:22:37 +0200
committeromagdy7 <omar.professional8777@gmail.com>2023-11-05 22:22:37 +0200
commit121becd9cf52c735e2d33032873fca8fc6e3db54 (patch)
treeafcbd0697808faf66d7cb84f2852d979b5da8550 /src/components
parent7c9ade2c19b5b65721918dd291de54bcd4dc805b (diff)
downloadollama-logseq-121becd9cf52c735e2d33032873fca8fc6e3db54.tar.xz
ollama-logseq-121becd9cf52c735e2d33032873fca8fc6e3db54.zip
Added all the main features of the plugin and also added setting and made the plugin respect themeing
Diffstat (limited to 'src/components')
-rw-r--r--src/components/CommandPallete.tsx96
-rw-r--r--src/components/OllamaCommandPallete.tsx54
-rw-r--r--src/components/PromptAI.tsx23
-rw-r--r--src/components/ui/command.tsx153
-rw-r--r--src/components/ui/dialog.tsx120
-rw-r--r--src/components/ui/input.tsx25
6 files changed, 365 insertions, 106 deletions
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" ? (
- <PromptAI type="prompt" />
- ) : isExecute && inputValue === "Define" ? (
- <PromptAI type="define" />
- ) : !isExecute ? (
- <div className='w-screen flex items-center justify-center'>
- <div className="rounded-2xl bg-gray-800 text-white p-4 dark:bg-slate-900 dark:text-gray-100 w-3/4">
- <input
- ref={inputRef}
- type="text"
- placeholder="AI action..."
- value={inputValue}
- onChange={handleInputChange}
- onKeyDown={handleKeyDown}
- id="ai-input"
- className="bg-gray-700 text-white px-2 py-1 rounded-md dark:bg-gray-800 w-full"
- />
- <ul className="mt-2 max-h-90 overflow-y-auto">
- {filteredOptions.map((option: { label: string }, index: number) => (
- <li
- key={index}
- onClick={() => 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}
- </li>
- ))}
- </ul>
- </div>
- </div>
- ) : 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' ? (<PromptAI theme={theme} type={selection} />) : (
+ <Command className={(theme === 'dark' ? "dark dark:bg-gray-900" : "bg-gray-200") + " rounded-lg border shadow-md w-1/2"}>
+ <CommandInput className="ai-input" placeholder="Type a command or search..." />
+ <CommandList>
+ <CommandEmpty>No results found.</CommandEmpty>
+ {
+ options.map((option) => (
+ <CommandItem key={option} onSelect={handleSelection} className="text-lg">
+ <span>{option}</span>
+ </CommandItem>
+ ))
+ }
+ </CommandList>
+ </Command>
+ )
+ )
+}
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<HTMLDivElement> = (e) => {
if (e.key === 'Enter') {
setHitEnter(true)
}
@@ -30,14 +33,14 @@ export const PromptAI = ({ type }) => {
return (
!hitEnter ? (
<div className='w-screen text-center'>
- <input
+ <Input
autoFocus
type="text"
placeholder={placeholder}
value={inputValue}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
- className="bg-gray-700 text-white px-2 py-1 rounded-md dark:bg-gray-800 inline-block w-3/4"
+ className={(theme === 'dark' ? "dark text-white dark:bg-gray-800" : "text-black bg-gray-200") + "px-2 py-1 rounded-md inline-block w-3/4"}
/>
</div>
) : 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<typeof CommandPrimitive>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive
+ ref={ref}
+ className={cn(
+ "flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground",
+ className
+ )}
+ {...props}
+ />
+))
+Command.displayName = CommandPrimitive.displayName
+
+interface CommandDialogProps extends DialogProps {}
+
+const CommandDialog = ({ children, ...props }: CommandDialogProps) => {
+ return (
+ <Dialog {...props}>
+ <DialogContent className="overflow-hidden p-0 shadow-lg">
+ <Command className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
+ {children}
+ </Command>
+ </DialogContent>
+ </Dialog>
+ )
+}
+
+const CommandInput = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Input>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>
+>(({ className, ...props }, ref) => (
+ <div className="flex items-center border-b px-3" cmdk-input-wrapper="">
+ <Search className="mr-2 h-4 w-4 shrink-0 opacity-50" />
+ <CommandPrimitive.Input
+ ref={ref}
+ className={cn(
+ "flex h-11 w-full rounded-md bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
+ className
+ )}
+ {...props}
+ />
+ </div>
+))
+
+CommandInput.displayName = CommandPrimitive.Input.displayName
+
+const CommandList = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.List>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.List>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive.List
+ ref={ref}
+ className={cn("max-h-[300px] overflow-y-auto overflow-x-hidden", className)}
+ {...props}
+ />
+))
+
+CommandList.displayName = CommandPrimitive.List.displayName
+
+const CommandEmpty = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Empty>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Empty>
+>((props, ref) => (
+ <CommandPrimitive.Empty
+ ref={ref}
+ className="py-6 text-center text-sm"
+ {...props}
+ />
+))
+
+CommandEmpty.displayName = CommandPrimitive.Empty.displayName
+
+const CommandGroup = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Group>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Group>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive.Group
+ ref={ref}
+ className={cn(
+ "overflow-hidden p-1 text-foreground [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground",
+ className
+ )}
+ {...props}
+ />
+))
+
+CommandGroup.displayName = CommandPrimitive.Group.displayName
+
+const CommandSeparator = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Separator>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Separator>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive.Separator
+ ref={ref}
+ className={cn("-mx-1 h-px bg-border", className)}
+ {...props}
+ />
+))
+CommandSeparator.displayName = CommandPrimitive.Separator.displayName
+
+const CommandItem = React.forwardRef<
+ React.ElementRef<typeof CommandPrimitive.Item>,
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item>
+>(({ className, ...props }, ref) => (
+ <CommandPrimitive.Item
+ ref={ref}
+ className={cn(
+ "relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none aria-selected:bg-accent aria-selected:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
+ className
+ )}
+ {...props}
+ />
+))
+
+CommandItem.displayName = CommandPrimitive.Item.displayName
+
+const CommandShortcut = ({
+ className,
+ ...props
+}: React.HTMLAttributes<HTMLSpanElement>) => {
+ return (
+ <span
+ className={cn(
+ "ml-auto text-xs tracking-widest text-muted-foreground",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+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<typeof DialogPrimitive.Overlay>,
+ React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
+>(({ className, ...props }, ref) => (
+ <DialogPrimitive.Overlay
+ ref={ref}
+ className={cn(
+ "fixed inset-0 z-50 bg-background/80 backdrop-blur-sm data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
+ className
+ )}
+ {...props}
+ />
+))
+DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
+
+const DialogContent = React.forwardRef<
+ React.ElementRef<typeof DialogPrimitive.Content>,
+ React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
+>(({ className, children, ...props }, ref) => (
+ <DialogPortal>
+ <DialogOverlay />
+ <DialogPrimitive.Content
+ ref={ref}
+ className={cn(
+ "fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg md:w-full",
+ className
+ )}
+ {...props}
+ >
+ {children}
+ <DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
+ <X className="h-4 w-4" />
+ <span className="sr-only">Close</span>
+ </DialogPrimitive.Close>
+ </DialogPrimitive.Content>
+ </DialogPortal>
+))
+DialogContent.displayName = DialogPrimitive.Content.displayName
+
+const DialogHeader = ({
+ className,
+ ...props
+}: React.HTMLAttributes<HTMLDivElement>) => (
+ <div
+ className={cn(
+ "flex flex-col space-y-1.5 text-center sm:text-left",
+ className
+ )}
+ {...props}
+ />
+)
+DialogHeader.displayName = "DialogHeader"
+
+const DialogFooter = ({
+ className,
+ ...props
+}: React.HTMLAttributes<HTMLDivElement>) => (
+ <div
+ className={cn(
+ "flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
+ className
+ )}
+ {...props}
+ />
+)
+DialogFooter.displayName = "DialogFooter"
+
+const DialogTitle = React.forwardRef<
+ React.ElementRef<typeof DialogPrimitive.Title>,
+ React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
+>(({ className, ...props }, ref) => (
+ <DialogPrimitive.Title
+ ref={ref}
+ className={cn(
+ "text-lg font-semibold leading-none tracking-tight",
+ className
+ )}
+ {...props}
+ />
+))
+DialogTitle.displayName = DialogPrimitive.Title.displayName
+
+const DialogDescription = React.forwardRef<
+ React.ElementRef<typeof DialogPrimitive.Description>,
+ React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
+>(({ className, ...props }, ref) => (
+ <DialogPrimitive.Description
+ ref={ref}
+ className={cn("text-sm text-muted-foreground", className)}
+ {...props}
+ />
+))
+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<HTMLInputElement> {}
+
+const Input = React.forwardRef<HTMLInputElement, InputProps>(
+ ({ className, type, ...props }, ref) => {
+ return (
+ <input
+ type={type}
+ className={cn(
+ "flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
+ className
+ )}
+ ref={ref}
+ {...props}
+ />
+ )
+ }
+)
+Input.displayName = "Input"
+
+export { Input }