/* eslint-disable react/jsx-props-no-spreading */ import React, { useCallback, useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { ExtensionManager, MODULE_TYPES } from '@ohif/core'; // import { extensionManager } from '../App'; import { useParams } from 'react-router'; import useSearchParams from '../hooks/useSearchParams'; import { useAppConfig } from '@state'; import { useStudyListQuery } from '../hooks'; /** * Uses route properties to determine the data source that should be passed * to the child layout template. In some instances, initiates requests and * passes data as props. * * @param {object} props * @param {function} props.children - Layout Template React Component */ function DataSourceWrapper(props: withAppTypes) { const { servicesManager } = props; const { children: LayoutTemplate, ...rest } = props; const params = useParams(); const lowerCaseSearchParams = useSearchParams({ lowerCaseKeys: true }); const query = useSearchParams(); const [appConfig] = useAppConfig(); // Route props --> studies.mapParams // mapParams --> studies.search // studies.search --> studies.processResults // studies.processResults --> // But only for LayoutTemplate type of 'list'? // Or no data fetching here, and just hand down my source const getInitialDataSourceName = useCallback(() => { // TODO - get the variable from the props all the time... let dataSourceName = lowerCaseSearchParams.get('datasources'); if (!dataSourceName && appConfig.defaultDataSourceName) { return ''; } if (!dataSourceName) { // Gets the first defined datasource with the right name // Mostly for historical reasons - new configs should use the defaultDataSourceName const dataSourceModules = extensionManager.modules[MODULE_TYPES.DATA_SOURCE]; // TODO: Good usecase for flatmap? const webApiDataSources = dataSourceModules.reduce((acc, curr) => { const mods = []; curr.module.forEach(mod => { if (mod.type === 'webApi') { mods.push(mod); } }); return acc.concat(mods); }, []); dataSourceName = webApiDataSources .map(ds => ds.name) .find(it => extensionManager.getDataSources(it)?.[0] !== undefined); } return dataSourceName; }, []); const [isDataSourceInitialized, setIsDataSourceInitialized] = useState(false); // The path to the data source to be used in the URL for a mode (e.g. mode/dataSourcePath?StudyIntanceUIDs=1.2.3) const [dataSourcePath, setDataSourcePath] = useState(() => { const dataSourceName = getInitialDataSourceName(); return dataSourceName ? `/${dataSourceName}` : ''; }); const [dataSource, setDataSource] = useState(() => { const dataSourceName = getInitialDataSourceName(); if (!dataSourceName) { return extensionManager.getActiveDataSource()[0]; } const dataSource = extensionManager.getDataSources(dataSourceName)?.[0]; if (!dataSource) { throw new Error(`No data source found for ${dataSourceName}`); } return dataSource; }); const { studies, isLoading, hasFetchedOnce, refresh } = useStudyListQuery({ dataSource, isDataSourceInitialized, servicesManager, }); /** * The effect to initialize the data source whenever it changes. Similar to * whenever a different Mode is entered, the Mode's data source is initialized, so * too this DataSourceWrapper must initialize its data source whenever a different * data source is activated. Furthermore, a data source might be initialized * several times as it gets activated/deactivated because the location URL * might change and data sources initialize based on the URL. */ useEffect(() => { const initializeDataSource = async () => { await dataSource.initialize({ params, query }); setIsDataSourceInitialized(true); }; initializeDataSource(); }, [dataSource]); useEffect(() => { const dataSourceChangedCallback = () => { setIsDataSourceInitialized(false); setDataSourcePath(''); setDataSource(extensionManager.getActiveDataSource()[0]); // Resets the cached data, the loading flag, and the first-fetch gate, // then triggers a new query just like the initial load. refresh(); }; const sub = extensionManager.subscribe( ExtensionManager.EVENTS.ACTIVE_DATA_SOURCE_CHANGED, dataSourceChangedCallback ); return () => sub.unsubscribe(); }, []); // TODO: Better way to pass DataSource? return ( ); } DataSourceWrapper.propTypes = { /** Layout Component to wrap with a Data Source */ children: PropTypes.oneOfType([PropTypes.element, PropTypes.func]).isRequired, }; export default DataSourceWrapper;