-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(nav): Upgrade org dropdown to new components #86824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b51a64
feat(nav): Upgrade org dropdown to new components
malwilley adff20f
Use OrganizationBadge and UserBadge
malwilley eb8ece6
Also use for switch org items
malwilley 494d6a4
Fix tests
malwilley ac51ea0
Use leadingItems for create org button
malwilley 6b9fd8d
Add usage & billing item when appropriate
malwilley 763f57f
Merge remote-tracking branch 'origin/master' into malwilley/feat/nav-…
malwilley bd240d8
Fix button import
malwilley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import {OrganizationFixture} from 'sentry-fixture/organization'; | ||
import {UserFixture} from 'sentry-fixture/user'; | ||
|
||
import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary'; | ||
|
||
import {OrgDropdown} from 'sentry/components/nav/orgDropdown'; | ||
import ConfigStore from 'sentry/stores/configStore'; | ||
import OrganizationsStore from 'sentry/stores/organizationsStore'; | ||
|
||
describe('OrgDropdown', function () { | ||
const organization = OrganizationFixture({ | ||
access: ['org:read', 'member:read', 'team:read'], | ||
}); | ||
|
||
beforeEach(() => { | ||
ConfigStore.set('user', UserFixture()); | ||
}); | ||
|
||
it('displays org info and links', async function () { | ||
render(<OrgDropdown />, {organization}); | ||
|
||
await userEvent.click(screen.getByRole('button', {name: 'Toggle organization menu'})); | ||
|
||
expect(screen.getByText('org-slug')).toBeInTheDocument(); | ||
expect(screen.getByText('0 Projects')).toBeInTheDocument(); | ||
|
||
expect(screen.getByRole('link', {name: 'Organization Settings'})).toHaveAttribute( | ||
'href', | ||
`/settings/${organization.slug}/` | ||
); | ||
expect(screen.getByRole('link', {name: 'Members'})).toHaveAttribute( | ||
'href', | ||
`/settings/${organization.slug}/members/` | ||
); | ||
expect(screen.getByRole('link', {name: 'Teams'})).toHaveAttribute( | ||
'href', | ||
`/settings/${organization.slug}/teams/` | ||
); | ||
}); | ||
|
||
it('displays user info and links', async function () { | ||
render(<OrgDropdown />, {organization}); | ||
|
||
await userEvent.click(screen.getByRole('button', {name: 'Toggle organization menu'})); | ||
|
||
expect(screen.getByText('Foo Bar')).toBeInTheDocument(); | ||
expect(screen.getByText('[email protected]')).toBeInTheDocument(); | ||
|
||
expect(screen.getByRole('link', {name: 'User Settings'})).toHaveAttribute( | ||
'href', | ||
'/settings/account/' | ||
); | ||
expect(screen.getByRole('link', {name: 'User Auth Tokens'})).toHaveAttribute( | ||
'href', | ||
'/settings/account/api/' | ||
); | ||
}); | ||
|
||
it('can sign out', async function () { | ||
const mockLogout = MockApiClient.addMockResponse({ | ||
url: '/auth/', | ||
method: 'DELETE', | ||
}); | ||
|
||
render(<OrgDropdown />, {organization}); | ||
|
||
await userEvent.click(screen.getByRole('button', {name: 'Toggle organization menu'})); | ||
|
||
await userEvent.click(screen.getByText('Sign Out')); | ||
|
||
await waitFor(() => { | ||
expect(mockLogout).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('hides admin link if user is not admin', async function () { | ||
render(<OrgDropdown />, {organization}); | ||
|
||
await userEvent.click(screen.getByRole('button', {name: 'Toggle organization menu'})); | ||
|
||
expect(screen.queryByRole('link', {name: 'Admin'})).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('shows admin link if user is admin', async function () { | ||
ConfigStore.set('user', UserFixture({isSuperuser: true})); | ||
|
||
render(<OrgDropdown />, {organization}); | ||
|
||
await userEvent.click(screen.getByRole('button', {name: 'Toggle organization menu'})); | ||
|
||
expect(screen.getByRole('link', {name: 'Admin'})).toHaveAttribute('href', `/manage/`); | ||
}); | ||
|
||
it('can switch orgs', async function () { | ||
OrganizationsStore.addOrReplace( | ||
OrganizationFixture({id: '1', name: 'Org 1', slug: 'org-1'}) | ||
); | ||
OrganizationsStore.addOrReplace( | ||
OrganizationFixture({id: '2', name: 'Org 2', slug: 'org-2'}) | ||
); | ||
|
||
render(<OrgDropdown />, {organization}); | ||
|
||
await userEvent.click(screen.getByRole('button', {name: 'Toggle organization menu'})); | ||
|
||
await userEvent.hover(screen.getByText('Switch Organization')); | ||
|
||
expect(await screen.findByRole('link', {name: /org-1/})).toHaveAttribute( | ||
'href', | ||
`/organizations/org-1/issues/` | ||
); | ||
expect(await screen.findByRole('link', {name: /org-2/})).toHaveAttribute( | ||
'href', | ||
`/organizations/org-2/issues/` | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import styled from '@emotion/styled'; | ||
import orderBy from 'lodash/orderBy'; | ||
|
||
import {logout} from 'sentry/actionCreators/account'; | ||
import {OrganizationAvatar} from 'sentry/components/core/avatar/organizationAvatar'; | ||
import {Button} from 'sentry/components/core/button'; | ||
import {DropdownMenu, type MenuItemProps} from 'sentry/components/dropdownMenu'; | ||
import OrganizationBadge from 'sentry/components/idBadge/organizationBadge'; | ||
import UserBadge from 'sentry/components/idBadge/userBadge'; | ||
import {IconAdd} from 'sentry/icons'; | ||
import {t, tn} from 'sentry/locale'; | ||
import ConfigStore from 'sentry/stores/configStore'; | ||
import OrganizationsStore from 'sentry/stores/organizationsStore'; | ||
import {useLegacyStore} from 'sentry/stores/useLegacyStore'; | ||
import {isDemoModeEnabled} from 'sentry/utils/demoMode'; | ||
import {localizeDomain, resolveRoute} from 'sentry/utils/resolveRoute'; | ||
import useApi from 'sentry/utils/useApi'; | ||
import useOrganization from 'sentry/utils/useOrganization'; | ||
import useProjects from 'sentry/utils/useProjects'; | ||
import {useUser} from 'sentry/utils/useUser'; | ||
|
||
function createOrganizationMenuItem(): MenuItemProps { | ||
const configFeatures = ConfigStore.get('features'); | ||
const sentryUrl = localizeDomain(ConfigStore.get('links').sentryUrl); | ||
const route = '/organizations/new/'; | ||
const canCreateOrg = ConfigStore.get('features').has('organizations:create'); | ||
|
||
const menuItemProps: MenuItemProps = { | ||
key: 'create-organization', | ||
leadingItems: <IconAdd />, | ||
label: t('Create a new organization'), | ||
}; | ||
|
||
if (configFeatures.has('system:multi-region')) { | ||
menuItemProps.externalHref = sentryUrl + route; | ||
} else { | ||
menuItemProps.to = route; | ||
} | ||
|
||
return { | ||
key: 'create-organization-section', | ||
children: [menuItemProps], | ||
hidden: !canCreateOrg, | ||
}; | ||
} | ||
|
||
export function OrgDropdown() { | ||
const api = useApi(); | ||
|
||
const config = useLegacyStore(ConfigStore); | ||
const organization = useOrganization(); | ||
const user = useUser(); | ||
|
||
// It's possible we do not have an org in context (e.g. RouteNotFound) | ||
// Otherwise, we should have the full org | ||
const hasOrgRead = organization.access?.includes('org:read'); | ||
const hasMemberRead = organization.access?.includes('member:read'); | ||
const hasTeamRead = organization.access?.includes('team:read'); | ||
const hasBillingAccess = organization.access?.includes('org:billing'); | ||
|
||
const {organizations} = useLegacyStore(OrganizationsStore); | ||
|
||
const {projects} = useProjects(); | ||
|
||
function handleLogout() { | ||
logout(api); | ||
} | ||
|
||
return ( | ||
<DropdownMenu | ||
trigger={props => ( | ||
<OrgDropdownTrigger | ||
size="zero" | ||
borderless | ||
aria-label={t('Toggle organization menu')} | ||
{...props} | ||
> | ||
<StyledOrganizationAvatar size={32} round={false} organization={organization} /> | ||
</OrgDropdownTrigger> | ||
)} | ||
minMenuWidth={200} | ||
items={[ | ||
{ | ||
key: 'organization', | ||
label: ( | ||
<SectionTitleWrapper> | ||
<OrganizationBadge | ||
organization={organization} | ||
description={tn('%s Project', '%s Projects', projects.length)} | ||
avatarSize={32} | ||
/> | ||
</SectionTitleWrapper> | ||
), | ||
children: [ | ||
{ | ||
key: 'organization-settings', | ||
label: t('Organization Settings'), | ||
to: `/settings/${organization.slug}/`, | ||
hidden: !hasOrgRead, | ||
}, | ||
{ | ||
key: 'members', | ||
label: t('Members'), | ||
to: `/settings/${organization.slug}/members/`, | ||
hidden: !hasMemberRead, | ||
}, | ||
{ | ||
key: 'teams', | ||
label: t('Teams'), | ||
to: `/settings/${organization.slug}/teams/`, | ||
hidden: !hasTeamRead, | ||
}, | ||
{ | ||
key: 'billing', | ||
label: t('Usage & Billing'), | ||
to: `/settings/${organization.slug}/billing/`, | ||
hidden: !hasBillingAccess, | ||
}, | ||
{ | ||
key: 'switch-organization', | ||
label: t('Switch Organization'), | ||
isSubmenu: true, | ||
disabled: !organizations?.length, | ||
hidden: config.singleOrganization || isDemoModeEnabled(), | ||
children: [ | ||
...orderBy(organizations, ['status.id', 'name']).map(switchOrg => ({ | ||
key: switchOrg.id, | ||
label: <OrganizationBadge organization={switchOrg} />, | ||
textValue: switchOrg.name, | ||
to: resolveRoute( | ||
`/organizations/${switchOrg.slug}/issues/`, | ||
organization, | ||
switchOrg | ||
), | ||
})), | ||
createOrganizationMenuItem(), | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
key: 'user', | ||
label: ( | ||
<SectionTitleWrapper> | ||
<UserBadge user={user} avatarSize={32} /> | ||
</SectionTitleWrapper> | ||
), | ||
textValue: t('User Summary'), | ||
children: [ | ||
{ | ||
key: 'user-settings', | ||
label: t('User Settings'), | ||
to: '/settings/account/', | ||
}, | ||
{ | ||
key: 'user-auth-tokens', | ||
label: t('User Auth Tokens'), | ||
to: '/settings/account/api/', | ||
}, | ||
{ | ||
key: 'admin', | ||
label: t('Admin'), | ||
to: '/manage/', | ||
hidden: !user?.isSuperuser, | ||
}, | ||
{ | ||
key: 'signout', | ||
label: t('Sign Out'), | ||
onAction: handleLogout, | ||
}, | ||
], | ||
}, | ||
]} | ||
/> | ||
); | ||
} | ||
|
||
const OrgDropdownTrigger = styled(Button)` | ||
height: 44px; | ||
width: 44px; | ||
`; | ||
|
||
const StyledOrganizationAvatar = styled(OrganizationAvatar)` | ||
border-radius: 6px; /* Fixes background bleeding on corners */ | ||
`; | ||
|
||
const SectionTitleWrapper = styled('div')` | ||
text-transform: none; | ||
font-size: ${p => p.theme.fontSizeMedium}; | ||
font-weight: ${p => p.theme.fontWeightNormal}; | ||
color: ${p => p.theme.textColor}; | ||
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.