Skip to content

Commit cc950a3

Browse files
committed
refactor: Use visibleTabs instead of showOnlyHomeTab
1 parent 4e5b333 commit cc950a3

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

src/course-outline/subsection-card/SubsectionCard.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { ComponentPicker } from '../../library-authoring';
2626
import { COMPONENT_TYPES } from '../../generic/block-type-utils/constants';
2727
import { ContainerType } from '../../generic/key-utils';
2828
import { useStudioHome } from '../../studio-home/hooks';
29+
import { ContentType } from '../../library-authoring/routes';
2930

3031
const SubsectionCard = ({
3132
section,
@@ -302,7 +303,7 @@ const SubsectionCard = ({
302303
extraFilter={['block_type = "unit"']}
303304
componentPickerMode="single"
304305
onComponentSelected={handleSelectLibraryUnit}
305-
showOnlyHomeTab
306+
visibleTabs={[ContentType.units]}
306307
/>
307308
</StandardModal>
308309
</>

src/library-authoring/LibraryAuthoringPage.tsx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { LibrarySidebar } from './library-sidebar';
4343
import { useComponentPickerContext } from './common/context/ComponentPickerContext';
4444
import { useLibraryContext } from './common/context/LibraryContext';
4545
import { SidebarBodyComponentId, useSidebarContext } from './common/context/SidebarContext';
46-
import { ContentType, useLibraryRoutes } from './routes';
46+
import { allLibraryPageTabs, ContentType, useLibraryRoutes } from './routes';
4747

4848
import messages from './messages';
4949

@@ -129,10 +129,13 @@ export const SubHeaderTitle = ({ title }: { title: ReactNode }) => {
129129

130130
interface LibraryAuthoringPageProps {
131131
returnToLibrarySelection?: () => void,
132-
showOnlyHomeTab?: boolean,
132+
visibleTabs?: ContentType[],
133133
}
134134

135-
const LibraryAuthoringPage = ({ returnToLibrarySelection, showOnlyHomeTab = false }: LibraryAuthoringPageProps) => {
135+
const LibraryAuthoringPage = ({
136+
returnToLibrarySelection,
137+
visibleTabs = allLibraryPageTabs,
138+
}: LibraryAuthoringPageProps) => {
136139
const intl = useIntl();
137140

138141
const {
@@ -164,7 +167,7 @@ const LibraryAuthoringPage = ({ returnToLibrarySelection, showOnlyHomeTab = fals
164167
// The activeKey determines the currently selected tab.
165168
const getActiveKey = () => {
166169
if (componentPickerMode) {
167-
return ContentType.home;
170+
return visibleTabs[0];
168171
}
169172
if (insideCollections) {
170173
return ContentType.collections;
@@ -246,16 +249,15 @@ const LibraryAuthoringPage = ({ returnToLibrarySelection, showOnlyHomeTab = fals
246249
// Disable filtering by block/problem type when viewing the Collections tab.
247250
const overrideTypesFilter = (insideCollections || insideUnits) ? new TypesFilterData() : undefined;
248251

249-
const visibleTabs = [
250-
<Tab eventKey={ContentType.home} title={intl.formatMessage(messages.homeTab)} />,
251-
];
252-
if (!showOnlyHomeTab) {
253-
visibleTabs.push(
254-
<Tab eventKey={ContentType.collections} title={intl.formatMessage(messages.collectionsTab)} />,
255-
<Tab eventKey={ContentType.components} title={intl.formatMessage(messages.componentsTab)} />,
256-
<Tab eventKey={ContentType.units} title={intl.formatMessage(messages.unitsTab)} />,
257-
);
258-
}
252+
const tabTitles = {
253+
[ContentType.home]: intl.formatMessage(messages.homeTab),
254+
[ContentType.collections]: intl.formatMessage(messages.collectionsTab),
255+
[ContentType.components]: intl.formatMessage(messages.componentsTab),
256+
[ContentType.units]: intl.formatMessage(messages.unitsTab),
257+
};
258+
const visibleTabsToRender = visibleTabs.map((contentType) => (
259+
<Tab eventKey={contentType} title={tabTitles[contentType]} />
260+
));
259261

260262
return (
261263
<div className="d-flex">
@@ -291,7 +293,7 @@ const LibraryAuthoringPage = ({ returnToLibrarySelection, showOnlyHomeTab = fals
291293
onSelect={handleTabChange}
292294
className="my-3"
293295
>
294-
{visibleTabs}
296+
{visibleTabsToRender}
295297
</Tabs>
296298
<ActionRow className="my-3">
297299
<SearchKeywordsField className="mr-3" />

src/library-authoring/component-picker/ComponentPicker.test.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '../data/api.mocks';
1818

1919
import { ComponentPicker } from './ComponentPicker';
20+
import { ContentType } from '../routes';
2021

2122
jest.mock('react-router-dom', () => ({
2223
...jest.requireActual('react-router-dom'),
@@ -278,7 +279,7 @@ describe('<ComponentPicker />', () => {
278279
});
279280

280281
it('should display all tabs', async () => {
281-
// Default `showOnlyHomeTab=false`
282+
// Default `visibleTabs = allLibraryPageTabs`
282283
render(<ComponentPicker />);
283284

284285
expect(await screen.findByText('Test Library 1')).toBeInTheDocument();
@@ -290,15 +291,15 @@ describe('<ComponentPicker />', () => {
290291
expect(screen.getByRole('tab', { name: /units/i })).toBeInTheDocument();
291292
});
292293

293-
it('should display only home tab when `showOnlyHomeTab` is true', async () => {
294-
render(<ComponentPicker showOnlyHomeTab />);
294+
it('should display only unit tab', async () => {
295+
render(<ComponentPicker visibleTabs={[ContentType.units]} />);
295296

296297
expect(await screen.findByText('Test Library 1')).toBeInTheDocument();
297298
fireEvent.click(screen.getByDisplayValue(/lib:sampletaxonomyorg1:tl1/i));
298299

299-
expect(await screen.findByRole('tab', { name: /all content/i })).toBeInTheDocument();
300+
expect(await screen.findByRole('tab', { name: /units/i })).toBeInTheDocument();
301+
expect(screen.queryByRole('tab', { name: /all content/i })).not.toBeInTheDocument();
300302
expect(screen.queryByRole('tab', { name: /collections/i })).not.toBeInTheDocument();
301303
expect(screen.queryByRole('tab', { name: /components/i })).not.toBeInTheDocument();
302-
expect(screen.queryByRole('tab', { name: /units/i })).not.toBeInTheDocument();
303304
});
304305
});

src/library-authoring/component-picker/ComponentPicker.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ import LibraryAuthoringPage from '../LibraryAuthoringPage';
1414
import LibraryCollectionPage from '../collections/LibraryCollectionPage';
1515
import SelectLibrary from './SelectLibrary';
1616
import messages from './messages';
17+
import { ContentType, allLibraryPageTabs } from '../routes';
1718

1819
interface LibraryComponentPickerProps {
1920
returnToLibrarySelection: () => void;
20-
showOnlyHomeTab: boolean;
21+
visibleTabs: ContentType[],
2122
}
2223

2324
const InnerComponentPicker: React.FC<LibraryComponentPickerProps> = ({
2425
returnToLibrarySelection,
25-
showOnlyHomeTab,
26+
visibleTabs,
2627
}) => {
2728
const { collectionId } = useLibraryContext();
2829

@@ -32,7 +33,7 @@ const InnerComponentPicker: React.FC<LibraryComponentPickerProps> = ({
3233
return (
3334
<LibraryAuthoringPage
3435
returnToLibrarySelection={returnToLibrarySelection}
35-
showOnlyHomeTab={showOnlyHomeTab}
36+
visibleTabs={visibleTabs}
3637
/>
3738
);
3839
};
@@ -51,7 +52,7 @@ type ComponentPickerProps = {
5152
libraryId?: string,
5253
showOnlyPublished?: boolean,
5354
extraFilter?: string[],
54-
showOnlyHomeTab?: boolean,
55+
visibleTabs?: ContentType[],
5556
} & (
5657
{
5758
componentPickerMode?: 'single',
@@ -70,7 +71,7 @@ export const ComponentPicker: React.FC<ComponentPickerProps> = ({
7071
showOnlyPublished,
7172
extraFilter,
7273
componentPickerMode = 'single',
73-
showOnlyHomeTab = false,
74+
visibleTabs = allLibraryPageTabs,
7475
/** This default callback is used to send the selected component back to the parent window,
7576
* when the component picker is used in an iframe.
7677
*/
@@ -133,7 +134,7 @@ export const ComponentPicker: React.FC<ComponentPickerProps> = ({
133134
)}
134135
<InnerComponentPicker
135136
returnToLibrarySelection={returnToLibrarySelection}
136-
showOnlyHomeTab={showOnlyHomeTab}
137+
visibleTabs={visibleTabs}
137138
/>
138139
</SidebarProvider>
139140
</LibraryProvider>

src/library-authoring/routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export enum ContentType {
4141
units = 'units',
4242
}
4343

44+
export const allLibraryPageTabs: ContentType[] = Object.values(ContentType);
45+
4446
export type NavigateToData = {
4547
componentId?: string,
4648
collectionId?: string,

0 commit comments

Comments
 (0)