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

Refactor/simplify breadcrumbs #36

Merged
merged 2 commits into from
Apr 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions webui/src/app/(atlas)/[entry]/folders/[path].tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { useLocalSearchParams } from 'expo-router';
import { useMemo } from 'react';

import type { ModuleGraphResponse } from '~/app/--/entries/[entry]/modules/graph+api';
import { BreadcrumbLinks } from '~/components/BreadcrumbLinks';
import { BundleGraph } from '~/components/BundleGraph';
import { BundleSelectForm } from '~/components/BundleSelectForm';
import { ModuleFiltersForm } from '~/components/ModuleFilterForm';
import { StateInfo } from '~/components/StateInfo';
import { EntrySelectForm } from '~/components/forms/EntrySelect';
import { ModuleFiltersForm } from '~/components/forms/ModuleFilter';
import { EntryDeltaToast, useEntry } from '~/providers/entries';
import { Layout, LayoutHeader, LayoutNavigation, LayoutTitle } from '~/ui/Layout';
import { Tag } from '~/ui/Tag';
import { fetchApi } from '~/utils/api';
import { breadcrumbsForPath } from '~/utils/entry';
import { type ModuleFilters, useModuleFilters, moduleFiltersToParams } from '~/utils/filters';
import { formatFileSize } from '~/utils/formatString';
import { type PartialAtlasEntry } from '~core/data/types';

