feat: store userId in localStorage

This commit is contained in:
Arnošt Pleskot 2023-07-12 17:22:46 +02:00
parent 2bdf09153c
commit db5149ab5d
No known key found for this signature in database
3 changed files with 31 additions and 11 deletions

View File

@ -47,8 +47,8 @@ import {
saveToFirebase, saveToFirebase,
} from "../data/firebase"; } from "../data/firebase";
import { import {
importUsernameFromLocalStorage, importUsernameAndIdFromLocalStorage,
saveUsernameToLocalStorage, saveUsernameAndIdToLocalStorage,
} from "../data/localStorage"; } from "../data/localStorage";
import Portal from "./Portal"; import Portal from "./Portal";
import RoomDialog from "./RoomDialog"; import RoomDialog from "./RoomDialog";
@ -124,11 +124,14 @@ class Collab extends PureComponent<Props, CollabState> {
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
const { username, userId } = importUsernameAndIdFromLocalStorage() || {};
this.state = { this.state = {
errorMessage: "", errorMessage: "",
username: importUsernameFromLocalStorage() || "", username: username || "",
userId: userId || "",
activeRoomLink: "", activeRoomLink: "",
userId: nanoid(),
}; };
this.portal = new Portal(this); this.portal = new Portal(this);
this.fileManager = new FileManager({ this.fileManager = new FileManager({
@ -524,6 +527,11 @@ class Collab extends PureComponent<Props, CollabState> {
}); });
} }
if (!this.state.userId) {
const userId = nanoid();
this.onUserIdChange(userId);
}
if (this.portal.socket) { if (this.portal.socket) {
return null; return null;
} }
@ -970,7 +978,12 @@ class Collab extends PureComponent<Props, CollabState> {
onUsernameChange = (username: string) => { onUsernameChange = (username: string) => {
this.setUsername(username); this.setUsername(username);
saveUsernameToLocalStorage(username); saveUsernameAndIdToLocalStorage(username, this.state.userId);
};
onUserIdChange = (userId: string) => {
this.setState({ userId });
saveUsernameAndIdToLocalStorage(this.state.username, userId);
}; };
render() { render() {

View File

@ -8,11 +8,14 @@ import { clearElementsForLocalStorage } from "../../element";
import { STORAGE_KEYS } from "../app_constants"; import { STORAGE_KEYS } from "../app_constants";
import { ImportedDataState } from "../../data/types"; import { ImportedDataState } from "../../data/types";
export const saveUsernameToLocalStorage = (username: string) => { export const saveUsernameAndIdToLocalStorage = (
username: string,
userId: string,
) => {
try { try {
localStorage.setItem( localStorage.setItem(
STORAGE_KEYS.LOCAL_STORAGE_COLLAB, STORAGE_KEYS.LOCAL_STORAGE_COLLAB,
JSON.stringify({ username }), JSON.stringify({ username, userId }),
); );
} catch (error: any) { } catch (error: any) {
// Unable to access window.localStorage // Unable to access window.localStorage
@ -20,11 +23,14 @@ export const saveUsernameToLocalStorage = (username: string) => {
} }
}; };
export const importUsernameFromLocalStorage = (): string | null => { export const importUsernameAndIdFromLocalStorage = (): {
username: string;
userId: string;
} | null => {
try { try {
const data = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB); const data = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
if (data) { if (data) {
return JSON.parse(data).username; return JSON.parse(data);
} }
} catch (error: any) { } catch (error: any) {
// Unable to access localStorage // Unable to access localStorage

View File

@ -65,7 +65,7 @@ import {
import { import {
getLibraryItemsFromStorage, getLibraryItemsFromStorage,
importFromLocalStorage, importFromLocalStorage,
importUsernameFromLocalStorage, importUsernameAndIdFromLocalStorage,
} from "./data/localStorage"; } from "./data/localStorage";
import CustomStats from "./CustomStats"; import CustomStats from "./CustomStats";
import { restore, restoreAppState, RestoredDataState } from "../data/restore"; import { restore, restoreAppState, RestoredDataState } from "../data/restore";
@ -411,7 +411,8 @@ const ExcalidrawWrapper = () => {
// don't sync if local state is newer or identical to browser state // don't sync if local state is newer or identical to browser state
if (isBrowserStorageStateNewer(STORAGE_KEYS.VERSION_DATA_STATE)) { if (isBrowserStorageStateNewer(STORAGE_KEYS.VERSION_DATA_STATE)) {
const localDataState = importFromLocalStorage(); const localDataState = importFromLocalStorage();
const username = importUsernameFromLocalStorage(); const username =
importUsernameAndIdFromLocalStorage()?.username ?? "";
let langCode = languageDetector.detect() || defaultLang.code; let langCode = languageDetector.detect() || defaultLang.code;
if (Array.isArray(langCode)) { if (Array.isArray(langCode)) {
langCode = langCode[0]; langCode = langCode[0];