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

Add default job tab setting #676

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions packages/ui/src/components/SettingsModal/SettingsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InputField } from '../Form/InputField/InputField';
import { SelectField } from '../Form/SelectField/InputField';
import { SwitchField } from '../Form/SwitchField/SwitchField';
import { Modal } from '../Modal/Modal';
import { availableJobTabs } from '../../hooks/useDetailsTabs';

export interface SettingsModalProps {
open: boolean;
Expand All @@ -23,6 +24,7 @@ export const SettingsModal = ({ open, onClose }: SettingsModalProps) => {
collapseJobData,
collapseJobOptions,
collapseJobError,
defaultJobTab,
setSettings,
} = useSettingsStore((state) => state);
const { t } = useTranslation();
Expand All @@ -44,6 +46,16 @@ export const SettingsModal = ({ open, onClose }: SettingsModalProps) => {
value={`${pollingInterval}`}
onChange={(event) => setSettings({ pollingInterval: +event.target.value })}
/>
<SelectField
label={t('SETTINGS.DEFAULT_JOB_TAB')}
id="default-job-tab"
options={availableJobTabs.map((tab) => ({
text: t(`JOB.TABS.${tab.toUpperCase()}`),
value: tab,
}))}
value={defaultJobTab}
onChange={(event) => setSettings({ defaultJobTab: event.target.value })}
/>
<InputField
label={t('SETTINGS.JOBS_PER_PAGE')}
id="jobs-per-page"
Expand Down
36 changes: 24 additions & 12 deletions packages/ui/src/hooks/useDetailsTabs.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import { useEffect, useState } from 'react';
import { Status } from '@bull-board/api/typings/app';
import { STATUSES } from '@bull-board/api/src/constants/statuses';
import { useSettingsStore } from './useSettings';

const regularItems = ['Data', 'Options', 'Logs'] as const;
export const availableJobTabs = ['Data', 'Options', 'Logs', 'Error'] as const;

export type TabsType = typeof regularItems[number] | 'Error';
export type TabsType = (typeof availableJobTabs)[number];

const FALLBACK_TAB: TabsType = 'Data';

export function useDetailsTabs(currentStatus: Status, isJobFailed: boolean) {
const [tabs, updateTabs] = useState<TabsType[]>([]);
const [selectedTabIdx, setSelectedTabIdx] = useState(0);
const selectedTab = tabs[selectedTabIdx];
const { defaultJobTab } = useSettingsStore();

const [selectedTab, setSelectedTab] = useState<TabsType>(defaultJobTab);
felixmosh marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
const nextState: TabsType[] =
currentStatus === STATUSES.failed
? ['Error', ...regularItems]
: isJobFailed
? [...regularItems, 'Error']
: [...regularItems];
let nextState = availableJobTabs.filter((tab) => tab !== 'Error');
if (isJobFailed) {
nextState = [...nextState, 'Error'];
} else if (currentStatus === STATUSES.failed) {
nextState = ['Error', ...nextState];
}

updateTabs(nextState);
}, [currentStatus]);

useEffect(() => {
if (!tabs.includes(defaultJobTab)) {
setSelectedTab(FALLBACK_TAB);
} else {
setSelectedTab(defaultJobTab);
}
}, [defaultJobTab, tabs]);

return {
tabs: tabs.map((title, index) => ({
tabs: tabs?.map((title) => ({
title,
isActive: title === selectedTab,
selectTab: () => setSelectedTabIdx(index),
selectTab: () => setSelectedTab(title),
})),
selectedTab,
};
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import { TabsType } from './useDetailsTabs';

interface SettingsState {
pollingInterval: number;
Expand All @@ -9,6 +10,7 @@ interface SettingsState {
collapseJobData: boolean;
collapseJobOptions: boolean;
collapseJobError: boolean;
defaultJobTab: TabsType;
setSettings: (settings: Partial<Omit<SettingsState, 'setSettings'>>) => void;
}

Expand All @@ -22,6 +24,7 @@ export const useSettingsStore = create<SettingsState>()(
collapseJobData: false,
collapseJobOptions: false,
collapseJobError: false,
defaultJobTab: 'Data',
setSettings: (settings) => set(() => settings),
}),
{
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/static/locales/en-US/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"MINS": "{{count}} minutes",
"MINS_one": "{{count}} minute"
},
"DEFAULT_JOB_TAB": "Default job tab",
"JOBS_PER_PAGE": "Jobs per page (1-50)",
"CONFIRM_QUEUE_ACTIONS": "Confirm queue actions",
"CONFIRM_JOB_ACTIONS": "Confirm job actions",
Expand Down