Skip to content

Commit

Permalink
Global username instead of per room (excalidraw#1382)
Browse files Browse the repository at this point in the history
* global username

* remove string interpolation

Co-Authored-By: Lipis <[email protected]>

* remove string interpolation

Co-Authored-By: Lipis <[email protected]>

* remove backwards compat

Co-authored-by: Lipis <[email protected]>
  • Loading branch information
kbariotis and lipis authored Apr 11, 2020
1 parent 166ad04 commit 2adae41
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ export class App extends React.Component<any, AppState> {
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,
});
Expand Down Expand Up @@ -921,12 +919,12 @@ export class App extends React.Component<any, AppState> {
);

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,
});
Expand Down
8 changes: 4 additions & 4 deletions src/data/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 2adae41

Please sign in to comment.