Better Anonymous chat user UX:
* Remember the chosen nickname in sessionStorage, to avoid entering it again too often. * Fix: if an anonymous chat user enter spaces in the nickname choice, it will allows them to keep the random nickname.
This commit is contained in:
@ -1,9 +1,41 @@
|
||||
/**
|
||||
* Generates a random nickname.
|
||||
* @param base Nickname prefix
|
||||
* @returns A nickname like "Given Base 12345"
|
||||
*/
|
||||
function randomNick (base: string): string {
|
||||
// using a 6 digit random number to generate a nickname with low colision risk
|
||||
const n = 100000 + Math.floor(Math.random() * 900000)
|
||||
return base + ' ' + n.toString()
|
||||
}
|
||||
|
||||
export {
|
||||
randomNick
|
||||
/**
|
||||
* Get the previous anonymous nickname (stored in sessionStorage).
|
||||
* @returns previous nickname or null
|
||||
*/
|
||||
function getPreviousAnonymousNick (): string | null {
|
||||
try {
|
||||
return sessionStorage.getItem('livechat-previous-anonymous-nickname')
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the chosen nickname in sessionStorage.
|
||||
*/
|
||||
function setPreviousAnonymousNick (nick: string): void {
|
||||
try {
|
||||
console.log('Storing anonymous nickname', nick)
|
||||
sessionStorage.setItem('livechat-previous-anonymous-nickname', nick)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
randomNick,
|
||||
getPreviousAnonymousNick,
|
||||
setPreviousAnonymousNick
|
||||
}
|
||||
|
Reference in New Issue
Block a user