| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 9 | import {Checkbox} from 'isl-components/Checkbox'; |
| b69ab31 | | | 10 | import {DatetimePicker} from 'isl-components/DatetimePicker'; |
| b69ab31 | | | 11 | import {TextField} from 'isl-components/TextField'; |
| b69ab31 | | | 12 | import {useEffect, useRef, useState} from 'react'; |
| b69ab31 | | | 13 | import {tracker} from './analytics'; |
| b69ab31 | | | 14 | import {t, T} from './i18n'; |
| b69ab31 | | | 15 | import {GotoOperation} from './operations/GotoOperation'; |
| b69ab31 | | | 16 | import {RebaseOperation} from './operations/RebaseOperation'; |
| b69ab31 | | | 17 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 18 | import {type ExactRevset, exactRevset} from './types'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | import './GotoTimeMenu.css'; |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | /** |
| b69ab31 | | | 23 | * Generates a succeedable revset for a specific number of hours ago |
| b69ab31 | | | 24 | * @param hours Number of hours ago |
| b69ab31 | | | 25 | * @returns A ExactRevset object |
| b69ab31 | | | 26 | */ |
| b69ab31 | | | 27 | function getRevsetForHoursAgo(hours: number): ExactRevset { |
| b69ab31 | | | 28 | const date = new Date(); |
| b69ab31 | | | 29 | // setHours() correctly handles going back a day/month/year as needed, if it would take us from, eg, Jan 1st to Dec 31st |
| b69ab31 | | | 30 | date.setHours(date.getHours() - hours); |
| b69ab31 | | | 31 | const datetimeStr = formatDateTimeHelper(date); |
| b69ab31 | | | 32 | return getRevsetForDate(datetimeStr); |
| b69ab31 | | | 33 | } |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | /** |
| b69ab31 | | | 36 | * Generates a succeedable revset for a specific date string |
| b69ab31 | | | 37 | * @param dateString The date string to use in the revset |
| b69ab31 | | | 38 | * @returns A ExactRevset object |
| b69ab31 | | | 39 | */ |
| b69ab31 | | | 40 | function getRevsetForDate(dateString: string): ExactRevset { |
| b69ab31 | | | 41 | return exactRevset(`bsearch(date(">${dateString}"),max(public()))`); |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | /** |
| b69ab31 | | | 45 | * Formats a date into a string compatible with the datetime-local input and SL revset.date() |
| b69ab31 | | | 46 | * @param date The date to format |
| b69ab31 | | | 47 | * @returns A string in the format YYYY-MM-DDTHH:MM |
| b69ab31 | | | 48 | */ |
| b69ab31 | | | 49 | function formatDateTimeHelper(date: Date): string { |
| b69ab31 | | | 50 | // Date.toISOString() is close to what we want, but it has fractional seconds and isn't in local time |
| b69ab31 | | | 51 | // Date.toLocaleString() is in local time, but uses slashes rather than dashes |
| b69ab31 | | | 52 | // Format date as YYYY-MM-DD in local timezone |
| b69ab31 | | | 53 | const year = date.getFullYear(); |
| b69ab31 | | | 54 | const month = String(date.getMonth() + 1).padStart(2, '0'); // getMonth() is 0-indexed |
| b69ab31 | | | 55 | const day = String(date.getDate()).padStart(2, '0'); |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | // Format time as HH:MM in local timezone |
| b69ab31 | | | 58 | const hours = String(date.getHours()).padStart(2, '0'); |
| b69ab31 | | | 59 | const minutes = String(date.getMinutes()).padStart(2, '0'); |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | // Return in format YYYY-MM-DDTHH:MM (required by datetime-local input and compatible with revset.date()) |
| b69ab31 | | | 62 | return `${year}-${month}-${day}T${hours}:${minutes}`; |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | /** |
| b69ab31 | | | 66 | * GotoTimeContent component that can be used directly or wrapped in an expander |
| b69ab31 | | | 67 | */ |
| b69ab31 | | | 68 | export function GotoTimeContent({dismiss}: {dismiss?: () => unknown}) { |
| b69ab31 | | | 69 | const runOperation = useRunOperation(); |
| b69ab31 | | | 70 | const [shouldRebase, setShouldRebase] = useState(false); |
| b69ab31 | | | 71 | const [hours, setHours] = useState(''); |
| b69ab31 | | | 72 | const [datetime, setDatetime] = useState(''); |
| b69ab31 | | | 73 | const maxDatetime = useRef(''); |
| b69ab31 | | | 74 | const hoursInputRef = useRef(null); |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | useEffect(() => { |
| b69ab31 | | | 77 | if (hoursInputRef.current) { |
| b69ab31 | | | 78 | (hoursInputRef.current as HTMLInputElement).focus(); |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | // Initialize datetime and maxDatetime with current time. |
| b69ab31 | | | 82 | const now = new Date(); |
| b69ab31 | | | 83 | const nowFormatted = formatDateTimeHelper(now); |
| b69ab31 | | | 84 | setDatetime(nowFormatted); |
| b69ab31 | | | 85 | maxDatetime.current = nowFormatted; |
| b69ab31 | | | 86 | }, [hoursInputRef]); |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | // When hours is edited, clear the datetime picker. Must be one or the other |
| b69ab31 | | | 89 | const handleHoursChange = (value: string) => { |
| b69ab31 | | | 90 | setHours(value); |
| b69ab31 | | | 91 | if (value.trim().length > 0) { |
| b69ab31 | | | 92 | setDatetime(''); |
| b69ab31 | | | 93 | } |
| b69ab31 | | | 94 | }; |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | // When datetime is edited, clear the hours input. Must be one or the other |
| b69ab31 | | | 97 | const handleDatetimeChange = (value: string) => { |
| b69ab31 | | | 98 | setDatetime(value); |
| b69ab31 | | | 99 | if (value.trim().length > 0) { |
| b69ab31 | | | 100 | setHours(''); |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | }; |
| b69ab31 | | | 103 | |
| b69ab31 | | | 104 | const doGoToCommit = () => { |
| b69ab31 | | | 105 | tracker.track('ClickGotoTimeButton', { |
| b69ab31 | | | 106 | extras: { |
| b69ab31 | | | 107 | isHours: hours.trim().length > 0, |
| b69ab31 | | | 108 | shouldRebase, |
| b69ab31 | | | 109 | }, |
| b69ab31 | | | 110 | }); |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | // Get the destination revset based on "hours ago" or datetime, whichever has a value |
| b69ab31 | | | 113 | let destinationRevset: ExactRevset; |
| b69ab31 | | | 114 | |
| b69ab31 | | | 115 | if (hours.trim().length > 0) { |
| b69ab31 | | | 116 | const hoursValue = parseFloat(hours); |
| b69ab31 | | | 117 | if (isNaN(hoursValue)) { |
| b69ab31 | | | 118 | return; |
| b69ab31 | | | 119 | } |
| b69ab31 | | | 120 | destinationRevset = getRevsetForHoursAgo(hoursValue); |
| b69ab31 | | | 121 | } else if (datetime.trim().length > 0) { |
| b69ab31 | | | 122 | destinationRevset = getRevsetForDate(datetime); |
| b69ab31 | | | 123 | } else { |
| b69ab31 | | | 124 | // No valid input |
| b69ab31 | | | 125 | return; |
| b69ab31 | | | 126 | } |
| b69ab31 | | | 127 | |
| b69ab31 | | | 128 | if (shouldRebase) { |
| b69ab31 | | | 129 | // Rebase current work onto the commit at the specified time |
| b69ab31 | | | 130 | runOperation(new RebaseOperation(exactRevset('.'), destinationRevset)); |
| b69ab31 | | | 131 | } else { |
| b69ab31 | | | 132 | // Go to the commit at the specified time |
| b69ab31 | | | 133 | runOperation(new GotoOperation(destinationRevset)); |
| b69ab31 | | | 134 | } |
| b69ab31 | | | 135 | |
| b69ab31 | | | 136 | // Dismiss the tooltip/dialog if it exists |
| b69ab31 | | | 137 | if (dismiss) { |
| b69ab31 | | | 138 | dismiss(); |
| b69ab31 | | | 139 | } |
| b69ab31 | | | 140 | }; |
| b69ab31 | | | 141 | |
| b69ab31 | | | 142 | return ( |
| b69ab31 | | | 143 | <div className="goto-time-content"> |
| b69ab31 | | | 144 | <div className="goto-time-input-row"> |
| b69ab31 | | | 145 | <TextField |
| b69ab31 | | | 146 | width="100%" |
| b69ab31 | | | 147 | placeholder={t('Hours ago')} |
| b69ab31 | | | 148 | value={hours} |
| b69ab31 | | | 149 | data-testid="goto-time-input" |
| b69ab31 | | | 150 | onInput={e => handleHoursChange((e.target as unknown as {value: string})?.value ?? '')} |
| b69ab31 | | | 151 | onKeyDown={e => { |
| b69ab31 | | | 152 | if (e.key === 'Enter') { |
| b69ab31 | | | 153 | if (hours.trim().length > 0) { |
| b69ab31 | | | 154 | doGoToCommit(); |
| b69ab31 | | | 155 | } |
| b69ab31 | | | 156 | } |
| b69ab31 | | | 157 | }} |
| b69ab31 | | | 158 | ref={hoursInputRef} |
| b69ab31 | | | 159 | /> |
| b69ab31 | | | 160 | </div> |
| b69ab31 | | | 161 | |
| b69ab31 | | | 162 | <div className="goto-time-or-divider"> |
| b69ab31 | | | 163 | <T>or</T> |
| b69ab31 | | | 164 | </div> |
| b69ab31 | | | 165 | |
| b69ab31 | | | 166 | <div className="goto-time-datetime-inputs"> |
| b69ab31 | | | 167 | <DatetimePicker |
| b69ab31 | | | 168 | width="100%" |
| b69ab31 | | | 169 | value={datetime} |
| b69ab31 | | | 170 | max={maxDatetime.current} |
| b69ab31 | | | 171 | onInput={e => handleDatetimeChange((e.target as unknown as {value: string})?.value ?? '')} |
| b69ab31 | | | 172 | onKeyDown={e => { |
| b69ab31 | | | 173 | if (e.key === 'Enter' && datetime.trim().length > 0) { |
| b69ab31 | | | 174 | doGoToCommit(); |
| b69ab31 | | | 175 | } else if (datetime.trim().length === 0) { |
| b69ab31 | | | 176 | setHours(''); // Clear hours on keyDown, not just onInput, which is only fired for a complete/valid date |
| b69ab31 | | | 177 | } |
| b69ab31 | | | 178 | }} |
| b69ab31 | | | 179 | /> |
| b69ab31 | | | 180 | </div> |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | <div className="goto-time-actions"> |
| b69ab31 | | | 183 | <Checkbox checked={shouldRebase} onChange={setShouldRebase}> |
| b69ab31 | | | 184 | <T>Rebase current stack here</T> |
| b69ab31 | | | 185 | </Checkbox> |
| b69ab31 | | | 186 | <Button |
| b69ab31 | | | 187 | data-testid="goto-time-button" |
| b69ab31 | | | 188 | primary |
| b69ab31 | | | 189 | disabled={hours.trim().length === 0 && datetime.trim().length === 0} |
| b69ab31 | | | 190 | onClick={doGoToCommit}> |
| b69ab31 | | | 191 | <T>Goto</T> |
| b69ab31 | | | 192 | </Button> |
| b69ab31 | | | 193 | </div> |
| b69ab31 | | | 194 | </div> |
| b69ab31 | | | 195 | ); |
| b69ab31 | | | 196 | } |