From 76b298ac8b983ec830c9dac990cce2b99cd3ef71 Mon Sep 17 00:00:00 2001 From: hzhang Date: Fri, 17 Jan 2025 09:20:20 +0000 Subject: [PATCH] add: markdown search feature --- src/components/Navigations/MarkdownNode.js | 38 ++++++++ src/components/Navigations/PathNode.js | 33 +------ src/components/Navigations/SideNavigation.js | 97 +++++++++++++++++--- src/utils/markdown-queries.js | 13 +++ 4 files changed, 138 insertions(+), 43 deletions(-) create mode 100644 src/components/Navigations/MarkdownNode.js diff --git a/src/components/Navigations/MarkdownNode.js b/src/components/Navigations/MarkdownNode.js new file mode 100644 index 0000000..938d0f2 --- /dev/null +++ b/src/components/Navigations/MarkdownNode.js @@ -0,0 +1,38 @@ +import {Link} from "react-router-dom"; +import PermissionGuard from "../PermissionGuard"; +import React from "react"; + +const MarkdownNode = ({markdown, handleMoveMarkdown}) => { + return ( +
  • +
    + + + {markdown.title} + + + +
    + + +
    +
    +
    + +
  • + ); +} + +export default MarkdownNode; \ No newline at end of file diff --git a/src/components/Navigations/PathNode.js b/src/components/Navigations/PathNode.js index 798ca92..7b06993 100644 --- a/src/components/Navigations/PathNode.js +++ b/src/components/Navigations/PathNode.js @@ -4,6 +4,7 @@ import PermissionGuard from "../PermissionGuard"; import "./PathNode.css"; import {useDeletePath, useMovePath, usePaths, useUpdatePath} from "../../utils/path-queries"; import {useIndexMarkdown, useMarkdownsByPath, useMoveMarkdown} from "../../utils/markdown-queries"; +import MarkdownNode from "./MarkdownNode"; const PathNode = ({ path, isRoot = false }) => { const [isExpanded, setIsExpanded] = useState(isRoot); @@ -175,34 +176,10 @@ const PathNode = ({ path, isRoot = false }) => { ))} {sortedMarkdowns.filter(md => md.title !== "index").map((markdown) => ( -
  • -
    - - - {markdown.title} - - - -
    - - -
    -
    -
    - -
  • + ))} )} diff --git a/src/components/Navigations/SideNavigation.js b/src/components/Navigations/SideNavigation.js index f0b8807..693cd30 100644 --- a/src/components/Navigations/SideNavigation.js +++ b/src/components/Navigations/SideNavigation.js @@ -3,12 +3,20 @@ import PathNode from "./PathNode"; import "./SideNavigation.css"; import {useDeletePath, usePaths, useUpdatePath} from "../../utils/path-queries"; import React from 'react'; +import {useSearchMarkdown} from "../../utils/markdown-queries"; +import MarkdownNode from "./MarkdownNode"; const SideNavigation = () => { const {data: paths, isLoading, error } = usePaths(1); const deletePath = useDeletePath(); const updatePath = useUpdatePath(); const [searchTerm, setSearchTerm] = React.useState(""); + const [keyword, setKeyword] = React.useState(""); + const [searchMode, setSearchMode] = React.useState(false); + + const {data: searchResults, isLoading: isSearching} = useSearchMarkdown(keyword, { + enabled: searchMode && !!searchMode, + }); const sortedPaths = paths ? paths.slice().sort((a, b) => a.order.localeCompare(b.order)) : []; @@ -22,6 +30,14 @@ const SideNavigation = () => { } }; + const handleSearch = () => { + setSearchMode(true); + setKeyword(searchTerm); + } + const exitSearch = () => { + setSearchMode(false); + } + const handleSave = (id, newName) => { updatePath.mutate({ id, data: {name: newName }} , { onError: (err) => { @@ -29,17 +45,56 @@ const SideNavigation = () => { } }); }; - if(isLoading){ + if(!searchMode && isLoading){ + return ; + } + if(searchMode && isSearching){ return ; } - if(error){ return ; } + return ( ); }; diff --git a/src/utils/markdown-queries.js b/src/utils/markdown-queries.js index e0ac65b..800fa84 100644 --- a/src/utils/markdown-queries.js +++ b/src/utils/markdown-queries.js @@ -91,3 +91,16 @@ export const useMoveMarkdown = () => { } ); }; + +export const useSearchMarkdown = (keyword) => { + const queryClient = useQueryClient(); + const config = useConfig(); + return useQuery(["markdownsByKeyword", keyword], + () => fetch_( + `${config.BACKEND_HOST}/api/markdown/search/${encodeURIComponent(keyword)}`, + ), + { + enabled: !!keyword, + } + ); +}; \ No newline at end of file