Skip to content

Commit 7132657

Browse files
committed
refactor: update search component to use typed responses for projects, contributors, and contributions
1 parent 9769b26 commit 7132657

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

web/src/components/search.tsx

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import React, { useCallback, useEffect, useMemo, useState } from "react";
22
import { Locale, useLocale } from "./locale";
33
import { useSearch } from "src/utils/search";
4-
import { ProjectEntity } from "@dzcode.io/models/dist/project";
5-
import { ContributorEntity } from "@dzcode.io/models/dist/contributor";
6-
import { ContributionEntity } from "@dzcode.io/models/dist/contribution";
7-
import { RepositoryEntity } from "@dzcode.io/models/dist/repository";
84
import { useSearchModal } from "src/utils/search-modal";
95
import { ContributionCard } from "./contribution-card";
106
import { ContributorCard } from "./contributor-card";
117
import { ProjectCard } from "./project-card";
8+
import { GetContributionsResponse } from "@dzcode.io/api/dist/contribution/types";
9+
import { GetContributorsResponse } from "@dzcode.io/api/dist/contributor/types";
10+
import { GetProjectsResponse } from "@dzcode.io/api/dist/project/types";
1211

1312
export function Search(): JSX.Element {
1413
const { localize } = useLocale();
@@ -40,42 +39,37 @@ export function Search(): JSX.Element {
4039
}, []);
4140

4241
const projectsList = useMemo(() => {
43-
return (results?.searchResults.results || [])
44-
.filter((result) => result.indexUid === "project")
45-
.flatMap((projects) => projects.hits) as unknown as Array<
46-
Pick<ProjectEntity, "id" | "name"> & {
47-
totalRepoContributorCount: number;
48-
totalRepoScore: number;
49-
totalRepoStars: number;
50-
ranking: number;
51-
}
52-
>;
53-
}, [results?.searchResults.results]);
42+
return (
43+
(results || [])
44+
.filter((result) => result.indexUid === "project")
45+
// @TODO-OB: fix type casting
46+
.flatMap((projects) => projects.hits) as unknown as Array<
47+
GetProjectsResponse["projects"][number]
48+
>
49+
);
50+
}, [results]);
5451

5552
const contributorsList = useMemo(() => {
56-
return (results?.searchResults.results || [])
57-
.filter((result) => result.indexUid === "contributor")
58-
.flatMap((contributors) => contributors.hits) as unknown as Array<
59-
Pick<ContributorEntity, "id" | "name" | "avatarUrl"> & {
60-
ranking: number;
61-
totalContributionScore: number;
62-
totalRepositoryCount: number;
63-
}
64-
>;
65-
}, [results?.searchResults.results]);
53+
return (
54+
(results || [])
55+
.filter((result) => result.indexUid === "contributor")
56+
// @TODO-OB: fix type casting
57+
.flatMap((contributors) => contributors.hits) as unknown as Array<
58+
GetContributorsResponse["contributors"][number]
59+
>
60+
);
61+
}, [results]);
6662

6763
const contributionsList = useMemo(() => {
68-
return (results?.searchResults.results || [])
69-
.filter((result) => result.indexUid === "contribution")
70-
.flatMap((contributions) => contributions.hits) as unknown as Array<
71-
Pick<ContributionEntity, "id" | "title" | "type" | "url" | "updatedAt" | "activityCount"> & {
72-
repository: Pick<RepositoryEntity, "id" | "owner" | "name"> & {
73-
project: Pick<ProjectEntity, "id" | "name">;
74-
};
75-
contributor: Pick<ContributorEntity, "id" | "name" | "username" | "avatarUrl">;
76-
}
77-
>;
78-
}, [results?.searchResults.results]);
64+
return (
65+
(results || [])
66+
.filter((result) => result.indexUid === "contribution")
67+
// @TODO-OB: fix type casting
68+
.flatMap((contributions) => contributions.hits) as unknown as Array<
69+
GetContributionsResponse["contributions"][number]
70+
>
71+
);
72+
}, [results]);
7973

8074
const searchTextOutput = useMemo(() => {
8175
if (isFetching) {

web/src/utils/search.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fetchV2 } from "./fetch";
33
import { SearchResponse } from "@dzcode.io/api/dist/search/types";
44

55
export const useSearch = (query: string, limit: number = 5) => {
6-
const [results, setResults] = useState<SearchResponse>();
6+
const [results, setResults] = useState<SearchResponse["searchResults"]["results"]>();
77
const [isFetching, setIsFetching] = useState(false);
88
const queryRef = useRef("");
99
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
@@ -17,7 +17,7 @@ export const useSearch = (query: string, limit: number = 5) => {
1717
],
1818
});
1919

20-
setResults(searchResults);
20+
setResults(searchResults.searchResults.results);
2121
setIsFetching(false);
2222
}, [limit]);
2323

@@ -31,11 +31,7 @@ export const useSearch = (query: string, limit: number = 5) => {
3131
queryRef.current = query;
3232
if (queryRef.current.length) search();
3333
else {
34-
setResults({
35-
searchResults: {
36-
results: [],
37-
},
38-
});
34+
setResults([]);
3935
setIsFetching(false);
4036
}
4137
}, 300);

0 commit comments

Comments
 (0)