From 9fe514811395bec4a4b4221c3b0a524ba267dad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=BD=E5=A6=82=E5=AF=84?= Date: Wed, 13 Apr 2022 23:59:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20refactor=20resolve=20=E2=99=BB=EF=B8=8F?= =?UTF-8?q?=20(#3358)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ib229549e114db67b04f2039b80c9015f78310cc8 --- app/actions/index.ts | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/app/actions/index.ts b/app/actions/index.ts index 6985dc934..b13eaa99b 100644 --- a/app/actions/index.ts +++ b/app/actions/index.ts @@ -10,6 +10,10 @@ import { MenuItemWithChildren, } from "~/types"; +function resolve(value: any, context: ActionContext): T { + return typeof value === "function" ? value(context) : value; +} + export function createAction(definition: Optional): Action { return { ...definition, @@ -21,18 +25,10 @@ export function actionToMenuItem( action: Action, context: ActionContext ): MenuItemButton | MenuItemWithChildren { - function resolve(value: any): T { - if (typeof value === "function") { - return value(context); - } - - return value; - } - - const resolvedIcon = resolve>(action.icon); - const resolvedChildren = resolve(action.children); + const resolvedIcon = resolve>(action.icon, context); + const resolvedChildren = resolve(action.children, context); const visible = action.visible ? action.visible(context) : true; - const title = resolve(action.name); + const title = resolve(action.name, context); const icon = resolvedIcon && action.iconInContextMenu !== false ? React.cloneElement(resolvedIcon, { @@ -69,23 +65,15 @@ export function actionToKBar( action: Action, context: ActionContext ): CommandBarAction[] { - function resolve(value: any): T { - if (typeof value === "function") { - return value(context); - } - - return value; - } - if (typeof action.visible === "function" && !action.visible(context)) { return []; } - const resolvedIcon = resolve>(action.icon); - const resolvedChildren = resolve(action.children); - const resolvedSection = resolve(action.section); - const resolvedName = resolve(action.name); - const resolvedPlaceholder = resolve(action.placeholder); + const resolvedIcon = resolve>(action.icon, context); + const resolvedChildren = resolve(action.children, context); + const resolvedSection = resolve(action.section, context); + const resolvedName = resolve(action.name, context); + const resolvedPlaceholder = resolve(action.placeholder, context); const children = resolvedChildren ? flattenDeep(resolvedChildren.map((a) => actionToKBar(a, context))).filter( (a) => !!a