dhammawatthumpra commited on
Commit
df77603
Β·
1 Parent(s): 6149a4f

feat: support vertical and diagonal resizing for AI Chat popup and persist panel size

Browse files
webapp/tipitaka-web/src/components/ai/AIPopup.tsx CHANGED
@@ -37,23 +37,36 @@ const AIPopup: React.FC = () => {
37
  const [pos, setPos] = useState<{ x: number; y: number } | null>(null);
38
 
39
  // ── Resize state ────────────────────────────────────────────
40
- const [isResizing, setIsResizing] = useState(false);
41
  const resizeStartX = useRef(0);
 
42
  const resizeStartW = useRef(400);
43
- const { panelWidth, setPanelWidth } = useAIStore();
 
44
  const [width, setWidth] = useState(panelWidth);
 
45
 
46
- // Restore saved position + width on mount
47
  useEffect(() => {
48
  if (dragPos) setPos(dragPos);
 
49
  const savedW = localStorage.getItem('tipitaka-ai-panel-width');
50
  if (savedW) {
51
  const w = parseInt(savedW, 10);
52
- if (w >= 300 && w <= window.innerWidth * 0.8) {
53
  setWidth(w);
54
  setPanelWidth(w);
55
  }
56
  }
 
 
 
 
 
 
 
 
 
57
  }, []);
58
 
59
  // Disclaimer Logic: Show for 5s when opening
@@ -88,8 +101,8 @@ const AIPopup: React.FC = () => {
88
  const x = e.clientX - dragOffset.current.x;
89
  const y = e.clientY - dragOffset.current.y;
90
  // Clamp to viewport
91
- const clampedX = Math.max(0, Math.min(x, window.innerWidth - 320));
92
- const clampedY = Math.max(0, Math.min(y, window.innerHeight - 200));
93
  setPos({ x: clampedX, y: clampedY });
94
  };
95
 
@@ -104,43 +117,59 @@ const AIPopup: React.FC = () => {
104
  document.removeEventListener('mousemove', onMove);
105
  document.removeEventListener('mouseup', onUp);
106
  };
107
- }, [isDragging, pos, setDragPos]);
108
 
109
  // ── Resize handlers ─────────────────────────────────────────
110
- const handleResizeStart = useCallback((e: React.MouseEvent) => {
111
  if (window.innerWidth < 768) return;
112
  if (expandedIndex !== null) return;
113
  e.preventDefault();
114
  e.stopPropagation();
115
- setIsResizing(true);
116
  resizeStartX.current = e.clientX;
117
- resizeStartW.current = (panelRef.current?.getBoundingClientRect().width ?? width);
118
- }, [expandedIndex, width]);
 
 
 
 
 
119
 
120
  useEffect(() => {
121
- if (!isResizing) return;
 
122
  const onMove = (e: MouseEvent) => {
123
- const newW = Math.max(300, Math.min(resizeStartW.current + (e.clientX - resizeStartX.current), window.innerWidth * 0.8));
124
- setWidth(newW);
 
 
 
 
 
 
125
  };
 
126
  const onUp = () => {
127
- setIsResizing(false);
128
- setWidth(w => {
129
- setPanelWidth(w);
130
- return w;
131
- });
132
  };
 
133
  document.addEventListener('mousemove', onMove);
134
  document.addEventListener('mouseup', onUp);
135
- document.body.style.cursor = 'ew-resize';
 
 
136
  document.body.style.userSelect = 'none';
 
137
  return () => {
138
  document.removeEventListener('mousemove', onMove);
139
  document.removeEventListener('mouseup', onUp);
140
  document.body.style.cursor = '';
141
  document.body.style.userSelect = '';
142
  };
143
- }, [isResizing, setPanelWidth]);
144
 
145
  const s = PANEL_STYLES[theme] ?? PANEL_STYLES.dark;
146
 
