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

View File

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

View File

@ -65,7 +65,7 @@ import {
import {
getLibraryItemsFromStorage,
importFromLocalStorage,
importUsernameFromLocalStorage,
importUsernameAndIdFromLocalStorage,
} from "./data/localStorage";
import CustomStats from "./CustomStats";
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
if (isBrowserStorageStateNewer(STORAGE_KEYS.VERSION_DATA_STATE)) {
const localDataState = importFromLocalStorage();
const username = importUsernameFromLocalStorage();
const username =
importUsernameAndIdFromLocalStorage()?.username ?? "";
let langCode = languageDetector.detect() || defaultLang.code;
if (Array.isArray(langCode)) {
langCode = langCode[0];