Skip to content

Commit

Permalink
refactor(webui): rename entries to bundles (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric authored Apr 20, 2024
1 parent 1bc0d08 commit 3b75b43
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 110 deletions.
8 changes: 4 additions & 4 deletions src/data/AtlasFileSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ export class AtlasFileSource implements AtlasSource {
//
}

listEntries() {
listBundles() {
return listAtlasEntries(this.filePath);
}

getEntry(id: string) {
getBundle(id: string) {
const numeric = parseInt(id, 10);
assert(!Number.isNaN(numeric) && numeric > 1, `Invalid entry ID: ${id}`);
return readAtlasEntry(this.filePath, Number(id));
}

entryDeltaEnabled() {
bundleDeltaEnabled() {
return false; // File source does not implement the delta mechanism
}

getEntryDelta() {
getBundleDelta() {
return null; // File source does not implement the delta mechanism
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/data/MetroGraphSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class MetroGraphSource implements AtlasSource {
this.serializeGraph = this.serializeGraph.bind(this);
}

listEntries() {
listBundles() {
return Array.from(this.entries.values()).map((item) => ({
id: item.entry.id,
platform: item.entry.platform,
Expand All @@ -45,17 +45,17 @@ export class MetroGraphSource implements AtlasSource {
}));
}

getEntry(id: string) {
getBundle(id: string) {
const item = this.entries.get(id);
if (!item) throw new Error(`Entry "${id}" not found.`);
return item.entry;
}

getEntryDelta(id: string) {
getBundleDelta(id: string) {
return this.entries.get(id)?.delta || null;
}

entryDeltaEnabled() {
bundleDeltaEnabled() {
return !!this.deltaListener;
}

Expand Down
10 changes: 5 additions & 5 deletions src/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { MixedOutput } from 'metro';

export interface AtlasSource {
/** List all available entries */
listEntries(): PartialAtlasBundle[] | Promise<PartialAtlasBundle[]>;
/** List the partial data of all available bundles */
listBundles(): PartialAtlasBundle[] | Promise<PartialAtlasBundle[]>;
/** Load the full entry, by reference */
getEntry(ref: string): AtlasBundle | Promise<AtlasBundle>;
getBundle(ref: string): AtlasBundle | Promise<AtlasBundle>;
/** Load the entry changes since last bundle collection, if any */
getEntryDelta(ref: string): null | AtlasBundleDelta | Promise<null | AtlasBundleDelta>;
getBundleDelta(ref: string): null | AtlasBundleDelta | Promise<null | AtlasBundleDelta>;
/** Determine if the source is watching for (live) changes. */
entryDeltaEnabled(): boolean;
bundleDeltaEnabled(): boolean;
}

export type PartialAtlasBundle = Pick<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { keepPreviousData, useQuery } from '@tanstack/react-query';

import type { ModuleGraphResponse } from '~/app/--/entries/[entry]/modules/graph+api';
import type { ModuleGraphResponse } from '~/app/--/bundles/[bundle]/modules/graph+api';
import { BundleGraph } from '~/components/BundleGraph';
import { BundleSelectForm } from '~/components/BundleSelectForm';
import { ModuleFiltersForm } from '~/components/ModuleFilterForm';
Expand Down Expand Up @@ -32,10 +32,10 @@ export default function BundlePage() {
<h1 className="text-lg font-bold mr-8">Bundle</h1>
<PropertySummary>
<Tag variant={bundle.platform} />
{!!modules.data && <span>{modules.data.entry.moduleFiles} modules</span>}
{!!modules.data && <span>{formatFileSize(modules.data.entry.moduleSize)}</span>}
{!!modules.data && <span>{modules.data.bundle.moduleFiles} modules</span>}
{!!modules.data && <span>{formatFileSize(modules.data.bundle.moduleSize)}</span>}
{modules.data &&
modules.data.filtered.moduleFiles !== modules.data.entry.moduleFiles && (
modules.data.filtered.moduleFiles !== modules.data.bundle.moduleFiles && (
<PropertySummary
className="text-tertiary italic"
prefix={<span className="select-none mr-2">visible:</span>}
Expand All @@ -58,7 +58,7 @@ export default function BundlePage() {
<p>Try restarting Expo Atlas. If this error keeps happening, open a bug report.</p>
</StateInfo>
) : treeHasData ? (
<BundleGraph entry={bundle} graph={modules.data!.data} />
<BundleGraph bundle={bundle} graph={modules.data!.data} />
) : (
<StateInfo title={filtersEnabled ? 'No data matching filters' : 'No data available'}>
<p>
Expand All @@ -71,11 +71,11 @@ export default function BundlePage() {
}

/** Load the bundle graph data from API, with default or custom filters */
function useModuleGraphData(entryId: string, filters: ModuleFilters) {
function useModuleGraphData(bundleId: string, filters: ModuleFilters) {
return useQuery<ModuleGraphResponse>({
refetchOnWindowFocus: false,
placeholderData: keepPreviousData,
queryKey: [`entries`, entryId, 'bundle-graph', filters],
queryKey: [`bundles`, bundleId, 'bundle-graph', filters],
queryFn: ({ queryKey }) => {
const [_key, entry, _graph, filters] = queryKey as [
string,
Expand All @@ -85,8 +85,8 @@ function useModuleGraphData(entryId: string, filters: ModuleFilters) {
];

const url = filters
? `/entries/${entry}/modules/graph?${moduleFiltersToParams(filters)}`
: `/entries/${entry}/modules/graph`;
? `/bundles/${entry}/modules/graph?${moduleFiltersToParams(filters)}`
: `/bundles/${entry}/modules/graph`;

return fetchApi(url)
.then((res) => (res.ok ? res : Promise.reject(res)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { useLocalSearchParams } from 'expo-router';

import type { ModuleGraphResponse } from '~/app/--/entries/[entry]/modules/graph+api';
import type { ModuleGraphResponse } from '~/app/--/bundles/[bundle]/modules/graph+api';
import { BreadcrumbLinks } from '~/components/BreadcrumbLinks';
import { BundleGraph } from '~/components/BundleGraph';
import { BundleSelectForm } from '~/components/BundleSelectForm';
Expand Down Expand Up @@ -60,7 +60,7 @@ export default function FolderPage() {
Try restarting Expo Atlas. If this error keeps happening, open a bug report.
</StateInfo>
) : treeHasData ? (
<BundleGraph entry={bundle} graph={modules.data!.data} />
<BundleGraph bundle={bundle} graph={modules.data!.data} />
) : (
!modules.isPending && (
<StateInfo title={filtersEnabled ? 'No data matching filters' : 'No data available'}>
Expand All @@ -73,13 +73,13 @@ export default function FolderPage() {
}

/** Load the folder data from API, by path reference only */
function useModuleGraphDataInFolder(entryId: string, path: string, filters: ModuleFilters) {
function useModuleGraphDataInFolder(bundleId: string, path: string, filters: ModuleFilters) {
return useQuery<ModuleGraphResponse>({
refetchOnWindowFocus: false,
placeholderData: keepPreviousData,
queryKey: [`entries`, entryId, `module`, path, filters],
queryKey: [`bundles`, bundleId, `module`, path, filters],
queryFn: async ({ queryKey }) => {
const [_key, entry, _module, path, filters] = queryKey as [
const [_key, bundle, _module, path, filters] = queryKey as [
string,
string,
string,
Expand All @@ -88,8 +88,8 @@ function useModuleGraphDataInFolder(entryId: string, path: string, filters: Modu
];

const url = filters
? `/entries/${entry}/modules/graph?path=${encodeURIComponent(path)}&${moduleFiltersToParams(filters)}`
: `/entries/${entry}/modules/graph?path=${encodeURIComponent(path)}`;
? `/bundles/${bundle}/modules/graph?path=${encodeURIComponent(path)}&${moduleFiltersToParams(filters)}`
: `/bundles/${bundle}/modules/graph?path=${encodeURIComponent(path)}`;

return fetchApi(url)
.then((res) => (res.ok ? res : Promise.reject(res)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export default function ModulePage() {
<Link
className="text-link hover:underline"
href={{
pathname: '/(atlas)/[entry]/modules/[path]',
params: { entry: bundle.id, path },
pathname: '/(atlas)/[bundle]/modules/[path]',
params: { bundle: bundle.id, path },
}}
>
{relativeBundlePath(bundle, path)}
Expand All @@ -82,13 +82,13 @@ function getModuleType(module: AtlasModule) {
}

/** Load the module data from API, by path reference only */
function useModuleData(entryId: string, path: string) {
function useModuleData(bundleId: string, path: string) {
return useQuery<AtlasModule>({
refetchOnWindowFocus: false,
queryKey: [`entries`, entryId, `module`, path],
queryKey: [`bundles`, bundleId, `module`, path],
queryFn: async ({ queryKey }) => {
const [_key, entry, _module, path] = queryKey as [string, string, string, string];
return fetchApi(`/entries/${entry}/modules`, {
const [_key, bundle, _module, path] = queryKey as [string, string, string, string];
return fetchApi(`/bundles/${bundle}/modules`, {
method: 'POST',
body: JSON.stringify({ path }),
})
Expand Down
21 changes: 21 additions & 0 deletions webui/src/app/--/bundles/[bundle]/delta+api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getSource } from '~/utils/atlas';
import type { AtlasBundleDelta } from '~core/data/types';

export type BundleDeltaResponse = {
isEnabled: boolean;
delta: null | AtlasBundleDelta;
};

export async function GET(_request: Request, params: Record<'bundle', string>) {
try {
const isEnabled = getSource().bundleDeltaEnabled();
const response: BundleDeltaResponse = {
isEnabled,
delta: !isEnabled ? null : await getSource().getBundleDelta(params.bundle),
};

return Response.json(response, { status: 200 });
} catch (error: any) {
return Response.json({ error: error.message }, { status: 406 });
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getSource } from '~/utils/atlas';

export async function GET(_request: Request, params: Record<'entry', string>) {
export async function GET(_request: Request, params: Record<'bundle', string>) {
try {
return Response.json(await getSource().getEntry(params.entry));
return Response.json(await getSource().getBundle(params.bundle));
} catch (error: any) {
return Response.json({ error: error.message }, { status: 406 });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { AtlasBundle } from '~core/data/types';

export type ModuleGraphResponse = {
data: TreemapNode;
entry: {
bundle: {
platform: 'android' | 'ios' | 'web';
moduleSize: number;
moduleFiles: number;
Expand All @@ -16,29 +16,29 @@ export type ModuleGraphResponse = {
};
};

export async function GET(request: Request, params: Record<'entry', string>) {
let entry: AtlasBundle;
export async function GET(request: Request, params: Record<'bundle', string>) {
let bundle: AtlasBundle;

try {
entry = await getSource().getEntry(params.entry);
bundle = await getSource().getBundle(params.bundle);
} catch (error: any) {
return Response.json({ error: error.message }, { status: 406 });
}

const query = new URL(request.url).searchParams;
const allModules = Array.from(entry.modules.values());
const allModules = Array.from(bundle.modules.values());
const filteredModules = filterModules(allModules, {
projectRoot: entry.projectRoot,
projectRoot: bundle.projectRoot,
filters: moduleFiltersFromParams(query),
rootPath: query.get('path') || undefined,
});

const response: ModuleGraphResponse = {
data: finalizeModuleTree(createModuleTree(filteredModules)),
entry: {
platform: entry.platform as any,
bundle: {
platform: bundle.platform as any,
moduleSize: allModules.reduce((size, module) => size + module.size, 0),
moduleFiles: entry.modules.size,
moduleFiles: bundle.modules.size,
},
filtered: {
moduleSize: filteredModules.reduce((size, module) => size + module.size, 0),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { getSource } from '~/utils/atlas';
import { filterModules, moduleFiltersFromParams } from '~/utils/filters';
import { type AtlasBundle, type AtlasModule } from '~core/data/types';
import type { AtlasBundle, AtlasModule } from '~core/data/types';

/** The partial module data, when listing all available modules from an entry */
export type PartialModule = Omit<AtlasModule, 'source' | 'output'>;

export type ModuleListResponse = {
data: PartialModule[];
entry: {
bundle: {
platform: 'android' | 'ios' | 'web';
moduleSize: number;
moduleFiles: number;
Expand All @@ -19,19 +19,19 @@ export type ModuleListResponse = {
};

/** Get all modules as simple list */
export async function GET(request: Request, params: Record<'entry', string>) {
let entry: AtlasBundle;
export async function GET(request: Request, params: Record<'bundle', string>) {
let bundle: AtlasBundle;

try {
entry = await getSource().getEntry(params.entry);
bundle = await getSource().getBundle(params.bundle);
} catch (error: any) {
return Response.json({ error: error.message }, { status: 406 });
}

const query = new URL(request.url).searchParams;
const allModules = Array.from(entry.modules.values());
const allModules = Array.from(bundle.modules.values());
const filteredModules = filterModules(allModules, {
projectRoot: entry.projectRoot,
projectRoot: bundle.projectRoot,
filters: moduleFiltersFromParams(query),
rootPath: query.get('path') || undefined,
});
Expand All @@ -42,10 +42,10 @@ export async function GET(request: Request, params: Record<'entry', string>) {
source: undefined,
output: undefined,
})),
entry: {
platform: entry.platform as any,
bundle: {
platform: bundle.platform as any,
moduleSize: allModules.reduce((size, module) => size + module.size, 0),
moduleFiles: entry.modules.size,
moduleFiles: bundle.modules.size,
},
filtered: {
moduleSize: filteredModules.reduce((size, module) => size + module.size, 0),
Expand All @@ -60,7 +60,7 @@ export async function GET(request: Request, params: Record<'entry', string>) {
* Get the full module information through a post request.
* This requires a `path` property in the request body.
*/
export async function POST(request: Request, params: Record<'entry', string>) {
export async function POST(request: Request, params: Record<'bundle', string>) {
const moduleRef: string | undefined = (await request.json()).path;
if (!moduleRef) {
return Response.json(
Expand All @@ -69,15 +69,15 @@ export async function POST(request: Request, params: Record<'entry', string>) {
);
}

let entry: AtlasBundle;
let bundle: AtlasBundle;

try {
entry = await getSource().getEntry(params.entry);
bundle = await getSource().getBundle(params.bundle);
} catch (error: any) {
return Response.json({ error: error.message }, { status: 406 });
}

const module = entry.modules.get(moduleRef);
const module = bundle.modules.get(moduleRef);
return module
? Response.json(module)
: Response.json({ error: `Module "${moduleRef}" not found.` }, { status: 404 });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { getSource } from '~/utils/atlas';

export async function GET(_request: Request, params: Record<'entry', string>) {
export async function GET(_request: Request, params: Record<'bundle', string>) {
try {
const entry = await getSource().getEntry(params.entry);
if (!entry) {
const bundle = await getSource().getBundle(params.bundle);
if (!bundle) {
return Response.json({ error: 'Entry not found' }, { status: 404 });
}

if (!entry.serializeOptions?.sourceUrl) {
if (!bundle.serializeOptions?.sourceUrl) {
return Response.json({ error: 'Entry has no `serializeOptions.sourceUrl`' }, { status: 406 });
}

return Response.redirect(entry.serializeOptions.sourceUrl, 302);
return Response.redirect(bundle.serializeOptions.sourceUrl, 302);
} catch (error: any) {
return Response.json({ error: error.message }, { status: 406 });
}
Expand Down
Loading

0 comments on commit 3b75b43

Please sign in to comment.