@micha.bigler/ui-core-micha 4.14.0 → 4.15.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.15.0 - UCM-PERF-4
4
+
5
+ Opt-in `AuthProvider` prop `bootstrap` (`true` for `/api/bootstrap/`, or a same-origin path; requires
6
+ `django-core-micha` >= 3.17.0): the app shell starts from one bootstrap request instead of CSRF, `users/current`,
7
+ `auth-methods`, feed and unread count; `NotificationsProvider` takes its initial feed and count from it once for the same
8
+ person and still opens the WebSocket only after that. Any bootstrap failure falls back to the separate start requests
9
+ exactly once. New hook `useBootstrapExtension(name)` for dcm bootstrap provider payloads. Without the prop nothing
10
+ changes.
11
+
3
12
  ## 4.14.0 - UCM-PERF-5
4
13
 
5
14
  New `installChunkLoadRecovery({ guardWindowMs?, reload? })` and `isChunkLoadError(error)`. Installed once at app
package/README.md CHANGED
@@ -86,6 +86,16 @@ const setupBillingApi = createBillingApi({
86
86
  <PlanPicker api={setupBillingApi} plans={plans} />;
87
87
  ```
88
88
 
89
+ ## Start from one bootstrap request (opt-in)
90
+
91
+ With `django-core-micha` >= 3.17.0, `<AuthProvider bootstrap>` starts from `GET /api/bootstrap/` (or pass a same-origin
92
+ path instead of `true`): one request instead of CSRF, `users/current` + `auth-methods`, then the notification feed and
93
+ unread count. User, auth methods and the initial notifications end up as from the separate requests, `loading` ends at
94
+ the same point, and the notification WebSocket still connects only after that state is in place. If the bootstrap fails
95
+ in any way (a part failure, 501, 404, network), the provider runs today's separate start requests once. App start data
96
+ registered as dcm bootstrap providers is read with `useBootstrapExtension('application_access')` (`undefined` when
97
+ absent, `null`, the opt-in is off or the bootstrap failed). Without the prop nothing changes.
98
+
89
99
  ## Stale chunks after a deploy
90
100
 
91
101
  An app that lazy-loads code calls `installChunkLoadRecovery()` once at startup (before rendering). When a deploy has
@@ -1,8 +1,8 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  // src/auth/AuthContext.jsx
3
- import React, { createContext, useState, useEffect, } from 'react';
3
+ import React, { createContext, useState, useEffect, useCallback, } from 'react';
4
4
  import { ensureCsrfToken } from './apiClient'; // <--- IMPORT ADDED
5
- import { fetchAuthMethods, fetchCurrentUser, logoutSession, } from './authApi';
5
+ import { fetchAuthMethods, fetchCurrentUser, fetchBootstrap, logoutSession, } from './authApi';
6
6
  import { ReauthModal } from './ReauthModal';
7
7
  export const AuthContext = createContext(null);
8
8
  const DEFAULT_AUTH_METHODS = {
@@ -23,10 +23,15 @@ const DEFAULT_AUTH_METHODS = {
23
23
  two_factor_required: false,
24
24
  qr_signup_enabled: false,
25
25
  };
26
- export const AuthProvider = ({ children }) => {
26
+ export const AuthProvider = ({ children, bootstrap = false }) => {
27
27
  const [user, setUser] = useState(null);
28
28
  const [authMethods, setAuthMethods] = useState(DEFAULT_AUTH_METHODS);
29
29
  const [loading, setLoading] = useState(true);
30
+ const [notificationSeed, setNotificationSeed] = useState(null);
31
+ const [bootstrapExtensions, setBootstrapExtensions] = useState({});
32
+ const consumeBootstrapNotificationSeed = useCallback(() => {
33
+ setNotificationSeed(null);
34
+ }, []);
30
35
  const mapUserFromApi = (data) => {
31
36
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
32
37
  const profile = (data === null || data === void 0 ? void 0 : data.profile) || {};
@@ -34,7 +39,7 @@ export const AuthProvider = ({ children }) => {
34
39
  };
35
40
  useEffect(() => {
36
41
  let isMounted = true;
37
- const initAuth = async () => {
42
+ const initSeparateAuth = async () => {
38
43
  try {
39
44
  // 1) Ensure CSRF cookie exists using the specific client
40
45
  await ensureCsrfToken();
@@ -70,11 +75,49 @@ export const AuthProvider = ({ children }) => {
70
75
  setLoading(false);
71
76
  }
72
77
  };
78
+ const initAuth = async () => {
79
+ if (!bootstrap) {
80
+ await initSeparateAuth();
81
+ return;
82
+ }
83
+ try {
84
+ const data = await fetchBootstrap(bootstrap === true ? '/api/bootstrap/' : bootstrap);
85
+ if (!isMounted)
86
+ return;
87
+ const mappedUser = (data === null || data === void 0 ? void 0 : data.user) ? mapUserFromApi(data.user) : null;
88
+ const methods = data === null || data === void 0 ? void 0 : data.auth_methods;
89
+ if (methods && typeof methods === 'object') {
90
+ setAuthMethods((prev) => (Object.assign(Object.assign({}, prev), methods)));
91
+ }
92
+ setUser(mappedUser);
93
+ setBootstrapExtensions((data === null || data === void 0 ? void 0 : data.extensions) && typeof data.extensions === 'object'
94
+ ? data.extensions : {});
95
+ setNotificationSeed(mappedUser && (data === null || data === void 0 ? void 0 : data.notifications)
96
+ ? { userId: mappedUser.id, notifications: data.notifications }
97
+ : null);
98
+ setLoading(false);
99
+ }
100
+ catch (error) {
101
+ if (isMounted) {
102
+ setBootstrapExtensions({});
103
+ setNotificationSeed(null);
104
+ await initSeparateAuth();
105
+ }
106
+ }
107
+ };
73
108
  initAuth();
74
109
  return () => { isMounted = false; };
75
- }, []);
110
+ }, [bootstrap]);
111
+ // Bootstrap data belongs to the person it was loaded for; drop it when the session changes hands.
112
+ const clearBootstrapData = () => {
113
+ setBootstrapExtensions({});
114
+ setNotificationSeed(null);
115
+ };
76
116
  const login = (userData) => {
77
- setUser((prev) => (Object.assign(Object.assign({}, prev), mapUserFromApi(userData))));
117
+ const mapped = mapUserFromApi(userData);
118
+ if (user && mapped.id !== user.id)
119
+ clearBootstrapData();
120
+ setUser((prev) => (Object.assign(Object.assign({}, prev), mapped)));
78
121
  };
79
122
  const refreshUser = async () => {
80
123
  const data = await fetchCurrentUser();
@@ -90,6 +133,7 @@ export const AuthProvider = ({ children }) => {
90
133
  console.error('Error during logout:', error);
91
134
  }
92
135
  finally {
136
+ clearBootstrapData();
93
137
  setUser(null);
94
138
  }
95
139
  };
@@ -100,5 +144,14 @@ export const AuthProvider = ({ children }) => {
100
144
  login,
101
145
  logout,
102
146
  refreshUser,
147
+ bootstrapNotificationSeed: notificationSeed,
148
+ consumeBootstrapNotificationSeed,
149
+ bootstrapExtensions,
103
150
  }, children: [children, _jsx(ReauthModal, {})] }));
104
151
  };
152
+ export function useBootstrapExtension(name) {
153
+ var _a;
154
+ const context = React.useContext(AuthContext);
155
+ const payload = (_a = context === null || context === void 0 ? void 0 : context.bootstrapExtensions) === null || _a === void 0 ? void 0 : _a[name];
156
+ return payload == null ? undefined : payload;
157
+ }
@@ -20,6 +20,10 @@ export async function fetchCurrentUser() {
20
20
  });
21
21
  return res.data;
22
22
  }
23
+ export async function fetchBootstrap(url) {
24
+ const res = await apiClient.get(url, { skipAuthRedirect: true });
25
+ return res.data;
26
+ }
23
27
  export async function fetchAuthMethods() {
24
28
  const res = await apiClient.get('/api/auth-methods/');
25
29
  return res.data || {};
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ export { createAppTheme, assertThemeComplete, assertKitSxDisjoint, calculateCont
10
10
  export { checkKitIntegration } from './testing/checkKitIntegration';
11
11
  export { installChunkLoadRecovery, isChunkLoadError } from './utils/chunkLoadRecovery';
12
12
  // --- 1. Auth Context (Essentiell für den Wrapper) ---
13
- export { AuthContext, AuthProvider } from './auth/AuthContext';
13
+ export { AuthContext, AuthProvider, useBootstrapExtension } from './auth/AuthContext';
14
14
  export { UserMenu } from './auth/UserMenu';
15
15
  export { default as apiClient, ensureCsrfToken, addPublicPath, removePublicPath, } from "./auth/apiClient";
16
16
  // --- 2. API & Services (Neue Struktur) ---
@@ -39,6 +39,8 @@ function patchStatus(notification, status) {
39
39
  export function NotificationsProvider({ children, wsUrlBase }) {
40
40
  const auth = useContext(AuthContext);
41
41
  const authenticated = Boolean(auth === null || auth === void 0 ? void 0 : auth.user);
42
+ const bootstrapSeed = auth === null || auth === void 0 ? void 0 : auth.bootstrapNotificationSeed;
43
+ const consumeBootstrapSeed = auth === null || auth === void 0 ? void 0 : auth.consumeBootstrapNotificationSeed;
42
44
  const [notifications, setNotifications] = useState([]);
43
45
  const [unreadCount, setUnreadCount] = useState(0);
44
46
  const notificationsRef = useRef([]);
@@ -113,12 +115,24 @@ export function NotificationsProvider({ children, wsUrlBase }) {
113
115
  return subscribe(DEFAULT_ENVELOPE, handleRealtimeMessage);
114
116
  }, [authenticated, subscribe, handleRealtimeMessage]);
115
117
  useEffect(() => {
118
+ var _a, _b, _c;
116
119
  if (!authenticated) {
117
120
  replaceNotifications([]);
118
121
  setUnreadCount(0);
119
122
  setSeeded(false);
120
123
  return undefined;
121
124
  }
125
+ if (bootstrapSeed && bootstrapSeed.userId === ((_a = auth === null || auth === void 0 ? void 0 : auth.user) === null || _a === void 0 ? void 0 : _a.id)) {
126
+ const feed = (_b = bootstrapSeed.notifications) === null || _b === void 0 ? void 0 : _b.feed;
127
+ const unread = (_c = bootstrapSeed.notifications) === null || _c === void 0 ? void 0 : _c.unread_count;
128
+ const nextNotifications = ((feed === null || feed === void 0 ? void 0 : feed.results) || []).map(normalizeNotification);
129
+ notificationsRef.current = nextNotifications;
130
+ setNotifications(nextNotifications);
131
+ setUnreadCount((unread === null || unread === void 0 ? void 0 : unread.count) || 0);
132
+ setSeeded(true);
133
+ consumeBootstrapSeed === null || consumeBootstrapSeed === void 0 ? void 0 : consumeBootstrapSeed();
134
+ return undefined;
135
+ }
122
136
  let cancelled = false;
123
137
  refresh()
124
138
  .catch(() => {
@@ -131,7 +145,9 @@ export function NotificationsProvider({ children, wsUrlBase }) {
131
145
  return () => {
132
146
  cancelled = true;
133
147
  };
134
- }, [authenticated, refresh, replaceNotifications]);
148
+ // The seed and the user id are read when the provider becomes authenticated; adding the user id as
149
+ // a dependency would refetch on a user switch without logout, which is not today's behaviour.
150
+ }, [authenticated, refresh, replaceNotifications, consumeBootstrapSeed]);
135
151
  const mark = useCallback((action, ids) => {
136
152
  const selectedIds = Array.isArray(ids) ? ids : [];
137
153
  const selected = new Set(selectedIds);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micha.bigler/ui-core-micha",
3
- "version": "4.14.0",
3
+ "version": "4.15.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "exports": {