From a4ce91d6e3ac7fc0fd3e5fb3263beca2045b531c Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 24 Apr 2025 10:38:19 +0800 Subject: [PATCH] feat(mobile): add status action --- apps/mobile/src/components/ui/form/Select.tsx | 11 +- .../src/modules/settings/actions/constant.ts | 11 +- .../modules/settings/routes/EditCondition.tsx | 118 +++++++++++++----- 3 files changed, 103 insertions(+), 37 deletions(-) diff --git a/apps/mobile/src/components/ui/form/Select.tsx b/apps/mobile/src/components/ui/form/Select.tsx index 55c7b2146..d73955b98 100644 --- a/apps/mobile/src/components/ui/form/Select.tsx +++ b/apps/mobile/src/components/ui/form/Select.tsx @@ -30,12 +30,11 @@ export function Select({ wrapperStyle, label, }: SelectProps) { - const [currentValue, setCurrentValue] = useState(() => { - if (!value) { - return options[0]!.value - } - return value - }) + const [currentValue, setCurrentValue] = useState(() => value) + + useEffect(() => { + setCurrentValue(value) + }, [value]) const valueToLabelMap = useMemo(() => { return options.reduce((acc, option) => { diff --git a/apps/mobile/src/modules/settings/actions/constant.ts b/apps/mobile/src/modules/settings/actions/constant.ts index a3e2f3322..5ca9c70bd 100644 --- a/apps/mobile/src/modules/settings/actions/constant.ts +++ b/apps/mobile/src/modules/settings/actions/constant.ts @@ -12,8 +12,13 @@ import { EditWebhooksScreen } from "../routes/EditWebhooks" export const filterFieldOptions: Array<{ label: Extract, `actions.action_card.feed_options.${string}`> value: string - type?: "text" | "number" | "view" + type?: "text" | "number" | "view" | "status" }> = [ + { + label: "actions.action_card.feed_options.status", + value: "status", + type: "status", + }, { label: "actions.action_card.feed_options.subscription_view", value: "view", @@ -61,7 +66,7 @@ export const filterFieldOptions: Array<{ export const filterOperatorOptions: Array<{ label: Extract, `actions.action_card.operation_options.${string}`> value: string - types: Array<"text" | "number" | "view"> + types: Array<"text" | "number" | "view" | "status"> }> = [ { label: "actions.action_card.operation_options.contains", @@ -76,7 +81,7 @@ export const filterOperatorOptions: Array<{ { label: "actions.action_card.operation_options.is_equal_to", value: "eq", - types: ["number", "text", "view"], + types: ["number", "text", "view", "status"], }, { label: "actions.action_card.operation_options.is_not_equal_to", diff --git a/apps/mobile/src/modules/settings/routes/EditCondition.tsx b/apps/mobile/src/modules/settings/routes/EditCondition.tsx index 40b5e1bbe..fcdbd0535 100644 --- a/apps/mobile/src/modules/settings/routes/EditCondition.tsx +++ b/apps/mobile/src/modules/settings/routes/EditCondition.tsx @@ -1,3 +1,4 @@ +import { useMemo } from "react" import { useTranslation } from "react-i18next" import { Text, View } from "react-native" @@ -46,7 +47,17 @@ function ConditionForm({ index }: { index: ConditionIndex }) { currentField?.type === "view" ? views.find((view) => view.view === Number(item.value)) : undefined - const { t: tCommon } = useTranslation("common") + + const operatorOptions = useMemo(() => { + return filterOperatorOptions + .map((i) => ({ ...i, label: t(i.label) })) + .filter((operator) => operator.types.includes(currentField?.type ?? "text")) + }, [t, currentField]) + if (operatorOptions.length === 1 && currentOperator?.value !== operatorOptions[0]!.value) { + actionActions.pathCondition(index, { + operator: operatorOptions[0]!.value as any, + }) + } return ( <> @@ -69,9 +80,7 @@ function ConditionForm({ index }: { index: ConditionIndex }) { {t("actions.action_card.operator")} ({ - label: tCommon(field.name), - value: String(field.view), - }))} - value={currentView?.view ? String(currentView.view) : undefined} - onValueChange={(val) => { - actionActions.pathCondition(index, { value: val }) - }} - wrapperClassName="min-w-40" - /> - ) : ( - { - actionActions.pathCondition(index, { value }) - }} - hitSlop={10} - selectionColor={accentColor} - placeholder="Enter value" - /> - )} + { + actionActions.pathCondition(index, { value }) + }} + /> {__DEV__ && ( @@ -118,3 +116,67 @@ function ConditionForm({ index }: { index: ConditionIndex }) { ) } + +function ValueField({ + type, + value, + onChange, +}: { + type: string + value?: string + onChange: (value: string | undefined) => void +}) { + const { t } = useTranslation("common") + + switch (type) { + case "view": { + return ( + { + onChange(val as string) + }} + wrapperClassName="min-w-40" + /> + ) + } + default: { + return ( + { + onChange(value) + }} + hitSlop={10} + selectionColor={accentColor} + placeholder="Enter value" + /> + ) + } + } +}