Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/components/EnvironmentBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Small badge shown next to the logo or the top bar title on every environment except production, so it's easy to tell which one you are using.
*/
import useEnvironment from '@hooks/useEnvironment';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';

import {isInternalTestBuild} from '@libs/Environment/Environment';

import CONST from '@src/CONST';

import type {StyleProp, ViewStyle} from 'react-native';

import React from 'react';

import pkg from '../../package.json';
import Badge from './Badge';

const ENVIRONMENT_SHORT_FORM = {
[CONST.ENVIRONMENT.DEV]: 'DEV',
[CONST.ENVIRONMENT.STAGING]: 'STG',
[CONST.ENVIRONMENT.QA]: 'QA',
[CONST.ENVIRONMENT.PRODUCTION]: 'PROD',
[CONST.ENVIRONMENT.ADHOC]: 'ADHOC',
};

type EnvironmentBadgeProps = {
style?: StyleProp<ViewStyle>;
};

function EnvironmentBadge({style}: EnvironmentBadgeProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {environment, isProduction} = useEnvironment();

if (isProduction) {
return null;
}

const adhoc = environment === CONST.ENVIRONMENT.ADHOC;
const success = environment === CONST.ENVIRONMENT.STAGING;
const error = environment !== CONST.ENVIRONMENT.STAGING && environment !== CONST.ENVIRONMENT.ADHOC;

const badgeEnvironmentStyle = StyleUtils.getEnvironmentBadgeStyle(success, error, adhoc);
const text = isInternalTestBuild() ? `v${pkg.version} PR:${CONST.PULL_REQUEST_NUMBER}` : ENVIRONMENT_SHORT_FORM[environment];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Let the ad-hoc label expand to its content

On internal ad-hoc PR builds, this branch produces a long label such as v9.5.37-0 PR:106694, but the badge still receives styles.headerEnvBadge, which fixes its width at 22 px, while Badge renders the text with numberOfLines={1}. The version and PR number will therefore be truncated or overflow the small colored background, making the identifying information unreadable; use a content-sized style for internal-test builds instead of the fixed-width short-form badge.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the fixed width was there since before the badge was removed. Removed it in b08e9da, environmentBadge already has minWidth: 22 so the short labels look the same and the ad hoc one grows to fit.


return (
<Badge
success={success}
error={error}
text={text}
badgeStyles={[styles.headerEnvBadge, styles.environmentBadge, badgeEnvironmentStyle, style]}
textStyles={styles.headerEnvBadgeText}
environment={environment}
/>
);
}

export default EnvironmentBadge;
2 changes: 2 additions & 0 deletions src/components/Navigation/FlatNavigationBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AccountNavigationAvatar from '@components/Avatar/AccountNavigationAvatar';
import EnvironmentBadge from '@components/EnvironmentBadge';
import Hoverable from '@components/Hoverable';
import DebugTabView from '@components/Navigation/DebugTabView';
import getTabAccessibilityLabel from '@components/Navigation/NavigationTabBar/getTabAccessibilityLabel';
Expand Down Expand Up @@ -85,6 +86,7 @@ function FlatNavigationBar({selectedTab}: FlatNavigationBarProps) {
>
<View style={styles.flatNavigationBarHeader}>
<FlatNavLogo onPress={navigateToHome} />
<EnvironmentBadge style={styles.flatNavigationBarEnvironmentBadge} />
</View>
<View style={styles.flatNavigationBarCreateButtonContainer}>
<FlatNavCreateButton />
Expand Down
4 changes: 4 additions & 0 deletions src/components/Navigation/TopBar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EnvironmentBadge from '@components/EnvironmentBadge';
import LoadingBar from '@components/LoadingBar';
import {PressableWithoutFeedback} from '@components/Pressable';
import SearchButton from '@components/Search/SearchRouter/SearchButton';
Expand Down Expand Up @@ -28,6 +29,7 @@ import {Keyboard, View} from 'react-native';
import Animated from 'react-native-reanimated';

import AccountAvatarButton from './AccountAvatarButton';
import useShouldUseFlatNavigationBar from './FlatNavigationBar/useShouldUseFlatNavigationBar';

type TopBarProps = {
breadcrumbLabel: string;
Expand All @@ -46,6 +48,7 @@ function TopBar({breadcrumbLabel, shouldDisplaySearch = true, shouldDisplayHelpB
const {translate} = useLocalize();
const [session] = useOnyx(ONYXKEYS.SESSION, {selector: authTokenTypeSelector});
const isAnonymousUser = isAnonymousUserUtil(session);
const shouldUseFlatNavigationBar = useShouldUseFlatNavigationBar();

const isInLandscapeMode = useIsInLandscapeMode();
const {contentHeaderHeightStyle} = useContentHeaderHeight();
Expand All @@ -71,6 +74,7 @@ function TopBar({breadcrumbLabel, shouldDisplaySearch = true, shouldDisplayHelpB
>
{breadcrumbLabel}
</Text>
{!shouldUseFlatNavigationBar && <EnvironmentBadge />}
</Animated.View>
</View>
{children}
Expand Down
10 changes: 5 additions & 5 deletions src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,11 @@ const staticStyles = (theme: ThemeColors) =>
paddingLeft: variables.flatNavigationBarRowInset + variables.flatNavigationBarRowPadding,
},

// The "y" at the end of the wordmark already leaves empty space on its right, so the badge sits closer than the default badge margin
flatNavigationBarEnvironmentBadge: {
marginLeft: 6,
},

flatNavigationBarCreateButtonContainer: {
marginHorizontal: variables.flatNavigationBarCreateButtonInset,
marginBottom: variables.flatNavigationBarCreateButtonGap,
Expand Down Expand Up @@ -5197,15 +5202,10 @@ const staticStyles = (theme: ThemeColors) =>
},

headerEnvBadge: {
position: 'absolute',
bottom: -8,
left: -8,
height: 12,
width: 22,
paddingLeft: 4,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddingLeft and paddingRight of 4 never show. environmentBadge is applied after this and sets paddingHorizontal: 7. Can we drop this then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is the other way around, in RN (and react-native-web) the longhand paddingLeft/paddingRight win over paddingHorizontal no matter the order, so the 4 is what shows and the paddingHorizontal: 7 in environmentBadge is the one that never applies. The DEV badge renders 22px wide (its minWidth), with 7 it would be wider than that.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. My bad

paddingRight: 4,
alignItems: 'center',
zIndex: -1,
},

headerEnvBadgeText: {
Expand Down
13 changes: 13 additions & 0 deletions src/styles/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,19 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({
return theme.icon;
},

getEnvironmentBadgeStyle: (isSuccess: boolean, isError: boolean, isAdhoc: boolean): ViewStyle => {
if (isAdhoc) {
return styles.badgeAdHocSuccess;
}
if (isSuccess) {
return styles.badgeEnvironmentSuccess;
}
if (isError) {
return styles.badgeEnvironmentDanger;
}
return {};
},

/**
* Generate a style for the background color of the button, based on its current state.
*
Expand Down
Loading