Skip to content

Commit

Permalink
fix: improve fuzzing (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra authored May 15, 2024
1 parent ccf39c3 commit 8b1a6c5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
8 changes: 5 additions & 3 deletions ui/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Image from 'next/image';
import { Search } from '@/components/Search';
import { SearchSkeleton } from '@/components/SearchSkeleton';
import { Footer } from '@/components/Footer';
import { type Address } from '@/types';
import { type SearchItem } from '@/types';
import logo from '@/assets/logo.svg';

function isEthereumAddress(value: any): value is string {
Expand All @@ -16,8 +16,8 @@ function flattenObject(
obj: any,
path: string[] = [],
chainId: number | null = null,
): Address[] {
const result: Address[] = [];
): SearchItem[] {
const result: SearchItem[] = [];
const entries = Object.entries(obj).sort(([keyA], [keyB]) => {
if (keyA === 'CHAIN_ID') return -1;
if (keyB === 'CHAIN_ID') return 1;
Expand All @@ -42,6 +42,8 @@ function flattenObject(
chainId,
link,
searchPath: newPath.join(''),
library: newPath[0],
key: newPath[newPath.length - 1],
});
}
}
Expand Down
10 changes: 5 additions & 5 deletions ui/src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
import { useState, useEffect, useCallback, useMemo, useRef } from 'react';
import { usePathname, useSearchParams } from 'next/navigation';
import { cn } from '@/utils/cn';
import { type Address } from '@/types';
import { type SearchItem } from '@/types';
import Fuse, { FuseResult } from 'fuse.js';
import { Box } from './Box';
import { SearchResult } from './SearchResult';

const fuseOptions = {
includeScore: true,
keys: ['searchPath', 'value'],
keys: ['searchPath', 'value', 'library', 'key'],
threshold: 0.2,
ignoreLocation: true,
// ignoreLocation: false,
useExtendedSearch: true,
};

const SEARCH_LIMIT = 32;
const DEBOUNCE_TIME = 150;

export const Search = ({ addresses }: { addresses: Address[] }) => {
export const Search = ({ addresses }: { addresses: SearchItem[] }) => {
const pathname = usePathname();
const searchParams = useSearchParams();

const searchString = searchParams.get('q');

const [search, setSearch] = useState(searchString || '');
const [results, setResults] = useState<FuseResult<Address>[]>([]);
const [results, setResults] = useState<FuseResult<SearchItem>[]>([]);
const [activeIndex, setActiveIndex] = useState(-1);

const refs = useRef<(HTMLAnchorElement | null)[]>([]);
Expand Down
4 changes: 2 additions & 2 deletions ui/src/components/SearchResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { FuseResult } from 'fuse.js';
import { Box } from '@/components/Box';
import { ChainIcon } from '@/components/ChainIcon';
import { cn } from '@/utils/cn';
import { type Address } from '@/types';
import { type SearchItem } from '@/types';

type SearchResultProps = {
result: FuseResult<Address>;
result: FuseResult<SearchItem>;
tabIndex: number;
};

Expand Down
8 changes: 6 additions & 2 deletions ui/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export type Address = {
export type SearchItem = {
path: string[];
value: string;
chainId: number | null;
link: string;
searchPath: string;
};
// always the root entry point
library: string;
// always the explicit key of the value
key: string;
};

0 comments on commit 8b1a6c5

Please sign in to comment.