From 0016a0742b7ecd107152e09cb629d0d2ea637764 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 29 Sep 2024 04:17:51 +0800 Subject: [PATCH] feat: webhook action setting --- apps/renderer/src/models/types.ts | 24 ++++++ .../src/modules/settings/action-card.tsx | 80 ++++++++++++------- .../src/pages/settings/(settings)/actions.tsx | 25 +----- locales/errors/en.json | 1 + locales/settings/en.json | 1 + packages/shared/src/hono.ts | 31 +++++++ 6 files changed, 110 insertions(+), 52 deletions(-) diff --git a/apps/renderer/src/models/types.ts b/apps/renderer/src/models/types.ts index a9b376609..91fac5826 100644 --- a/apps/renderer/src/models/types.ts +++ b/apps/renderer/src/models/types.ts @@ -84,3 +84,27 @@ export type MediaModel = Exclude< ExtractBizResponse["data"], undefined >["entries"]["media"] + +export type ActionsInput = { + name: string + condition: { + field?: ActionFeedField + operator?: ActionOperation + value?: string + }[] + result: { + translation?: string + summary?: boolean + readability?: boolean + rewriteRules?: { + from: string + to: string + }[] + blockRules?: { + field?: ActionEntryField + operator?: ActionOperation + value?: string | number + }[] + webhooks?: string[] + } +}[] diff --git a/apps/renderer/src/modules/settings/action-card.tsx b/apps/renderer/src/modules/settings/action-card.tsx index 6c2a4c066..9700a9bc5 100644 --- a/apps/renderer/src/modules/settings/action-card.tsx +++ b/apps/renderer/src/modules/settings/action-card.tsx @@ -27,35 +27,7 @@ import { import { ViewSelectContent } from "~/components/view-select-content" import { stopPropagation } from "~/lib/dom" import { cn } from "~/lib/utils" -import type { - ActionEntryField, - ActionFeedField, - ActionOperation, - SupportedLanguages, -} from "~/models" - -type ActionsInput = { - name: string - condition: { - field?: ActionFeedField - operator?: ActionOperation - value?: string - }[] - result: { - translation?: string - summary?: boolean - readability?: boolean - rewriteRules?: { - from: string - to: string - }[] - blockRules?: { - field?: ActionEntryField - operator?: ActionOperation - value?: string | number - }[] - } -}[] +import type { ActionFeedField, ActionOperation, ActionsInput, SupportedLanguages } from "~/models" const TransitionOptions: { name: string @@ -620,6 +592,56 @@ export function ActionCard({ )} + + + { + if (open) { + data.result.webhooks = [""] + } else { + delete data.result.webhooks + } + onChange(data) + }} + > + {data.result.webhooks && data.result.webhooks.length > 0 && ( + <> + {data.result.webhooks.map((webhook, rewriteIdx) => { + return ( +
+ { + if (data.result.webhooks?.length === 1) { + delete data.result.webhooks + } else { + data.result.webhooks?.splice(rewriteIdx, 1) + } + onChange(data) + }} + /> + { + data.result.webhooks![rewriteIdx] = e.target.value + onChange(data) + }} + /> +
+ ) + })} + { + data.result.webhooks!.push("") + onChange(data) + }} + /> + + )} +
diff --git a/apps/renderer/src/pages/settings/(settings)/actions.tsx b/apps/renderer/src/pages/settings/(settings)/actions.tsx index 89fc2e25f..95858cdd4 100644 --- a/apps/renderer/src/pages/settings/(settings)/actions.tsx +++ b/apps/renderer/src/pages/settings/(settings)/actions.tsx @@ -7,7 +7,7 @@ import { Button } from "~/components/ui/button" import { useAuthQuery } from "~/hooks/common" import { apiClient } from "~/lib/api-fetch" import { toastFetchError } from "~/lib/error-parser" -import type { ActionEntryField, ActionFeedField, ActionOperation, ActionsResponse } from "~/models" +import type { ActionsInput, ActionsResponse } from "~/models" import { ActionCard } from "~/modules/settings/action-card" import { SettingsTitle } from "~/modules/settings/title" import { defineSettingPageData } from "~/modules/settings/utils" @@ -22,28 +22,6 @@ export const loader = defineSettingPageData({ priority, }) -type ActionsInput = { - name: string - condition: { - field?: ActionFeedField - operator?: ActionOperation - value?: string - }[] - result: { - translation?: string - summary?: boolean - rewriteRules?: { - from: string - to: string - }[] - blockRules?: { - field?: ActionEntryField - operator?: ActionOperation - value?: string | number - }[] - } -}[] - export function Component() { const { t } = useTranslation("settings") const actions = useAuthQuery(Queries.action.getAll()) @@ -63,6 +41,7 @@ export function Component() { 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: { diff --git a/locales/errors/en.json b/locales/errors/en.json index b27719fb8..2645c057f 100644 --- a/locales/errors/en.json +++ b/locales/errors/en.json @@ -4,6 +4,7 @@ "1001": "create session failed", "1002": "Invalid parameter", "1003": "Invalid invitation", + "1004": "No permission", "2000": "Only admins can refresh feeds", "2001": "Feed not found", "2002": "feedId or url required", diff --git a/locales/settings/en.json b/locales/settings/en.json index 9f29e3a3a..25a780215 100644 --- a/locales/settings/en.json +++ b/locales/settings/en.json @@ -39,6 +39,7 @@ "actions.action_card.to": "To", "actions.action_card.translate_into": "Translate into", "actions.action_card.value": "Value", + "actions.action_card.webhooks": "Webhooks", "actions.action_card.when_feeds_match": "When feeds match…", "actions.newRule": "New Rule", "actions.save": "Save", diff --git a/packages/shared/src/hono.ts b/packages/shared/src/hono.ts index 274984fdb..9199002b4 100644 --- a/packages/shared/src/hono.ts +++ b/packages/shared/src/hono.ts @@ -224,6 +224,7 @@ declare const actions: drizzle_orm_pg_core.PgTableWithColumns<{ operator: z.infer; value: string | number; }[]; + webhooks?: string[]; }; }[]; driverParam: unknown; @@ -281,6 +282,7 @@ declare const actionsItemOpenAPISchema: z.ZodObject<{ field: "title" | "all" | "content" | "author" | "url" | "order"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }>, "many">>; + webhooks: z.ZodOptional>; }, "strip", z.ZodTypeAny, { summary?: boolean | undefined; translation?: "en" | "ja" | "zh-CN" | "zh-TW" | undefined; @@ -294,6 +296,7 @@ declare const actionsItemOpenAPISchema: z.ZodObject<{ field: "title" | "all" | "content" | "author" | "url" | "order"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }[] | undefined; + webhooks?: string[] | undefined; }, { summary?: boolean | undefined; translation?: "en" | "ja" | "zh-CN" | "zh-TW" | undefined; @@ -307,6 +310,7 @@ declare const actionsItemOpenAPISchema: z.ZodObject<{ field: "title" | "all" | "content" | "author" | "url" | "order"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }[] | undefined; + webhooks?: string[] | undefined; }>; }, "strip", z.ZodTypeAny, { name: string; @@ -323,6 +327,7 @@ declare const actionsItemOpenAPISchema: z.ZodObject<{ field: "title" | "all" | "content" | "author" | "url" | "order"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }[] | undefined; + webhooks?: string[] | undefined; }; condition: { value: string; @@ -344,6 +349,7 @@ declare const actionsItemOpenAPISchema: z.ZodObject<{ field: "title" | "all" | "content" | "author" | "url" | "order"; operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex"; }[] | undefined; + webhooks?: string[] | undefined; }; condition: { value: string; @@ -401,6 +407,7 @@ declare const actionsOpenAPISchema: z.ZodObject, "many">>; + webhooks: z.ZodOptional>; }, "strip", z.ZodTypeAny, { summary?: boolean | undefined; translation?: "en" | "ja" | "zh-CN" | "zh-TW" | undefined; @@ -414,6 +421,7 @@ declare const actionsOpenAPISchema: z.ZodObject; }, "strip", z.ZodTypeAny, { name: string; @@ -443,6 +452,7 @@ declare const actionsOpenAPISchema: z.ZodObject; declare const _routes: hono_hono_base.HonoBase