@lukoweb/apitogo-module-user-panel 0.2.42 → 0.2.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/userPanelModule.tsx +182 -152
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lukoweb/apitogo-module-user-panel",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.44",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.tsx"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@types/react": "19.2.14",
|
|
16
16
|
"@types/react-dom": "19.2.3",
|
|
17
|
-
"@lukoweb/apitogo": "0.2.
|
|
17
|
+
"@lukoweb/apitogo": "0.2.44",
|
|
18
18
|
"react": "19.2.4",
|
|
19
19
|
"react-dom": "19.2.4",
|
|
20
20
|
"react-router": "7.13.1",
|
package/src/userPanelModule.tsx
CHANGED
|
@@ -1,152 +1,182 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ResolvedDashboardModule,
|
|
3
|
-
ResolvedPlansModule,
|
|
4
|
-
ResolvedUserManagementModule,
|
|
5
|
-
ResolvedUserPanelModule,
|
|
6
|
-
} from "@lukoweb/apitogo";
|
|
7
|
-
import {
|
|
8
|
-
CreditCardIcon,
|
|
9
|
-
LayoutDashboardIcon,
|
|
10
|
-
SettingsIcon,
|
|
11
|
-
} from "@lukoweb/apitogo/icons";
|
|
12
|
-
import type {
|
|
13
|
-
ProfileMenuPlugin,
|
|
14
|
-
ApitogoPlugin,
|
|
15
|
-
} from "@lukoweb/apitogo/plugins";
|
|
16
|
-
import type { ComponentType, ReactNode } from "react";
|
|
17
|
-
import type
|
|
18
|
-
import { toAccountChildPath } from "./account-paths.js";
|
|
19
|
-
import { AccountLayout } from "./components/AccountLayout.js";
|
|
20
|
-
import { DefaultDashboardPage } from "./pages/DefaultDashboardPage.js";
|
|
21
|
-
import { DefaultPlansPage } from "./pages/DefaultPlansPage.js";
|
|
22
|
-
import { SettingsPage } from "./pages/SettingsPage.js";
|
|
23
|
-
import { renderModuleElement } from "./renderModuleElement.js";
|
|
24
|
-
|
|
25
|
-
type DashboardModuleOptions = ResolvedDashboardModule & {
|
|
26
|
-
component?: ComponentType | ReactNode;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
type UserManagementModuleOptions = ResolvedUserManagementModule & {
|
|
30
|
-
component?: ComponentType | ReactNode;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
type PlansModuleOptions = ResolvedPlansModule & {
|
|
34
|
-
component?: ComponentType | ReactNode;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export type UserPanelModuleOptions = ResolvedUserPanelModule & {
|
|
38
|
-
dashboard: DashboardModuleOptions;
|
|
39
|
-
userManagement: UserManagementModuleOptions;
|
|
40
|
-
plans: PlansModuleOptions;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
)
|
|
56
|
-
if (
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
1
|
+
import type {
|
|
2
|
+
ResolvedDashboardModule,
|
|
3
|
+
ResolvedPlansModule,
|
|
4
|
+
ResolvedUserManagementModule,
|
|
5
|
+
ResolvedUserPanelModule,
|
|
6
|
+
} from "@lukoweb/apitogo";
|
|
7
|
+
import {
|
|
8
|
+
CreditCardIcon,
|
|
9
|
+
LayoutDashboardIcon,
|
|
10
|
+
SettingsIcon,
|
|
11
|
+
} from "@lukoweb/apitogo/icons";
|
|
12
|
+
import type {
|
|
13
|
+
ProfileMenuPlugin,
|
|
14
|
+
ApitogoPlugin,
|
|
15
|
+
} from "@lukoweb/apitogo/plugins";
|
|
16
|
+
import type { ComponentType, ReactNode } from "react";
|
|
17
|
+
import { Navigate, type RouteObject } from "react-router";
|
|
18
|
+
import { toAccountChildPath } from "./account-paths.js";
|
|
19
|
+
import { AccountLayout } from "./components/AccountLayout.js";
|
|
20
|
+
import { DefaultDashboardPage } from "./pages/DefaultDashboardPage.js";
|
|
21
|
+
import { DefaultPlansPage } from "./pages/DefaultPlansPage.js";
|
|
22
|
+
import { SettingsPage } from "./pages/SettingsPage.js";
|
|
23
|
+
import { renderModuleElement } from "./renderModuleElement.js";
|
|
24
|
+
|
|
25
|
+
type DashboardModuleOptions = ResolvedDashboardModule & {
|
|
26
|
+
component?: ComponentType | ReactNode;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type UserManagementModuleOptions = ResolvedUserManagementModule & {
|
|
30
|
+
component?: ComponentType | ReactNode;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type PlansModuleOptions = ResolvedPlansModule & {
|
|
34
|
+
component?: ComponentType | ReactNode;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type UserPanelModuleOptions = ResolvedUserPanelModule & {
|
|
38
|
+
dashboard: DashboardModuleOptions;
|
|
39
|
+
userManagement: UserManagementModuleOptions;
|
|
40
|
+
plans: PlansModuleOptions;
|
|
41
|
+
/**
|
|
42
|
+
* The billing plugin is on: it owns the plans page (`/account/plans`), so this module registers no placeholder of its
|
|
43
|
+
* own, and its pages show the billing plugin's side menu (API keys, Usage, Billing, Settings).
|
|
44
|
+
*/
|
|
45
|
+
billingNav?: boolean;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** The billing plugin's plans page, where "Plans" leads once that plugin is on. */
|
|
49
|
+
const BILLING_PLANS_PATH = "/account/plans";
|
|
50
|
+
|
|
51
|
+
const protectAccountRoute = (
|
|
52
|
+
protectedRoutes: string[],
|
|
53
|
+
enabled: boolean,
|
|
54
|
+
path: string,
|
|
55
|
+
) => {
|
|
56
|
+
if (enabled) {
|
|
57
|
+
protectedRoutes.push(path);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const userPanelModule = (
|
|
62
|
+
options: UserPanelModuleOptions,
|
|
63
|
+
): (ApitogoPlugin & ProfileMenuPlugin) | undefined => {
|
|
64
|
+
if (!options.enabled) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const routes: RouteObject[] = [];
|
|
69
|
+
const protectedRoutes: string[] = [];
|
|
70
|
+
const profileMenuItems: ReturnType<ProfileMenuPlugin["getProfileMenuItems"]> =
|
|
71
|
+
[];
|
|
72
|
+
const protectAccountRoutes = options.userManagement.enabled;
|
|
73
|
+
const basePath = options.basePath ?? "/account";
|
|
74
|
+
const accountChildren: RouteObject[] = [];
|
|
75
|
+
|
|
76
|
+
if (options.dashboard.enabled) {
|
|
77
|
+
accountChildren.push({
|
|
78
|
+
path: toAccountChildPath(basePath, options.dashboard.path),
|
|
79
|
+
element: renderModuleElement(
|
|
80
|
+
options.dashboard.component,
|
|
81
|
+
<DefaultDashboardPage content={options.dashboard.content} />,
|
|
82
|
+
),
|
|
83
|
+
handle: { layout: "none" },
|
|
84
|
+
});
|
|
85
|
+
protectAccountRoute(
|
|
86
|
+
protectedRoutes,
|
|
87
|
+
protectAccountRoutes,
|
|
88
|
+
options.dashboard.path,
|
|
89
|
+
);
|
|
90
|
+
profileMenuItems.push({
|
|
91
|
+
label: "Dashboard",
|
|
92
|
+
path: options.dashboard.path,
|
|
93
|
+
category: "top",
|
|
94
|
+
icon: LayoutDashboardIcon,
|
|
95
|
+
weight: 10,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (options.userManagement.enabled) {
|
|
100
|
+
const profilePath = options.userManagement.routes.profile;
|
|
101
|
+
accountChildren.push({
|
|
102
|
+
path: toAccountChildPath(basePath, profilePath),
|
|
103
|
+
element: renderModuleElement(
|
|
104
|
+
options.userManagement.component,
|
|
105
|
+
<SettingsPage content={options.userManagement.content} />,
|
|
106
|
+
),
|
|
107
|
+
handle: { layout: "none" },
|
|
108
|
+
});
|
|
109
|
+
protectAccountRoute(protectedRoutes, protectAccountRoutes, profilePath);
|
|
110
|
+
profileMenuItems.push({
|
|
111
|
+
label: "Settings",
|
|
112
|
+
path: profilePath,
|
|
113
|
+
category: "middle",
|
|
114
|
+
icon: SettingsIcon,
|
|
115
|
+
weight: 20,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (options.plans.enabled && options.billingNav) {
|
|
120
|
+
// The placeholder would be an empty page next to the real one: its old address leads to the real one instead.
|
|
121
|
+
if (options.plans.path !== BILLING_PLANS_PATH)
|
|
122
|
+
accountChildren.push({
|
|
123
|
+
path: toAccountChildPath(basePath, options.plans.path),
|
|
124
|
+
element: <Navigate to={BILLING_PLANS_PATH} replace />,
|
|
125
|
+
handle: { layout: "none" },
|
|
126
|
+
});
|
|
127
|
+
profileMenuItems.push({
|
|
128
|
+
label: "Plans",
|
|
129
|
+
path: BILLING_PLANS_PATH,
|
|
130
|
+
category: "middle",
|
|
131
|
+
icon: CreditCardIcon,
|
|
132
|
+
weight: 30,
|
|
133
|
+
});
|
|
134
|
+
} else if (options.plans.enabled) {
|
|
135
|
+
accountChildren.push({
|
|
136
|
+
path: toAccountChildPath(basePath, options.plans.path),
|
|
137
|
+
element: renderModuleElement(
|
|
138
|
+
options.plans.component,
|
|
139
|
+
<DefaultPlansPage content={options.plans.content} />,
|
|
140
|
+
),
|
|
141
|
+
handle: { layout: "none" },
|
|
142
|
+
});
|
|
143
|
+
protectAccountRoute(protectedRoutes, true, options.plans.path);
|
|
144
|
+
profileMenuItems.push({
|
|
145
|
+
label: "Plans",
|
|
146
|
+
path: options.plans.path,
|
|
147
|
+
category: "middle",
|
|
148
|
+
icon: CreditCardIcon,
|
|
149
|
+
weight: 30,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (options.apiKeys.enabled) {
|
|
154
|
+
protectAccountRoute(
|
|
155
|
+
protectedRoutes,
|
|
156
|
+
protectAccountRoutes,
|
|
157
|
+
options.apiKeys.path,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (accountChildren.length > 0) {
|
|
162
|
+
routes.push({
|
|
163
|
+
path: basePath,
|
|
164
|
+
// The same side menu as the billing plugin's pages, and "Overview" only when its page is registered here.
|
|
165
|
+
element: (
|
|
166
|
+
<AccountLayout
|
|
167
|
+
basePath={basePath}
|
|
168
|
+
includeOverview={options.dashboard.enabled}
|
|
169
|
+
includeBillingNav={options.billingNav === true}
|
|
170
|
+
/>
|
|
171
|
+
),
|
|
172
|
+
handle: { layout: "default" },
|
|
173
|
+
children: accountChildren,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
getRoutes: () => routes,
|
|
179
|
+
getProtectedRoutes: () => protectedRoutes,
|
|
180
|
+
getProfileMenuItems: () => profileMenuItems,
|
|
181
|
+
};
|
|
182
|
+
};
|