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
1 change: 1 addition & 0 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10463,6 +10463,7 @@ const CONST = {
CONTROL_GROUP_CURRENCY: 'Insights-ControlGroupCurrency',
CONTROL_GROUP_BY: 'Insights-ControlGroupBy',
CONTROL_COMPARE: 'Insights-ControlCompare',
AI_EXPLAIN: 'Insights-AIExplain',
DASHBOARD_TAB: 'Insights-DashboardTab',
},
EXPENSE_RULES: {
Expand Down
10 changes: 7 additions & 3 deletions src/components/Search/SearchRouter/useAskConcierge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {createTaskFromMarkdown} from '@userActions/Task';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {SidePanelContext} from '@src/types/onyx';
import type {FileObject} from '@src/types/utils/Attachment';

import {getReportParentReportID} from '@selectors/Report';

/**
* Returns a callback that opens the side panel (or Concierge chat on native)
* and sends the provided search query as a message.
Expand All @@ -28,9 +31,9 @@ function useAskConcierge({forceConcierge = false}: {forceConcierge?: boolean} =
const sidePanelReportID = useSidePanelReportID();
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
const {openConciergeAnywhere, isInSidePanel} = useOpenConciergeAnywhere();
const [sidePanelReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(sidePanelReportID)}`);
const [sidePanelParentReportID] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(sidePanelReportID)}`, {selector: getReportParentReportID});

const isSidePanelOnConciergeThread = !!conciergeReportID && sidePanelReport?.parentReportID === conciergeReportID;
const isSidePanelOnConciergeThread = !!conciergeReportID && sidePanelParentReportID === conciergeReportID;
const targetReportID = !forceConcierge && isInSidePanel && sidePanelReportID && !isSidePanelOnConciergeThread ? sidePanelReportID : conciergeReportID;
const [targetReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(targetReportID)}`);
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
Expand All @@ -42,7 +45,7 @@ function useAskConcierge({forceConcierge = false}: {forceConcierge?: boolean} =

const shouldRespondInThread = targetReportID === conciergeReportID;

const askConcierge = (searchQuery: string) => {
const askConcierge = (searchQuery: string, sidePanelContext?: SidePanelContext) => {
const trimmedQuery = searchQuery.trim();
if (!trimmedQuery || !shouldShowAskConcierge) {
return;
Expand All @@ -65,6 +68,7 @@ function useAskConcierge({forceConcierge = false}: {forceConcierge?: boolean} =
currentUserAccountID,
shouldPlaySound: true,
isInSidePanel,
sidePanelContext,
delegateAccountID,
conciergeReportID,
conciergeThreadReportID,
Expand Down
2 changes: 2 additions & 0 deletions src/languages/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'Compliance',
runningTotal: 'Laufende Summe',
explain: 'Erklären',
explainChartPrompt: (chartTitle: string) => `Erklären Sie die Daten in ${chartTitle}.`,
},
allSettingsScreen: {
subscription: 'Abonnement',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/el.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'Συμμόρφωση',
runningTotal: 'Τρέχον σύνολο',
explain: 'Επεξήγηση',
explainChartPrompt: (chartTitle: string) => `Εξηγήστε τα δεδομένα στο ${chartTitle}.`,
},
allSettingsScreen: {
subscription: 'Συνδρομή',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,8 @@ const translations = {
},
insightsPage: {
viewOnSpend: 'View on Spend',
explain: 'Explain',
explainChartPrompt: (chartTitle: string) => `Explain the data in ${chartTitle}.`,
compliance: 'Compliance',
runningTotal: 'Running total',
compare: {
Expand Down
2 changes: 2 additions & 0 deletions src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'Cumplimiento',
runningTotal: 'Total acumulado',
explain: 'Explicar',
explainChartPrompt: (chartTitle: string) => `Explica los datos en ${chartTitle}.`,
},
allSettingsScreen: {
subscription: 'Suscripcion',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'Conformité',
runningTotal: 'Total cumulé',
explain: 'Expliquer',
explainChartPrompt: (chartTitle: string) => `Expliquez les données dans ${chartTitle}.`,
},
allSettingsScreen: {
subscription: 'Abonnement',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'Conformità',
runningTotal: 'Totale progressivo',
explain: 'Spiega',
explainChartPrompt: (chartTitle: string) => `Spiega i dati in ${chartTitle}.`,
},
allSettingsScreen: {
subscription: 'Abbonamento',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'コンプライアンス',
runningTotal: '累計合計',
explain: '説明',
explainChartPrompt: (chartTitle: string) => `${chartTitle} のデータを説明します。`,
},
allSettingsScreen: {
subscription: 'サブスクリプション',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'Naleving',
runningTotal: 'Doorlopend totaal',
explain: 'Uitleg',
explainChartPrompt: (chartTitle: string) => `Leg de gegevens in ${chartTitle} uit.`,
},
allSettingsScreen: {
subscription: 'Abonnement',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/pl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'Zgodność',
runningTotal: 'Suma narastająco',
explain: 'Wyjaśnij',
explainChartPrompt: (chartTitle: string) => `Wyjaśnij dane w ${chartTitle}.`,
},
allSettingsScreen: {
subscription: 'Subskrypcja',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/pt-BR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: 'Conformidade',
runningTotal: 'Total acumulado',
explain: 'Explicar',
explainChartPrompt: (chartTitle: string) => `Explique os dados em ${chartTitle}.`,
},
allSettingsScreen: {
subscription: 'Assinatura',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/zh-hans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,8 @@ const translations: TranslationDeepObject<typeof en> = {
},
compliance: '合规',
runningTotal: '累计总额',
explain: '解释',
explainChartPrompt: (chartTitle: string) => `解释「${chartTitle}」中的数据。`,
},
allSettingsScreen: {
subscription: '订阅',
Expand Down
13 changes: 12 additions & 1 deletion src/pages/Insights/InsightsDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import variables from '@styles/variables';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {AIInsight, InsightsDashboard as InsightsDashboardRecord, InsightsDashboardID} from '@src/types/onyx';
import type {AIInsight, InsightsDashboard as InsightsDashboardRecord, InsightsDashboardID, InsightsScope} from '@src/types/onyx';
import viewRef from '@src/types/utils/viewRef';

import type {OnyxCollection} from 'react-native-onyx';
Expand Down Expand Up @@ -61,6 +61,7 @@ import useInsightsFilters from './useInsightsFilters';
type InsightsDashboardContentProps = {
state: InsightsDashboardState;
aiInsights: AIInsight[] | undefined;
insightsScope: InsightsScope;
isLoadingAIInsights: boolean;
headlineChart: InsightsDashboardChart;

Expand All @@ -86,6 +87,7 @@ type InsightsDashboardContentProps = {
function InsightsDashboardContent({
state,
aiInsights,
insightsScope,
isLoadingAIInsights,
headlineChart,
supportingCharts,
Expand Down Expand Up @@ -222,6 +224,7 @@ function InsightsDashboardContent({
{isBetaEnabled(CONST.BETAS.INSIGHTS_AI) && (
<InsightsAICardRow
insights={aiInsights}
insightsScope={insightsScope}
isLoading={isLoadingAIInsights}
/>
)}
Expand All @@ -234,6 +237,7 @@ function InsightsDashboardContent({
averageTotal={headlineChart.averageTotal}
isDashboardLoaded={headlineChart.isDashboardLoaded}
filters={filters}
insightsScope={insightsScope}
onRetry={onRetry}
onGroupByChange={onGroupByChange}
/>
Expand All @@ -255,6 +259,7 @@ function InsightsDashboardContent({
averageTotal={averageTotal}
isDashboardLoaded={isDashboardLoaded}
filters={filters}
insightsScope={insightsScope}
onRetry={onRetry}
/>
))}
Expand Down Expand Up @@ -408,6 +413,12 @@ function InsightsDashboard({dashboardID, accessibleDashboards}: {dashboardID: In
<InsightsDashboardContent
state={state}
aiInsights={dashboard?.aiInsights}
insightsScope={{
searchKey: INSIGHTS_DASHBOARD_SPECS[dashboardID].searchKey,
policyIDs: filters.policyIDs,
date: filters.date,
groupCurrency: filters.groupCurrency,
}}
// Offline no response is coming, so the row hides instead of loading forever
isLoadingAIInsights={!hasDashboardResponse(dashboard) && !isOffline}
headlineChart={headlineChart}
Expand Down
16 changes: 12 additions & 4 deletions src/pages/Insights/ai/InsightsAICard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@ import Text from '@components/Text';

import useThemeStyles from '@hooks/useThemeStyles';

import type {AIInsight} from '@src/types/onyx';
import type {AIInsight, InsightsScope} from '@src/types/onyx';

import React from 'react';
import {View} from 'react-native';

import InsightsExplainButton from './InsightsExplainButton';

type InsightsAICardProps = {
insight: AIInsight;
insightsScope: InsightsScope;
};

function InsightsAICard({insight}: InsightsAICardProps) {
function InsightsAICard({insight, insightsScope}: InsightsAICardProps) {
const styles = useThemeStyles();

return (
<View style={styles.pl3}>
<View style={[styles.gap3, styles.justifyContentBetween]}>
<Text
variant="h2"
style={styles.insightsAICardText}
style={[styles.ml3, styles.insightsAICardText]}
>
{insight.text}
</Text>
<InsightsExplainButton
explainPrompt={insight.explainPrompt}
insightsScope={insightsScope}
isAccented
/>
</View>
);
}
Expand Down
17 changes: 14 additions & 3 deletions src/pages/Insights/ai/InsightsAICardRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useThemeStyles from '@hooks/useThemeStyles';

import {hasHoverSupport} from '@libs/DeviceCapabilities';

import type {AIInsight} from '@src/types/onyx';
import type {AIInsight, InsightsScope} from '@src/types/onyx';

import type {ReactNode} from 'react';

Expand All @@ -21,12 +21,15 @@ const SKELETON_CARD_COUNT = 4;
type InsightsAICardRowProps = {
insights: AIInsight[] | undefined;

/** Dashboard filters the insights were written for, sent to Concierge when the user asks to explain. */
insightsScope: InsightsScope;

/** Whether the dashboard has no response for the current filters yet */
isLoading: boolean;
};

