From d1090c328ce2ad58e889fbb85f87110a1149c0b0 Mon Sep 17 00:00:00 2001 From: mhawryluk Date: Thu, 26 Nov 2026 17:41:58 +0100 Subject: [PATCH 01/36] [Insights polish] Details RHP: Biggest changes --- src/CONST/SENTRY_LABEL.ts | 4 + src/CONST/index.ts | 2 + src/ONYXKEYS.ts | 4 + src/languages/en.ts | 4 + src/libs/actions/Insights.ts | 33 ++++- .../controls/InsightsGroupByPopup.tsx | 9 +- src/pages/Insights/dashboardSpecs.ts | 2 +- .../details/InsightsBiggestChangeRow.tsx | 135 ++++++++++++++++++ .../details/InsightsBiggestChanges.tsx | 77 ++++++++++ .../details/InsightsBiggestChangesControl.tsx | 62 ++++++++ .../InsightsBiggestChangesSkeleton.tsx | 82 +++++++++++ .../Insights/details/InsightsDetailsPage.tsx | 56 +++++++- .../details/InsightsDetailsSectionHeader.tsx | 10 +- .../details/buildInsightsDetailsQuery.ts | 8 +- .../getBiggestChangesGroupByOptions.ts | 14 ++ .../details/useRequestInsightsDetails.ts | 35 ++++- src/styles/variables.ts | 2 + src/types/onyx/InsightsDetails.ts | 18 ++- src/types/onyx/index.ts | 4 +- tests/unit/buildInsightsDetailsQueryTest.ts | 48 +++++++ .../getBiggestChangesGroupByOptionsTest.ts | 41 ++++++ 21 files changed, 635 insertions(+), 15 deletions(-) create mode 100644 src/pages/Insights/details/InsightsBiggestChangeRow.tsx create mode 100644 src/pages/Insights/details/InsightsBiggestChanges.tsx create mode 100644 src/pages/Insights/details/InsightsBiggestChangesControl.tsx create mode 100644 src/pages/Insights/details/InsightsBiggestChangesSkeleton.tsx create mode 100644 src/pages/Insights/details/getBiggestChangesGroupByOptions.ts create mode 100644 tests/unit/getBiggestChangesGroupByOptionsTest.ts diff --git a/src/CONST/SENTRY_LABEL.ts b/src/CONST/SENTRY_LABEL.ts index 8d08b800782b..1e1bbd4e4698 100644 --- a/src/CONST/SENTRY_LABEL.ts +++ b/src/CONST/SENTRY_LABEL.ts @@ -938,6 +938,7 @@ const SENTRY_LABEL = { WORKSPACE: 'Insights-ControlPopupApply-Workspace', GROUP_BY: 'Insights-ControlPopupApply-GroupBy', TAG_LEVEL: 'Insights-ControlPopupApply-TagLevel', + BIGGEST_CHANGES: 'Insights-ControlPopupApply-BiggestChanges', GROUP_CURRENCY: 'Insights-ControlPopupApply-GroupCurrency', COMPARE: 'Insights-ControlPopupApply-Compare', }, @@ -945,12 +946,15 @@ const SENTRY_LABEL = { WORKSPACE: 'Insights-ControlPopupReset-Workspace', GROUP_BY: 'Insights-ControlPopupReset-GroupBy', TAG_LEVEL: 'Insights-ControlPopupReset-TagLevel', + BIGGEST_CHANGES: 'Insights-ControlPopupReset-BiggestChanges', GROUP_CURRENCY: 'Insights-ControlPopupReset-GroupCurrency', COMPARE: 'Insights-ControlPopupReset-Compare', }, DETAILS_TOP_EXPENSE: 'Insights-DetailsTopExpense', DETAILS_TOP_EXPENSES_VIEW_ALL: 'Insights-DetailsTopExpensesViewAll', DETAILS_RECENT_HISTORY_VIEW_ALL: 'Insights-DetailsRecentHistoryViewAll', + DETAILS_BIGGEST_CHANGES_GROUP_BY: 'Insights-DetailsBiggestChangesGroupBy', + DETAILS_BIGGEST_CHANGE: 'Insights-DetailsBiggestChange', FUTURE_EXPENSES_VIEW: 'Insights-FutureExpensesView', }, EXPENSE_RULES: { diff --git a/src/CONST/index.ts b/src/CONST/index.ts index 0a6225e2dfb8..c5a41a21f8eb 100644 --- a/src/CONST/index.ts +++ b/src/CONST/index.ts @@ -10873,8 +10873,10 @@ const CONST = { DETAILS_SECTION: { SUMMARY: 'summary', RECENT_HISTORY: 'recentHistory', + BIGGEST_CHANGES: 'biggestChanges', TOP_EXPENSES: 'topExpenses', }, + BIGGEST_CHANGES_LIMIT: 5, }, SECTION_LIST_ITEM_TYPE: { diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 88b7afa0b7a9..79fc0317ad0b 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -247,6 +247,9 @@ const ONYXKEYS = { /** Contains the user preference for the active inbox tab filter */ NVP_INBOX_TAB: 'nvp_inboxTab', + /** What the Insights Details panel's Biggest changes section groups by */ + NVP_INSIGHTS_BIGGEST_CHANGES_GROUP_BY: 'nvp_insightsBiggestChangesGroupBy', + /** Contains the users's block expiration (if they have one) */ NVP_BLOCKED_FROM_CONCIERGE: 'nvp_private_blockedFromConcierge', @@ -1904,6 +1907,7 @@ type OnyxValuesMapping = { [ONYXKEYS.NVP_MUTED_PLATFORMS]: Partial>; [ONYXKEYS.NVP_PRIORITY_MODE]: ValueOf; [ONYXKEYS.NVP_INBOX_TAB]: ValueOf; + [ONYXKEYS.NVP_INSIGHTS_BIGGEST_CHANGES_GROUP_BY]: OnyxTypes.InsightsBiggestChangesGroupBy; [ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE]: OnyxTypes.BlockedFromConcierge; [ONYXKEYS.QUEUE_FLUSHED_DATA]: AnyOnyxUpdate[]; [ONYXKEYS.TRANSACTIONS_PENDING_3DS_REVIEW]: OnyxTypes.TransactionsPending3DSReview; diff --git a/src/languages/en.ts b/src/languages/en.ts index e618c28e9cbd..de671a041277 100644 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -252,6 +252,7 @@ const translations = { settings: 'Settings', termsOfService: 'Terms of Service', members: 'Members', + merchants: 'Merchants', invite: 'Invite', here: 'here', avatar: 'Avatar', @@ -1445,6 +1446,9 @@ const translations = { monthAverageFor: (count: number, item: string) => `${count}-month average for ${item}`, quarterAverageFor: (count: number, item: string) => `${count}-quarter average for ${item}`, }, + biggestChanges: { + title: 'Biggest changes', + }, compliance: 'Compliance', violationsOverTime: 'Violations over time', topViolations: 'Top violations', diff --git a/src/libs/actions/Insights.ts b/src/libs/actions/Insights.ts index bd170cabb24c..11c9436eb251 100644 --- a/src/libs/actions/Insights.ts +++ b/src/libs/actions/Insights.ts @@ -8,7 +8,7 @@ import Log from '@libs/Log'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {OnyxKey} from '@src/ONYXKEYS'; -import type {InsightsDashboardID, InsightsSearchKey} from '@src/types/onyx'; +import type {InsightsBiggestChangesGroupBy, InsightsDashboardID, InsightsSearchKey} from '@src/types/onyx'; import type {OnyxData} from '@src/types/onyx/Request'; import type {SearchDataTypes} from '@src/types/onyx/SearchResults'; @@ -125,8 +125,37 @@ function getInsightsDetails(hash: number, jsonQuery: string, topExpensesSnapshot requestInsights(READ_COMMANDS.GET_INSIGHTS_DETAILS, jsonQuery, key, {optimisticData, successData, failureData}); } +/** Fetches the Details panel's Biggest changes for another grouping, which the backend also saves as the user's choice */ +function getInsightsBiggestChanges(hash: number, jsonQuery: string) { + const key = `${ONYXKEYS.COLLECTION.INSIGHTS_DETAILS}${hash}` as const; + const optimisticData: Array> = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key, + value: {errors: null, responseJsonCode: null}, + }, + ]; + const failureData: Array> = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key, + value: { + responseJsonCode: CONST.JSON_CODE.NO_RESPONSE, + errors: getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'), + }, + }, + ]; + + requestInsights(READ_COMMANDS.GET_INSIGHTS_DETAILS, jsonQuery, key, {optimisticData, failureData}); +} + +/** Saves what Biggest changes groups by, which the next Details request fetches and sends to the backend */ +function setInsightsBiggestChangesGroupBy(groupBy: InsightsBiggestChangesGroupBy) { + Onyx.set(ONYXKEYS.NVP_INSIGHTS_BIGGEST_CHANGES_GROUP_BY, groupBy); +} + function setInsightsFilters(searchKey: InsightsSearchKey, query: SearchQueryString) { Onyx.merge(ONYXKEYS.SEARCH_FILTERS, {[searchKey]: {query}}); } -export {getInsights, getInsightsDetails, setInsightsFilters}; +export {getInsights, getInsightsBiggestChanges, getInsightsDetails, setInsightsBiggestChangesGroupBy, setInsightsFilters}; diff --git a/src/pages/Insights/controls/InsightsGroupByPopup.tsx b/src/pages/Insights/controls/InsightsGroupByPopup.tsx index bfe630b0c2fe..ef99e9de3ac9 100644 --- a/src/pages/Insights/controls/InsightsGroupByPopup.tsx +++ b/src/pages/Insights/controls/InsightsGroupByPopup.tsx @@ -8,11 +8,16 @@ import type {ListItem} from '@components/SelectionList/types'; import useThemeStyles from '@hooks/useThemeStyles'; +import variables from '@styles/variables'; + import type {SentryLabel} from '@src/types/utils/SentryLabel'; import React, {useState} from 'react'; import {View} from 'react-native'; +/** Height of the line between option groups, with its vertical margins */ +const DIVIDER_HEIGHT = 25; + type InsightsGroupByPopupProps = { optionGroups: Array>; selectedValue: T; @@ -62,7 +67,9 @@ function InsightsGroupByPopup({ > count + options.length, optionGroups.length - 1)} + itemCount={optionGroups.reduce((count, options) => count + options.length, 0)} + itemHeight={variables.optionRowHeight} + extraHeight={(optionGroups.length - 1) * DIVIDER_HEIGHT} hasHeader > ; + +type InsightsBiggestChangeRowProps = { + change: InsightsDetailsBiggestChange; + groupBy: InsightsBiggestChangesGroupBy; + currency: string; + asOfDate: string | undefined; +}; + +function InsightsBiggestChangeRow({change, groupBy, currency, asOfDate}: InsightsBiggestChangeRowProps) { + const styles = useThemeStyles(); + const theme = useTheme(); + const StyleUtils = useStyleUtils(); + const {translate, preferredLocale, dateFnsLocale} = useLocalize(); + const {convertToDisplayString} = useCurrencyListActions(); + const icons = useMemoizedLazyExpensifyIcons(['ArrowUpLong', 'ArrowDownLong', 'Folder', 'Basket']); + const accountID = 'accountID' in change ? change.accountID : undefined; + const [member] = usePersonalDetail(accountID); + const itemKey = getRankingItemKey(groupBy, change); + const title = itemKey === undefined ? undefined : resolveInsightsDetailsItem({groupBy, itemKey, member, dateFnsLocale, translate})?.title; + + const difference = change.total - change.average; + const isIncrease = difference > 0; + const changeColor = StyleUtils.getSpendChangeColor(isIncrease); + const amount = convertToDisplayString(change.total, currency); + const differenceAmount = convertToDisplayString(Math.abs(difference), currency); + const percentage = + change.average === 0 + ? CONST.INSIGHTS.NO_CHANGE_PLACEHOLDER + : format(preferredLocale, difference / Math.abs(change.average), {style: 'percent', maximumFractionDigits: 1, signDisplay: 'exceptZero'}); + + const openItem = () => { + if (itemKey === undefined) { + return; + } + Navigation.navigate(ROUTES.INSIGHTS_DETAILS.getRoute({dashboardID: CONST.INSIGHTS.DASHBOARD.SPEND, graphKey: ITEM_GRAPH_KEYS[groupBy], itemKey, asOfDate})); + }; + + return ( + + + {accountID === undefined ? ( + + ) : ( + + )} + + + {title} + + {amount} + + + + + {difference !== 0 && ( + + )} + {differenceAmount} + + + {percentage} + + + + ); +} + +export default InsightsBiggestChangeRow; diff --git a/src/pages/Insights/details/InsightsBiggestChanges.tsx b/src/pages/Insights/details/InsightsBiggestChanges.tsx new file mode 100644 index 000000000000..a8fcdf40ffd1 --- /dev/null +++ b/src/pages/Insights/details/InsightsBiggestChanges.tsx @@ -0,0 +1,77 @@ +/** Biggest changes section of the Details panel, listing the items whose spend in the period moved furthest from their own average. */ +import ChartOfflineState from '@components/Search/ChartOfflineState'; + +import useLocalize from '@hooks/useLocalize'; +import useThemeStyles from '@hooks/useThemeStyles'; + +import CONST from '@src/CONST'; +import type {InsightsBiggestChangesGroupBy, InsightsDetailsBiggestChange} from '@src/types/onyx'; + +import React from 'react'; +import {View} from 'react-native'; + +import type {InsightsDetailsSectionState} from './insightsDetailsSectionState'; + +import InsightsBiggestChangeRow from './InsightsBiggestChangeRow'; +import InsightsBiggestChangesControl from './InsightsBiggestChangesControl'; +import InsightsBiggestChangesSkeleton from './InsightsBiggestChangesSkeleton'; +import {getRankingItemKey} from './insightsDetailsItem'; +import InsightsDetailsSectionHeader from './InsightsDetailsSectionHeader'; +import {INSIGHTS_DETAILS_SECTION_STATE} from './insightsDetailsSectionState'; + +type InsightsBiggestChangesProps = { + /** Items of the picked grouping, largest change first */ + changes: InsightsDetailsBiggestChange[] | undefined; + + groupBy: InsightsBiggestChangesGroupBy; + + /** Groupings the user can pick, without Members for people who can't see Top spenders */ + groupByOptions: InsightsBiggestChangesGroupBy[]; + + onGroupByChange: (groupBy: InsightsBiggestChangesGroupBy) => void; + state: InsightsDetailsSectionState; + currency: string; + + /** Last day the dashboard counted expenses through, passed on to the panels the rows open */ + asOfDate: string | undefined; +}; + +function InsightsBiggestChanges({changes, groupBy, groupByOptions, onGroupByChange, state, currency, asOfDate}: InsightsBiggestChangesProps) { + const styles = useThemeStyles(); + const {translate} = useLocalize(); + const rows = changes?.slice(0, CONST.INSIGHTS.BIGGEST_CHANGES_LIMIT) ?? []; + + if (state === INSIGHTS_DETAILS_SECTION_STATE.READY && rows.length === 0) { + return null; + } + + return ( + + + + + {state === INSIGHTS_DETAILS_SECTION_STATE.OFFLINE && } + {state === INSIGHTS_DETAILS_SECTION_STATE.LOADING && } + {state === INSIGHTS_DETAILS_SECTION_STATE.READY && + rows.map((change, index) => ( + + + + ))} + + ); +} + +export default InsightsBiggestChanges; diff --git a/src/pages/Insights/details/InsightsBiggestChangesControl.tsx b/src/pages/Insights/details/InsightsBiggestChangesControl.tsx new file mode 100644 index 000000000000..6a3757232c17 --- /dev/null +++ b/src/pages/Insights/details/InsightsBiggestChangesControl.tsx @@ -0,0 +1,62 @@ +/** Dropdown picking whether Biggest changes lists categories, merchants or members. */ +import DropdownButton from '@components/Search/FilterDropdowns/DropdownButton'; +import type {PopoverComponentProps} from '@components/Search/FilterDropdowns/FilterPopupButton'; + +import useLocalize from '@hooks/useLocalize'; +import useThemeStyles from '@hooks/useThemeStyles'; + +import INSIGHTS_CONTROL_ANCHOR_ALIGNMENT from '@pages/Insights/controls/insightsControls'; +import InsightsGroupByPopup from '@pages/Insights/controls/InsightsGroupByPopup'; + +import variables from '@styles/variables'; + +import CONST from '@src/CONST'; +import type {TranslationPaths} from '@src/languages/types'; +import type {InsightsBiggestChangesGroupBy} from '@src/types/onyx'; + +import React from 'react'; + +const GROUP_BY_LABEL_KEYS = { + [CONST.SEARCH.GROUP_BY.CATEGORY]: 'workspace.common.categories', + [CONST.SEARCH.GROUP_BY.MERCHANT]: 'common.merchants', + [CONST.SEARCH.GROUP_BY.FROM]: 'common.members', +} as const satisfies Record; + +type InsightsBiggestChangesControlProps = { + groupBy: InsightsBiggestChangesGroupBy; + options: InsightsBiggestChangesGroupBy[]; + onChange: (groupBy: InsightsBiggestChangesGroupBy) => void; +}; + +function InsightsBiggestChangesControl({groupBy, options, onChange}: InsightsBiggestChangesControlProps) { + const styles = useThemeStyles(); + const {translate} = useLocalize(); + const popover = ({closeOverlay}: PopoverComponentProps) => ( + ({value, label: translate(GROUP_BY_LABEL_KEYS[value])}))]} + selectedValue={groupBy} + defaultValue={CONST.SEARCH.GROUP_BY.CATEGORY} + label={translate('insightsPage.biggestChanges.title')} + resetSentryLabel={CONST.SENTRY_LABEL.INSIGHTS.CONTROL_POPUP_RESET.BIGGEST_CHANGES} + applySentryLabel={CONST.SENTRY_LABEL.INSIGHTS.CONTROL_POPUP_APPLY.BIGGEST_CHANGES} + closeOverlay={closeOverlay} + onChange={onChange} + /> + ); + + return ( + + ); +} + +export default InsightsBiggestChangesControl; diff --git a/src/pages/Insights/details/InsightsBiggestChangesSkeleton.tsx b/src/pages/Insights/details/InsightsBiggestChangesSkeleton.tsx new file mode 100644 index 000000000000..fcdd26793bcf --- /dev/null +++ b/src/pages/Insights/details/InsightsBiggestChangesSkeleton.tsx @@ -0,0 +1,82 @@ +/** Loading skeleton of the Biggest changes section's rows */ +import SkeletonRect from '@components/SkeletonRect'; +import SkeletonTextLine from '@components/Skeletons/SkeletonTextLine'; +import SkeletonViewContentLoader from '@components/SkeletonViewContentLoader'; + +import useStyleUtils from '@hooks/useStyleUtils'; +import useTheme from '@hooks/useTheme'; +import useThemeStyles from '@hooks/useThemeStyles'; + +import {lineHeightScale} from '@styles/typography'; +import variables from '@styles/variables'; + +import CONST from '@src/CONST'; + +import React from 'react'; +import {View} from 'react-native'; + +const NAME_WIDTH = 120; +const AMOUNT_WIDTH = 80; +const CHANGE_WIDTH = 72; +const PERCENTAGE_WIDTH = 40; + +function InsightsBiggestChangesSkeleton() { + const styles = useThemeStyles(); + const theme = useTheme(); + const StyleUtils = useStyleUtils(); + + return Array.from({length: CONST.INSIGHTS.BIGGEST_CHANGES_LIMIT}, (_, index) => ( + + + + + + + + + + + + + + + + + + + + )); +} + +export default InsightsBiggestChangesSkeleton; diff --git a/src/pages/Insights/details/InsightsDetailsPage.tsx b/src/pages/Insights/details/InsightsDetailsPage.tsx index a714dc0a5397..269e17801c67 100644 --- a/src/pages/Insights/details/InsightsDetailsPage.tsx +++ b/src/pages/Insights/details/InsightsDetailsPage.tsx @@ -6,17 +6,20 @@ import ScrollView from '@components/ScrollView'; import {buildChartDrillDownQuery, buildViewOnSpendQuery} from '@components/Search/chartDrillDown'; import Text from '@components/Text'; +import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; import {usePersonalDetail} from '@hooks/usePersonalDetails'; import useThemeStyles from '@hooks/useThemeStyles'; +import {setInsightsBiggestChangesGroupBy} from '@libs/actions/Insights'; import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types'; import type {InsightsDetailsParamList} from '@libs/Navigation/types'; import {buildSearchQueryJSON, buildSearchQueryString} from '@libs/SearchQueryUtils'; -import INSIGHTS_DASHBOARD_SPECS, {getChartSpec} from '@pages/Insights/dashboardSpecs'; +import type {InsightsDetailsSection} from '@pages/Insights/dashboardSpecs'; +import INSIGHTS_DASHBOARD_SPECS, {getChartSpec, getPoliciesInScope} from '@pages/Insights/dashboardSpecs'; import getInsightsLastDay from '@pages/Insights/getInsightsLastDay'; import {getQueryGroupBy} from '@pages/Insights/insightsGroupByUtils'; import { @@ -32,6 +35,7 @@ import useInsightsFilters from '@pages/Insights/useInsightsFilters'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; +import type {InsightsBiggestChangesGroupBy} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import {getYear, parseISO} from 'date-fns'; @@ -39,7 +43,10 @@ import React from 'react'; import {View} from 'react-native'; import buildInsightsDetailsQuery from './buildInsightsDetailsQuery'; +import getBiggestChangesGroupByOptions from './getBiggestChangesGroupByOptions'; +import getInsightsAverageComparison, {INSIGHTS_AVERAGE_COMPARISON} from './getInsightsAverageComparison'; import getInsightsItemColor from './getInsightsItemColor'; +import InsightsBiggestChanges from './InsightsBiggestChanges'; import {resolveInsightsDetailsItem} from './insightsDetailsItem'; import InsightsDetailsSectionError from './InsightsDetailsSectionError'; import {getInsightsDetailsSectionState, INSIGHTS_DETAILS_SECTION_STATE} from './insightsDetailsSectionState'; @@ -95,11 +102,18 @@ function InsightsDetailsPage({route}: InsightsDetailsPageProps) { const drillDownQuery = queryJSONThroughToday && item ? buildChartDrillDownQuery(queryJSONThroughToday, item.filterQuery) : undefined; const topExpensesQueryJSON = chartSpec && drillDownQuery ? withInsightsSnapshotHash(buildTopExpensesQueryJSON(drillDownQuery), chartSpec.graphKey) : undefined; const topExpensesSnapshotHash = topExpensesQueryJSON?.hash; - const detailsQuery = + const [savedBiggestChangesGroupBy] = useOnyx(ONYXKEYS.NVP_INSIGHTS_BIGGEST_CHANGES_GROUP_BY); + const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); + const {login} = useCurrentUserPersonalDetails(); + const biggestChangesGroupByOptions = getBiggestChangesGroupByOptions(getPoliciesInScope(policies, filters.policyIDs), login); + const biggestChangesGroupBy = + savedBiggestChangesGroupBy && biggestChangesGroupByOptions.includes(savedBiggestChangesGroupBy) ? savedBiggestChangesGroupBy : CONST.SEARCH.GROUP_BY.CATEGORY; + const buildDetailsQuery = (sections: InsightsDetailsSection[] | undefined, groupBy: InsightsBiggestChangesGroupBy) => chartSpec && topExpensesSnapshotHash !== undefined ? buildInsightsDetailsQuery({ graphKey: chartSpec.graphKey, - sections: chartSpec.detailsSections, + sections, + biggestChangesGroupBy: groupBy, filters, itemGroupBy: chartSpec.groupBy, itemKey, @@ -108,7 +122,16 @@ function InsightsDetailsPage({route}: InsightsDetailsPageProps) { lastDay: getInsightsLastDay(asOf), }) : undefined; - const requestDetails = useRequestInsightsDetails(detailsQuery, isTopSpenders ? undefined : topExpensesQueryJSON, isCompliance); + const detailsQuery = buildDetailsQuery(chartSpec?.detailsSections, biggestChangesGroupBy); + const biggestChangesQuery = chartSpec?.detailsSections?.includes(CONST.INSIGHTS.DETAILS_SECTION.BIGGEST_CHANGES) + ? buildDetailsQuery([CONST.INSIGHTS.DETAILS_SECTION.BIGGEST_CHANGES], biggestChangesGroupBy) + : undefined; + const requestDetails = useRequestInsightsDetails( + detailsQuery, + isTopSpenders ? undefined : topExpensesQueryJSON, + isCompliance, + biggestChangesQuery && {query: biggestChangesQuery, groupBy: biggestChangesGroupBy}, + ); const {isOffline} = useNetwork(); const [details] = useOnyx(`${ONYXKEYS.COLLECTION.INSIGHTS_DETAILS}${detailsQuery?.hash}`); const viewAllQueryJSON = chartSpec ? scopeInsightsDrillDownQuery(topExpensesQueryJSON, chartSpec.graphKey, details?.eligiblePolicyIDs) : undefined; @@ -129,6 +152,20 @@ function InsightsDetailsPage({route}: InsightsDetailsPageProps) { }); const hasDetailsFailed = !!detailsQuery && (detailsState === INSIGHTS_DETAILS_SECTION_STATE.STALE || detailsState === INSIGHTS_DETAILS_SECTION_STATE.ERROR); + const biggestChanges = details?.biggestChanges?.[biggestChangesGroupBy]; + const biggestChangesState = getInsightsDetailsSectionState({ + isOffline, + isLoaded: !!biggestChanges, + hasErrors: !isEmptyObject(details?.errors), + responseJsonCode: details?.responseJsonCode, + }); + const comparison = summary ? getInsightsAverageComparison(summary) : undefined; + const shouldShowBiggestChanges = + !!detailsQuery && + !!chartSpec?.detailsSections?.includes(CONST.INSIGHTS.DETAILS_SECTION.BIGGEST_CHANGES) && + detailsQuery.periodGroupBy !== CONST.SEARCH.GROUP_BY.YEAR && + comparison?.type !== INSIGHTS_AVERAGE_COMPARISON.NOT_ENOUGH_HISTORY; + const chartTitle = chartSpec?.hasViolationStageControl ? translate( 'insightsPage.violationStage.chartTitle', @@ -198,6 +235,17 @@ function InsightsDetailsPage({route}: InsightsDetailsPageProps) { currentYear={getYear(parseISO(getInsightsLastDay(asOf)))} /> )} + {shouldShowBiggestChanges && ( + + )} {!!topExpensesQueryJSON && (!isTopSpenders || !!detailsQuery) && (