|
| 1 | +// Library imports |
| 2 | +import React, {useContext, useEffect, useState, FC} from 'react' |
| 3 | +import {useSelector, useDispatch} from 'react-redux' |
| 4 | +import { |
| 5 | + ComponentSize, |
| 6 | + FlexBox, |
| 7 | + IconFont, |
| 8 | + Icon, |
| 9 | + JustifyContent, |
| 10 | +} from '@influxdata/clockface' |
| 11 | + |
| 12 | +// Selectors and Context |
| 13 | +import {selectQuartzIdentity} from 'src/identity/selectors' |
| 14 | +import {UserAccountContext} from 'src/accounts/context/userAccount' |
| 15 | + |
| 16 | +// Components |
| 17 | +import {OrgDropdown} from 'src/identity/components/GlobalHeader/OrgDropdown' |
| 18 | +import {AccountDropdown} from 'src/identity/components/GlobalHeader/AccountDropdown' |
| 19 | + |
| 20 | +// Thunks |
| 21 | +import {getQuartzOrganizationsThunk} from 'src/identity/quartzOrganizations/actions/thunks' |
| 22 | + |
| 23 | +// Styles |
| 24 | +import 'src/identity/components/GlobalHeader/GlobalHeaderStyle.scss' |
| 25 | + |
| 26 | +import { |
| 27 | + emptyAccount, |
| 28 | + emptyOrg, |
| 29 | +} from 'src/identity/components/GlobalHeader/DefaultEntities' |
| 30 | +import {alphaSortSelectedFirst} from 'src/identity/utils/alphaSortSelectedFirst' |
| 31 | + |
| 32 | +export const GlobalHeader: FC = () => { |
| 33 | + const dispatch = useDispatch() |
| 34 | + const identity = useSelector(selectQuartzIdentity) |
| 35 | + const orgsList = identity.quartzOrganizations.orgs |
| 36 | + const {userAccounts} = useContext(UserAccountContext) |
| 37 | + |
| 38 | + const accountsList = userAccounts ? userAccounts : [emptyAccount] // eslint-disable-line react-hooks/exhaustive-deps |
| 39 | + |
| 40 | + const [sortedOrgs, setSortedOrgs] = useState([emptyOrg]) |
| 41 | + const [sortedAccounts, setSortedAccts] = useState([emptyAccount]) |
| 42 | + |
| 43 | + const [activeOrg, setActiveOrg] = useState(emptyOrg) |
| 44 | + const [activeAccount, setActiveAccount] = useState(emptyAccount) |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + dispatch(getQuartzOrganizationsThunk()) |
| 48 | + }, [dispatch]) |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + if (accountsList[0].id !== 0) { |
| 52 | + const currentActiveAccount = accountsList?.find( |
| 53 | + account => account?.isActive === true |
| 54 | + ) |
| 55 | + |
| 56 | + setActiveAccount(currentActiveAccount) |
| 57 | + |
| 58 | + setSortedAccts(alphaSortSelectedFirst(accountsList, currentActiveAccount)) |
| 59 | + } |
| 60 | + }, [accountsList]) |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (orgsList[0].id !== '') { |
| 64 | + const currentActiveOrg = orgsList?.find(org => org.isActive === true) |
| 65 | + |
| 66 | + setActiveOrg(currentActiveOrg) |
| 67 | + |
| 68 | + setSortedOrgs(alphaSortSelectedFirst(orgsList, currentActiveOrg)) |
| 69 | + } |
| 70 | + }, [orgsList]) |
| 71 | + |
| 72 | + return ( |
| 73 | + <FlexBox |
| 74 | + margin={ComponentSize.Large} |
| 75 | + justifyContent={JustifyContent.SpaceBetween} |
| 76 | + className="multiaccountorg--header" |
| 77 | + > |
| 78 | + <FlexBox margin={ComponentSize.Medium}> |
| 79 | + {activeOrg && activeAccount && ( |
| 80 | + <> |
| 81 | + <AccountDropdown |
| 82 | + activeOrg={activeOrg} |
| 83 | + activeAccount={activeAccount} |
| 84 | + accountsList={sortedAccounts} |
| 85 | + /> |
| 86 | + <Icon glyph={IconFont.CaretRight} /> |
| 87 | + <OrgDropdown activeOrg={activeOrg} orgsList={sortedOrgs} /> |
| 88 | + </> |
| 89 | + )} |
| 90 | + </FlexBox> |
| 91 | + </FlexBox> |
| 92 | + ) |
| 93 | +} |
0 commit comments