Skip to content

Commit d577e9d

Browse files
committed
Refactors project data handling and API structure
Consolidates project data handling by improving caching logic and removing unused API routes. Updates API endpoint paths for better consistency. Simplifies error handling and streamlines blob retrieval in the GET route. Enhances readability and reliability by refining conditional checks and eliminating redundant code.
1 parent e1a2a93 commit d577e9d

File tree

7 files changed

+15
-46
lines changed

7 files changed

+15
-46
lines changed

src/app/(authed)/layout.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { redirect } from "next/navigation";
22
import { SessionProvider } from "next-auth/react";
3-
import { session } from "@/composition";
3+
import { session, projectDataSource } from "@/composition";
44
import ErrorHandler from "@/common/ui/ErrorHandler";
55
import SessionBarrier from "@/features/auth/view/SessionBarrier";
66
import ProjectsContextProvider from "@/features/projects/view/ProjectsContextProvider";
7-
import { projectDataSource } from "@/composition";
87
import {
98
SidebarTogglableContextProvider,
109
SplitView,
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { NextRequest, NextResponse } from "next/server"
22
import { session, userGitHubClient } from "@/composition"
33
import { makeUnauthenticatedAPIErrorResponse } from "@/common"
4-
import { revalidatePath } from "next/cache"
5-
64

75
interface GetBlobParams {
86
owner: string
97
repository: string
108
path: [string]
119
}
1210

13-
export async function GET(req: NextRequest, { params }: { params: Promise<GetBlobParams> } ) {
11+
export async function GET(req: NextRequest, { params }: { params: Promise<GetBlobParams> }) {
1412
const isAuthenticated = await session.getIsAuthenticated()
1513
if (!isAuthenticated) {
1614
return makeUnauthenticatedAPIErrorResponse()
@@ -25,18 +23,14 @@ export async function GET(req: NextRequest, { params }: { params: Promise<GetBlo
2523
})
2624
const url = new URL(item.downloadURL)
2725
const imageRegex = /\.(jpg|jpeg|png|webp|avif|gif)$/;
28-
const res = await fetch(url, { next: { revalidate: 6000 } })
29-
const file = await res.blob()
30-
revalidatePath('/(authed)/projects')
26+
const file = await fetch(url).then(r => r.blob())
3127
const headers = new Headers()
32-
if (res.status !== 200) {
33-
headers.set("Content-Type", "text/plain");
34-
headers.set("Cache-Control", `max-age=3000`)
35-
}
3628
if (new RegExp(imageRegex).exec(path)) {
3729
const cacheExpirationInSeconds = 60 * 60 * 24 * 30 // 30 days
3830
headers.set("Content-Type", "image/*");
3931
headers.set("Cache-Control", `max-age=${cacheExpirationInSeconds}`)
40-
}
32+
} else {
33+
headers.set("Content-Type", "text/plain");
34+
}
4135
return new NextResponse(file, { status: 200, headers })
42-
}
36+
}

src/app/api/projects/route.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/app/api/refreshedProjects/route.ts renamed to src/app/api/refresh-projects/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { NextResponse } from "next/server";
1+
2+
import { NextResponse } from "next/server"
23
import { projectDataSource } from "@/composition";
34

45

src/features/projects/domain/CachingProjectDataSource.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ export default class CachingProjectDataSource implements IProjectDataSource {
1717

1818
async getProjects(): Promise<Project[]> {
1919
const cache = await this.repository.get();
20-
21-
if (cache) return cache;
22-
else {
23-
const projects = await this.dataSource.getProjects();
24-
await this.repository.set(projects);
25-
26-
return projects;
20+
if (cache && cache.length > 0) {
21+
return cache;
2722
}
23+
const projects = await this.dataSource.getProjects();
24+
await this.repository.set(projects);
25+
return projects;
2826
}
2927

3028
async refreshProjects(): Promise<Project[]> {

src/features/projects/view/ProjectsContextProvider.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const ProjectsContextProvider = ({
2727
}
2828
isLoadingRef.current = true;
2929
setRefreshing(true);
30-
fetch("/api/refreshedProjects", { method: "POST" })
30+
fetch("/api/refresh-projects", { method: "POST" })
3131
.then((res) => res.json())
3232
.then(({ projects }) => {
3333
if (projects) setProjectsAndRefreshed(projects);
@@ -40,7 +40,6 @@ const ProjectsContextProvider = ({
4040
};
4141
// Initial refresh
4242
refreshProjects();
43-
4443
const handleVisibilityChange = () => {
4544
if (!document.hidden) refreshProjects();
4645
};

src/features/sidebar/view/internal/sidebar/projects/ProjectList.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ const ProjectList = () => {
1919

2020
export default ProjectList
2121

22-
/* const DataFetchingProjectList = async ({
23-
projectDataSource
24-
}: {
25-
projectDataSource: IProjectDataSource
26-
}) => {
27-
const projects = await projectDataSource.getProjects()
28-
if (projects.length > 0) {
29-
return <PopulatedProjectList projects={projects} />
30-
} else {
31-
return <EmptyProjectList/>
32-
}
33-
} */
34-
3522
const EmptyProjectList = () => {
3623
return (
3724
<Typography

0 commit comments

Comments
 (0)