Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | /* * Copyright 2021 Harness Inc. All rights reserved. * Use of this source code is governed by the PolyForm Shield 1.0.0 license * that can be found in the licenses directory at the root of this repository, also available at * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt. */ import { useState, useEffect } from 'react' import { useParams } from 'react-router-dom' import { useToaster } from '@common/exports' import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces' import routes from '@common/RouteDefinitions' import { OnboardingEntites } from '@cv/components/CVSetupSourcesView/SetupSourceTabs/SetupSourceTabs.constants' import { getRoutePathByType } from '@cv/utils/routeUtils' import { useIndexedDBHook, CVObjectStoreNames } from '../IndexedDBHook/IndexedDBHook' export type OnClickHandlerParams<T> = { data?: T & BaseSetupTabsObject prevTab?: number newTab?: number } export interface BaseSetupTabsObject { name?: string identifier?: string sourceType?: 'CHANGE_SOURCE' | 'MONITORING_SOURCE' | 'VERIFICATION_JOBS' type?: string // Replace with types in apis } type CVTabsHookReturnType<T> = { currentTab: number setCurrentTab: (val: number) => void maxEnabledTab: number setMaxEnabledTab: (val: number) => void onNext: ({ data, prevTab, newTab }: OnClickHandlerParams<T>) => void onPrevious: (prevTab?: number, newTab?: number) => void currentData?: T setCurrentData: (data?: T) => void } const IndexDBSourceID = 'sourceID' interface UseCVTabsHookProps { totalTabs: number } function addItemToCache(sources: any[], item: any[]): any[] { if (!sources?.length) { return item } const existingIndex = sources.findIndex(source => { if (!source.identifier) return return source.identifier === item[0].identifier }) if (existingIndex !== -1) { sources[existingIndex] = item[0] } else { sources.push(item[0]) } return sources } export default function useCVTabsHook<T>(props?: UseCVTabsHookProps): CVTabsHookReturnType<T> { const [currentTab, setCurrentTab] = useState<number>(1) const [currentData, setCurrentData] = useState<T>() const [maxEnabledTab, setMaxEnabledTab] = useState<number>(1) const { showWarning } = useToaster() const { projectIdentifier, orgIdentifier, accountId } = useParams<ProjectPathProps>() const entryKey = `${accountId}-${orgIdentifier}-${projectIdentifier}` const { isInitializingDB, dbInstance } = useIndexedDBHook({ clearStroreList: [CVObjectStoreNames.ONBOARDING_SOURCES] }) useEffect(() => { if (!isInitializingDB && dbInstance) { dbInstance.get(CVObjectStoreNames.ONBOARDING_SOURCES, IndexDBSourceID)?.then(data => { if (data) { setCurrentData(data.currentData) setCurrentTab(data.currentTab) setMaxEnabledTab(data.maxEnabledTab) } }) } }, [isInitializingDB, dbInstance]) const setDBData = async (data: unknown, curTab: number, maxTab: number) => { try { await dbInstance?.put(CVObjectStoreNames.ONBOARDING_SOURCES, { sourceID: IndexDBSourceID, currentData: data, currentTab: curTab, maxEnabledTab: maxTab }) } catch (e) { showWarning(e) } } const getRouteUrlForOnboarding = ({ type, sourceType, identifier }: any) => { if (sourceType === 'CHANGE_SOURCE') { return routes.toCVActivitySourceEditSetup({ projectIdentifier, orgIdentifier, accountId, activitySource: getRoutePathByType(type), activitySourceId: identifier }) } else if (sourceType === 'MONITORING_SOURCE') { return routes.toCVAdminSetupMonitoringSourceEdit({ projectIdentifier, orgIdentifier, accountId, monitoringSource: getRoutePathByType(type), identifier }) } } return { currentTab, currentData, setCurrentData, setCurrentTab, maxEnabledTab, setMaxEnabledTab, onNext: async ({ data, prevTab, newTab }) => { if (props?.totalTabs === currentTab) { if (dbInstance) { dbInstance.get(CVObjectStoreNames.SETUP, entryKey)?.then(async resultData => { const item = [ { type: data?.type, name: data?.name, identifier: data?.identifier, routeUrl: getRouteUrlForOnboarding({ type: data?.type, sourceType: data?.sourceType, identifier: data?.identifier }) } ] try { await dbInstance.put(CVObjectStoreNames.SETUP, { setupID: entryKey, monitoringSources: data?.sourceType === OnboardingEntites.MONITORING_SOURCE ? addItemToCache(resultData?.monitoringSources, item) : resultData?.monitoringSources, activitySources: data?.sourceType === OnboardingEntites.CHANGE_SOURCE ? addItemToCache(resultData?.activitySources, item) : resultData?.activitySources, verificationJobs: data?.sourceType === OnboardingEntites.VERIFICATION_JOBS ? addItemToCache(resultData?.verificationJobs, item) : resultData?.verificationJobs }) } catch (e) { showWarning(e) } }) } } else { if (currentTab == maxEnabledTab) { await setDBData(data, currentTab + 1, maxEnabledTab + 1) setCurrentTab(currentTab + 1) setMaxEnabledTab(maxEnabledTab + 1) } else if (currentTab < maxEnabledTab) { if (prevTab && newTab) { await setDBData(currentData, newTab, maxEnabledTab) setCurrentTab(newTab) } else { await setDBData(data, currentTab + 1, maxEnabledTab) setCurrentTab(currentTab + 1) } } } }, onPrevious: async (prevTab, newTab) => { if (prevTab && newTab) { await setDBData(currentData, newTab, maxEnabledTab) setCurrentTab(newTab) } else { await setDBData(currentData, currentTab - 1, maxEnabledTab) setCurrentTab(currentTab - 1) } } } } |