/** Shows the AI insights in a sideways-scrolling row, with placeholders while the dashboard loads. */
function InsightsAICardRow({insights, isLoading}: InsightsAICardRowProps) {
function InsightsAICardRow({insights, insightsScope, isLoading}: InsightsAICardRowProps) {
const styles = useThemeStyles();
const {shouldUseNarrowLayout} = useResponsiveLayout();
const {pageGutter, pageGutterEdgeToEdge} = useLayoutSpacing();
Expand All @@ -51,7 +54,15 @@ function InsightsAICardRow({insights, isLoading}: InsightsAICardRowProps) {
>
{isLoading
? Array.from({length: SKELETON_CARD_COUNT}, (item, index) => renderWithDivider(<InsightsAICardSkeleton />, index))
: insights?.map((insight, index) => renderWithDivider(<InsightsAICard insight={insight} />, index))}
: insights?.map((insight, index) =>
renderWithDivider(
<InsightsAICard
insight={insight}
insightsScope={insightsScope}
/>,
index,
),
)}
</ScrollView>
);
}
Expand Down
19 changes: 18 additions & 1 deletion src/pages/Insights/ai/InsightsAICardSkeleton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ const LINE_HEIGHT = variables.lineHeightSizeH2;
const BAR_HEIGHT = 16;
const BAR_OFFSET_Y = (LINE_HEIGHT - BAR_HEIGHT) / 2;

