Skip to content

Commit 7ffbaaf

Browse files
Merge pull request #26 from mctekk/feat/clickable-header
Improve speed
2 parents 749103d + 5d7ea31 commit 7ffbaaf

File tree

5 files changed

+61
-63
lines changed

5 files changed

+61
-63
lines changed

package-lock.json

Lines changed: 5 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"date-fns": "^2.16.1",
1717
"duration": "^0.2.2",
1818
"fuse.js": "^6.4.6",
19+
"js-cookie": "^2.2.1",
1920
"twilio-chat": "^4.0.0",
2021
"vue": "^2.6.11"
2122
},

src/components/chat/index.vue

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:show-ui="false"
1010
@logged="createClient"
1111
></chat-Login>
12-
<chat-loading v-if="!showChannelList"> </chat-loading>
12+
<chat-loading v-if="!showChannelList && !isLoaded"> </chat-loading>
1313
</div>
1414

1515
<div class="home-container h-full" v-else>
@@ -21,6 +21,7 @@
2121
<chat-side
2222
v-if="showChannelList"
2323
:is-expanded="isExpanded"
24+
:is-loading="!isLoaded"
2425
:user-context="userContext"
2526
:channels="channels"
2627
:active-channel="activeChannel"
@@ -124,6 +125,7 @@ export default {
124125
return {
125126
client: null,
126127
channels: [],
128+
isLoaded: false,
127129
activeChannel: null,
128130
userContext: { identity: null }
129131
};
@@ -143,6 +145,7 @@ export default {
143145
},
144146
methods: {
145147
async createClient(data) {
148+
console.time('initialLoad')
146149
const client = await Twilio.Client.create(data[this.tokenField], {
147150
logLevel: "info"
148151
});
@@ -157,17 +160,15 @@ export default {
157160
client.on("tokenAboutToExpire", this.onTokenAboutToExpire);
158161
this.loadChannelEvents(client);
159162
this.updateChannels();
163+
console.timeEnd('tnitialLoad')
160164
},
161165
162166
async loadChannel() {
163167
if (!this.activeChannel && !this.showChannelList) {
164-
this.client
165-
.getChannelBySid(this.userContext.channel_sid)
166-
.then(channel => {
167-
if (channel) {
168-
this.joinChannel(channel);
169-
}
170-
});
168+
const channel = await this.client.getChannelBySid(this.userContext.channel_sid)
169+
if (channel) {
170+
this.joinChannel(channel);
171+
}
171172
}
172173
},
173174
@@ -194,14 +195,16 @@ export default {
194195
}
195196
196197
if (!this.showChannelList) {
197-
this.loadChannel();
198+
await this.loadChannel();
199+
this.isLoaded = true;
198200
} else {
199-
const subscribed = await this.client
201+
const subscribed = await this.client
200202
.getSubscribedChannels({ limit: 100 })
201203
.then(page => {
202-
return this.appendChannels(page, []);
204+
return this.appendChannels(page, []);
203205
});
204206
this.channels = subscribed;
207+
this.isLoaded = true;
205208
}
206209
},
207210

src/components/chat/login.vue

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
<script>
3030
import axios from "axios";
31+
import Cookies from "js-cookie";
3132
3233
export default {
3334
name: "Login",
@@ -43,7 +44,7 @@ export default {
4344
}
4445
},
4546
receiver: {
46-
type: String,
47+
type: String
4748
},
4849
httpMethod: {
4950
type: Function
@@ -59,8 +60,11 @@ export default {
5960
}
6061
};
6162
},
62-
mounted() {
63-
if (this.receiver) {
63+
created() {
64+
const loginData = this.getData();
65+
if (loginData) {
66+
this.$emit("logged", loginData);
67+
} else if (this.receiver) {
6468
this.formData.identity = this.receiver;
6569
this.login();
6670
}
@@ -70,17 +74,27 @@ export default {
7074
this.getAccessToken(this.formData.identity);
7175
},
7276
77+
getData() {
78+
const token = Cookies.get("kanvas:chat");
79+
return JSON.parse(token);
80+
},
81+
82+
setData(data) {
83+
Cookies.set("kanvas:chat", data);
84+
this.$emit("logged", data);
85+
},
86+
7387
getAccessToken(identity) {
7488
if (this.httpMethod) {
7589
this.httpMethod(`${this.endpoint}/${identity}`).then(({ data }) => {
76-
this.$emit("logged", data);
90+
this.setData(data);
7791
});
7892
return;
7993
} else {
8094
axios
8195
.get(`${this.endpoint}/${identity}`, this.httpOptions)
8296
.then(({ data }) => {
83-
this.$emit("logged", data);
97+
this.setData(data);
8498
});
8599
}
86100
}

src/components/chat/side.vue

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,21 @@
4343
</div>
4444

4545
<div class="chat-side__list chat-scroller">
46-
<chat-side-item
47-
v-for="channel in filteredChannels"
48-
:key="channel.sid"
49-
:channel="channel"
50-
:channel-data="channelData[channel.sid]"
51-
:user-context="userContext"
52-
:active-channel="activeChannel"
53-
@click="$emit('click', channel)"
54-
>
55-
</chat-side-item>
46+
<template v-if="!isLoading">
47+
<chat-side-item
48+
v-for="channel in filteredChannels"
49+
:key="channel.sid"
50+
:channel="channel"
51+
:channel-data="channelData[channel.sid]"
52+
:user-context="userContext"
53+
:active-channel="activeChannel"
54+
@click="$emit('click', channel)"
55+
>
56+
</chat-side-item>
57+
</template>
58+
<chat-loading v-else>
59+
60+
</chat-loading>
5661
</div>
5762
</div>
5863
</template>
@@ -73,12 +78,14 @@
7378
import ChatHeader from "./header";
7479
import ChatSideItem from "./side-list-item";
7580
import Fuse from "fuse.js";
81+
import ChatLoading from "./loading";
7682
7783
export default {
7884
name: "ChatSider",
7985
components: {
8086
ChatHeader,
8187
ChatSideItem,
88+
ChatLoading
8289
},
8390
props: {
8491
mobileDisplay: {
@@ -107,6 +114,11 @@ export default {
107114
type: Boolean,
108115
required: true,
109116
},
117+
isLoading: {
118+
type: Boolean,
119+
required: true,
120+
},
121+
110122
},
111123
data() {
112124
return {

0 commit comments

Comments
 (0)