Repository navigation
Add start/stop timer buttons to the Chronos report header #86690
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
Changes from all commits
da4425a
0c8ee2d
24ed239
158cd75
bb45a41
3ecf06b
44606bc
741401a
1ee5f23
87c596c
b279689
3b3dbc9
a0e2cbf
2250a06
40f36b6
15c8204
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import React from 'react'; | ||
| import {View} from 'react-native'; | ||
| import type {OnyxEntry, OnyxKey} from 'react-native-onyx'; | ||
| import useAncestors from '@hooks/useAncestors'; | ||
| import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; | ||
| import useIsInSidePanel from '@hooks/useIsInSidePanel'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import useReportIsArchived from '@hooks/useReportIsArchived'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import {isChronosTimerRunningFromVisibleActions} from '@libs/ChronosUtils'; | ||
| import {getSortedReportActionsForDisplay} from '@libs/ReportActionsUtils'; | ||
| import {canUserPerformWriteAction, canWriteInReport} from '@libs/ReportUtils'; | ||
| import {addComment} from '@userActions/Report'; | ||
| import {callFunctionIfActionIsAllowed} from '@userActions/Session'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type * as OnyxTypes from '@src/types/onyx'; | ||
| import type {ReportActions} from '@src/types/onyx/ReportAction'; | ||
| import Button from './Button'; | ||
|
|
||
| type ChronosTimerHeaderButtonProps = { | ||
| report: OnyxTypes.Report; | ||
| }; | ||
|
|
||
| function ChronosTimerHeaderButton({report}: ChronosTimerHeaderButtonProps) { | ||
| const {translate} = useLocalize(); | ||
| const styles = useThemeStyles(); | ||
| const isReportArchived = useReportIsArchived(report?.reportID); | ||
| const canPerformWriteAction = canUserPerformWriteAction(report, isReportArchived); | ||
| const [visibleReportActionsData] = useOnyx(ONYXKEYS.DERIVED.VISIBLE_REPORT_ACTIONS); | ||
|
|
||
| const {accountID: currentUserAccountID, timezone: timezoneParam} = useCurrentUserPersonalDetails(); | ||
| const reportActionsOnyxKey = `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}` as OnyxKey; | ||
| const [isTimerRunning] = useOnyx<OnyxKey, boolean>( | ||
| reportActionsOnyxKey, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-11 (docs)The The component only needs to know whether the timer is running (a boolean). Compute the final boolean directly in the selector instead of returning the full array: const [isTimerRunning] = useOnyx<OnyxKey, boolean>(
reportActionsOnyxKey,
{
selector: (reportActions: unknown): boolean => {
const sorted = getSortedReportActionsForDisplay(
reportActions as OnyxEntry<ReportActions>,
canPerformWriteAction,
false,
visibleReportActionsData,
report.reportID,
);
return isChronosTimerRunningFromVisibleActions(sorted, currentUserAccountID);
},
},
[canPerformWriteAction, visibleReportActionsData, report.reportID, currentUserAccountID],
);This reduces Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| { | ||
| selector: (reportActions: unknown): boolean => { | ||
| const sorted = getSortedReportActionsForDisplay(reportActions as OnyxEntry<ReportActions>, canPerformWriteAction, false, visibleReportActionsData, report.reportID); | ||
| return isChronosTimerRunningFromVisibleActions(sorted, currentUserAccountID); | ||
| }, | ||
| }, | ||
| [canPerformWriteAction, visibleReportActionsData, report.reportID, currentUserAccountID], | ||
| ); | ||
|
|
||
| const ancestors = useAncestors(report); | ||
| const isInSidePanel = useIsInSidePanel(); | ||
|
|
||
| function sendCommentToChronos() { | ||
| addComment({ | ||
| report, | ||
| notifyReportID: report.reportID, | ||
| ancestors, | ||
| text: isTimerRunning ? CONST.CHRONOS.TIMER_COMMAND.STOP : CONST.CHRONOS.TIMER_COMMAND.START, | ||
| timezoneParam: timezoneParam ?? CONST.DEFAULT_TIME_ZONE, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ CONSISTENCY-2 (docs)The literal strings Define constants in // In CONST
CHRONOS: {
TIMER_COMMAND: {
START: 'start',
STOP: 'stop',
},
},Then use them in the component: text: isTimerRunning ? CONST.CHRONOS.TIMER_COMMAND.STOP : CONST.CHRONOS.TIMER_COMMAND.START,And in return getLatestUserChronosTimerCommand(...) === CONST.CHRONOS.TIMER_COMMAND.START;Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| currentUserAccountID, | ||
| shouldPlaySound: false, | ||
| isInSidePanel, | ||
| }); | ||
| } | ||
|
|
||
| if (!canWriteInReport(report)) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <View style={[styles.flexRow, styles.alignItemsCenter, styles.justifyContentEnd]}> | ||
| <Button | ||
| success={!isTimerRunning} | ||
| text={translate(isTimerRunning ? 'chronos.stopTimer' : 'chronos.startTimer')} | ||
| onPress={callFunctionIfActionIsAllowed(sendCommentToChronos)} | ||
| style={styles.flex1} | ||
| sentryLabel={CONST.SENTRY_LABEL.HEADER_VIEW.CHRONOS_TIMER_BUTTON} | ||
| /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| export default ChronosTimerHeaderButton; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import type {OnyxEntry, OnyxInputOrEntry} from 'react-native-onyx'; | ||
| import type {ValueOf} from 'type-fest'; | ||
| import CONST from '@src/CONST'; | ||
| import type ReportAction from '@src/types/onyx/ReportAction'; | ||
| import {getReportActionText} from './ReportActionsUtils'; | ||
|
|
||
| type GetReportActionTextArg = Parameters<typeof getReportActionText>[0]; | ||
|
|
||
| /** | ||
| * Next visible report action after `actionIndex` in a newest-first list (same behavior as `findPreviousAction` in ReportActionsUtils). | ||
| */ | ||
| function findPreviousVisibleReportAction(reportActions: ReportAction[], actionIndex: number, isOffline: boolean): OnyxEntry<ReportAction> { | ||
| for (let i = actionIndex + 1; i < reportActions.length; i++) { | ||
| const action = reportActions.at(i); | ||
|
|
||
| if (!isOffline && action?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) { | ||
| continue; | ||
| } | ||
|
|
||
| if (action?.shouldShow === false) { | ||
| continue; | ||
| } | ||
|
|
||
| return action; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| function isChronosOOOListAction(reportAction: OnyxInputOrEntry<ReportAction>): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.CHRONOS_OOO_LIST> { | ||
| const action = reportAction as {actionName?: string} | null | undefined; | ||
| return action?.actionName === CONST.REPORT.ACTIONS.TYPE.CHRONOS_OOO_LIST; | ||
| } | ||
|
|
||
| // The entire trimmed message must be a timer command (not a word embedded in normal chat). Case-insensitive. | ||
| // "at ..." is only allowed after past-tense "started"/"stopped" so phrases like "stop at the corner" do not match. | ||
| const CHRONOS_TIMER_COMMAND_STOP_REGEX = /^(?:stop(?:ping)?(?:\s+now)?|stopped(?:\s+now)?(?:\s+at\b.*)?)$/i; | ||
| const CHRONOS_TIMER_COMMAND_START_REGEX = /^(?:start(?:ing)?(?:\s+now)?|started(?:\s+now)?(?:\s+at\b.*)?)$/i; | ||
|
|
||
| type ChronosTimerCommandValue = ValueOf<typeof CONST.CHRONOS.TIMER_COMMAND>; | ||
|
|
||
| /** | ||
| * Returns `CONST.CHRONOS.TIMER_COMMAND.START`, `CONST.CHRONOS.TIMER_COMMAND.STOP`, or `null` when the trimmed text is only a Chronos timer command (e.g. "start", "stop now", "started at …", "stopped at …"), not when "start"/"stop" appear inside a sentence. | ||
| */ | ||
| function isChronosStartOrStopMessage(text: string): ChronosTimerCommandValue | null { | ||
| const trimmedText = text.trim(); | ||
| if (CHRONOS_TIMER_COMMAND_STOP_REGEX.test(trimmedText)) { | ||
| return CONST.CHRONOS.TIMER_COMMAND.STOP; | ||
| } | ||
| if (CHRONOS_TIMER_COMMAND_START_REGEX.test(trimmedText)) { | ||
| return CONST.CHRONOS.TIMER_COMMAND.START; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function isChronosAutomaticTimerAction(reportAction: OnyxInputOrEntry<ReportAction>, isChronosReport: boolean): boolean { | ||
| return isChronosReport && isChronosStartOrStopMessage(getReportActionText(reportAction as GetReportActionTextArg)) !== null; | ||
| } | ||
|
|
||
| /** | ||
| * From visible report actions sorted newest-first, returns the latest ADD_COMMENT from the current user that looks like a Chronos timer command. | ||
| */ | ||
| function getLatestUserChronosTimerCommand(sortedVisibleReportActionsDesc: ReportAction[], currentUserAccountID: number): ChronosTimerCommandValue | null { | ||
| for (const action of sortedVisibleReportActionsDesc) { | ||
| if (action.actionName !== CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT) { | ||
| continue; | ||
| } | ||
| if (action.actorAccountID !== currentUserAccountID) { | ||
| continue; | ||
| } | ||
| const kind = isChronosStartOrStopMessage(getReportActionText(action as GetReportActionTextArg)); | ||
| if (kind !== null) { | ||
| return kind; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function isChronosTimerRunningFromVisibleActions(sortedVisibleReportActionsDesc: ReportAction[], currentUserAccountID: number): boolean { | ||
| return getLatestUserChronosTimerCommand(sortedVisibleReportActionsDesc, currentUserAccountID) === CONST.CHRONOS.TIMER_COMMAND.START; | ||
| } | ||
|
|
||
| /** | ||
| * If the user sends consecutive actions to Chronos to automatically start/stop the timer, | ||
| * then detect that and show each individually so that the user can easily see when they were sent. | ||
| */ | ||
| function isConsecutiveChronosAutomaticTimerAction(reportActions: ReportAction[], actionIndex: number, isChronosReport: boolean, isOffline: boolean): boolean { | ||
| const previousAction = findPreviousVisibleReportAction(reportActions, actionIndex, isOffline); | ||
| const currentAction = reportActions?.at(actionIndex); | ||
| return isChronosAutomaticTimerAction(currentAction, isChronosReport) && isChronosAutomaticTimerAction(previousAction, isChronosReport); | ||
| } | ||
|
|
||
| export type {ChronosTimerCommandValue}; | ||
| export {getLatestUserChronosTimerCommand, isChronosOOOListAction, isChronosStartOrStopMessage, isChronosTimerRunningFromVisibleActions, isConsecutiveChronosAutomaticTimerAction}; | ||
Uh oh!
There was an error while loading. Please reload this page.