export default function FolderPage() {
const { path: absolutePath } = useLocalSearchParams<{ path: string }>();
Expand All @@ -27,11 +24,11 @@ export default function FolderPage() {
return (
<Layout variant="viewport">
<LayoutNavigation>
<EntrySelectForm />
<BundleSelectForm />
</LayoutNavigation>
<LayoutHeader>
<LayoutTitle>
<FolderTitle entry={entry} folderPath={absolutePath!} />
<BreadcrumbLinks entry={entry} path={absolutePath!} />
{!!modules.data && <FolderSummary data={modules.data} />}
</LayoutTitle>
<ModuleFiltersForm disableNodeModules />
Expand All @@ -54,11 +51,6 @@ export default function FolderPage() {
);
}

function FolderTitle({ entry, folderPath }: { entry: PartialAtlasEntry; folderPath: string }) {
const links = useMemo(() => breadcrumbsForPath(entry, folderPath), [entry, folderPath]);
return <BreadcrumbLinks entryId={entry.id} links={links} />;
}

function FolderSummary({ data }: { data: ModuleGraphResponse }) {
return (
<div className="font-sm text-secondary">
Expand Down
6 changes: 3 additions & 3 deletions webui/src/app/(atlas)/[entry]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { keepPreviousData, useQuery } from '@tanstack/react-query';

import type { ModuleGraphResponse } from '~/app/--/entries/[entry]/modules/graph+api';
import { BundleGraph } from '~/components/BundleGraph';
import { BundleSelectForm } from '~/components/BundleSelectForm';
import { ModuleFiltersForm } from '~/components/ModuleFilterForm';
import { StateInfo } from '~/components/StateInfo';
import { EntrySelectForm } from '~/components/forms/EntrySelect';
import { ModuleFiltersForm } from '~/components/forms/ModuleFilter';
import { EntryDeltaToast, useEntry } from '~/providers/entries';
import { Layout, LayoutHeader, LayoutNavigation, LayoutTitle } from '~/ui/Layout';
import { Spinner } from '~/ui/Spinner';
Expand All @@ -22,7 +22,7 @@ export default function BundlePage() {
return (
<Layout variant="viewport">
<LayoutNavigation>
<EntrySelectForm />
<BundleSelectForm />
</LayoutNavigation>
<LayoutHeader>
<LayoutTitle>
Expand Down
14 changes: 4 additions & 10 deletions webui/src/app/(atlas)/[entry]/modules/[path].tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { useQuery } from '@tanstack/react-query';
import { Link, useLocalSearchParams } from 'expo-router';
import { useMemo } from 'react';

import { BreadcrumbLinks } from '~/components/BreadcrumbLinks';
import { BundleSelectForm } from '~/components/BundleSelectForm';
import { ModuleCode } from '~/components/ModuleCode';
import { StateInfo } from '~/components/StateInfo';
import { EntrySelectForm } from '~/components/forms/EntrySelect';
import { EntryDeltaToast, useEntry } from '~/providers/entries';
import { Layout, LayoutHeader, LayoutNavigation, LayoutTitle } from '~/ui/Layout';
import { Skeleton } from '~/ui/Skeleton';
import { Tag } from '~/ui/Tag';
import { fetchApi } from '~/utils/api';
import { breadcrumbsForPath, relativeEntryPath } from '~/utils/entry';
import { relativeEntryPath } from '~/utils/entry';
import { formatFileSize } from '~/utils/formatString';
import { type PartialAtlasEntry, type AtlasModule } from '~core/data/types';

Expand All @@ -23,11 +22,11 @@ export default function ModulePage() {
return (
<Layout>
<LayoutNavigation>
<EntrySelectForm />
<BundleSelectForm />
</LayoutNavigation>
<LayoutHeader>
<LayoutTitle>
<ModuleTitle entry={entry} modulePath={absolutePath!} />
<BreadcrumbLinks entry={entry} path={absolutePath!} />
{!!module.data && <ModuleSummary platform={entry?.platform} module={module.data} />}
</LayoutTitle>
</LayoutHeader>
Expand Down Expand Up @@ -69,11 +68,6 @@ export default function ModulePage() {
);
}

function ModuleTitle({ entry, modulePath }: { entry: PartialAtlasEntry; modulePath: string }) {
const links = useMemo(() => breadcrumbsForPath(entry, modulePath), [entry, modulePath]);
return <BreadcrumbLinks entryId={entry.id} links={links} />;
}

function ModuleSummary({
module,
platform,
Expand Down
44 changes: 36 additions & 8 deletions webui/src/components/BreadcrumbLinks.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Link } from 'expo-router';
import { ComponentProps, Fragment } from 'react';
import { ComponentProps, Fragment, useMemo } from 'react';

import {
Breadcrumb,
Expand All @@ -9,28 +9,29 @@ import {
BreadcrumbPage,
BreadcrumbSeparator,
} from '~/ui/Breadcrumb';
import { relativeEntryPath } from '~/utils/entry';
import { type PartialAtlasEntry } from '~core/data/types';

type BreadcrumbLinksProps = {
entryId: string;
links: {
label: string;
href?: ComponentProps<typeof Link>['href'];
}[];
entry: PartialAtlasEntry;
path: string;
};

export function BreadcrumbLinks(props: BreadcrumbLinksProps) {
const links = useMemo(() => getBreadcrumbLinks(props), [props.entry.id, props.path]);

return (
<Breadcrumb>
<BreadcrumbList className="mr-8">
<BreadcrumbLink asChild>
<Link
className="text-lg font-bold text-default underline-offset-4 hover:underline"
href={{ pathname: '/(atlas)/[entry]/', params: { entry: props.entryId } }}
href={{ pathname: '/(atlas)/[entry]/', params: { entry: props.entry.id } }}
>
Bundle
</Link>
</BreadcrumbLink>
{props.links.map((link, index) => (
{links.map((link, index) => (
<Fragment key={`link-${index}`}>
<BreadcrumbSeparator className="text-secondary" />
<BreadcrumbItem>
Expand All @@ -53,3 +54,30 @@ export function BreadcrumbLinks(props: BreadcrumbLinksProps) {
</Breadcrumb>
);
}

type BreadcrumbLinkItem = {
label: string;
href?: ComponentProps<typeof Link>['href'];
};

function getBreadcrumbLinks(props: BreadcrumbLinksProps): BreadcrumbLinkItem[] {
const relativePath = relativeEntryPath(props.entry, props.path);

return relativePath.split('/').map((label, index, breadcrumbs) => {
const isLastSegment = index === breadcrumbs.length - 1;
const breadcrumb: BreadcrumbLinkItem = { label };

// NOTE(cedric): a bit of a workaround to avoid linking the module page, might need to change this
if (!isLastSegment || !label.includes('.')) {
breadcrumb.href = {
pathname: '/(atlas)/[entry]/folders/[path]',
params: {
entry: props.entry.id,
path: `${props.entry.projectRoot}/${breadcrumbs.slice(0, index + 1).join('/')}`,
},
};
}

return breadcrumb;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Button } from '~/ui/Button';
import { Tag } from '~/ui/Tag';
import { relativeEntryPath } from '~/utils/entry';

export function EntrySelectForm() {
export function BundleSelectForm() {
const router = useRouter();
const { entry, entries } = useEntry();

Expand Down
31 changes: 0 additions & 31 deletions webui/src/utils/entry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { ComponentProps } from 'react';

import { BreadcrumbLinks } from '~/components/BreadcrumbLinks';
import { PartialAtlasEntry } from '~core/data/types';

/**
Expand All @@ -10,31 +7,3 @@ import { PartialAtlasEntry } from '~core/data/types';
export function relativeEntryPath(entry: Pick<PartialAtlasEntry, 'projectRoot'>, path: string) {
return path.replace(entry.projectRoot + '/', '');
}

/**
* Create a list of breadcrumbs, including label and possible links, for a given path.
*/
export function breadcrumbsForPath(
entry: Pick<PartialAtlasEntry, 'id' | 'projectRoot'>,
absolutePath: string
) {
const relativePath = relativeEntryPath(entry, absolutePath);

return relativePath.split('/').map((label, index, breadcrumbs) => {
const isLastSegment = index === breadcrumbs.length - 1;
const breadcrumb: ComponentProps<typeof BreadcrumbLinks>['links'][0] = { label };

// NOTE(cedric): a bit of a workaround to avoid linking the module page, might need to change this
if (!isLastSegment || !label.includes('.')) {
breadcrumb.href = {
pathname: '/(atlas)/[entry]/folders/[path]',
params: {
entry: entry.id,
path: `${entry.projectRoot}/${breadcrumbs.slice(0, index + 1).join('/')}`,
},
};
}

return breadcrumb;
});
}