Skip to content

Commit 7e9519a

Browse files
committed
Chat and mermaid bug fix
1 parent 454a9bb commit 7e9519a

4 files changed

Lines changed: 71 additions & 30 deletions

File tree

frontend/src/components/Editor.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const insertChatBlockItem = (editor: BlockNoteEditor<any, any, any>) => ({
6262
props: {
6363
chatId: nanoid(),
6464
title: "Chat Discussion",
65-
minimized: false,
6665
height: 400,
6766
width: 600,
6867
}
@@ -88,7 +87,6 @@ const insertMermaidBlockItem = (editor: BlockNoteEditor<any, any, any>) => ({
8887
props: {
8988
diagramId: nanoid(),
9089
title: "Mermaid Diagram",
91-
minimized: false,
9290
height: 400,
9391
width: 600,
9492
}

frontend/src/components/chat/ChatBlock.tsx

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
saveFloatingState,
2121
clearFloatingState,
2222
getDefaultFloatingPosition,
23+
loadMinimizedState,
24+
saveMinimizedState,
2325
} from "../../lib/floatingChats";
2426

2527
// Context for sharing editor-level data with chat blocks
@@ -42,7 +44,6 @@ interface ChatBlockProps {
4244
props: {
4345
chatId: string;
4446
title: string;
45-
minimized: boolean;
4647
height: number;
4748
width: number;
4849
};
@@ -52,7 +53,10 @@ interface ChatBlockProps {
5253
export function ChatBlock({ block }: ChatBlockProps) {
5354
const context = useContext(EditorContext);
5455
const editor = useBlockNoteEditor();
55-
const [isMinimized, setIsMinimized] = useState(block.props.minimized);
56+
const chatId = block.props.chatId;
57+
58+
// Load minimized state from localStorage (local to each user)
59+
const [isMinimized, setIsMinimized] = useState(() => loadMinimizedState(chatId));
5660
const [height, setHeight] = useState(block.props.height || 400);
5761
const [width, setWidth] = useState(block.props.width || 600);
5862
const [isResizing, setIsResizing] = useState(false);
@@ -89,7 +93,6 @@ export function ChatBlock({ block }: ChatBlockProps) {
8993
}
9094

9195
const { doc, channel, userId, userName, userColor, presenceUsers } = context;
92-
const chatId = block.props.chatId;
9396

9497
// Use all chat hooks
9598
const { messages, sendMessage, addReaction } = useChat(
@@ -124,11 +127,6 @@ export function ChatBlock({ block }: ChatBlockProps) {
124127
}
125128
}, [chatId]);
126129

127-
// Sync minimized state from block props
128-
useEffect(() => {
129-
setIsMinimized(block.props.minimized);
130-
}, [block.props.minimized]);
131-
132130
// Toggle floating handler
133131
const handleToggleFloating = useCallback(() => {
134132
if (isFloating) {
@@ -524,12 +522,8 @@ export function ChatBlock({ block }: ChatBlockProps) {
524522
const newMinimizedState = !isMinimized;
525523
setIsMinimized(newMinimizedState);
526524

527-
// Persist minimized state to block props
528-
if (editor) {
529-
editor.updateBlock(block.id, {
530-
props: { ...block.props, minimized: newMinimizedState },
531-
});
532-
}
525+
// Persist minimized state to localStorage (local, not synced)
526+
saveMinimizedState(chatId, newMinimizedState);
533527
}}
534528
style={{
535529
width: "24px",

frontend/src/components/mermaid/MermaidBlock.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useBlockNoteEditor } from '@blocknote/react';
1515
import { useMermaid } from '../../hooks/useMermaid';
1616
import { MermaidCanvas } from './MermaidCanvas';
1717
import { MermaidCodeEditor } from './MermaidCodeEditor';
18+
import { loadMinimizedState, saveMinimizedState } from '../../lib/floatingChats';
1819

1920
interface MermaidBlockProps {
2021
block: {
@@ -23,7 +24,6 @@ interface MermaidBlockProps {
2324
props: {
2425
diagramId: string;
2526
title: string;
26-
minimized: boolean;
2727
height: number;
2828
width: number;
2929
};
@@ -33,8 +33,10 @@ interface MermaidBlockProps {
3333
export function MermaidBlock({ block }: MermaidBlockProps) {
3434
const context = useContext(EditorContext);
3535
const editor = useBlockNoteEditor();
36+
const diagramId = block.props.diagramId;
3637

37-
const [isMinimized, setIsMinimized] = useState(block.props.minimized);
38+
// Load minimized state from localStorage (local to each user)
39+
const [isMinimized, setIsMinimized] = useState(() => loadMinimizedState(diagramId));
3840
const [isEditMode, setIsEditMode] = useState(true); // Start in edit mode for new blocks
3941
const [height, setHeight] = useState(block.props.height || 400);
4042
const [width, setWidth] = useState(block.props.width || 600);
@@ -65,7 +67,6 @@ export function MermaidBlock({ block }: MermaidBlockProps) {
6567
}
6668

6769
const { doc } = context;
68-
const diagramId = block.props.diagramId;
6970

7071
const { yCode, isInitialized } = useMermaid(doc, diagramId);
7172

@@ -91,11 +92,6 @@ export function MermaidBlock({ block }: MermaidBlockProps) {
9192
};
9293
}, [yCode]);
9394

94-
// Sync minimized state from block props
95-
useEffect(() => {
96-
setIsMinimized(block.props.minimized);
97-
}, [block.props.minimized]);
98-
9995

10096
// Prevent BlockNote from handling keyboard events in the textarea
10197
const handleTextareaKeyDown = useCallback((e: React.KeyboardEvent<HTMLTextAreaElement>) => {
@@ -316,12 +312,8 @@ export function MermaidBlock({ block }: MermaidBlockProps) {
316312
const newMinimizedState = !isMinimized;
317313
setIsMinimized(newMinimizedState);
318314

319-
// Persist minimized state to block props
320-
if (editor) {
321-
editor.updateBlock(block.id, {
322-
props: { ...block.props, minimized: newMinimizedState },
323-
});
324-
}
315+
// Persist minimized state to localStorage (local, not synced)
316+
saveMinimizedState(diagramId, newMinimizedState);
325317
}}
326318
style={{
327319
width: '24px',

frontend/src/lib/floatingChats.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Utilities for persisting floating chat window state in localStorage.
55
* Handles position validation, off-screen detection, and state persistence.
6+
* Also stores minimized state per-user (local, not synced).
67
*/
78

89
export interface FloatingChatState {
@@ -12,9 +13,11 @@ export interface FloatingChatState {
1213
width: number;
1314
height: number;
1415
zIndex?: number;
16+
isMinimized?: boolean; // Local minimized state (not synced)
1517
}
1618

1719
const STORAGE_KEY = "markdoc-floating-chats";
20+
const MINIMIZED_STORAGE_KEY = "markdoc-minimized-blocks";
1821

1922
/**
2023
* Load floating state for a specific chat from localStorage
@@ -93,3 +96,57 @@ export function getDefaultFloatingPosition(width: number, height: number): { x:
9396
y: Math.max(0, (window.innerHeight - height) / 2),
9497
};
9598
}
99+
100+
/**
101+
* Load minimized state for a specific block from localStorage
102+
* This is stored separately and locally per-user (not synced)
103+
*/
104+
export function loadMinimizedState(blockId: string): boolean {
105+
try {
106+
const stored = localStorage.getItem(MINIMIZED_STORAGE_KEY);
107+
if (!stored) return false;
108+
109+
const allStates: Record<string, boolean> = JSON.parse(stored);
110+
return allStates[blockId] || false;
111+
} catch (error) {
112+
console.error("Error loading minimized state:", error);
113+
return false;
114+
}
115+
}
116+
117+
/**
118+
* Save minimized state for a specific block to localStorage
119+
* This is stored separately and locally per-user (not synced)
120+
*/
121+
export function saveMinimizedState(blockId: string, isMinimized: boolean): void {
122+
try {
123+
const stored = localStorage.getItem(MINIMIZED_STORAGE_KEY);
124+
const allStates: Record<string, boolean> = stored ? JSON.parse(stored) : {};
125+
126+
if (isMinimized) {
127+
allStates[blockId] = true;
128+
} else {
129+
delete allStates[blockId]; // Remove entry when expanded
130+
}
131+
132+
localStorage.setItem(MINIMIZED_STORAGE_KEY, JSON.stringify(allStates));
133+
} catch (error) {
134+
console.error("Error saving minimized state:", error);
135+
}
136+
}
137+
138+
/**
139+
* Clear minimized state for a specific block from localStorage
140+
*/
141+
export function clearMinimizedState(blockId: string): void {
142+
try {
143+
const stored = localStorage.getItem(MINIMIZED_STORAGE_KEY);
144+
if (!stored) return;
145+
146+
const allStates: Record<string, boolean> = JSON.parse(stored);
147+
delete allStates[blockId];
148+
localStorage.setItem(MINIMIZED_STORAGE_KEY, JSON.stringify(allStates));
149+
} catch (error) {
150+
console.error("Error clearing minimized state:", error);
151+
}
152+
}

0 commit comments

Comments
 (0)