const BUTTON_HEIGHT = variables.componentSizeSmall;
const BUTTON_BAR_WIDTH = 48;
const BUTTON_BAR_HEIGHT = 8;

/** Stands in for an AI card's narration while the dashboard loads. */
function InsightsAICardSkeleton() {
const styles = useThemeStyles();
const theme = useTheme();

return (
<View style={styles.pl3}>
<View style={[styles.pl3, styles.gap6]}>
<SkeletonViewContentLoader
animate
width={variables.insightsAICardTextWidth}
Expand All @@ -38,6 +42,19 @@ function InsightsAICardSkeleton() {
height={BAR_HEIGHT}
/>
</SkeletonViewContentLoader>
<SkeletonViewContentLoader
animate
width={BUTTON_BAR_WIDTH}
height={BUTTON_HEIGHT}
backgroundColor={theme.skeletonLHNIn}
foregroundColor={theme.skeletonLHNOut}
>
<SkeletonRect
transform={[{translateY: (BUTTON_HEIGHT - BUTTON_BAR_HEIGHT) / 2}]}
width={BUTTON_BAR_WIDTH}
height={BUTTON_BAR_HEIGHT}
/>
</SkeletonViewContentLoader>
</View>
);
}
Expand Down
58 changes: 58 additions & 0 deletions src/pages/Insights/ai/InsightsExplainButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Button from '@components/Button';
import useAskConcierge from '@components/Search/SearchRouter/useAskConcierge';

import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';

import CONST from '@src/CONST';
import type {InsightsScope} from '@src/types/onyx';

import React from 'react';

type InsightsExplainButtonProps = {
Comment thread
mhawryluk marked this conversation as resolved.
Resolved
/** Message sent to Concierge on press */
explainPrompt: string;

/** Dashboard and filters sent with the prompt so Concierge explains the data the user sees */
insightsScope: InsightsScope;

/** Tints the glyph and label with the link color instead of the muted one */
isAccented?: boolean;
};

/** Asks Concierge to explain an AI insight or chart, sending along the dashboard filters so the answer covers the data on screen. */
function InsightsExplainButton({explainPrompt, insightsScope, isAccented = false}: InsightsExplainButtonProps) {
const styles = useThemeStyles();
const theme = useTheme();
const {translate} = useLocalize();
const icons = useMemoizedLazyExpensifyIcons(['Sparkles']);
const {askConcierge, shouldShowAskConcierge} = useAskConcierge({forceConcierge: true});
Comment thread
mhawryluk marked this conversation as resolved.
Resolved

if (!shouldShowAskConcierge) {
return null;
}

const glyphColor = isAccented ? theme.link : theme.icon;

return (
<Button
size={CONST.BUTTON_SIZE.SMALL}
onPress={() => askConcierge(explainPrompt, {insightsScope})}
sentryLabel={CONST.SENTRY_LABEL.INSIGHTS.AI_EXPLAIN}
style={styles.alignSelfStart}
innerStyles={styles.bgTransparent}
hoverStyles={styles.hoveredComponentBG}
>
<Button.Icon
src={icons.Sparkles}
fill={glyphColor}
hoverFill={glyphColor}
/>
<Button.Text style={[styles.textMicroBold, isAccented ? styles.link : styles.textSupporting]}>{translate('insightsPage.explain')}</Button.Text>
</Button>
);
}

export default InsightsExplainButton;
Loading
Loading