-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
148 lines (126 loc) · 4.52 KB
/
Copy pathindex.ts
File metadata and controls
148 lines (126 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { AdminForthPlugin } from "adminforth";
import type { AdminUser, IAdminForth, IHttpServer } from "adminforth";
import type { PluginOptions } from "./types.js";
import { randomUUID } from 'crypto';
import path from 'path';
import { registerDashboardEndpoints } from './endpoint/dashboard.js';
import { registerGroupEndpoints } from "./endpoint/groups.js";
import { registerWidgetEndpoints } from './endpoint/widgets.js';
import { createDashboardConfigService } from "./services/dashboardConfigService.js";
import { createWidgetDataService } from "./services/widgetDataService.js";
const DEFAULT_DASHBOARD_CONFIG = {
version: 1,
groups: [{
id: 'default',
label: 'Default Group',
order: 1,
}],
widgets: [],
};
export default class DashboardPlugin extends AdminForthPlugin {
options: PluginOptions;
pluginsScope: "resource" | "global" = "global";
private didRegisterMenuProvider = false;
private didInstallSeedHook = false;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
this.customFolderName = 'custom';
this.customFolderPath = path.join(this.pluginDir, this.customFolderName);
}
async modifyGlobalConfig(adminforth: IAdminForth) {
super.modifyGlobalConfig(adminforth);
if (!this.didRegisterMenuProvider) {
this.didRegisterMenuProvider = true;
const adminforthWithDynamicMenu = adminforth as any;
adminforthWithDynamicMenu.registerMenuContributionProvider(async () => {
if (this.adminforth.statuses.dbDiscover !== 'done') {
return [];
}
const dashboards = await this.adminforth.resource(this.options.dashboardConfigsResourceId).list([]);
return [
{
item: {
itemId: 'dashboardMenu',
type: 'group',
label: 'Dashboards',
icon: 'flowbite:chart-pie-solid',
open: true,
children: dashboards.map((dashboard: any) => {
const config = typeof dashboard.config === 'string'
? JSON.parse(dashboard.config)
: dashboard.config;
return {
itemId: `dashboard-${dashboard.id}`,
type: 'page',
label: dashboard.label,
icon: config?.icon ?? 'flowbite:chart-pie-solid',
url: `/dashboard/${dashboard.slug}`,
};
}),
},
placement: { position: 'first' },
},
];
});
}
if (!this.didInstallSeedHook) {
this.didInstallSeedHook = true;
const discoverDatabases = adminforth.discoverDatabases.bind(adminforth);
adminforth.discoverDatabases = async () => {
await discoverDatabases();
await this.ensureDefaultDashboardConfig();
};
}
adminforth.config.customization.customPages.push({
path: '/dashboard/:slug',
component: {
file: this.componentPath('runtime/DashboardPage.vue'),
meta: {
title: 'Dashboard',
},
},
});
}
private async ensureDefaultDashboardConfig() {
const dashboardConfigs = this.adminforth.resource(this.options.dashboardConfigsResourceId);
const dashboardsCount = await dashboardConfigs.count();
if (dashboardsCount > 0) {
return;
}
const createResult = await dashboardConfigs.create({
id: randomUUID(),
slug: 'default',
label: 'Default Dashboard',
revision: 1,
config: DEFAULT_DASHBOARD_CONFIG,
});
if (!createResult.ok) {
throw new Error(createResult.error || 'Failed to create default dashboard config');
}
}
setupEndpoints(server: IHttpServer) {
const dashboardConfigService = createDashboardConfigService(
this.adminforth,
this.options.dashboardConfigsResourceId,
);
const widgetDataService = createWidgetDataService(this.adminforth);
const ctx = {
adminforth: this.adminforth,
dashboardConfigsResourceId: this.options.dashboardConfigsResourceId,
canEditDashboard: (adminUser: AdminUser) => this.canEditDashboard(adminUser),
...dashboardConfigService,
...widgetDataService,
};
registerDashboardEndpoints(server, ctx);
registerGroupEndpoints(server, ctx);
registerWidgetEndpoints(server, ctx);
}
instanceUniqueRepresentation(): string {
return "dashboard";
}
private canEditDashboard(adminUser: AdminUser) {
const editRoles = this.options.editRoles ?? ['superadmin'];
return editRoles.includes(adminUser.dbUser.role);
}
}