Skip to content

Commit

Permalink
feat: fetch insight repos in parallel on insight pages
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Jan 18, 2024
1 parent 265204b commit 06c0afd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
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 SEO from "layouts/SEO/SEO";
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 @@ interface InsightPageProps {
}

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 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
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`);

Check failure

Code scanning / CodeQL

Server-side request forgery Critical

The
URL
of this request depends on a
user-provided value
.
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 { WithPageLayout } from "interfaces/with-page-layout";
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 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
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`);

Check failure

Code scanning / CodeQL

Server-side request forgery Critical

The
URL
of this request depends on a
user-provided value
.
const insight = response.ok ? ((await response.json()) as DbUserInsight) : null;

if (!insight) {
Expand Down

0 comments on commit 06c0afd

Please sign in to comment.