Skip to content

Commit

Permalink
Use context to pass the windowId prop around
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Dec 17, 2024
1 parent 41e2a86 commit 472c27b
Show file tree
Hide file tree
Showing 87 changed files with 271 additions and 159 deletions.
3 changes: 1 addition & 2 deletions __tests__/src/components/CompanionWindowFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import { CompanionWindowFactory } from '../../../src/components/CompanionWindowF
function createWrapper({ content = 'closed', ...props }) {
return render(
<CompanionWindowFactory
windowId="x"
id="123"
content={content}
{...props}
/>,
{ preloadedState: { companionWindows: { 123: { content }, thumb: {} }, windows: { x: { thumbnailNavigationId: 'thumb' } } } },
{ preloadedState: { companionWindows: { 123: { content }, thumb: {} }, windows: { x: { thumbnailNavigationId: 'thumb' } } }, windowId: 'x' },
);
}

Expand Down
2 changes: 1 addition & 1 deletion __tests__/src/components/GalleryView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ function createWrapper(props) {
return render(
<GalleryView
canvases={Utils.parseManifest(manifestJson).getSequences()[0].getCanvases()}
windowId="1234"
selectedCanvasIndex={0}
{...props}
/>,
{ windowId: '1234' },
);
}

Expand Down
11 changes: 3 additions & 8 deletions __tests__/src/components/PrimaryWindow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ function createWrapper(props) {
return render(
<PrimaryWindow
classes={{}}
windowId="xyz"
{...props}
/>,
{ preloadedState: { windows: { xyz: { collectionPath: [{}], companionWindowIds: [] } } } },
{ preloadedState: { windows: { xyz: { collectionPath: [{}], companionWindowIds: [] } } }, windowId: 'xyz' },
);
}

Expand All @@ -30,10 +29,7 @@ describe('PrimaryWindow', () => {
});
it('should render <GalleryView> if fetching is complete and view is gallery', async () => {
createWrapper({ isFetching: false, view: 'gallery' });
await screen.findByTestId('test-window');
await waitFor(() => {
expect(document.querySelector('#xyz-gallery')).toBeInTheDocument(); // eslint-disable-line testing-library/no-node-access
});
expect(await screen.findByRole('region', { accessibleName: 'gallery section' })).toBeInTheDocument();
});
it('should render <CollectionDialog> and <SelectCollection> if manifest is collection and isCollectionDialogVisible', async () => {
render(<div id="xyz" />);
Expand All @@ -42,9 +38,8 @@ describe('PrimaryWindow', () => {
classes={{}}
isCollection
isCollectionDialogVisible
windowId="xyz"
/>,
{ preloadedState: { windows: { xyz: { collectionPath: [{}] } } } },
{ preloadedState: { windows: { xyz: { collectionPath: [{}] } } }, windowId: 'xyz' },
);
await screen.findByRole('button', { accessibleName: 'show collection' });
});
Expand Down
1 change: 1 addition & 0 deletions __tests__/src/components/SearchResults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function createWrapper(props) {
window: {},
},
},
windowId: 'window',
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/src/components/WindowSideBar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { WindowSideBar } from '../../../src/components/WindowSideBar';
function createWrapper({ ...props }) {
return render(
<WindowSideBar
windowId="xyz"
{...props}
/>,
{
Expand All @@ -17,6 +16,7 @@ function createWrapper({ ...props }) {
},
},
},
windowId: 'xyz',
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ function createWrapper(props, state) {
annotationCount={4}
classes={{}}
id="xyz"
windowId="abc"
{...props}
/>,
{ preloadedState: { companionWindows: { xyz: { content: 'annotations' } }, windows: { abc: {} }, ...state } },
{ preloadedState: { companionWindows: { xyz: { content: 'annotations' } }, windows: { abc: {} }, ...state }, windowId: 'abc' },
);
}

Expand Down
20 changes: 13 additions & 7 deletions __tests__/utils/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { I18nextProvider } from 'react-i18next';
import createRootReducer from '../../src/state/reducers/rootReducer';
import settings from '../../src/config/settings';
import createI18nInstance from '../../src/i18n';
import WindowContext from '../../src/contexts/WindowContext';