@@ -273,24 +302,39 @@ const AIPopup: React.FC = () => {
273
  ${pos && window.innerWidth >= 768 ? '' : 'left-1/2 -translate-x-1/2 md:left-auto md:right-14 md:translate-x-0'}
274
  md:w-auto
275
  w-[calc(100vw-32px)]
276
- h-[80vh] md:h-[550px]
277
  ${isDragging ? 'cursor-grabbing select-none' : ''}
278
- ${isResizing ? 'select-none' : ''}
279
  ${expandedIndex !== null ? 'pointer-events-none opacity-0' : ''}
280
  transition-opacity duration-300
281
  `}
282
  style={{
283
  width: window.innerWidth >= 768 ? width : undefined,
 
284
  ...(pos && window.innerWidth >= 768 ? { left: pos.x, top: pos.y } : {}),
285
  }}
286
  >
287
- {/* ── Resize handle (right edge) ── */}
288
  {window.innerWidth >= 768 && (
289
- <div
290
- onMouseDown={handleResizeStart}
291
- className="absolute right-0 top-0 bottom-0 w-2 cursor-ew-resize hover:bg-[#c8860a]/20 active:bg-[#c8860a]/40 transition-colors rounded-r-2xl z-10"
292
- style={{ touchAction: 'none' }}
293
- />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  )}
295
  {/* ═══════════ Compact Header + Drag Handle ═══════════ */}
296
  <div
 
37
  const [pos, setPos] = useState<{ x: number; y: number } | null>(null);
38
 
39
  // ── Resize state ────────────────────────────────────────────
40
+ const [resizeDir, setResizeDir] = useState<'w' | 'h' | 'both' | null>(null);
41
  const resizeStartX = useRef(0);
42
+ const resizeStartY = useRef(0);
43
  const resizeStartW = useRef(400);
44
+ const resizeStartH = useRef(550);
45
+ const { panelWidth, panelHeight, setPanelWidth, setPanelHeight } = useAIStore();
46
  const [width, setWidth] = useState(panelWidth);
47
+ const [height, setHeight] = useState(panelHeight);
48
 
49
+ // Restore saved position + width/height on mount
50
  useEffect(() => {
51
  if (dragPos) setPos(dragPos);
52
+
53
  const savedW = localStorage.getItem('tipitaka-ai-panel-width');
54
  if (savedW) {
55
  const w = parseInt(savedW, 10);
56
+ if (w >= 300 && w <= window.innerWidth * 0.95) {
57
  setWidth(w);
58
  setPanelWidth(w);
59
  }
60
  }
61
+
62
+ const savedH = localStorage.getItem('tipitaka-ai-panel-height');
63
+ if (savedH) {
64
+ const h = parseInt(savedH, 10);
65
+ if (h >= 200 && h <= window.innerHeight * 0.9) {
66
+ setHeight(h);
67
+ setPanelHeight(h);
68
+ }
69
+ }
70
  }, []);
71
 
72
  // Disclaimer Logic: Show for 5s when opening
 
101
  const x = e.clientX - dragOffset.current.x;
102
  const y = e.clientY - dragOffset.current.y;
103
  // Clamp to viewport
104
+ const clampedX = Math.max(0, Math.min(x, window.innerWidth - width));
105
+ const clampedY = Math.max(0, Math.min(y, window.innerHeight - height));
106
  setPos({ x: clampedX, y: clampedY });
107
  };
108
 
 
117
  document.removeEventListener('mousemove', onMove);
118
  document.removeEventListener('mouseup', onUp);
119
  };
120
+ }, [isDragging, pos, setDragPos, width, height]);
121
 
122
  // ── Resize handlers ─────────────────────────────────────────
123
+ const handleResizeStart = useCallback((e: React.MouseEvent, dir: 'w' | 'h' | 'both') => {
124
  if (window.innerWidth < 768) return;
125
  if (expandedIndex !== null) return;
126
  e.preventDefault();
127
  e.stopPropagation();
128
+ setResizeDir(dir);
129
  resizeStartX.current = e.clientX;
130
+ resizeStartY.current = e.clientY;
131
+ const rect = panelRef.current?.getBoundingClientRect();
132
+ if (rect) {
133
+ resizeStartW.current = rect.width;
134
+ resizeStartH.current = rect.height;
135
+ }
136
+ }, [expandedIndex]);
137
 
138
  useEffect(() => {
139
+ if (!resizeDir) return;
140
+
141
  const onMove = (e: MouseEvent) => {
142
+ if (resizeDir === 'w' || resizeDir === 'both') {
143
+ const newW = Math.max(300, Math.min(resizeStartW.current + (e.clientX - resizeStartX.current), window.innerWidth * 0.9));
144
+ setWidth(newW);
145
+ }
146
+ if (resizeDir === 'h' || resizeDir === 'both') {
147
+ const newH = Math.max(200, Math.min(resizeStartH.current + (e.clientY - resizeStartY.current), window.innerHeight * 0.9));
148
+ setHeight(newH);
149
+ }
150
  };
151
+
152
  const onUp = () => {
153
+ const finalDir = resizeDir;
154
+ setResizeDir(null);
155
+ if (finalDir === 'w' || finalDir === 'both') setPanelWidth(width);
156
+ if (finalDir === 'h' || finalDir === 'both') setPanelHeight(height);
 
157
  };
158
+
159
  document.addEventListener('mousemove', onMove);
160
  document.addEventListener('mouseup', onUp);
161
+
162
+ const cursor = resizeDir === 'both' ? 'nwse-resize' : resizeDir === 'w' ? 'ew-resize' : 'ns-resize';
163
+ document.body.style.cursor = cursor;
164
  document.body.style.userSelect = 'none';
165
+
166
  return () => {
167
  document.removeEventListener('mousemove', onMove);
168
  document.removeEventListener('mouseup', onUp);
169
  document.body.style.cursor = '';
170
  document.body.style.userSelect = '';
171
  };
172
+ }, [resizeDir, width, height, setPanelWidth, setPanelHeight]);
173
 
174
  const s = PANEL_STYLES[theme] ?? PANEL_STYLES.dark;
175
 
 
302
  ${pos && window.innerWidth >= 768 ? '' : 'left-1/2 -translate-x-1/2 md:left-auto md:right-14 md:translate-x-0'}
303
  md:w-auto
304
  w-[calc(100vw-32px)]
305
+ h-[80vh] md:h-auto
306
  ${isDragging ? 'cursor-grabbing select-none' : ''}
307
+ ${resizeDir ? 'select-none' : ''}
308
  ${expandedIndex !== null ? 'pointer-events-none opacity-0' : ''}
309
  transition-opacity duration-300
310
  `}
