-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
216 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { ModuleMetadata } from '~/app/api/stats/[entry]/modules/index+api'; | ||
import { getSource } from '~/utils/atlas'; | ||
import { StatsEntry } from '~core/data/types'; | ||
|
||
export type FolderGraphData = { | ||
metadata: { | ||
platform: 'android' | 'ios' | 'web'; | ||
size: number; | ||
modulesCount: number; | ||
folder: string; | ||
}; | ||
data: { | ||
size: number; | ||
modulesCount: number; | ||
modules: ModuleMetadata[]; | ||
}; | ||
}; | ||
|
||
export async function POST(request: Request, params: Record<'entry', string>) { | ||
const folderRef: string | undefined = (await request.json()).path; | ||
if (!folderRef) { | ||
return Response.json( | ||
{ error: `Folder ID not provided, expected a "path" property.` }, | ||
{ status: 406 } | ||
); | ||
} | ||
|
||
let entry: StatsEntry; | ||
|
||
try { | ||
entry = await getSource().getEntry(params.entry); | ||
} catch (error: any) { | ||
return Response.json({ error: error.message }, { status: 406 }); | ||
} | ||
|
||
const folder = collectFolderInfo(entry, folderRef); | ||
return folder | ||
? Response.json(folder) | ||
: Response.json({ error: `Folder "${folderRef}" not found.` }, { status: 404 }); | ||
} | ||
|
||
function collectFolderInfo(entry: StatsEntry, folderRef: string): FolderGraphData | null { | ||
const modules: ModuleMetadata[] = []; | ||
|
||
for (const [moduleRef, module] of entry.modules) { | ||
if (moduleRef.startsWith(folderRef)) { | ||
modules.push({ ...module, source: undefined, output: undefined }); | ||
} | ||
} | ||
|
||
if (!modules.length) { | ||
return null; | ||
} | ||
|
||
const size = modules.reduce((size, module) => size + module.size, 0); | ||
|
||
return { | ||
metadata: { | ||
platform: entry.platform as any, | ||
size, | ||
modulesCount: modules.length, | ||
folder: folderRef, | ||
}, | ||
data: { | ||
modules, | ||
size, | ||
modulesCount: modules.length, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useLocalSearchParams } from 'expo-router'; | ||
|
||
import { type FolderGraphData } from '../api/stats/[entry]/folders/index+api'; | ||
|
||
import { Page, PageHeader, PageTitle } from '~/components/Page'; | ||
import { TreemapGraph } from '~/components/graphs/TreemapGraph'; | ||
import { useStatsEntryContext } from '~/providers/stats'; | ||
import { Skeleton } from '~/ui/Skeleton'; | ||
import { Tag } from '~/ui/Tag'; | ||
import { formatFileSize } from '~/utils/formatString'; | ||
import { type PartialStatsEntry } from '~core/data/types'; | ||
|
||
export default function FolderPage() { | ||
const { entryId, entry, entryFilePath } = useStatsEntryContext(); | ||
const { path: absolutePath } = useLocalSearchParams<{ path: string }>(); | ||
const folder = useFolderData(entryId, absolutePath!); | ||
|
||
if (folder.isLoading) { | ||
return <FolderPageSkeleton />; | ||
} | ||
|
||
if (!folder.data || folder.isError) { | ||
// TODO: improve | ||
return ( | ||
<div className="flex flex-1 flex-col justify-center items-center"> | ||
<h2 className="text-slate-50 font-bold text-lg">Folder not found</h2> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<Page variant="viewport"> | ||
<div className="flex flex-1 flex-col"> | ||
<PageHeader> | ||
<PageTitle> | ||
<h1 | ||
className="text-slate-50 font-bold text-lg mr-4" | ||
title={folder.data.metadata.folder} | ||
> | ||
{entryFilePath(folder.data.metadata.folder)}/ | ||
</h1> | ||
<FolderSummary platform={entry?.platform} folder={folder.data.metadata} /> | ||
</PageTitle> | ||
</PageHeader> | ||
|
||
<TreemapGraph | ||
key={`folder-graph-${entryId}`} | ||
name={entryFilePath(folder.data.metadata.folder)} | ||
modules={folder.data?.data?.modules ?? []} | ||
/> | ||
</div> | ||
</Page> | ||
); | ||
} | ||
|
||
function FolderSummary({ | ||
folder, | ||
platform, | ||
}: { | ||
folder: FolderGraphData['metadata']; | ||
platform?: PartialStatsEntry['platform']; | ||
}) { | ||
return ( | ||
<div className="font-sm text-secondary"> | ||
{!!platform && ( | ||
<> | ||
<Tag variant={platform} /> | ||
<span className="text-tertiary mx-2 select-none">-</span> | ||
</> | ||
)} | ||
<span>folder</span> | ||
<span className="text-tertiary mx-2 select-none">-</span> | ||
<span> | ||
{folder?.modulesCount === 1 | ||
? `${folder.modulesCount} module` | ||
: `${folder.modulesCount} modules`} | ||
</span> | ||
<span className="text-tertiary mx-2 select-none">-</span> | ||
<span>{formatFileSize(folder.size)}</span> | ||
</div> | ||
); | ||
} | ||
|
||
/** Load the folder data from API, by path reference only */ | ||
function useFolderData(entryId: string, path: string) { | ||
return useQuery<FolderGraphData>({ | ||
queryKey: [`module`, entryId, path], | ||
queryFn: async ({ queryKey }) => { | ||
const [_key, entry, path] = queryKey as [string, string, string]; | ||
return fetch(`/api/stats/${entry}/folders`, { | ||
method: 'POST', | ||
body: JSON.stringify({ path }), | ||
}) | ||
.then((res) => (res.ok ? res : Promise.reject(res))) | ||
.then((res) => res.json()); | ||
}, | ||
}); | ||
} | ||
|
||
function FolderPageSkeleton() { | ||
return ( | ||
<div className="flex flex-1 flex-col overflow-auto"> | ||
<PageHeader> | ||
<PageTitle> | ||
<Skeleton className="w-48 h-6 bg-selected" /> | ||
<Skeleton className="w-96 h-6 mx-2" /> | ||
</PageTitle> | ||
</PageHeader> | ||
|
||
<div className="mx-8 flex-1"> | ||
<Skeleton className="h-full rounded-md" /> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters