Skip to content

Commit

Permalink
🤓 Add loading spinner & improve local users
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed May 17, 2021
1 parent dfb6ce6 commit 3575afb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
52 changes: 52 additions & 0 deletions client/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,55 @@ footer {
.userAway {
color: #c0bdba !important;
}

.loaderOverlay {
position: absolute;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.7);
z-index: 10;
}

.loader,
.loader:after {
border-radius: 50%;
width: 10em;
height: 10em;
}
.loader {
margin: 60px auto;
font-size: 15px;
position: relative;
text-indent: -9999em;

border-top: 1.1em solid rgba(39, 147, 218, 0.2);
border-right: 1.1em solid rgba(39, 147, 218, 0.2);
border-bottom: 1.1em solid rgba(39, 147, 218, 0.2);
border-left: 1.1em solid #2793da;
-webkit-transform: translateZ(0);
-ms-transform: translateZ(0);
transform: translateZ(0);
-webkit-animation: load8 1.1s infinite linear;
animation: load8 1.1s infinite linear;
}
@-webkit-keyframes load8 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes load8 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
8 changes: 7 additions & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
</head>

<body>

<!-- Main app -->
<section class="section" id="app">
<!-- display spinner if we're not online yet -->
<div v-if="!online || !user || !ws" class="loaderOverlay">
<div class="loader">Loading...</div>
</div>

<div class="notification is-danger" v-if="error">{{ error }}</div>
<div class="columns" v-if="ws">
<!-- box for group chats -->
Expand Down Expand Up @@ -132,6 +138,6 @@

</section>

<footer>Chatr v0.0.7, Ben Coleman 2021 <a href="https://github.com/benc-uk/chatr" target="_blank">[GitHub Project]</a></footer>
<footer>Chatr v0.0.8, Ben Coleman 2021 <a href="https://github.com/benc-uk/chatr" target="_blank">[GitHub Project]</a></footer>
</body>
</html>
21 changes: 14 additions & 7 deletions client/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ async function startApp() {
joinedChats: {},
// Main WebSocket instance
ws: null,
// Toggle to see if user is online
online: false,
// User object which is an instance of SWA clientPrincipal
// See https://docs.microsoft.com/en-us/azure/static-web-apps/user-information?tabs=javascript#client-principal-data
user: {},
Expand Down Expand Up @@ -59,11 +61,11 @@ async function startApp() {
}
} catch (err) {
// When auth endpoint not available, fallback to a prompt and fake clientPrincipal data
// In reality this is not really need anymore as the SWA emulator does a good job
const userName = prompt('What is your name')
// In reality this is not really need anymore as we use the SWA emulator
const userName = prompt('Please set your user name')
if (!userName) window.location.href = window.location.href
this.user = {
userId: utils.uuidv4(),
userId: utils.hashString(userName),
userDetails: userName,
identityProvider: 'fake',
}
Expand Down Expand Up @@ -112,7 +114,7 @@ async function startApp() {

// System events
if (msg.type === 'system' && msg.event === 'connected') {
utils.toastMessage(`🔌 Connected to ${evt.origin}`, 'success')
utils.toastMessage(`🔌 Connected to ${evt.origin.replace('wss://', '')}`, 'success')
}

// Server events
Expand All @@ -131,9 +133,14 @@ async function startApp() {
}

if (msg.from === 'server' && msg.data.chatEvent === 'userOnline') {
let user = JSON.parse(msg.data.data)
this.$set(this.allUsers, user.userId, user)
utils.toastMessage(`🤩 ${user.userName} has just joined`, 'success')
let newUser = JSON.parse(msg.data.data)
// If the new user is ourselves, that means we're connected and online
if (newUser.userId == this.user.userId) {
this.online = true
} else {
utils.toastMessage(`🤩 ${newUser.userName} has just joined`, 'success')
}
this.$set(this.allUsers, newUser.userId, newUser)
}

if (msg.from === 'server' && msg.data.chatEvent === 'userOffline') {
Expand Down
13 changes: 13 additions & 0 deletions client/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ export default {
animate: { in: 'fadeIn', out: 'fadeOut' },
})
},

hashString(str, seed = 0) {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i)
h1 = Math.imul(h1 ^ ch, 2654435761)
h2 = Math.imul(h2 ^ ch, 1597334677)
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909)
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909)
return 4294967296 * (2097151 & h2) + (h1 >>> 0)
},
}
2 changes: 1 addition & 1 deletion client/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h1 class="title is-1">💬 Chatr</h1>
</div>
</section>

<footer>Chatr v0.0.7, Ben Coleman 2021 <a href="https://github.com/benc-uk/chatr" target="_blank">[GitHub Project]</a></footer>
<footer>Chatr v0.0.8, Ben Coleman 2021 <a href="https://github.com/benc-uk/chatr" target="_blank">[GitHub Project]</a></footer>

</body>
</html>

0 comments on commit 3575afb

Please sign in to comment.