Streamline adding params to app routes
This commit is contained in:
@@ -18,6 +18,16 @@ type AppRouteInfo = {
|
|||||||
|
|
||||||
type TAppRoutes = Record<string, AppRouteInfo & { childRoutes?: TAppRoutes }>;
|
type TAppRoutes = Record<string, AppRouteInfo & { childRoutes?: TAppRoutes }>;
|
||||||
|
|
||||||
|
const createParam = (name: string, value: any): string => (value ? `${name}=${encodeURIComponent(value)}` : '');
|
||||||
|
|
||||||
|
const createQueryParam = (query: string | null | undefined): string => createParam('query', query);
|
||||||
|
|
||||||
|
const addParams = (path: string, ...params: string[]) => {
|
||||||
|
const joinedParams = params.filter(Boolean).join('&');
|
||||||
|
|
||||||
|
return `${path}${joinedParams ? `?${joinedParams}` : ''}`;
|
||||||
|
};
|
||||||
|
|
||||||
export const AppRoutes = {
|
export const AppRoutes = {
|
||||||
root: {
|
root: {
|
||||||
match: '/',
|
match: '/',
|
||||||
@@ -94,11 +104,11 @@ export const AppRoutes = {
|
|||||||
sources: {
|
sources: {
|
||||||
match: 'sources',
|
match: 'sources',
|
||||||
path: '/sources',
|
path: '/sources',
|
||||||
|
|
||||||
childRoutes: {
|
childRoutes: {
|
||||||
browse: {
|
browse: {
|
||||||
match: ':sourceId',
|
match: ':sourceId',
|
||||||
path: (sourceId: SourceType['id']) => `/sources/${sourceId}`,
|
path: (sourceId: SourceType['id'], query?: string | null | undefined) =>
|
||||||
|
addParams(`/sources/${sourceId}`, createQueryParam(query)),
|
||||||
},
|
},
|
||||||
configure: {
|
configure: {
|
||||||
match: ':sourceId/configure',
|
match: ':sourceId/configure',
|
||||||
@@ -106,11 +116,7 @@ export const AppRoutes = {
|
|||||||
},
|
},
|
||||||
searchAll: {
|
searchAll: {
|
||||||
match: 'all/search',
|
match: 'all/search',
|
||||||
path: (query?: string | null | undefined) => {
|
path: (query?: string | null | undefined) => addParams('/sources/all/search', createQueryParam(query)),
|
||||||
const queryParam = query ? `query=${encodeURIComponent(query)}` : '';
|
|
||||||
|
|
||||||
return `/sources/all/search${queryParam ? `?${queryParam}` : ''}`;
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -132,14 +138,8 @@ export const AppRoutes = {
|
|||||||
},
|
},
|
||||||
library: {
|
library: {
|
||||||
match: 'library',
|
match: 'library',
|
||||||
path: (tab?: string, search?: string) => {
|
path: (tab?: string, search?: string) =>
|
||||||
const tabParam = tab ? `tab=${tab}` : '';
|
addParams('/library', createParam('tab', tab), createQueryParam(search)),
|
||||||
const searchParam = search ? `query=${encodeURIComponent(search)}` : '';
|
|
||||||
|
|
||||||
const params = [tabParam, searchParam].filter(Boolean).join('&');
|
|
||||||
|
|
||||||
return `/library${params ? `?${params}` : ''}`;
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
updates: {
|
updates: {
|
||||||
match: 'updates',
|
match: 'updates',
|
||||||
@@ -164,8 +164,8 @@ export const AppRoutes = {
|
|||||||
childRoutes: {
|
childRoutes: {
|
||||||
search: {
|
search: {
|
||||||
match: 'manga/:mangaId/search',
|
match: 'manga/:mangaId/search',
|
||||||
path: (sourceId: SourceType['id'], mangaId: MangaIdInfo['id']) =>
|
path: (sourceId: SourceType['id'], mangaId: MangaIdInfo['id'], query?: string | null | undefined) =>
|
||||||
`/migrate/source/${sourceId}/manga/${mangaId}/search`,
|
addParams(`/migrate/source/${sourceId}/manga/${mangaId}/search`, createQueryParam(query)),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ const SourceSearchPreview = React.memo(
|
|||||||
<Card sx={{ mb: 1 }}>
|
<Card sx={{ mb: 1 }}>
|
||||||
<CardActionArea
|
<CardActionArea
|
||||||
component={Link}
|
component={Link}
|
||||||
to={`${AppRoutes.sources.childRoutes.browse.path(id)}?query=${searchString}`}
|
to={AppRoutes.sources.childRoutes.browse.path(id, searchString)}
|
||||||
sx={{ p: 3 }}
|
sx={{ p: 3 }}
|
||||||
>
|
>
|
||||||
<Typography variant="h5">{displayName}</Typography>
|
<Typography variant="h5">{displayName}</Typography>
|
||||||
|
|||||||
@@ -157,7 +157,7 @@ export const MangaActionMenuItems = ({
|
|||||||
)}
|
)}
|
||||||
{isSingleMode && (
|
{isSingleMode && (
|
||||||
<Link
|
<Link
|
||||||
to={`${AppRoutes.migrate.childRoutes.search.path(manga?.sourceId ?? -1, manga?.id ?? -1)}?query=${manga?.title}`}
|
to={AppRoutes.migrate.childRoutes.search.path(manga?.sourceId ?? -1, manga?.id ?? -1, manga?.title)}
|
||||||
state={{ mangaTitle: manga?.title }}
|
state={{ mangaTitle: manga?.title }}
|
||||||
style={{ textDecoration: 'none', color: 'inherit' }}
|
style={{ textDecoration: 'none', color: 'inherit' }}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -66,7 +66,11 @@ export const MangaToolbarMenu = ({ manga, onRefresh, refreshing }: IProps) => {
|
|||||||
<>
|
<>
|
||||||
<CustomTooltip title={t('global.button.migrate')}>
|
<CustomTooltip title={t('global.button.migrate')}>
|
||||||
<Link
|
<Link
|
||||||
to={`${AppRoutes.migrate.childRoutes.search.path(manga.sourceId, manga.id)}?query=${manga.title}`}
|
to={AppRoutes.migrate.childRoutes.search.path(
|
||||||
|
manga.sourceId,
|
||||||
|
manga.id,
|
||||||
|
manga.title,
|
||||||
|
)}
|
||||||
state={{ mangaTitle: manga.title }}
|
state={{ mangaTitle: manga.title }}
|
||||||
style={{ textDecoration: 'none', color: 'inherit' }}
|
style={{ textDecoration: 'none', color: 'inherit' }}
|
||||||
>
|
>
|
||||||
@@ -126,7 +130,7 @@ export const MangaToolbarMenu = ({ manga, onRefresh, refreshing }: IProps) => {
|
|||||||
<MenuItem
|
<MenuItem
|
||||||
key="migrate"
|
key="migrate"
|
||||||
component={Link}
|
component={Link}
|
||||||
to={`${AppRoutes.migrate.childRoutes.search.path(manga.sourceId, manga.id)}?query=${manga.title}`}
|
to={AppRoutes.migrate.childRoutes.search.path(manga.sourceId, manga.id, manga.title)}
|
||||||
state={{ mangaTitle: manga.title }}
|
state={{ mangaTitle: manga.title }}
|
||||||
style={{ textDecoration: 'none', color: 'inherit' }}
|
style={{ textDecoration: 'none', color: 'inherit' }}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ const getMangaLinkTo = (
|
|||||||
case 'duplicate':
|
case 'duplicate':
|
||||||
return AppRoutes.manga.path(mangaId);
|
return AppRoutes.manga.path(mangaId);
|
||||||
case 'migrate.search':
|
case 'migrate.search':
|
||||||
return `${AppRoutes.migrate.childRoutes.search.path(sourceId ?? '-1', mangaId)}?query=${mangaTitle}`;
|
return AppRoutes.migrate.childRoutes.search.path(sourceId ?? '-1', mangaId, mangaTitle);
|
||||||
case 'migrate.select':
|
case 'migrate.select':
|
||||||
return '';
|
return '';
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user