diff --git a/src/components/App.tsx b/src/components/App.tsx index 7fcdd249..956e2695 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -244,9 +244,7 @@ export class App extends React.Component { onRoomCreate={this.openPortal} onRoomDestroy={this.closePortal} onUsernameChange={(username) => { - if (this.portal.socket && this.portal.roomID && username) { - saveUsernameToLocalStorage(this.portal.roomID, username); - } + saveUsernameToLocalStorage(username); this.setState({ username, }); @@ -921,12 +919,12 @@ export class App extends React.Component { ); this.portal.socket!.on("init-room", () => { - if (this.portal.socket && this.portal.roomID) { - const username = restoreUsernameFromLocalStorage(this.portal.roomID); + if (this.portal.socket) { + const username = restoreUsernameFromLocalStorage(); this.portal.socket.emit("join-room", this.portal.roomID); - if (username) { + if (username !== null) { this.setState({ username, }); diff --git a/src/data/localStorage.ts b/src/data/localStorage.ts index d49b4767..2f8e807d 100644 --- a/src/data/localStorage.ts +++ b/src/data/localStorage.ts @@ -7,10 +7,10 @@ const LOCAL_STORAGE_KEY = "excalidraw"; const LOCAL_STORAGE_KEY_STATE = "excalidraw-state"; const LOCAL_STORAGE_KEY_COLLAB = "excalidraw-collab"; -export function saveUsernameToLocalStorage(id: string, username: string) { +export function saveUsernameToLocalStorage(username: string) { try { localStorage.setItem( - `${LOCAL_STORAGE_KEY_COLLAB}-${id}`, + LOCAL_STORAGE_KEY_COLLAB, JSON.stringify({ username }), ); } catch (error) { @@ -19,9 +19,9 @@ export function saveUsernameToLocalStorage(id: string, username: string) { } } -export function restoreUsernameFromLocalStorage(id: string): string | null { +export function restoreUsernameFromLocalStorage(): string | null { try { - const data = localStorage.getItem(`${LOCAL_STORAGE_KEY_COLLAB}-${id}`); + const data = localStorage.getItem(LOCAL_STORAGE_KEY_COLLAB); if (data) { return JSON.parse(data).username; }