diff --git a/packages/client/src/components/search/index.tsx b/packages/client/src/components/search/index.tsx index 41c2fe7..0bddb5b 100644 --- a/packages/client/src/components/search/index.tsx +++ b/packages/client/src/components/search/index.tsx @@ -1,47 +1,102 @@ -import React, { useCallback, useEffect, useMemo, useState } from "react"; -import Link from "next/link"; -import Router from "next/router"; -import { Typography, Button, Modal, Input } from "@douyinfe/semi-ui"; -import { IconSearch } from "components/icons"; -import { IDocument } from "@think/domains"; -import { useRecentDocuments } from "data/document"; -import { useToggle } from "hooks/useToggle"; -import { searchDocument } from "services/document"; -import { Empty } from "components/empty"; -import { DataRender } from "components/data-render"; -import { LocaleTime } from "components/locale-time"; -import { DocumentStar } from "components/document/star"; -import { IconDocumentFill } from "components/icons/IconDocumentFill"; -import styles from "./index.module.scss"; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import Link from 'next/link'; +import Router from 'next/router'; +import { Typography, Button, Modal, Input, Spin } from '@douyinfe/semi-ui'; +import { IconSearch as SemiIconSearch } from '@douyinfe/semi-icons'; +import { IconSearch } from 'components/icons'; +import { IDocument } from '@think/domains'; +import { useRecentDocuments } from 'data/document'; +import { useToggle } from 'hooks/useToggle'; +import { useAsyncLoading } from 'hooks/useAsyncLoading'; +import { searchDocument } from 'services/document'; +import { Empty } from 'components/empty'; +import { DataRender } from 'components/data-render'; +import { LocaleTime } from 'components/locale-time'; +import { DocumentStar } from 'components/document/star'; +import { IconDocumentFill } from 'components/icons/IconDocumentFill'; +import styles from './index.module.scss'; const { Text } = Typography; +const List: React.FC<{ data: IDocument[] }> = ({ data }) => { + return ( +
+ {data.length ? ( + data.map((doc) => { + return ( +
+ + +
+ +
+ + {doc.title} + + + + 创建者: + {doc.createUser && doc.createUser.name} •{' '} + + +
+
+
+ +
+
+ +
+ ); + }) + ) : ( + + )} +
+ ); +}; + export const Search = () => { const [visible, toggleVisible] = useToggle(false); - const { data: recentDocs, loading, error } = useRecentDocuments(); - const [keyword, setKeyword] = useState(""); + const { data: recentDocs } = useRecentDocuments(); + const [searchApi, loading] = useAsyncLoading(searchDocument, 10); + const [keyword, setKeyword] = useState(''); + const [error, setError] = useState(null); const [searchDocs, setSearchDocs] = useState([]); - const data = useMemo( - () => (searchDocs.length ? searchDocs : recentDocs), - [searchDocs.length, recentDocs] - ); const search = useCallback(() => { - searchDocument(keyword).then((res) => { - setSearchDocs(res); - }); - }, [keyword]); + setError(null); + searchApi(keyword) + .then((res) => { + setSearchDocs(res); + }) + .catch((err) => { + setError(err); + }); + }, [searchApi, keyword]); useEffect(() => { const fn = () => { toggleVisible(false); }; - Router.events.on("routeChangeStart", fn); + + Router.events.on('routeChangeStart', fn); return () => { - Router.events.off("routeChangeStart", fn); + Router.events.off('routeChangeStart', fn); }; - }, []); + }, [toggleVisible]); return ( <> @@ -56,68 +111,53 @@ export const Search = () => { title="文档搜索" footer={null} onCancel={toggleVisible} + style={{ + maxWidth: '96vw', + }} >
- setKeyword(val)} - onEnterPress={search} - /> - { - return ( -
- {data.length ? ( - data.map((doc) => { - return ( -
- - -
- -
- - {doc.title} - - - - 创建者: - {doc.createUser && - doc.createUser.name} •{" "} - - -
-
-
- -
-
- -
- ); - }) - ) : ( - - )} +
+ { + setSearchDocs([]); + setKeyword(val); + }} + onEnterPress={search} + suffix={ + + } + showClear + /> +
+
+ +
- ); - }} - /> + } + error={error} + normalContent={() => } + /> +
+ 最近访问的文档 + +
+