stepper.ts 976 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { locationService } from '@grafana/runtime';
  2. import { StepKey } from '../../types';
  3. import { BASE_URL } from '../constants';
  4. import { reportSteps } from '../index';
  5. export const getNextStep = (currentStep?: StepKey) => {
  6. if (!currentStep) {
  7. return reportSteps[0];
  8. }
  9. const index = reportSteps.findIndex((step) => step.id === currentStep);
  10. // If the last step, return it
  11. if (index === reportSteps.length - 1) {
  12. return reportSteps[index];
  13. }
  14. return reportSteps[index + 1];
  15. };
  16. export const getPreviousStep = (currentStep?: StepKey) => {
  17. if (!currentStep || currentStep === reportSteps[0].id) {
  18. return;
  19. }
  20. const index = reportSteps.findIndex((step) => step.id === currentStep);
  21. return reportSteps[index - 1];
  22. };
  23. export const goToPreviousStep = (currentStep?: StepKey) => {
  24. const previous = getPreviousStep(currentStep);
  25. if (previous && previous.id !== currentStep) {
  26. locationService.push(`${BASE_URL}/${previous.id}`);
  27. }
  28. };