@sybilion/uilib 1.4.30 → 1.4.32

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.
@@ -5,6 +5,7 @@ import { isGraphIntakeAssistantStepComplete, matchUserTextToQuickReply, isPreset
5
5
  import { loadingLabelFromSendPayload, displayTextFromSendPayload, buildChatSendMessagePayload } from '../buildChatSendMessagePayload.js';
6
6
  import { usedPresetIdsFromMessages } from '../chat-preset-utils.js';
7
7
  import { useChatsForScopeId, useChat, useChatOutboundPending, useSyncChatPanelBusy, isChatEmpty } from '../../../../contexts/chat-context.js';
8
+ import { loadScopeIndex } from '../../../../contexts/chatSessionStorage.js';
8
9
  import { shellFitsSidebarsLayout } from '../../../../hooks/panelWidth.js';
9
10
  import useEvent from '../../../../hooks/useEvent.js';
10
11
  import { useIsMobile } from '../../../../hooks/useIsMobile.js';
@@ -60,7 +61,7 @@ function useChatPanelChromeModel({ embedAsPage, presets, scopeId, onMessage, onS
60
61
  const { setOpen: setSidebarNavOpen, setChatWidthPx, getShellWidth, setChatPanelOpen, } = useSidebarActions();
61
62
  const [localUiBusy, setLocalUiBusy] = useState(false);
62
63
  const [outboundLoadingLabel, setOutboundLoadingLabel] = useState();
63
- const { chats, currentChatId, setCurrentChatId, newChat, sendMessage, addMessage, removeMessageById, } = useChatsForScopeId(effectiveScopeId);
64
+ const { chats, currentChatId, setCurrentChatId, newChat, linkSessionToScope, sendMessage, addMessage, removeMessageById, } = useChatsForScopeId(effectiveScopeId);
64
65
  const chat = useChat(effectiveScopeId, currentChatId);
65
66
  const isOutboundPending = useChatOutboundPending(effectiveScopeId, currentChatId);
66
67
  const isLoading = isOutboundPending || localUiBusy;
@@ -90,11 +91,24 @@ function useChatPanelChromeModel({ embedAsPage, presets, scopeId, onMessage, onS
90
91
  return;
91
92
  if (chats.length > 0) {
92
93
  setCurrentChatId(chats[0].session_id);
94
+ return;
93
95
  }
94
- else {
95
- newChat();
96
+ const index = loadScopeIndex(effectiveScopeId);
97
+ const persistedSessionId = index?.currentSessionId ?? index?.sessionIds[0] ?? null;
98
+ if (persistedSessionId) {
99
+ linkSessionToScope(persistedSessionId);
100
+ return;
96
101
  }
97
- }, [panelActive, currentChatId, chats, setCurrentChatId, newChat]);
102
+ newChat();
103
+ }, [
104
+ panelActive,
105
+ currentChatId,
106
+ chats,
107
+ effectiveScopeId,
108
+ setCurrentChatId,
109
+ linkSessionToScope,
110
+ newChat,
111
+ ]);
98
112
  const [scriptByChatId, setScriptByChatId] = useState({});
99
113
  /** Local demo: `[Label]` in assistant text → next assistant body from map. */
100
114
  const [quickReplyBranchesByChat, setQuickReplyBranchesByChat] = useState({});
@@ -18,6 +18,10 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
18
18
  const iframeRef = useRef(null);
19
19
  const containerRef = useRef(null);
20
20
  const playerRef = useRef(null);
21
+ // Generation token: incremented on every [id, hash] change so that stale
22
+ // async destroys / ready callbacks from a previous video can't clobber the
23
+ // current player ref.
24
+ const generationRef = useRef(0);
21
25
  const [isPlaying, setIsPlaying] = useState(false);
22
26
  const [isPlayerReady, setIsPlayerReady] = useState(false);
23
27
  // Intrinsic video aspect ratio (width / height) reported by the Vimeo SDK.
@@ -35,14 +39,23 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
35
39
  // Cleanup player when id/hash changes
36
40
  useEffect(() => {
37
41
  setAspectRatio(null);
38
- if (playerRef.current) {
39
- playerRef.current
42
+ generationRef.current += 1;
43
+ const token = generationRef.current;
44
+ const previousPlayer = playerRef.current;
45
+ playerRef.current = null;
46
+ setIsPlayerReady(false);
47
+ setIsPlaying(false);
48
+ if (previousPlayer) {
49
+ previousPlayer
40
50
  .destroy()
41
51
  .catch(() => {
42
52
  // Ignore destroy errors
43
53
  })
44
54
  .finally(() => {
45
- playerRef.current = null;
55
+ if (generationRef.current !== token)
56
+ return;
57
+ if (playerRef.current)
58
+ return;
46
59
  setIsPlayerReady(false);
47
60
  setIsPlaying(false);
48
61
  });
@@ -52,46 +65,60 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
52
65
  const handleIframeLoad = useCallback(() => {
53
66
  if (!iframeRef.current)
54
67
  return;
55
- // Clean up existing player if any
56
- if (playerRef.current) {
57
- playerRef.current
58
- .destroy()
59
- .catch(() => {
60
- // Ignore destroy errors
61
- })
62
- .finally(() => {
63
- playerRef.current = null;
64
- });
68
+ const token = generationRef.current;
69
+ const previousPlayer = playerRef.current;
70
+ playerRef.current = null;
71
+ if (previousPlayer) {
72
+ previousPlayer.destroy().catch(() => { });
65
73
  }
66
74
  try {
67
75
  const player = new Player(iframeRef.current);
76
+ if (generationRef.current !== token) {
77
+ player.destroy().catch(() => { });
78
+ return;
79
+ }
68
80
  playerRef.current = player;
69
- // Wait for player to be ready before setting up events
70
81
  player
71
82
  .ready()
72
83
  .then(() => {
84
+ if (generationRef.current !== token) {
85
+ if (playerRef.current === player)
86
+ playerRef.current = null;
87
+ player.destroy().catch(() => { });
88
+ return;
89
+ }
73
90
  setIsPlayerReady(true);
74
- // Read the video's intrinsic dimensions so the container can adopt
75
- // the exact aspect ratio — eliminates letterbox stripes from the
76
- // hardcoded box ratio mismatching the source video.
77
91
  Promise.all([player.getVideoWidth(), player.getVideoHeight()])
78
92
  .then(([w, h]) => {
93
+ if (generationRef.current !== token)
94
+ return;
79
95
  if (w > 0 && h > 0)
80
96
  setAspectRatio(w / h);
81
97
  })
82
98
  .catch(() => {
83
99
  // Ignore dimension errors; CSS fallback ratio stays in effect
84
100
  });
85
- // Listen to SDK events
86
- player.on('play', () => setIsPlaying(true));
87
- player.on('pause', () => setIsPlaying(false));
88
- player.on('ended', () => setIsPlaying(false));
101
+ player.on('play', () => {
102
+ if (generationRef.current !== token)
103
+ return;
104
+ setIsPlaying(true);
105
+ });
106
+ player.on('pause', () => {
107
+ if (generationRef.current !== token)
108
+ return;
109
+ setIsPlaying(false);
110
+ });
111
+ player.on('ended', () => {
112
+ if (generationRef.current !== token)
113
+ return;
114
+ setIsPlaying(false);
115
+ });
89
116
  })
90
117
  .catch(() => {
91
118
  // Ignore ready errors
92
119
  });
93
120
  }
94
- catch (error) {
121
+ catch {
95
122
  // Ignore initialization errors
96
123
  }
97
124
  }, []);
@@ -146,7 +173,7 @@ function VimeoEmbed({ videoId, videoHash, gifMode = false, playButtonColor, clas
146
173
  observer.disconnect();
147
174
  };
148
175
  }, [play, pause]);
149
- return (jsxs("div", { ref: containerRef, className: cn(S.root, className, isPlaying && S.isPlaying), style: aspectRatio ? { aspectRatio: String(aspectRatio) } : undefined, ...props, children: [jsx("iframe", { ref: iframeRef, src: embedUrl, className: S.iframe, allow: "autoplay; fullscreen; picture-in-picture; encrypted-media", allowFullScreen: true, title: "Vimeo video player", onLoad: handleIframeLoad }), !gifMode && (jsx("button", { type: "button", className: S.playButton, onClick: play, "aria-label": "Play video", children: jsx("svg", { className: S.playIcon, viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: jsx("path", { d: "M8 5v14l11-7z", fill: "currentColor" }) }) }))] }));
176
+ return (jsxs("div", { ref: containerRef, className: cn(S.root, className, isPlaying && S.isPlaying), style: aspectRatio ? { aspectRatio: String(aspectRatio) } : undefined, ...props, children: [jsx("iframe", { ref: iframeRef, src: embedUrl, className: S.iframe, allow: "autoplay; fullscreen; picture-in-picture; encrypted-media", allowFullScreen: true, title: "Vimeo video player", onLoad: handleIframeLoad }, embedUrl), !gifMode && (jsx("button", { type: "button", className: S.playButton, onClick: play, "aria-label": "Play video", children: jsx("svg", { className: S.playIcon, viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: jsx("path", { d: "M8 5v14l11-7z", fill: "currentColor" }) }) }))] }));
150
177
  }
151
178
 
152
179
  export { VimeoEmbed };
@@ -1,5 +1,5 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
- import { createContext, useState, useCallback, useEffect, useContext, useMemo } from 'react';
2
+ import { createContext, useState, useCallback, useLayoutEffect, useContext, useMemo, useEffect } from 'react';
3
3
  import { MessageRole } from '../components/ui/Chat/Chat.types.js';
4
4
  import { normalizeUserTextFileAttachments } from '../components/ui/Chat/buildChatSendMessagePayload.js';
5
5
  import { normalizeJsonDashboardChatText, stripJsonDashboardFences } from '../lib/dashboard-spec/jsonDashboardFence.js';
@@ -363,7 +363,9 @@ function ChatProvider({ children, userSwitchKey, sendChatMessage: sendChatMessag
363
363
  setCurrentChatId,
364
364
  userSwitchKey,
365
365
  ]);
366
- useEffect(() => {
366
+ // Layout effect so LS hydration finishes before child `useEffect` hooks that
367
+ // may call `newChat()` when `?chat=open` opens the panel on first mount.
368
+ useLayoutEffect(() => {
367
369
  if (userSwitchKey === null) {
368
370
  setChats({});
369
371
  setCurrentChatIdState({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sybilion/uilib",
3
- "version": "1.4.30",
3
+ "version": "1.4.32",
4
4
  "description": "Sybilion Design System — React UI components (Webpack + Stylus)",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -40,6 +40,7 @@ import {
40
40
  useChatsForScopeId,
41
41
  useSyncChatPanelBusy,
42
42
  } from '#uilib/contexts/chat-context';
43
+ import { loadScopeIndex } from '#uilib/contexts/chatSessionStorage';
43
44
  import { shellFitsSidebarsLayout } from '#uilib/hooks/panelWidth';
44
45
  import useEvent from '#uilib/hooks/useEvent';
45
46
  import { useIsMobile } from '#uilib/hooks/useIsMobile';
@@ -204,6 +205,7 @@ export function useChatPanelChromeModel({
204
205
  currentChatId,
205
206
  setCurrentChatId,
206
207
  newChat,
208
+ linkSessionToScope,
207
209
  sendMessage,
208
210
  addMessage,
209
211
  removeMessageById,
@@ -253,10 +255,27 @@ export function useChatPanelChromeModel({
253
255
 
254
256
  if (chats.length > 0) {
255
257
  setCurrentChatId(chats[0].session_id);
256
- } else {
257
- newChat();
258
+ return;
258
259
  }
259
- }, [panelActive, currentChatId, chats, setCurrentChatId, newChat]);
260
+
261
+ const index = loadScopeIndex(effectiveScopeId);
262
+ const persistedSessionId =
263
+ index?.currentSessionId ?? index?.sessionIds[0] ?? null;
264
+ if (persistedSessionId) {
265
+ linkSessionToScope(persistedSessionId);
266
+ return;
267
+ }
268
+
269
+ newChat();
270
+ }, [
271
+ panelActive,
272
+ currentChatId,
273
+ chats,
274
+ effectiveScopeId,
275
+ setCurrentChatId,
276
+ linkSessionToScope,
277
+ newChat,
278
+ ]);
260
279
  const [scriptByChatId, setScriptByChatId] = useState<
261
280
  Record<string, PresetScriptState>
262
281
  >({});
@@ -30,6 +30,10 @@ function VimeoEmbed({
30
30
  const iframeRef = useRef<HTMLIFrameElement>(null);
31
31
  const containerRef = useRef<HTMLDivElement>(null);
32
32
  const playerRef = useRef<Player | null>(null);
33
+ // Generation token: incremented on every [id, hash] change so that stale
34
+ // async destroys / ready callbacks from a previous video can't clobber the
35
+ // current player ref.
36
+ const generationRef = useRef(0);
33
37
  const [isPlaying, setIsPlaying] = useState(false);
34
38
  const [isPlayerReady, setIsPlayerReady] = useState(false);
35
39
  // Intrinsic video aspect ratio (width / height) reported by the Vimeo SDK.
@@ -50,14 +54,23 @@ function VimeoEmbed({
50
54
  // Cleanup player when id/hash changes
51
55
  useEffect(() => {
52
56
  setAspectRatio(null);
53
- if (playerRef.current) {
54
- playerRef.current
57
+ generationRef.current += 1;
58
+ const token = generationRef.current;
59
+
60
+ const previousPlayer = playerRef.current;
61
+ playerRef.current = null;
62
+ setIsPlayerReady(false);
63
+ setIsPlaying(false);
64
+
65
+ if (previousPlayer) {
66
+ previousPlayer
55
67
  .destroy()
56
68
  .catch(() => {
57
69
  // Ignore destroy errors
58
70
  })
59
71
  .finally(() => {
60
- playerRef.current = null;
72
+ if (generationRef.current !== token) return;
73
+ if (playerRef.current) return;
61
74
  setIsPlayerReady(false);
62
75
  setIsPlaying(false);
63
76
  });
@@ -68,49 +81,61 @@ function VimeoEmbed({
68
81
  const handleIframeLoad = useCallback(() => {
69
82
  if (!iframeRef.current) return;
70
83
 
71
- // Clean up existing player if any
72
- if (playerRef.current) {
73
- playerRef.current
74
- .destroy()
75
- .catch(() => {
76
- // Ignore destroy errors
77
- })
78
- .finally(() => {
79
- playerRef.current = null;
80
- });
84
+ const token = generationRef.current;
85
+
86
+ const previousPlayer = playerRef.current;
87
+ playerRef.current = null;
88
+ if (previousPlayer) {
89
+ previousPlayer.destroy().catch(() => {});
81
90
  }
82
91
 
83
92
  try {
84
93
  const player = new Player(iframeRef.current);
85
94
 
95
+ if (generationRef.current !== token) {
96
+ player.destroy().catch(() => {});
97
+ return;
98
+ }
99
+
86
100
  playerRef.current = player;
87
101
 
88
- // Wait for player to be ready before setting up events
89
102
  player
90
103
  .ready()
91
104
  .then(() => {
105
+ if (generationRef.current !== token) {
106
+ if (playerRef.current === player) playerRef.current = null;
107
+ player.destroy().catch(() => {});
108
+ return;
109
+ }
110
+
92
111
  setIsPlayerReady(true);
93
112
 
94
- // Read the video's intrinsic dimensions so the container can adopt
95
- // the exact aspect ratio — eliminates letterbox stripes from the
96
- // hardcoded box ratio mismatching the source video.
97
113
  Promise.all([player.getVideoWidth(), player.getVideoHeight()])
98
114
  .then(([w, h]) => {
115
+ if (generationRef.current !== token) return;
99
116
  if (w > 0 && h > 0) setAspectRatio(w / h);
100
117
  })
101
118
  .catch(() => {
102
119
  // Ignore dimension errors; CSS fallback ratio stays in effect
103
120
  });
104
121
 
105
- // Listen to SDK events
106
- player.on('play', () => setIsPlaying(true));
107
- player.on('pause', () => setIsPlaying(false));
108
- player.on('ended', () => setIsPlaying(false));
122
+ player.on('play', () => {
123
+ if (generationRef.current !== token) return;
124
+ setIsPlaying(true);
125
+ });
126
+ player.on('pause', () => {
127
+ if (generationRef.current !== token) return;
128
+ setIsPlaying(false);
129
+ });
130
+ player.on('ended', () => {
131
+ if (generationRef.current !== token) return;
132
+ setIsPlaying(false);
133
+ });
109
134
  })
110
135
  .catch(() => {
111
136
  // Ignore ready errors
112
137
  });
113
- } catch (error) {
138
+ } catch {
114
139
  // Ignore initialization errors
115
140
  }
116
141
  }, []);
@@ -175,6 +200,7 @@ function VimeoEmbed({
175
200
  {...props}
176
201
  >
177
202
  <iframe
203
+ key={embedUrl}
178
204
  ref={iframeRef}
179
205
  src={embedUrl}
180
206
  className={S.iframe}
@@ -4,6 +4,7 @@ import {
4
4
  useCallback,
5
5
  useContext,
6
6
  useEffect,
7
+ useLayoutEffect,
7
8
  useMemo,
8
9
  useState,
9
10
  } from 'react';
@@ -651,7 +652,9 @@ export function ChatProvider({
651
652
  ],
652
653
  );
653
654
 
654
- useEffect(() => {
655
+ // Layout effect so LS hydration finishes before child `useEffect` hooks that
656
+ // may call `newChat()` when `?chat=open` opens the panel on first mount.
657
+ useLayoutEffect(() => {
655
658
  if (userSwitchKey === null) {
656
659
  setChats({});
657
660
  setCurrentChatIdState({});