diff --git a/.changeset/healthy-peaches-pull.md b/.changeset/healthy-peaches-pull.md new file mode 100644 index 0000000..92f5741 --- /dev/null +++ b/.changeset/healthy-peaches-pull.md @@ -0,0 +1,5 @@ +--- +"useful-dependents": patch +--- + +feat: add empty result diff --git a/src/components/result.tsx b/src/components/result.tsx index d6f960e..8ff99d3 100644 --- a/src/components/result.tsx +++ b/src/components/result.tsx @@ -12,12 +12,24 @@ import { Button } from './ui/button' import { Icons } from './icons' import { PackageSelect } from './package-select' +const EmptySlate: React.FC = ({ children }) => { + return ( +
+
{children}
+

+ Please try again later. +

+
+ ) +} + export const Result: React.FC = () => { const searchParams = useSearchParams() const repo = searchParams.get('repo') const packageId = searchParams.get('package_id') const [loadPage, setLoadPage] = useState(5) - const { data, setSize, packages, size, isLoading, isFinished } = useDependents(repo as string, packageId as string) + const { data, setSize, packages, size, isLoading, isFinished, emptyText } = + useDependents(repo as string, packageId as string) const [ignoreZeroStar, setIgnoreZeroStar] = useState(true) const sortedData = useMemo(() => { const filteredData = ignoreZeroStar ? data.filter(d => d.stars > 0) : data @@ -80,21 +92,23 @@ export const Result: React.FC = () => { avatarUrl={result.avatarUrl} /> ))} + {emptyText ? {emptyText} : null} -
- {isFetching ? ( - - ) : null} - {!isFetching && !isFinished ? ( - - ) : null} -
+ {!isFinished ? ( +
+ {isFetching ? ( + + ) : ( + + )} +
+ ) : null} ) diff --git a/src/hooks/useDependents.ts b/src/hooks/useDependents.ts index 77f6f52..db6c519 100644 --- a/src/hooks/useDependents.ts +++ b/src/hooks/useDependents.ts @@ -45,6 +45,7 @@ export const useDependents = (repo: string, packageId?: string) => { return { ...response, isFinished: response.data?.some(d => !d?.nextURL), + emptyText: response.data?.[0]?.empty, data: data.filter(Boolean), packages: response.data ? response.data[0]?.packages : [], } diff --git a/src/lib/parse-page.ts b/src/lib/parse-page.ts index 7a3e2b2..913cb4d 100644 --- a/src/lib/parse-page.ts +++ b/src/lib/parse-page.ts @@ -18,6 +18,7 @@ export type ParseResult = { dependents: Dependent[] nextURL: string | null packages: Package[] + empty: string } function parsePackages($: CheerioAPI): Package[] { @@ -39,39 +40,47 @@ export async function parsePage(res?: string): Promise { const $ = cheerio.load(res) const packages = parsePackages($) const dependents: Dependent[] = [] + let empty = '' + let nextURL = null - $('#dependents > div.Box > div').each((i, e) => { - // The first element is the header row - if (i === 0) return + if ($('#dependents > .blankslate')) { + empty = $('#dependents .blankslate-heading').text().trim() + } - const avatarUrl = $(e).find('.avatar').attr('src') ?? '' + if (!empty) { + $('#dependents > div.Box > div').each((i, e) => { + // The first element is the header row + if (i === 0) return - const repo = - `${$(e).find('span > a:nth-child(1)').text().trim()}/${$(e).find('span > a:nth-child(2)').text().trim()}` + const avatarUrl = $(e).find('.avatar').attr('src') ?? '' - const stars = extractNumbers( - $(e).find('div > span:nth-child(1)').last().text().trim() - ) - const forks = extractNumbers( - $(e).find('div > span:nth-child(2)').last().text().trim() - ) + const repo = + `${$(e).find('span > a:nth-child(1)').text().trim()}/${$(e).find('span > a:nth-child(2)').text().trim()}` - dependents.push({ - avatarUrl, - repo, - stars, - forks, - }) - }) + const stars = extractNumbers( + $(e).find('div > span:nth-child(1)').last().text().trim() + ) + const forks = extractNumbers( + $(e).find('div > span:nth-child(2)').last().text().trim() + ) - const nextURL = - $('#dependents > div.paginate-container > div > a:contains("Next")')?.prop( - 'href' - ) || null + dependents.push({ + avatarUrl, + repo, + stars, + forks, + }) + }) + nextURL = + $('#dependents > div.paginate-container > div > a:contains("Next")')?.prop( + 'href' + ) || null + } return { dependents, nextURL, packages, + empty, } }