311
  style={{
312
  width: window.innerWidth >= 768 ? width : undefined,
313
+ height: window.innerWidth >= 768 ? height : undefined,
314
  ...(pos && window.innerWidth >= 768 ? { left: pos.x, top: pos.y } : {}),
315
  }}
316
  >
317
+ {/* ── Resize handles (Desktop only) ── */}
318
  {window.innerWidth >= 768 && (
319
+ <>
320
+ {/* Right edge */}
321
+ <div
322
+ onMouseDown={(e) => handleResizeStart(e, 'w')}
323
+ className="absolute right-0 top-0 bottom-0 w-1.5 cursor-ew-resize hover:bg-[#c8860a]/20 active:bg-[#c8860a]/40 transition-colors z-10"
324
+ />
325
+ {/* Bottom edge */}
326
+ <div
327
+ onMouseDown={(e) => handleResizeStart(e, 'h')}
328
+ className="absolute left-0 right-0 bottom-0 h-1.5 cursor-ns-resize hover:bg-[#c8860a]/20 active:bg-[#c8860a]/40 transition-colors z-10"
329
+ />
330
+ {/* Corner (Bottom-right) */}
331
+ <div
332
+ onMouseDown={(e) => handleResizeStart(e, 'both')}
333
+ className="absolute right-0 bottom-0 w-4 h-4 cursor-nwse-resize z-20 flex items-end justify-end p-0.5"
334
+ >
335
+ <div className="w-1.5 h-1.5 rounded-full bg-[#c8860a]/30" />
336
+ </div>
337
+ </>
338
  )}
339
  {/* ═══════════ Compact Header + Drag Handle ═══════════ */}
340
  <div
webapp/tipitaka-web/src/stores/aiStore.ts CHANGED
@@ -17,6 +17,7 @@ interface AIState {
17
  ragLoading: boolean; // true when Qdrant exists but Reranker still loading
18
  dragPos: { x: number; y: number } | null; // saved drag position (desktop)
19
  panelWidth: number; // saved panel width (desktop)
 
20
 
21
  // Actions
22
  toggleOpen: () => void;
@@ -26,6 +27,7 @@ interface AIState {
26
  clearHistory: () => void;
27
  setDragPos: (pos: { x: number; y: number }) => void;
28
  setPanelWidth: (w: number) => void;
 
29
 
30
  // SSE Streaming logic
31
  askAI: (question: string, context: string) => Promise<void>;
@@ -43,6 +45,7 @@ export const useAIStore = create<AIState>((set, get) => ({
43
  ragLoading: false,
44
  dragPos: null,
45
  panelWidth: 400,
 
46
 
47
  toggleOpen: () => set((state) => ({ isOpen: !state.isOpen })),
48
  setMode: (mode) => set({ mode }),
@@ -60,6 +63,11 @@ export const useAIStore = create<AIState>((set, get) => ({
60
  try { localStorage.setItem('tipitaka-ai-panel-width', String(w)); } catch {}
61
  },
62
 
 
 
 
 
 
63
  checkRagStatus: async () => {
64
  set({ ragChecking: true });
65
  try {
 
17
  ragLoading: boolean; // true when Qdrant exists but Reranker still loading
18
  dragPos: { x: number; y: number } | null; // saved drag position (desktop)
19
  panelWidth: number; // saved panel width (desktop)
20
+ panelHeight: number; // saved panel height (desktop)
21
 
22
  // Actions
23
  toggleOpen: () => void;
 
27
  clearHistory: () => void;
28
  setDragPos: (pos: { x: number; y: number }) => void;
29
  setPanelWidth: (w: number) => void;
30
+ setPanelHeight: (h: number) => void;
31
 
32
  // SSE Streaming logic
33
  askAI: (question: string, context: string) => Promise<void>;
 
45
  ragLoading: false,
46
  dragPos: null,
47
  panelWidth: 400,
48
+ panelHeight: 550,
49
 
50
  toggleOpen: () => set((state) => ({ isOpen: !state.isOpen })),
51
  setMode: (mode) => set({ mode }),
 
63
  try { localStorage.setItem('tipitaka-ai-panel-width', String(w)); } catch {}
64
  },
65
 
66
+ setPanelHeight: (h) => {
67
+ set({ panelHeight: h });
68
+ try { localStorage.setItem('tipitaka-ai-panel-height', String(h)); } catch {}
69
+ },
70
+
71
  checkRagStatus: async () => {
72
  set({ ragChecking: true });
73
  try {