Skip to content

Commit

Permalink
Merge pull request #13 from byrdocs/minisearch
Browse files Browse the repository at this point in the history
重构搜索和优化布局
  • Loading branch information
YouXam authored Dec 9, 2024
2 parents 63413ce + 1bc8789 commit c8a489b
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 186 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"@radix-ui/react-tooltip": "^1.1.4",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"fuse.js": "^7.0.0",
"lucide-react": "^0.459.0",
"minisearch": "^7.1.1",
"next-themes": "^0.4.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
17 changes: 8 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/components/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function ItemCover({ src, alt, index, className, onClick }: { index?: number, sr
}
}}
className={cn(
"object-cover transition-opacity duration-100 max-w-full max-h-full w-full my-auto " + className,
"object-cover transition-opacity duration-100 max-w-full max-h-full w-full my-auto" + className,
{
"group-hover:opacity-30": !isError,
}
Expand Down Expand Up @@ -300,7 +300,7 @@ export const ItemDisplay: React.FC<{ item: Item, index?: number, onPreview: (url
<div className="space-x-1 -my-[1px] md:mt-2">
<ItemBadge variant={"default"} color="green">试卷</ItemBadge>
<ItemBadge>{item.data.filetype}</ItemBadge>
{item.data.college ? item.data.college.map(x => <ItemBadge>{x}</ItemBadge>) : null}
{item.data.college ? item.data.college.map(x => <ItemBadge key={x}>{x}</ItemBadge>) : null}
{item.data.filesize ? <ItemBadge>{formatFileSize(item.data.filesize)}</ItemBadge> : null}
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/components/logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function Logo({
switch (size) {
case 0:
return (
<h1 className={`text-2xl sm:text-3xl md:text-4xl text-center font-bold italic select-none ` + className} style={{ fontFamily: "'Saira Variable', sans-serif" }}>
byrdocs<span className={`sm:text-xl md:text-2xl`}>.org</span>
<h1 className={`text-3xl md:text-4xl text-center font-bold italic select-none ` + className} style={{ fontFamily: "'Saira Variable', sans-serif" }}>
byrdocs<span className={`text-xl md:text-2xl`}>.org</span>
</h1>
)
case 1:
Expand All @@ -20,8 +20,8 @@ export function Logo({
)
case 2:
return (
<h1 className={`text-4xl sm:text-5xl md:text-6xl text-center font-bold italic select-none ` + className} style={{ fontFamily: "'Saira Variable', sans-serif" }}>
byrdocs<span className={`text-2xl sm:text-3xl md:text-4xl`}>.org</span>
<h1 className={`text-5xl md:text-6xl text-center font-bold italic select-none ` + className} style={{ fontFamily: "'Saira Variable', sans-serif" }}>
byrdocs<span className={`text-3xl md:text-4xl`}>.org</span>
</h1>
)
default:
Expand Down
77 changes: 77 additions & 0 deletions src/components/search-list.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Item } from "@/types"
import { useEffect, useState, } from "react"
import { ItemDisplay } from "./item"
import MiniSearch from "minisearch"

const minisearch = new MiniSearch({
fields: ["data.title", "data.authors", "data.translators", "data.publisher",
"data.edition", "data.course.name", "data.course.type", "data.stage",
"data.college"],
storeFields: ['type', 'data', 'id'],
tokenize: s => s.split(''),
extractField: (document, fieldName) => {
return fieldName.split('.').reduce((doc, key) => doc && doc[key], document)
}
})

export function SearchList({
keyword,
documents,
searching,
category,
debounceing,
onPreview
}: {
keyword: string
documents: Item[]
searching: boolean
debounceing: boolean
category: string
onPreview: (url: string) => void
}) {
const [searchResults, setSearchResults] = useState<Item[]>([]);

useEffect(() => {
minisearch.addAll(documents)
return () => {
minisearch.removeAll()
}
}, [documents])

useEffect(() => {
const results = minisearch.search(keyword, {
filter: (result) => category === 'all' || category === result.type
});
setSearchResults(results as unknown as Item[])
}, [keyword, category, documents]);

if (!searching || searchResults.length === 0 && debounceing) {
return (
<div className="min-h-[calc(100vh-280px)] xl:min-h-[calc(100vh-256px)] text-center text-muted-foreground p-0 md:p-5 flex">
<div className="text-xl sm:text-2xl font-light m-auto ">
搜索书籍、试卷和资料
</div>
</div>
)
}

return searchResults.length !== 0 ?
(<div className="min-h-[calc(100vh-280px)] xl:min-h-[calc(100vh-256px)] space-y-3 md:w-[800px] w-full md:mx-auto p-0 md:p-5">
{searchResults.map((item, index) => (
<ItemDisplay key={item.id} item={item as unknown as Item} index={index} onPreview={onPreview} />
))}
</div>) : (
<div className="min-h-[calc(100vh-280px)] xl:min-h-[calc(100vh-256px)] text-center text-muted-foreground p-0 md:p-5 flex">
<div className="text-xl sm:text-2xl font-light m-auto ">
<div className="px-2">
<div className="mb-4">没有找到相关结果</div>
<div className="text-xs sm:text-base mb-2">注意使用全称搜索,例如“高等数学”而非“高数”</div>
<hr />
<div className="text-xs sm:text-base mt-2">
已有文件?<a className="text-blue-500 hover:text-blue-400" target="_blank" href="https://github.com/byrdocs/byrdocs-archive/blob/master/CONTRIBUTING.md">上传到 BYR Docs</a>
</div>
</div>
</div>
</div>
)
}
Loading

0 comments on commit c8a489b

Please sign in to comment.