@shellui/core 0.7.0-alpha.7 → 0.7.0-alpha.9

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shellui/core",
3
- "version": "0.7.0-alpha.7",
3
+ "version": "0.7.0-alpha.9",
4
4
  "description": "Shellui Core - Core React application runtime",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -73,7 +73,7 @@
73
73
  "workbox-routing": "^7.4.0",
74
74
  "workbox-strategies": "^7.4.0",
75
75
  "workbox-window": "^7.4.0",
76
- "@shellui/sdk": "0.7.0-alpha.7"
76
+ "@shellui/sdk": "0.7.0-alpha.9"
77
77
  },
78
78
  "peerDependencies": {
79
79
  "react": "^18.0.0 || ^19.0.0",
@@ -73,8 +73,10 @@ export const ContentView = ({
73
73
  const iframeRef = useRef<HTMLIFrameElement>(null);
74
74
  const cancelRevealRef = useRef<(() => void) | null>(null);
75
75
  const mountTimeRef = useRef(Date.now());
76
- /** Shell path last reported by the iframe — skip driving iframe when we caused the location change. */
76
+ /** Shell path last synced with the iframe. */
77
77
  const lastShellPathFromIframeRef = useRef<string | null>(null);
78
+ /** Iframe reports not rendered back yet: their echo must not drive the iframe. */
79
+ const pendingFromIframeRef = useRef<string[]>([]);
78
80
  const skipInitialShellSyncRef = useRef(true);
79
81
  const prevNavPathRef = useRef(navItem?.path ?? '');
80
82
 
@@ -119,9 +121,16 @@ export const ContentView = ({
119
121
  return;
120
122
  }
121
123
 
124
+ const echo = pendingFromIframeRef.current.indexOf(currentShellPath);
125
+ if (echo >= 0 && !navItemChanged) {
126
+ pendingFromIframeRef.current = pendingFromIframeRef.current.slice(echo + 1);
127
+ lastShellPathFromIframeRef.current = currentShellPath;
128
+ return;
129
+ }
122
130
  if (lastShellPathFromIframeRef.current === currentShellPath && !navItemChanged) {
123
131
  return;
124
132
  }
133
+ pendingFromIframeRef.current = [];
125
134
 
126
135
  lastShellPathFromIframeRef.current = currentShellPath;
127
136
  mountTimeRef.current = Date.now();
@@ -173,7 +182,7 @@ export const ContentView = ({
173
182
  return;
174
183
  }
175
184
 
176
- const { pathname, search, hash } = data.payload as ShellUIUrlPayload;
185
+ const { pathname, search, hash, replace } = data.payload as ShellUIUrlPayload;
177
186
  // Shell URL is always path + search only (no hash) so it's transparent whether the sub-app uses hash routing or not
178
187
  let pathSegment: string;
179
188
  if (isHashRouterNavItem(navItem) && hash) {
@@ -223,17 +232,19 @@ export const ContentView = ({
223
232
  const normalizedNewPathname = newPathParts?.[1]?.replace(/\/+$/, '') || '/';
224
233
  const normalizedNewPath = normalizedNewPathname + (newPathParts?.[2] || '');
225
234
 
226
- // Mark before navigate so the shell-sync effect does not reload the iframe
227
- lastShellPathFromIframeRef.current = normalizedNewPath;
228
-
229
235
  if (currentPath !== normalizedNewPath) {
230
236
  if (!isSafeShellPath(normalizedNewPath)) {
231
237
  logger.warn('ContentView: refused unsafe shell path from iframe', normalizedNewPath);
232
238
  return;
233
239
  }
234
- // Push — iframe pushState is replace-only when embedded, so the shell
235
- // is the sole owner of back/forward entries.
236
- navigate(newShellPath);
240
+ // Mark before navigate so the shell-sync effect does not drive the iframe back.
241
+ pendingFromIframeRef.current = [
242
+ ...pendingFromIframeRef.current.slice(-19),
243
+ normalizedNewPath,
244
+ ];
245
+ // Iframe pushState is replace-only when embedded, so the shell owns back and
246
+ // forward. A child redirect replaces, so it never adds an entry.
247
+ navigate(newShellPath, { replace: replace === true });
237
248
  }
238
249
  },
239
250
  );
@@ -39,6 +39,7 @@ import {
39
39
  type InFlightSlot,
40
40
  } from './utils';
41
41
  import { buildAuthUrlWithNext } from './utils/buildAuthUrlWithNext';
42
+ import { resolveSdkLoginRedirectPath } from './utils/resolveSdkLoginRedirectPath';
42
43
  import {
43
44
  beginDesktopOAuth,
44
45
  clearDesktopPkce,
@@ -942,7 +943,15 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
942
943
  return;
943
944
  }
944
945
 
945
- startOAuth(provider, payload.redirectPath || urls.login, payload.oauthClientId);
946
+ const currentPath =
947
+ typeof window !== 'undefined'
948
+ ? `${window.location.pathname}${window.location.search}`
949
+ : '/';
950
+ startOAuth(
951
+ provider,
952
+ resolveSdkLoginRedirectPath(payload.redirectPath, currentPath),
953
+ payload.oauthClientId,
954
+ );
946
955
  });
947
956
 
948
957
  return () => cleanup();
@@ -4,6 +4,7 @@ export {
4
4
  getShellUILoginDeviceId,
5
5
  } from './clientLoginContext';
6
6
  export { buildAuthUrlWithNext } from './buildAuthUrlWithNext';
7
+ export { resolveSdkLoginRedirectPath } from './resolveSdkLoginRedirectPath';
7
8
  export { getLoginCancelTarget, isPublicNavigationPage } from './loginCancelTarget';
8
9
  export {
9
10
  buildOAuthSessionRedirectTo,
@@ -0,0 +1,41 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import urls from '../../../constants/urls';
3
+ import { resolveSdkLoginRedirectPath } from './resolveSdkLoginRedirectPath';
4
+
5
+ describe('resolveSdkLoginRedirectPath', () => {
6
+ it('returns to the callback with the current page as next', () => {
7
+ expect(resolveSdkLoginRedirectPath(undefined, '/account')).toBe(
8
+ `${urls.loginCallback}?next=%2Faccount`,
9
+ );
10
+ expect(resolveSdkLoginRedirectPath('', '/files?tab=shared')).toBe(
11
+ `${urls.loginCallback}?next=%2Ffiles%3Ftab%3Dshared`,
12
+ );
13
+ });
14
+
15
+ it('uses redirectPath as the page after sign-in', () => {
16
+ expect(resolveSdkLoginRedirectPath('/dashboard', '/account')).toBe(
17
+ `${urls.loginCallback}?next=%2Fdashboard`,
18
+ );
19
+ });
20
+
21
+ it('keeps an explicit callback path', () => {
22
+ const callback = `${urls.loginCallback}?next=%2Fapps`;
23
+ expect(resolveSdkLoginRedirectPath(callback, '/account')).toBe(callback);
24
+ expect(resolveSdkLoginRedirectPath(urls.loginCallback, '/account')).toBe(urls.loginCallback);
25
+ });
26
+
27
+ it('falls back to the current page when redirectPath is the login page', () => {
28
+ expect(resolveSdkLoginRedirectPath(urls.login, '/account')).toBe(
29
+ `${urls.loginCallback}?next=%2Faccount`,
30
+ );
31
+ });
32
+
33
+ it('never sends people back to login or to another origin', () => {
34
+ expect(resolveSdkLoginRedirectPath(undefined, urls.login)).toBe(urls.loginCallback);
35
+ expect(resolveSdkLoginRedirectPath(undefined, '/')).toBe(urls.loginCallback);
36
+ expect(resolveSdkLoginRedirectPath('//evil.example.com', '/account')).toBe(
37
+ `${urls.loginCallback}?next=%2Faccount`,
38
+ );
39
+ expect(resolveSdkLoginRedirectPath('https://evil.example.com', '/')).toBe(urls.loginCallback);
40
+ });
41
+ });
@@ -0,0 +1,31 @@
1
+ import urls from '../../../constants/urls';
2
+ import { buildAuthUrlWithNext } from './buildAuthUrlWithNext';
3
+ import { normalizeNextPath } from './normalizeNextPath';
4
+
5
+ const isCallbackPath = (value: string) =>
6
+ value === urls.loginCallback || value.startsWith(`${urls.loginCallback}?`);
7
+
8
+ const pageAfterSignIn = (value: string | null | undefined): string | null => {
9
+ const next = normalizeNextPath(value?.trim() || null);
10
+ return next && !isCallbackPath(next) ? next : null;
11
+ };
12
+
13
+ /**
14
+ * Pick the `redirect_to` path for a `SHELLUI_LOGIN` request from an app.
15
+ *
16
+ * Only the callback route exchanges the OAuth code, so the request always
17
+ * returns there. `redirectPath` names the page to open after sign-in. With no
18
+ * usable `redirectPath`, people come back to the page they started from.
19
+ * An explicit callback path is kept as is.
20
+ */
21
+ export const resolveSdkLoginRedirectPath = (
22
+ redirectPath: string | null | undefined,
23
+ currentPath: string,
24
+ ): string => {
25
+ const requested = redirectPath?.trim();
26
+ if (requested && isCallbackPath(requested)) {
27
+ return requested;
28
+ }
29
+ const next = pageAfterSignIn(requested) ?? pageAfterSignIn(currentPath);
30
+ return buildAuthUrlWithNext(urls.loginCallback, next);
31
+ };
@@ -32,10 +32,12 @@ describe('filterNavigationForAuthState', () => {
32
32
  ]);
33
33
  });
34
34
 
35
- it('hides login and logged-in entries while the session restore is loading', () => {
35
+ it('hides login entries but keeps logged-in entries while the session restore is loading', () => {
36
36
  expect(
37
37
  paths(filterNavigationForAuthState([home, login, dashboard], false, false, true)),
38
- ).toEqual(['']);
39
- expect(filterNavigationForAuthState([accountGroup], false, false, true)).toEqual([]);
38
+ ).toEqual(['', 'dashboard']);
39
+ expect(paths(filterNavigationForAuthState([accountGroup], false, false, true))).toEqual([
40
+ 'dashboard',
41
+ ]);
40
42
  });
41
43
  });
@@ -227,7 +227,8 @@ export function hasLoginNavigationItem(navigation: (NavigationItem | NavigationG
227
227
  * Hide login navigation entries only when authenticated, so custom login entries
228
228
  * can still be used while signed out (e.g. open login in modal/drawer).
229
229
  * While the initial session restore runs (`isAuthLoading`), login entries are hidden too:
230
- * the account skeleton takes their place until the auth state is known.
230
+ * the account skeleton takes their place until the auth state is known. Logged-in only entries
231
+ * stay visible during the restore so the navigation does not pop in after a reload.
231
232
  */
232
233
  export function filterNavigationForAuthState(
233
234
  navigation: (NavigationItem | NavigationGroup)[],
@@ -242,7 +243,7 @@ export function filterNavigationForAuthState(
242
243
  if ('title' in item && 'items' in item) {
243
244
  const group = item as NavigationGroup;
244
245
  const visibleItems = group.items.filter((navItem) => {
245
- if (navItem.hideWhenLoggedOut && !isAuthenticated) return false;
246
+ if (navItem.hideWhenLoggedOut && !isAuthenticated && !isAuthLoading) return false;
246
247
  if (navItem.requiresDevMode && !isDevModeEnabled) return false;
247
248
  if (hideLoginEntries && isLoginNavigationUrl(navItem.url)) return false;
248
249
  return true;
@@ -251,7 +252,7 @@ export function filterNavigationForAuthState(
251
252
  return { ...group, items: visibleItems };
252
253
  }
253
254
  const navItem = item as NavigationItem;
254
- if (navItem.hideWhenLoggedOut && !isAuthenticated) return null;
255
+ if (navItem.hideWhenLoggedOut && !isAuthenticated && !isAuthLoading) return null;
255
256
  if (navItem.requiresDevMode && !isDevModeEnabled) return null;
256
257
  if (hideLoginEntries && isLoginNavigationUrl(navItem.url)) return null;
257
258
  return item;