Skip to content

Commit

Permalink
feat(app): init search command
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jun 1, 2023
1 parent 95db4c0 commit e03f43a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/animegarden/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface FetchResourceDetailOptions {
retry?: number;
}

interface SearchParams {
export interface SearchParams {
search: string[];

include: string[][];
Expand Down
23 changes: 23 additions & 0 deletions packages/app/src/components/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { SearchParams } from 'animegarden';

import useSWR from 'swr';
import { Command } from 'cmdk';
import { tradToSimple } from 'simptrad';
Expand Down Expand Up @@ -47,6 +49,10 @@ const useActiveElement = () => {
};
};

const initialized = {
url: location as Location | undefined
};

export default function Search() {
const ref = useRef<HTMLDivElement | null>(null);
const inputRef = useRef<HTMLInputElement | null>(null);
Expand All @@ -55,6 +61,17 @@ export default function Search() {
const [input, setInput] = useState('');
const [search, setSearch] = useState('');

// Init search input
if (initialized.url !== undefined) {
const searchParams = new URLSearchParams(initialized.url.search);
const cmd = searchParams.get('cmd');
if (cmd !== undefined && typeof cmd === 'string') {
setInput(cmd);
setSearch(cmd);
}
initialized.url = undefined;
}

const setDebounceSearch = debounce((value: string) => {
if (value !== search) {
setSearch(value);
Expand Down Expand Up @@ -214,6 +231,11 @@ export default function Search() {
);
}

function stringifySearch(search: SearchParams | undefined) {
if (!search) return '';
return `${search.search.join(' ')}`;
}

function parseSearch(search: string) {
const splitted = search
.split(' ')
Expand Down Expand Up @@ -309,6 +331,7 @@ function goToSearch(search: string) {
if (after !== undefined) {
query.push(`after=${after.toISOString()}`);
}
query.push(`cmd=${encodeURIComponent(search)}`);
goTo(`/resources/1?${query.join('&')}`);
}
}
Expand Down

0 comments on commit e03f43a

Please sign in to comment.