Skip to content

Commit

Permalink
feat(app): store search history
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jun 12, 2023
1 parent 7972d6e commit b678878
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
"@astrojs/react": "^2.2.1",
"@astrojs/rss": "^2.4.3",
"@astrojs/sitemap": "^1.3.3",
"@nanostores/persistent": "^0.9.0",
"@nanostores/react": "^0.7.1",
"astro": "^2.6.3",
"astro-robots-txt": "^0.5.0",
"cmdk": "^0.2.0",
"nanostores": "^0.9.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"simptrad": "^0.1.0",
Expand Down
26 changes: 25 additions & 1 deletion packages/app/src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import type { SearchParams } from 'animegarden';

import useSWR from 'swr';
import { Command } from 'cmdk';
import { useStore } from '@nanostores/react';
import { tradToSimple } from 'simptrad';
import { useCallback, useEffect, useRef, useState } from 'react';

import '../styles/cmdk.css';

import { fansubs, types } from '../constant';
import { histories } from '../history';
import { fetchResources } from '../fetch';
import { fansubs, types } from '../constant';

{
document.addEventListener('keypress', (ev) => {
Expand Down Expand Up @@ -58,6 +60,7 @@ export default function Search() {
const inputRef = useRef<HTMLInputElement | null>(null);
const { active } = useActiveElement();

const history = useStore(histories);
const [input, setInput] = useState('');
const [search, setSearch] = useState('');

Expand Down Expand Up @@ -123,6 +126,9 @@ export default function Search() {

const selectGoToSearch = useCallback(() => {
if (input) {
const newHistories = [...new Set([input, ...histories.get()])];
histories.set(newHistories);

stopFetch();
goToSearch(input);
disable();
Expand Down Expand Up @@ -222,6 +228,24 @@ export default function Search() {
)}
</>
)}
{enable && history.length > 0 && (
<Command.Group heading="搜索历史">
{history.map((h) => (
<Command.Item
key={h}
value={h}
onMouseDown={() => {
onInputChange(h);
}}
onSelect={() => {
onInputChange(h);
}}
>
{h}
</Command.Item>
))}
</Command.Group>
)}
{enable && (
<>
{filteredTypes.length > 0 && (
Expand Down
6 changes: 6 additions & 0 deletions packages/app/src/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { persistentAtom } from '@nanostores/persistent';

export const histories = persistentAtom<string[]>('animegarden:histories', [], {
encode: JSON.stringify,
decode: JSON.parse
});
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit b678878

Please sign in to comment.