Store username for every room (#1381)
* store username for every room * add missing fun
This commit is contained in:
parent
5e2f164026
commit
7b3816d0d3
@ -113,6 +113,10 @@ import { unstable_batchedUpdates } from "react-dom";
|
|||||||
import { SceneStateCallbackRemover } from "../scene/globalScene";
|
import { SceneStateCallbackRemover } from "../scene/globalScene";
|
||||||
import { isLinearElement } from "../element/typeChecks";
|
import { isLinearElement } from "../element/typeChecks";
|
||||||
import { actionFinalize } from "../actions";
|
import { actionFinalize } from "../actions";
|
||||||
|
import {
|
||||||
|
restoreUsernameFromLocalStorage,
|
||||||
|
saveUsernameToLocalStorage,
|
||||||
|
} from "../data/localStorage";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param func handler taking at most single parameter (event).
|
* @param func handler taking at most single parameter (event).
|
||||||
@ -239,6 +243,14 @@ export class App extends React.Component<any, AppState> {
|
|||||||
elements={globalSceneState.getElements()}
|
elements={globalSceneState.getElements()}
|
||||||
onRoomCreate={this.openPortal}
|
onRoomCreate={this.openPortal}
|
||||||
onRoomDestroy={this.closePortal}
|
onRoomDestroy={this.closePortal}
|
||||||
|
onUsernameChange={(username) => {
|
||||||
|
if (this.portal.socket && this.portal.roomID && username) {
|
||||||
|
saveUsernameToLocalStorage(this.portal.roomID, username);
|
||||||
|
}
|
||||||
|
this.setState({
|
||||||
|
username,
|
||||||
|
});
|
||||||
|
}}
|
||||||
onLockToggle={this.toggleLock}
|
onLockToggle={this.toggleLock}
|
||||||
/>
|
/>
|
||||||
<main>
|
<main>
|
||||||
@ -909,8 +921,17 @@ export class App extends React.Component<any, AppState> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
this.portal.socket!.on("init-room", () => {
|
this.portal.socket!.on("init-room", () => {
|
||||||
this.portal.socket &&
|
if (this.portal.socket && this.portal.roomID) {
|
||||||
|
const username = restoreUsernameFromLocalStorage(this.portal.roomID);
|
||||||
|
|
||||||
this.portal.socket.emit("join-room", this.portal.roomID);
|
this.portal.socket.emit("join-room", this.portal.roomID);
|
||||||
|
|
||||||
|
if (username) {
|
||||||
|
this.setState({
|
||||||
|
username,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
this.portal.socket!.on(
|
this.portal.socket!.on(
|
||||||
"client-broadcast",
|
"client-broadcast",
|
||||||
|
@ -34,6 +34,7 @@ interface LayerUIProps {
|
|||||||
setAppState: any;
|
setAppState: any;
|
||||||
elements: readonly NonDeletedExcalidrawElement[];
|
elements: readonly NonDeletedExcalidrawElement[];
|
||||||
onRoomCreate: () => void;
|
onRoomCreate: () => void;
|
||||||
|
onUsernameChange: (username: string) => void;
|
||||||
onRoomDestroy: () => void;
|
onRoomDestroy: () => void;
|
||||||
onLockToggle: () => void;
|
onLockToggle: () => void;
|
||||||
}
|
}
|
||||||
@ -46,6 +47,7 @@ export const LayerUI = React.memo(
|
|||||||
canvas,
|
canvas,
|
||||||
elements,
|
elements,
|
||||||
onRoomCreate,
|
onRoomCreate,
|
||||||
|
onUsernameChange,
|
||||||
onRoomDestroy,
|
onRoomDestroy,
|
||||||
onLockToggle,
|
onLockToggle,
|
||||||
}: LayerUIProps) => {
|
}: LayerUIProps) => {
|
||||||
@ -98,6 +100,7 @@ export const LayerUI = React.memo(
|
|||||||
actionManager={actionManager}
|
actionManager={actionManager}
|
||||||
exportButton={renderExportDialog()}
|
exportButton={renderExportDialog()}
|
||||||
setAppState={setAppState}
|
setAppState={setAppState}
|
||||||
|
onUsernameChange={onUsernameChange}
|
||||||
onRoomCreate={onRoomCreate}
|
onRoomCreate={onRoomCreate}
|
||||||
onRoomDestroy={onRoomDestroy}
|
onRoomDestroy={onRoomDestroy}
|
||||||
onLockToggle={onLockToggle}
|
onLockToggle={onLockToggle}
|
||||||
@ -132,11 +135,7 @@ export const LayerUI = React.memo(
|
|||||||
isCollaborating={appState.isCollaborating}
|
isCollaborating={appState.isCollaborating}
|
||||||
collaboratorCount={appState.collaborators.size}
|
collaboratorCount={appState.collaborators.size}
|
||||||
username={appState.username}
|
username={appState.username}
|
||||||
onUsernameChange={(username) => {
|
onUsernameChange={onUsernameChange}
|
||||||
setAppState({
|
|
||||||
username,
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
onRoomCreate={onRoomCreate}
|
onRoomCreate={onRoomCreate}
|
||||||
onRoomDestroy={onRoomDestroy}
|
onRoomDestroy={onRoomDestroy}
|
||||||
/>
|
/>
|
||||||
|
@ -24,6 +24,7 @@ type MobileMenuProps = {
|
|||||||
setAppState: any;
|
setAppState: any;
|
||||||
elements: readonly NonDeletedExcalidrawElement[];
|
elements: readonly NonDeletedExcalidrawElement[];
|
||||||
onRoomCreate: () => void;
|
onRoomCreate: () => void;
|
||||||
|
onUsernameChange: (username: string) => void;
|
||||||
onRoomDestroy: () => void;
|
onRoomDestroy: () => void;
|
||||||
onLockToggle: () => void;
|
onLockToggle: () => void;
|
||||||
};
|
};
|
||||||
@ -35,6 +36,7 @@ export function MobileMenu({
|
|||||||
exportButton,
|
exportButton,
|
||||||
setAppState,
|
setAppState,
|
||||||
onRoomCreate,
|
onRoomCreate,
|
||||||
|
onUsernameChange,
|
||||||
onRoomDestroy,
|
onRoomDestroy,
|
||||||
onLockToggle,
|
onLockToggle,
|
||||||
}: MobileMenuProps) {
|
}: MobileMenuProps) {
|
||||||
@ -87,7 +89,7 @@ export function MobileMenu({
|
|||||||
isCollaborating={appState.isCollaborating}
|
isCollaborating={appState.isCollaborating}
|
||||||
collaboratorCount={appState.collaborators.size}
|
collaboratorCount={appState.collaborators.size}
|
||||||
username={appState.username}
|
username={appState.username}
|
||||||
onUsernameChange={(username) => setAppState({ username })}
|
onUsernameChange={onUsernameChange}
|
||||||
onRoomCreate={onRoomCreate}
|
onRoomCreate={onRoomCreate}
|
||||||
onRoomDestroy={onRoomDestroy}
|
onRoomDestroy={onRoomDestroy}
|
||||||
/>
|
/>
|
||||||
|
@ -5,6 +5,33 @@ import { restore } from "./restore";
|
|||||||
|
|
||||||
const LOCAL_STORAGE_KEY = "excalidraw";
|
const LOCAL_STORAGE_KEY = "excalidraw";
|
||||||
const LOCAL_STORAGE_KEY_STATE = "excalidraw-state";
|
const LOCAL_STORAGE_KEY_STATE = "excalidraw-state";
|
||||||
|
const LOCAL_STORAGE_KEY_COLLAB = "excalidraw-collab";
|
||||||
|
|
||||||
|
export function saveUsernameToLocalStorage(id: string, username: string) {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(
|
||||||
|
`${LOCAL_STORAGE_KEY_COLLAB}-${id}`,
|
||||||
|
JSON.stringify({ username }),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// Unable to access window.localStorage
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function restoreUsernameFromLocalStorage(id: string): string | null {
|
||||||
|
try {
|
||||||
|
const data = localStorage.getItem(`${LOCAL_STORAGE_KEY_COLLAB}-${id}`);
|
||||||
|
if (data) {
|
||||||
|
return JSON.parse(data).username;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Unable to access localStorage
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export function saveToLocalStorage(
|
export function saveToLocalStorage(
|
||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user