Skip to content

Commit a85e3cf

Browse files
authored
Port local storage for display name from CallwithChat to Chat sample (#5681)
* Use a randomized default name * Save name to local storage instead of using random name * Change files
1 parent 9d79be1 commit a85e3cf

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "patch",
3+
"area": "improvement",
4+
"workstream": "",
5+
"comment": "Port saving display name to local storage from CallwithChat Sample to Chat Sample",
6+
"packageName": "@azure/communication-react",
7+
"email": "[email protected]",
8+
"dependentChangeType": "patch"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "patch",
3+
"area": "improvement",
4+
"workstream": "",
5+
"comment": "Port saving display name to local storage from CallwithChat Sample to Chat Sample",
6+
"packageName": "@azure/communication-react",
7+
"email": "[email protected]",
8+
"dependentChangeType": "patch"
9+
}

samples/Chat/src/app/ConfigurationScreen.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ import { getEndpointUrl } from './utils/getEndpointUrl';
4444
import { refreshToken } from './utils/refreshToken';
4545
/* @conditional-compile-remove(rich-text-editor-composite-support) */
4646
import { RichTextEditorToggle } from './RichTextEditorToggle';
47+
import {
48+
getDisplayNameFromLocalStorage,
49+
localStorageAvailable,
50+
saveDisplayNameToLocalStorage
51+
} from './utils/localStorage';
4752

4853
// These props are set by the caller of ConfigurationScreen in the JSX and not found in context
4954
export interface ConfigurationScreenProps {
@@ -94,6 +99,14 @@ export default (props: ConfigurationScreenProps): JSX.Element => {
9499
const theme = useTheme();
95100
const { joinChatHandler, setToken, setUserId, setDisplayName, setThreadId, setEndpointUrl } = props;
96101

102+
useEffect(() => {
103+
// Get display name from local storage if available
104+
const defaultDisplayName = localStorageAvailable ? getDisplayNameFromLocalStorage() : null;
105+
if (defaultDisplayName) {
106+
setName(defaultDisplayName);
107+
}
108+
}, []);
109+
97110
// Used when new user is being registered.
98111
const setupAndJoinChatThreadWithNewUser = useCallback(() => {
99112
const internalSetupAndJoinChatThread = async (): Promise<void> => {
@@ -183,6 +196,7 @@ export default (props: ConfigurationScreenProps): JSX.Element => {
183196
if (!name) {
184197
setEmptyWarning(true);
185198
} else {
199+
saveDisplayNameToLocalStorage(name);
186200
setEmptyWarning(false);
187201
setDisableJoinChatButton(true);
188202
setConfigurationScreenState(CONFIGURATIONSCREEN_SHOWING_SPINNER_INITIALIZE_CHAT);
@@ -252,6 +266,7 @@ export default (props: ConfigurationScreenProps): JSX.Element => {
252266
</Stack>
253267
</FocusZone>
254268
<DisplayNameField
269+
defaultName={name}
255270
setName={setName}
256271
setEmptyWarning={setEmptyWarning}
257272
validateName={validateName}

samples/Chat/src/app/utils/localStorage.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@
44
export const localStorageAvailable = typeof Storage !== 'undefined';
55

66
export enum LocalStorageKeys {
7+
DisplayName = 'DisplayName',
78
Theme = 'AzureCommunicationUI_Theme'
89
}
910

11+
/**
12+
* Get display name from local storage.
13+
*/
14+
export const getDisplayNameFromLocalStorage = (): string | null =>
15+
window.localStorage.getItem(LocalStorageKeys.DisplayName);
16+
17+
/**
18+
* Save display name into local storage.
19+
*/
20+
export const saveDisplayNameToLocalStorage = (displayName: string): void =>
21+
window.localStorage.setItem(LocalStorageKeys.DisplayName, displayName);
22+
1023
/**
1124
* Get theme from local storage.
1225
*/

0 commit comments

Comments
 (0)