diff --git a/apps/renderer/src/modules/settings/tabs/actions/index.tsx b/apps/renderer/src/modules/settings/tabs/actions/index.tsx new file mode 100644 index 000000000..73da83a54 --- /dev/null +++ b/apps/renderer/src/modules/settings/tabs/actions/index.tsx @@ -0,0 +1,102 @@ +import { useMutation } from "@tanstack/react-query" +import { useEffect, useState } from "react" +import { useTranslation } from "react-i18next" +import { toast } from "sonner" + +import { Button } from "~/components/ui/button" +import { LoadingWithIcon } from "~/components/ui/loading" +import { useAuthQuery } from "~/hooks/common" +import { apiClient } from "~/lib/api-fetch" +import { toastFetchError } from "~/lib/error-parser" +import type { ActionsInput, ActionsResponse } from "~/models" +import { ActionCard } from "~/modules/settings/action-card" +import { Queries } from "~/queries" + +export const ActionSetting = () => { + const { t } = useTranslation("settings") + const actions = useAuthQuery(Queries.action.getAll()) + const [actionsData, setActionsData] = useState([]) + + useEffect(() => { + if (actions.data?.rules) { + setActionsData(actions.data.rules) + } + }, [actions.data?.rules]) + + const mutation = useMutation({ + mutationFn: async () => { + actionsData.forEach((action) => { + action.condition = action.condition.filter((c) => c.field && c.operator && c.value) + action.result.rewriteRules = action.result.rewriteRules?.filter((r) => r.from && r.to) + action.result.blockRules = action.result.blockRules?.filter( + (r) => r.field && r.operator && r.value, + ) + action.result.webhooks = action.result.webhooks?.filter((w) => w) + }) + await apiClient.actions.$put({ + json: { + rules: actionsData as ActionsResponse, + }, + }) + }, + onSuccess: () => { + Queries.action.getAll().invalidate() + toast(t("actions.saveSuccess")) + }, + onError: (error) => { + toastFetchError(error) + }, + }) + + if (actions.isPending) { + return ( +
+ } + size="large" + className="-translate-y-full" + /> +
+ ) + } + + return ( +
+ {actionsData.map((action, actionIdx) => ( + { + if (!newAction) { + setActionsData(actionsData.filter((_, idx) => idx !== actionIdx)) + } else { + setActionsData(actionsData.map((a, idx) => (idx === actionIdx ? newAction : a))) + } + }} + /> + ))} + +
+ +
+
+ ) +} diff --git a/apps/renderer/src/pages/settings/(settings)/actions.tsx b/apps/renderer/src/pages/settings/(settings)/actions.tsx index 83e5ef56f..61050d35b 100644 --- a/apps/renderer/src/pages/settings/(settings)/actions.tsx +++ b/apps/renderer/src/pages/settings/(settings)/actions.tsx @@ -1,18 +1,6 @@ -import { useMutation } from "@tanstack/react-query" -import { useEffect, useState } from "react" -import { useTranslation } from "react-i18next" -import { toast } from "sonner" - -import { Button } from "~/components/ui/button" -import { LoadingWithIcon } from "~/components/ui/loading" -import { useAuthQuery } from "~/hooks/common" -import { apiClient } from "~/lib/api-fetch" -import { toastFetchError } from "~/lib/error-parser" -import type { ActionsInput, ActionsResponse } from "~/models" -import { ActionCard } from "~/modules/settings/action-card" +import { ActionSetting } from "~/modules/settings/tabs/actions" import { SettingsTitle } from "~/modules/settings/title" import { defineSettingPageData } from "~/modules/settings/utils" -import { Queries } from "~/queries" const iconName = "i-mgc-magic-2-cute-re" const priority = 1020 @@ -24,97 +12,10 @@ export const loader = defineSettingPageData({ }) export function Component() { - const { t } = useTranslation("settings") - const actions = useAuthQuery(Queries.action.getAll()) - const [actionsData, setActionsData] = useState([]) - - useEffect(() => { - if (actions.data?.rules) { - setActionsData(actions.data.rules) - } - }, [actions.data?.rules]) - - const mutation = useMutation({ - mutationFn: async () => { - actionsData.forEach((action) => { - action.condition = action.condition.filter((c) => c.field && c.operator && c.value) - action.result.rewriteRules = action.result.rewriteRules?.filter((r) => r.from && r.to) - action.result.blockRules = action.result.blockRules?.filter( - (r) => r.field && r.operator && r.value, - ) - action.result.webhooks = action.result.webhooks?.filter((w) => w) - }) - await apiClient.actions.$put({ - json: { - rules: actionsData as ActionsResponse, - }, - }) - }, - onSuccess: () => { - Queries.action.getAll().invalidate() - toast(t("actions.saveSuccess")) - }, - onError: (error) => { - toastFetchError(error) - }, - }) - - if (actions.isPending) { - return ( -
- } - size="large" - className="-translate-y-full" - /> -
- ) - } - return ( <> -
- {actionsData.map((action, actionIdx) => ( - { - if (!newAction) { - setActionsData(actionsData.filter((_, idx) => idx !== actionIdx)) - } else { - setActionsData(actionsData.map((a, idx) => (idx === actionIdx ? newAction : a))) - } - }} - /> - ))} - -
- -
-
+ ) }