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:
John Livingston
2024-01-09 12:54:30 +01:00
parent 10406aaed0
commit d0a250a91d
4 changed files with 70 additions and 10 deletions

View File

@ -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
}