Skip to content

Commit

Permalink
refactor: links uses URL object instead of string manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed May 1, 2024
1 parent d920e47 commit e05fd57
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/ui/src/components/JobCard/JobCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Timeline } from './Timeline/Timeline';

interface JobCardProps {
job: AppJob;
jobUrl?: string;
jobUrl?: { pathname: string; search: string };
status: Status;
readOnlyMode: boolean;
allowRetries: boolean;
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/src/components/StatusMenu/StatusMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AppQueue } from '@bull-board/api/typings/app';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { NavLink, useRouteMatch } from 'react-router-dom';
import { NavLink } from 'react-router-dom';
import { links } from '../../utils/links';
import { QueueDropdownActions } from '../QueueDropdownActions/QueueDropdownActions';
import s from './StatusMenu.module.css';

export const StatusMenu = ({ queue, actions }: { queue: AppQueue; actions: any }) => {
const { url } = useRouteMatch();
const { t } = useTranslation();

return (
Expand All @@ -16,7 +16,7 @@ export const StatusMenu = ({ queue, actions }: { queue: AppQueue; actions: any }
const displayStatus = t(`QUEUE.STATUS.${status.toUpperCase()}`).toLocaleUpperCase();
return (
<NavLink
to={`${url}${isLatest ? '' : `?status=${status}`}`}
to={links.queuePage(queue.name, { [queue.name]: status })}
activeClassName={s.active}
isActive={(_path, { search }) => {
const query = new URLSearchParams(search);
Expand Down
35 changes: 26 additions & 9 deletions packages/ui/src/utils/links.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { SelectedStatuses } from '../../typings/app';

export const links = {
queuePage(queueName: string, selectedStatuses: SelectedStatuses = {}): string {
const withoutStatus =
!selectedStatuses[queueName] || selectedStatuses[queueName] === 'latest';
return `/queue/${encodeURIComponent(queueName)}${
withoutStatus ? '' : `?status=${selectedStatuses[queueName]}`
}`;
queuePage(
queueName: string,
selectedStatuses: SelectedStatuses = {}
): { pathname: string; search: string } {
const { pathname, searchParams } = new URL(`/queue/${queueName}`, 'http://fake.com');

const withStatus = selectedStatuses[queueName] && selectedStatuses[queueName] !== 'latest';
if (withStatus) {
searchParams.set('status', selectedStatuses[queueName]);
}

return {
pathname,
search: searchParams.toString(),
};
},
jobPage(queueName: string, jobId: string, selectedStatuses: SelectedStatuses = {}): string {
const [queuePage, search] = links.queuePage(queueName, selectedStatuses).split('?');
return [`${queuePage}/${encodeURIComponent(jobId)}`, search].filter(Boolean).join('?');
jobPage(
queueName: string,
jobId: string,
selectedStatuses: SelectedStatuses = {}
): { pathname: string; search: string } {
const { pathname: queuePath, search } = links.queuePage(queueName, selectedStatuses);
const { pathname } = new URL(`${queuePath}/${jobId}`, 'http://fake.com');
return {
pathname,
search,
};
},
};

0 comments on commit e05fd57

Please sign in to comment.