|
| 1 | +// Copyright (c) Cosmo Tech. |
| 2 | +// Licensed under the MIT license. |
| 3 | +import React, { useCallback } from 'react'; |
| 4 | +import { RUNNER_RUN_STATE } from '../../common/apiConstants'; |
| 5 | +import DashboardPlaceholderLayout from './DashboardPlaceholderLayout'; |
| 6 | + |
| 7 | +const DEFAULT_LABELS = { |
| 8 | + noScenario: { |
| 9 | + title: 'No scenario yet', |
| 10 | + label: 'You can create a scenario by clicking on the CREATE button', |
| 11 | + }, |
| 12 | + noDashboard: { label: "There isn't any dashboard configured for this run type" }, |
| 13 | + noRun: { label: 'The scenario has not been run yet' }, |
| 14 | + inProgress: { label: 'Scenario run in progress...' }, |
| 15 | + hasErrors: { label: 'An error occured during the scenario run' }, |
| 16 | + hasUnknownStatus: { |
| 17 | + label: 'This scenario has an unknown state, if the problem persists, please, contact your administrator', |
| 18 | + }, |
| 19 | + resultsDisplayDisabled: 'Display of results is disabled', |
| 20 | + downloadButton: 'Download logs', |
| 21 | + errors: { |
| 22 | + unknown: 'Unknown error', |
| 23 | + details: 'Something went wrong when fetching PowerBI reports info', |
| 24 | + }, |
| 25 | +}; |
| 26 | + |
| 27 | +const forgeDashboardPlaceholder = ({ |
| 28 | + alwaysShowReports, |
| 29 | + disabled, |
| 30 | + downloadLogsFile, |
| 31 | + noDashboardConfigured, |
| 32 | + labels: tmpLabels, |
| 33 | + scenario, |
| 34 | + scenarioDTO, |
| 35 | +}) => { |
| 36 | + const labels = { ...DEFAULT_LABELS, ...tmpLabels }; |
| 37 | + |
| 38 | + const noScenario = scenario === null; |
| 39 | + const scenarioLastRunStatus = noScenario ? RUNNER_RUN_STATE.CREATED : scenarioDTO.lastRunStatus; |
| 40 | + const noRun = scenarioLastRunStatus === RUNNER_RUN_STATE.CREATED || scenarioLastRunStatus === null; |
| 41 | + const runInProgress = scenarioLastRunStatus === RUNNER_RUN_STATE.RUNNING; |
| 42 | + const hasError = scenarioLastRunStatus === RUNNER_RUN_STATE.FAILED; |
| 43 | + const hasUnknownStatus = scenarioLastRunStatus === RUNNER_RUN_STATE.UNKNOWN; |
| 44 | + |
| 45 | + if (alwaysShowReports) return null; |
| 46 | + if (noScenario) return <DashboardPlaceholderLayout label={labels.noScenario.label} title={labels.noScenario.title} />; |
| 47 | + if (noRun) return <DashboardPlaceholderLayout label={labels.noRun.label} title={labels.noRun.title} />; |
| 48 | + if (runInProgress) |
| 49 | + return <DashboardPlaceholderLayout label={labels.inProgress.label} title={labels.inProgress.title} inProgress />; |
| 50 | + if (hasError) |
| 51 | + return ( |
| 52 | + <DashboardPlaceholderLayout |
| 53 | + label={labels.hasErrors.label} |
| 54 | + title={labels.hasErrors.title} |
| 55 | + downloadLogsFile={downloadLogsFile} |
| 56 | + downloadLabel={labels.downloadButton} |
| 57 | + /> |
| 58 | + ); |
| 59 | + if (hasUnknownStatus) return <DashboardPlaceholderLayout label={labels.hasUnknownStatus.label} />; |
| 60 | + if (disabled) return <DashboardPlaceholderLayout label={labels.resultsDisplayDisabled} />; |
| 61 | + if (noDashboardConfigured) return <DashboardPlaceholderLayout label={labels.noDashboard.label} />; |
| 62 | + return null; |
| 63 | +}; |
| 64 | + |
| 65 | +export const useDashboardPlaceholder = () => { |
| 66 | + const getDashboardPlaceholder = useCallback((params) => { |
| 67 | + const placeholder = forgeDashboardPlaceholder(params); |
| 68 | + return { placeholder, showPlaceholder: placeholder != null }; |
| 69 | + }, []); |
| 70 | + |
| 71 | + return { getDashboardPlaceholder }; |
| 72 | +}; |
0 commit comments