/** * @file * @copyright 2020 Aleksej Komarov * @license MIT */ import { selectBackend } from './backend'; import { Icon, Stack } from './components'; import { selectDebug } from './debug/selectors'; import { Window } from './layouts'; const requireInterface = require.context('./interfaces'); export const routingError = (type, name) => () => { return ( {type === 'notFound' && (
Interface {name} was not found.
)} {type === 'missingExport' && (
Interface {name} is missing an export.
)}
); }; const SuspendedWindow = () => { return ( ); }; const RefreshingWindow = () => { return ( Please wait... ); }; export const getRoutedComponent = (store) => { const state = store.getState(); const { suspended, config } = selectBackend(state); if (suspended) { return SuspendedWindow; } if (config.refreshing) { return RefreshingWindow; } if (process.env.NODE_ENV !== 'production') { const debug = selectDebug(state); // Show a kitchen sink if (debug.kitchenSink) { return require('./debug').KitchenSink; } } const name = config?.interface; const interfacePathBuilders = [ (name) => `./${name}.tsx`, (name) => `./${name}.js`, (name) => `./${name}/index.tsx`, (name) => `./${name}/index.js`, ]; let esModule; while (!esModule && interfacePathBuilders.length > 0) { const interfacePathBuilder = interfacePathBuilders.shift(); const interfacePath = interfacePathBuilder(name); try { esModule = requireInterface(interfacePath); } catch (err) { if (err.code !== 'MODULE_NOT_FOUND') { throw err; } } } if (!esModule) { return routingError('notFound', name); } const Component = esModule[name]; if (!Component) { return routingError('missingExport', name); } return Component; };