Skip to content

Commit acca2aa

Browse files
authored
Merge pull request #646 from devforth/feature/AdminForth/1695/add-dynamic-label-support-for-
fix: add function support to menu label
2 parents 96e82ec + df5e269 commit acca2aa

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

adminforth/modules/restApi.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,8 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
946946
if (menuItem.label) {
947947
translateRoutines.push(
948948
(async () => {
949-
menuItem.label = await tr(menuItem.label, `menu.${menuItem.itemId}`);
949+
const rawLabel = typeof menuItem.label === 'function' ? await menuItem.label(adminUser, this.adminforth) : menuItem.label;
950+
menuItem.label = await tr(rawLabel, `menu.${menuItem.itemId}`);
950951
})()
951952
);
952953
}

adminforth/types/Common.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,16 @@ export interface AdminForthConfigMenuItem {
11041104

11051105
/**
11061106
* Label for menu item which will be displayed in the admin panel.
1107+
* Can be a static string or a callback which receives the current admin user
1108+
* and returns the label dynamically.
1109+
*
1110+
* Example:
1111+
*
1112+
* ```ts
1113+
* label: (adminUser) => adminUser.dbUser.role === 'superadmin' ? 'Dashboard (CRS)' : 'Dashboard',
1114+
* ```
11071115
*/
1108-
label?: string,
1116+
label?: string | ((user: AdminUser, adminForth: IAdminForth) => Promise<string> | string),
11091117

11101118
/**
11111119
* Icon for menu item which will be displayed in the admin panel.

0 commit comments

Comments
 (0)