/** Mirador viewer setup for Integration tests */
import Mirador from '../../src/index';
Expand All @@ -29,19 +30,24 @@ function renderWithProviders(
preloadedState = {},
// Automatically create a store instance if no store was passed in
store = createStore(rootReducer, preloadedState, applyMiddleware(thunk)),
windowId,
...renderOptions
} = {},
) {
const windowContext = windowId ? { id: windowId } : {};

/** :nodoc: */
function Wrapper({ children }) {
return (
<I18nextProvider i18n={i18n}>
<ThemeProvider theme={theme}>
<Provider store={store}>
{children}
</Provider>
</ThemeProvider>
</I18nextProvider>
<WindowContext.Provider value={windowContext}>
<I18nextProvider i18n={i18n}>
<ThemeProvider theme={theme}>
<Provider store={store}>
{children}
</Provider>
</ThemeProvider>
</I18nextProvider>
</WindowContext.Provider>
);
}

Expand Down
1 change: 0 additions & 1 deletion src/components/AnnotationSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ AnnotationSettings.propTypes = {
displayAll: PropTypes.bool.isRequired,
displayAllDisabled: PropTypes.bool.isRequired,
toggleAnnotationDisplay: PropTypes.func.isRequired,
windowId: PropTypes.string.isRequired, // eslint-disable-line react/no-unused-prop-types
};
3 changes: 0 additions & 3 deletions src/components/AttributionPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export function AttributionPanel({
manifestLogo = null,
requiredStatement = null,
rights = null,
windowId,
id,
}) {
const { t } = useTranslation();
Expand All @@ -37,7 +36,6 @@ export function AttributionPanel({
<CompanionWindow
title={t('attributionTitle')}
paperClassName={ns('attribution-panel')}
windowId={windowId}
id={id}
>
<CompanionWindowSection>
Expand Down Expand Up @@ -86,5 +84,4 @@ AttributionPanel.propTypes = {
value: PropTypes.string,
})),
rights: PropTypes.arrayOf(PropTypes.string),
windowId: PropTypes.string.isRequired,
};
2 changes: 1 addition & 1 deletion src/components/CompanionArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function CompanionArea({
className={`${ns('companion-windows')}`}
>
{companionWindowIds.map((id) => (
<CompanionWindowFactory id={id} key={id} windowId={windowId} />
<CompanionWindowFactory id={id} key={id} />
))}
</Container>
</Slide>
Expand Down
10 changes: 4 additions & 6 deletions src/components/CompanionWindowFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,30 @@ import ErrorContent from '../containers/ErrorContent';
* Render a companion window using the appropriate component for the content
*/
export function CompanionWindowFactory({
content = null, id, windowId,
content = null, id,
}) {
const { t } = useTranslation();
const ErroredCompanionWindow = useCallback(({ error }) => (
<CompanionWindow
title={t('error')}
windowId={windowId}
id={id}
>
<ErrorContent error={error} windowId={windowId} companionWindowId={id} />
<ErrorContent error={error} companionWindowId={id} />
</CompanionWindow>
), [windowId, t, id]);
), [t, id]);

const DynamicCompanionWindowType = CompanionWindowRegistry[content];

if (!DynamicCompanionWindowType) return null;

return (
<ErrorBoundary fallbackComponent={ErroredCompanionWindow}>
<DynamicCompanionWindowType id={id} windowId={windowId} />
<DynamicCompanionWindowType id={id} />
</ErrorBoundary>
);
}

CompanionWindowFactory.propTypes = {
content: PropTypes.string,
id: PropTypes.string.isRequired,
windowId: PropTypes.string.isRequired,
};
4 changes: 1 addition & 3 deletions src/components/CustomPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import CompanionWindow from '../containers/CompanionWindow';
* a custom panel that can be used for anything
*/
export function CustomPanel({
id, children = null, title, windowId,
id, children = null, title,
}) {
const { t } = useTranslation();
return (
<CompanionWindow
title={t(title)}
id={id}
windowId={windowId}
>
{children}
</CompanionWindow>
Expand All @@ -24,5 +23,4 @@ CustomPanel.propTypes = {
children: PropTypes.node,
id: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
windowId: PropTypes.string.isRequired,
};
5 changes: 1 addition & 4 deletions src/components/GalleryView.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Root = styled(Paper, { name: 'GalleryView', slot: 'root' })(({ theme }) =>
/**
* Renders a GalleryView overview of the manifest.
*/
export function GalleryView({ canvases, viewingDirection = '', windowId }) {
export function GalleryView({ canvases, viewingDirection = '' }) {
const htmlDir = viewingDirection === 'right-to-left' ? 'rtl' : 'ltr';
return (
<Root
Expand All @@ -26,13 +26,11 @@ export function GalleryView({ canvases, viewingDirection = '', windowId }) {
dir={htmlDir}
square
elevation={0}
id={`${windowId}-gallery`}
>
{
canvases.map(canvas => (
<GalleryViewThumbnail
key={canvas.id}
windowId={windowId}
canvas={canvas}
/>
))
Expand All @@ -44,5 +42,4 @@ export function GalleryView({ canvases, viewingDirection = '', windowId }) {
GalleryView.propTypes = {
canvases: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
viewingDirection: PropTypes.string,
windowId: PropTypes.string.isRequired,
};
1 change: 0 additions & 1 deletion src/components/IIIFAuthentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export function IIIFAuthentication({
authServiceId,
hasLogoutService: !!logoutServiceId,
status,
windowId,
});

/** handle the IIIF logout workflow */
Expand Down
5 changes: 1 addition & 4 deletions src/components/LayersPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ import CanvasLayers from '../containers/CanvasLayers';
* a panel showing the canvases for a given manifest
*/
export function LayersPanel({
canvasIds = [], id, windowId,
canvasIds = [], id,
}) {
const { t } = useTranslation();
return (
<CompanionWindow
title={t('layers')}
id={id}
windowId={windowId}
>
{canvasIds.map((canvasId, index) => (
<CanvasLayers
canvasId={canvasId}
index={index}
key={canvasId}
totalSize={canvasIds.length}
windowId={windowId}
/>
))}
</CompanionWindow>
Expand All @@ -32,5 +30,4 @@ export function LayersPanel({
LayersPanel.propTypes = {
canvasIds: PropTypes.arrayOf(PropTypes.string),
id: PropTypes.string.isRequired,
windowId: PropTypes.string.isRequired,
};
2 changes: 1 addition & 1 deletion src/components/OpenSeadragonViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function OpenSeadragonViewer({
);
})}
{ drawAnnotations
&& <AnnotationsOverlay viewer={viewer} windowId={windowId} /> }
&& <AnnotationsOverlay viewer={viewer} /> }
{ enhancedChildren }
<PluginHook viewer={viewer} {...pluginProps} />
</OpenSeadragonComponent>
Expand Down
33 changes: 10 additions & 23 deletions src/components/PrimaryWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,31 @@ const Root = styled('div', { name: 'PrimaryWindow', slot: 'root' })(() => ({
/** */
const TypeSpecificViewer = ({
audioResources = [], isCollection = false,
isFetching = false, videoResources = [], view = undefined, windowId,
isFetching = false, videoResources = [], view = undefined,
}) => {
if (isCollection) {
return (
<SelectCollection
windowId={windowId}
/>
<SelectCollection />
);
}
if (isFetching === false) {
if (view === 'gallery') {
return (
<GalleryView
windowId={windowId}
/>
<GalleryView />
);
}
if (videoResources.length > 0) {
return (
<VideoViewer
windowId={windowId}
/>
<VideoViewer />
);
}
if (audioResources.length > 0) {
return (
<AudioViewer
windowId={windowId}
/>
<AudioViewer />
);
}
return (
<WindowViewer
windowId={windowId}
/>
<WindowViewer />
);
}
return null;
Expand All @@ -72,7 +62,6 @@ TypeSpecificViewer.propTypes = {
isFetching: PropTypes.bool,
videoResources: PropTypes.arrayOf(PropTypes.object), // eslint-disable-line react/forbid-prop-types
view: PropTypes.string,
windowId: PropTypes.string.isRequired,
};

/**
Expand All @@ -81,22 +70,21 @@ TypeSpecificViewer.propTypes = {
*/
export function PrimaryWindow({
audioResources = undefined, isCollection = false, isFetching = false, videoResources = undefined,
view = undefined, windowId, isCollectionDialogVisible = false, children = null, className = undefined,
view = undefined, isCollectionDialogVisible = false, children = null, className = undefined,
}) {
const viewerProps = {
audioResources,
isCollection,
isFetching,
videoResources,
view,
windowId,
};

return (
<Root data-testid="test-window" className={classNames(ns('primary-window'), className)}>
<WindowSideBar windowId={windowId} />
<CompanionArea windowId={windowId} position="left" />
{ isCollectionDialogVisible && <CollectionDialog windowId={windowId} /> }
<WindowSideBar />
<CompanionArea position="left" />
{ isCollectionDialogVisible && <CollectionDialog /> }
<Suspense fallback={<div />}>
{children || <TypeSpecificViewer {...viewerProps} />}
</Suspense>
Expand All @@ -113,5 +101,4 @@ PrimaryWindow.propTypes = {
isFetching: PropTypes.bool,
videoResources: PropTypes.arrayOf(PropTypes.object), // eslint-disable-line react/forbid-prop-types
view: PropTypes.string,
windowId: PropTypes.string.isRequired,
};
Loading

0 comments on commit 472c27b

Please sign in to comment.