Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch insight repos in parallel on insight pages #2496

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/hooks/useInsightRepositories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import useSWR, { Fetcher } from "swr";
import publicApiFetcher from "lib/utils/public-api-fetcher";

const useInsightRepositories = (id: number) => {
const baseEndpoint = `insights/${id}/repos`;
const endpointString = `${baseEndpoint}`;

const { data, error, mutate } = useSWR<DbUserInsightRepo[], Error>(
id ? endpointString : null,
publicApiFetcher as Fetcher<DbUserInsightRepo[], Error>
);

return {
data: data ?? [],
isLoading: !error && !data,
isError: !!error && Object.keys(error).length > 0,
mutate,
};
};

export default useInsightRepositories;
6 changes: 4 additions & 2 deletions pages/pages/[userOrg]/[pageId]/[toolName].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fetchSocialCard from "lib/utils/fetch-social-card";
import getInsightTeamMemberAccess from "lib/utils/get-insight-team-member";
import { MemberAccess } from "components/molecules/TeamMembersConfig/team-members-config";
import useInsightRepositories from "lib/hooks/useInsightRepositories";

interface InsightPageProps {
insight: DbUserInsight;
Expand All @@ -19,7 +20,8 @@
}

const HubPage: WithPageLayout<InsightPageProps> = ({ insight, pageName, ogImage }: InsightPageProps) => {
const repositories = insight.repos.map((repo) => repo.repo_id);
const { data: insightRepos } = useInsightRepositories(insight.id);
const repositories = insightRepos.map((repo) => repo.repo_id);
const [hydrated, setHydrated] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -61,7 +63,7 @@
const bearerToken = session ? session.access_token : "";
const insightId = ctx.params!["pageId"] as string;
const pageName = ctx.params!["toolName"] as string;
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/insights/${insightId}`);
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/insights/${insightId}?include=none`);
Dismissed Show dismissed Hide dismissed
const insight = response.ok ? ((await response.json()) as DbUserInsight) : null;

if (!insight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import changeCapitalization from "lib/utils/change-capitalization";
import getInsightTeamMemberAccess from "lib/utils/get-insight-team-member";
import { MemberAccess } from "components/molecules/TeamMembersConfig/team-members-config";
import useInsightRepositories from "lib/hooks/useInsightRepositories";

interface InsightFilterPageProps {
insight: DbUserInsight;
pageName: string;
}

const HubPage: WithPageLayout<InsightFilterPageProps> = ({ insight, pageName }: InsightFilterPageProps) => {
const repositories = insight.repos.map((repo) => repo.repo_id);
const { data: insightRepos } = useInsightRepositories(insight.id);
const repositories = insightRepos.map((repo) => repo.repo_id);

const title = `${insight.name} | Open Sauced Insights Hub`;

Expand All @@ -38,7 +40,7 @@
const bearerToken = session ? session.access_token : "";
const insightId = ctx.params!["pageId"] as string;
const pageName = ctx.params!["toolName"] as string;
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/insights/${insightId}`);
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/insights/${insightId}?include=none`);
github-advanced-security[bot] marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
const insight = response.ok ? ((await response.json()) as DbUserInsight) : null;

if (!insight) {
Expand Down