From 7903a73c757b9a9161af685e0737f6cb5976b648 Mon Sep 17 00:00:00 2001 From: Mengdi Chen Date: Tue, 23 Apr 2024 10:38:11 -0400 Subject: [PATCH] Simplify Instructions & show in main UI (#130) * refactor: remove unused state * refactor: rename notes to instructions & hide json mode * feat: show matched notes in main UI * fix: skip knowledge display if no notes * fix: tsc issue --- src/common/App.tsx | 2 +- .../CustomKnowledgeBase/DefaultKnowledge.tsx | 4 +- .../CustomKnowledgeBase/HostKnowledge.tsx | 93 ++++++++++++------- .../CustomKnowledgeBase/MatchedNotes.tsx | 55 +++++++++++ .../CustomKnowledgeBase/NewKnowledgeForm.tsx | 22 ++--- src/common/CustomKnowledgeBase/Notes.tsx | 14 +++ src/common/CustomKnowledgeBase/index.tsx | 22 ++--- src/common/Settings.tsx | 4 +- src/common/TaskUI.tsx | 2 + src/state/currentTask.ts | 11 ++- 10 files changed, 163 insertions(+), 66 deletions(-) create mode 100644 src/common/CustomKnowledgeBase/MatchedNotes.tsx create mode 100644 src/common/CustomKnowledgeBase/Notes.tsx diff --git a/src/common/App.tsx b/src/common/App.tsx index 954d201..857dda0 100644 --- a/src/common/App.tsx +++ b/src/common/App.tsx @@ -23,7 +23,7 @@ const App = () => { return ( - + WebWand 🪄 diff --git a/src/common/CustomKnowledgeBase/DefaultKnowledge.tsx b/src/common/CustomKnowledgeBase/DefaultKnowledge.tsx index 861d47b..9d3f980 100644 --- a/src/common/CustomKnowledgeBase/DefaultKnowledge.tsx +++ b/src/common/CustomKnowledgeBase/DefaultKnowledge.tsx @@ -20,7 +20,7 @@ const DefaultKnowledge = () => { return ( <> { > - Default Knowledge Base + Default Instructions {Object.keys(defaultKnowledgeBase).map((host) => ( diff --git a/src/common/CustomKnowledgeBase/HostKnowledge.tsx b/src/common/CustomKnowledgeBase/HostKnowledge.tsx index 52dd6c2..862bef4 100644 --- a/src/common/CustomKnowledgeBase/HostKnowledge.tsx +++ b/src/common/CustomKnowledgeBase/HostKnowledge.tsx @@ -1,4 +1,4 @@ -import { DeleteIcon, CopyIcon, EditIcon } from "@chakra-ui/icons"; +import { DeleteIcon, EditIcon } from "@chakra-ui/icons"; import { Heading, Accordion, @@ -9,11 +9,11 @@ import { IconButton, Tooltip, Flex, - useToast, Box, } from "@chakra-ui/react"; import { fetchAllDefaultKnowledge } from "../../helpers/knowledge"; import { useAppState } from "@root/src/state/store"; +import Notes from "./Notes"; type HostKnowledgeProps = { host: string; @@ -26,7 +26,6 @@ const HostKnowledge = ({ isDefaultKnowledge, onEdit, }: HostKnowledgeProps) => { - const toast = useToast(); const updateSettings = useAppState((state) => state.settings.actions.update); const customKnowledgeBase = useAppState( (state) => state.settings.customKnowledgeBase, @@ -35,9 +34,20 @@ const HostKnowledge = ({ ? fetchAllDefaultKnowledge() : customKnowledgeBase; - const getJsonString = (): string => { - return JSON.stringify(knowledgeBase[host], null, 2); - }; + if (knowledgeBase[host] === undefined) { + return null; + } + const rules = knowledgeBase[host].rules; + if (rules === undefined) { + return null; + } + const hasNotes = rules.some( + (rule) => (rule.knowledge?.notes?.length ?? 0) > 0, + ); + // skip if no notes + if (!hasNotes) { + return null; + } const handleRemove = () => { const newKnowledge = { ...knowledgeBase }; @@ -45,6 +55,12 @@ const HostKnowledge = ({ updateSettings({ customKnowledgeBase: newKnowledge }); }; + // temporarily disable copy feature + /* + const getJsonString = (): string => { + return JSON.stringify(knowledgeBase[host], null, 2); + }; + const handleCopy = async () => { try { await navigator.clipboard.writeText(getJsonString()); @@ -65,13 +81,23 @@ const HostKnowledge = ({ }); } }; + */ return ( <> - - + + {!isDefaultKnowledge && ( - + - - } - size="sm" - variant="ghost" - onClick={handleCopy} - /> - - {knowledgeBase[host].rules?.map((rule, ruleIndex) => ( - -

- - - Rule {ruleIndex + 1} - - - -

- -
{getJsonString()}
-
-
- ))} + {rules.map((rule, ruleIndex) => { + // Skip rules without notes + if ( + rule.knowledge === undefined || + rule.knowledge.notes === undefined || + rule.knowledge.notes.length === 0 + ) { + return null; + } + return ( + +

+ + + Instructions Set {ruleIndex + 1} + + + +

+ + + +
+ ); + })}
); diff --git a/src/common/CustomKnowledgeBase/MatchedNotes.tsx b/src/common/CustomKnowledgeBase/MatchedNotes.tsx new file mode 100644 index 0000000..06e410e --- /dev/null +++ b/src/common/CustomKnowledgeBase/MatchedNotes.tsx @@ -0,0 +1,55 @@ +import { useAppState } from "../../state/store"; +import { + Alert, + AlertIcon, + AlertDescription, + Button, + Modal, + ModalBody, + ModalCloseButton, + ModalContent, + ModalHeader, + ModalOverlay, + useDisclosure, +} from "@chakra-ui/react"; +import Notes from "./Notes"; + +export default function MatchedNotes() { + const { isOpen, onOpen, onClose } = useDisclosure(); + const knowledge = useAppState((state) => state.currentTask.knowledgeInUse); + const notes = knowledge?.notes; + if (!notes || notes.length === 0) { + return null; + } + + return ( + <> + + + + + + WebWand uses the following instructions and tips: + + + + + + + + You can customize instructions in the settings menu. + + + + + + + ); +} diff --git a/src/common/CustomKnowledgeBase/NewKnowledgeForm.tsx b/src/common/CustomKnowledgeBase/NewKnowledgeForm.tsx index d5cbe0d..11aac7c 100644 --- a/src/common/CustomKnowledgeBase/NewKnowledgeForm.tsx +++ b/src/common/CustomKnowledgeBase/NewKnowledgeForm.tsx @@ -9,7 +9,6 @@ import { ModalBody, ModalFooter, Box, - Heading, InputGroup, InputRightElement, IconButton, @@ -235,7 +234,7 @@ const NewKnowledgeForm = ({ // ) return ( - + @@ -264,13 +263,13 @@ const NewKnowledgeForm = ({ onClose={() => setShowDuplicateAlert(false)} /> - {isEditMode ? "Edit" : "New"} Host Knowledge + {isEditMode ? "Edit" : "New"} Instructions - Host + Instructions for: - e.g. github.com + host of the website (e.g. google.com) {/* Rules Section */} - - Rules - {values.rules.map((rule, ruleIndex) => ( - Notes + Instructions {rule.knowledge.notes?.map((note, noteIndex) => ( {rule.knowledge.notes && rule.knowledge.notes.length > 1 && ( } variant="ghost" onClick={() => { @@ -476,7 +472,7 @@ const NewKnowledgeForm = ({ ); }} > - Add more notes + Add another instruction {/* Annotation Rules Section */} @@ -509,7 +505,7 @@ const NewKnowledgeForm = ({ setFieldValue("rules", updatedRules); }} > - Add more rules + Add another set of instructions diff --git a/src/common/CustomKnowledgeBase/Notes.tsx b/src/common/CustomKnowledgeBase/Notes.tsx new file mode 100644 index 0000000..b9f889a --- /dev/null +++ b/src/common/CustomKnowledgeBase/Notes.tsx @@ -0,0 +1,14 @@ +import { UnorderedList, ListItem } from "@chakra-ui/react"; + +export default function Notes({ notes }: { notes: string[] | undefined }) { + if (!notes || notes.length === 0) { + return null; + } + return ( + + {notes.map((note, index) => ( + {note} + ))} + + ); +} diff --git a/src/common/CustomKnowledgeBase/index.tsx b/src/common/CustomKnowledgeBase/index.tsx index fdb401c..1f49733 100644 --- a/src/common/CustomKnowledgeBase/index.tsx +++ b/src/common/CustomKnowledgeBase/index.tsx @@ -1,11 +1,11 @@ import React, { useState } from "react"; -import { Button, Text, VStack, Box, useDisclosure } from "@chakra-ui/react"; +import { Button, Text, VStack, Box } from "@chakra-ui/react"; import { useAppState } from "@root/src/state/store"; import NewKnowledgeForm from "./NewKnowledgeForm"; import { type EditingData } from "../../helpers/knowledge"; import DefaultKnowledge from "./DefaultKnowledge"; import HostKnowledge from "./HostKnowledge"; -import NewKnowledgeJson from "./NewKnowledgeJson"; +// import NewKnowledgeJson from "./NewKnowledgeJson"; import { findActiveTab } from "../../helpers/browserUtils"; const CustomKnowledgeBase = () => { @@ -16,11 +16,11 @@ const CustomKnowledgeBase = () => { const customKnowledgeBase = useAppState( (state) => state.settings.customKnowledgeBase, ); - const { - isOpen: isJsonInputOpen, - onOpen: openJsonInput, - onClose: closeJsonInput, - } = useDisclosure(); + // const { + // isOpen: isJsonInputOpen, + // onOpen: openJsonInput, + // onClose: closeJsonInput, + // } = useDisclosure(); const [defaultHost, setDefaultHost] = useState(""); const [currentURL, setCurrentUrl] = useState(""); @@ -76,10 +76,10 @@ const CustomKnowledgeBase = () => {
)) ) : ( - No custom knowledge found + No instructions found )} - - + + {/* */} { defaultHost={defaultHost} currentURL={currentURL} /> - + {/* */} ); }; diff --git a/src/common/Settings.tsx b/src/common/Settings.tsx index a1abbb6..ef2f451 100644 --- a/src/common/Settings.tsx +++ b/src/common/Settings.tsx @@ -109,7 +109,7 @@ const Settings = ({ setInSettingsView }: SettingsProps) => { {view === "knowledge" && ( - Knowledge + Instructions )} {view === "api" && ( @@ -204,7 +204,7 @@ const Settings = ({ setInSettingsView }: SettingsProps) => { /> - Custom Knowledge Base + Custom Instructions