feat(matrix): restore last selected chat on app load

Save the last selected room ID to localStorage and automatically
restore it when the app loads and sync is prepared.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Till-JS 2026-02-12 14:07:05 +01:00
parent 7a2acd4bbe
commit 027d3d2140

View file

@ -29,6 +29,7 @@ import type {
} from './types';
const STORAGE_KEY = 'matrix_credentials';
const LAST_ROOM_KEY = 'matrix_last_room';
/**
* Reactive Matrix store using Svelte 5 runes
@ -260,6 +261,14 @@ class MatrixStore {
if (state === 'PREPARED') {
this._rooms = this._client!.getRooms();
console.log(`Matrix sync prepared, ${this._rooms.length} rooms loaded`);
// Restore last selected room
if (browser && !this._currentRoomId) {
const lastRoomId = localStorage.getItem(LAST_ROOM_KEY);
if (lastRoomId && this._client!.getRoom(lastRoomId)) {
this.selectRoom(lastRoomId);
}
}
}
if (state === 'ERROR') {
@ -471,6 +480,11 @@ class MatrixStore {
if (lastEvent) {
this._client?.sendReadReceipt(lastEvent).catch(console.error);
}
// Save last room to localStorage
if (browser) {
localStorage.setItem(LAST_ROOM_KEY, roomId);
}
} else {
this._timeline = [];
}
@ -1684,6 +1698,7 @@ class MatrixStore {
this.destroy();
if (browser) {
localStorage.removeItem(STORAGE_KEY);
localStorage.removeItem(LAST_ROOM_KEY);
}
}