From 2adae4132bc14da7c84b0a9c55bc19fb0bfa477c Mon Sep 17 00:00:00 2001 From: Kostas Bariotis Date: Sat, 11 Apr 2020 21:23:12 +0100 Subject: [PATCH] Global username instead of per room (#1382) * global username * remove string interpolation Co-Authored-By: Lipis * remove string interpolation Co-Authored-By: Lipis * remove backwards compat Co-authored-by: Lipis --- src/components/App.tsx | 10 ++++------ src/data/localStorage.ts | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) 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; }