codeant-ai-for-open-source[bot] commented on code in PR #41527:
URL: https://github.com/apache/superset/pull/41527#discussion_r3503475525
##########
superset-frontend/src/explore/components/controls/DateFilterControl/components/CommonFrame.tsx:
##########
@@ -16,26 +16,128 @@
* specific language governing permissions and limitations
* under the License.
*/
+import { useEffect, useState } from 'react';
import { t } from '@apache-superset/core/translation';
+import { InputNumber, Select } from '@superset-ui/core/components';
import { Radio } from '@superset-ui/core/components/Radio';
import {
COMMON_RANGE_OPTIONS,
- COMMON_RANGE_SET,
+ COMMON_RANGE_REGEX,
DateFilterTestKey,
} from 'src/explore/components/controls/DateFilterControl/utils';
import {
- CommonRangeType,
FrameComponentProps,
} from 'src/explore/components/controls/DateFilterControl/types';
+const PRESET_VALUES = new Set(COMMON_RANGE_OPTIONS.map(o => o.value as
string));
+
+/** Sentinel value used as the radio option value for the custom "Other" row.
*/
+const CUSTOM_SENTINEL = '__custom__';
+
+const UNIT_OPTIONS = [
+ { value: 'second', label: t('Seconds') },
+ { value: 'minute', label: t('Minutes') },
+ { value: 'hour', label: t('Hours') },
+ { value: 'day', label: t('Days') },
+ { value: 'week', label: t('Weeks') },
+ { value: 'month', label: t('Months') },
+ { value: 'quarter', label: t('Quarters') },
+ { value: 'year', label: t('Years') },
+];
+
+/**
+ * Parse "Last N unit(s)" into { n, unit }, returns null for preset-style
+ * strings without an explicit number (e.g. "Last day").
+ */
+function parseLastN(value: string): { n: number; unit: string } | null {
+ const m = value.match(COMMON_RANGE_REGEX);
+ // m[1] is the optional "<number> " capture group — absent for preset-style
+ // strings like "Last week" that have no explicit number.
+ if (!m || m[1] === undefined) return null;
+ return { n: parseInt(m[1], 10), unit: m[2].toLowerCase() };
+}
+
+/** Build the canonical "Last N unit(s)" string from parts. */
+function buildLastN(n: number, unit: string): string {
+ return `Last ${n} ${unit}${n !== 1 ? 's' : ''}`;
+}
+
export function CommonFrame(props: FrameComponentProps) {
- let commonRange = 'Last week';
- if (COMMON_RANGE_SET.has(props.value as CommonRangeType)) {
- commonRange = props.value;
- } else {
- props.onChange(commonRange);
+ const isPreset = PRESET_VALUES.has(props.value);
+ const parsedCustom = parseLastN(props.value);
+ const isCustom = !isPreset && parsedCustom !== null;
+
+ // Local state for the custom inputs — persists across preset ↔ Other
switches.
+ // Default to 4 hours so the initial "Other" emission ("Last 4 hours") never
+ // accidentally matches a preset and causes the radio to snap back.
+ const [customN, setCustomN] = useState<number>(parsedCustom?.n ?? 4);
+ const [customUnit, setCustomUnit] = useState<string>(
+ parsedCustom?.unit ?? 'hour',
+ );
+
+ // If the current value doesn't match any known pattern, reset to a default.
+ // Use useEffect to avoid calling onChange synchronously during render.
+ useEffect(() => {
+ if (!isPreset && !isCustom) {
+ props.onChange('Last week');
+ }
+ }, [props.value]); // eslint-disable-line react-hooks/exhaustive-deps
+
+ const radioValue = isCustom ? CUSTOM_SENTINEL : props.value;
+
+ function handleRadioChange(e: any) {
+ const val: string = e.target.value;
Review Comment:
**Suggestion:** Replace the `any` event parameter with the concrete radio
change event type expected by the component so the handler remains fully
type-safe. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
The new TypeScript code in the final file declares the handler parameter as
`any`, which directly violates the no-any-types rule. The suggestion correctly
identifies this real rule violation.
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=46b0a2a6d8ec4f39a1699c3f465dcb7a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=46b0a2a6d8ec4f39a1699c3f465dcb7a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:**
superset-frontend/src/explore/components/controls/DateFilterControl/components/CommonFrame.tsx
**Line:** 88:89
**Comment:**
*Custom Rule: Replace the `any` event parameter with the concrete radio
change event type expected by the component so the handler remains fully
type-safe.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41527&comment_hash=12c3ee9bf13e90b788cbdd3f3d9ab59336d5b2d58d00cac18a1c916aa207d0af&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41527&comment_hash=12c3ee9bf13e90b788cbdd3f3d9ab59336d5b2d58d00cac18a1c916aa207d0af&reaction=dislike'>👎</a>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]