Skip to content

Commit 4034d37

Browse files
committed
feat(game): add layout item save functionality on close and move events
1 parent fe452ca commit 4034d37

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

electron/renderer/pages/game.tsx

+58-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { GameEventType } from '../../common/game/types.js';
1414
import type { Layout } from '../../common/layout/types.js';
1515
import { GameContainer } from '../components/game/game-container.jsx';
1616
import { GameStream } from '../components/game/game-stream.jsx';
17-
import { useLoadedLayout } from '../hooks/layouts.jsx';
17+
import { useLoadedLayout, useSaveLayout } from '../hooks/layouts.jsx';
18+
import { useLogger } from '../hooks/logger.jsx';
1819
import { useSubscribe } from '../hooks/pubsub.jsx';
1920
import { useTheme } from '../hooks/theme.jsx';
2021
import type { GameLogLine } from '../types/game.types.js';
@@ -23,11 +24,15 @@ import type {
2324
GridItemContent,
2425
GridItemInfo,
2526
} from '../types/grid.types.js';
27+
import type { PubSubEvent } from '../types/pubsub.types.js';
2628

2729
const GamePage: React.FC = (): ReactNode => {
2830
const { colorMode } = useTheme();
2931

30-
const layout = useLoadedLayout();
32+
const logger = useLogger('renderer:page:game');
33+
34+
const { layoutName, layout } = useLoadedLayout();
35+
const saveLayout = useSaveLayout();
3136

3237
// I started tracking these via `useState` but when calling their setter
3338
// the value did not update fast enough before a text game event
@@ -184,6 +189,57 @@ const GamePage: React.FC = (): ReactNode => {
184189
});
185190
});
186191

192+
useSubscribe(
193+
'layout:item:closed',
194+
async (event: PubSubEvent.LayoutItemClosed) => {
195+
const { itemId } = event;
196+
197+
logger.debug('layout item closed, saving layout', event);
198+
199+
if (layout?.items) {
200+
const streamLayout = layout.items.find((streamLayout) => {
201+
return streamLayout.id === itemId;
202+
});
203+
204+
if (streamLayout) {
205+
streamLayout.visible = false;
206+
}
207+
208+
await saveLayout({
209+
layoutName,
210+
layout,
211+
});
212+
}
213+
}
214+
);
215+
216+
useSubscribe(
217+
'layout:item:moved',
218+
async (event: PubSubEvent.LayoutItemMoved) => {
219+
const { itemId, x, y, width, height } = event;
220+
221+
logger.debug('layout item moved, saving layout', event);
222+
223+
if (layout?.items) {
224+
const streamLayout = layout.items.find((streamLayout) => {
225+
return streamLayout.id === itemId;
226+
});
227+
228+
if (streamLayout) {
229+
streamLayout.x = x;
230+
streamLayout.y = y;
231+
streamLayout.width = width;
232+
streamLayout.height = height;
233+
}
234+
235+
await saveLayout({
236+
layoutName,
237+
layout,
238+
});
239+
}
240+
}
241+
);
242+
187243
const contentItems = useMemo<Array<GridItemContent>>(() => {
188244
if (!layout) {
189245
// On initial render, the layout won't be loaded yet, skip

0 commit comments

Comments
 (0)