Skip to content

Commit

Permalink
refactor(conversation): improve hooks to avoid repeated logic (#4779)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmcg authored Sep 14, 2022
1 parent 6572b6e commit 28f3ee9
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 49 deletions.
57 changes: 46 additions & 11 deletions components/compositions/conversations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, ComputedRef } from 'vue'
import { computed, ComputedRef, reactive } from 'vue'
import { Conversation } from '~/libraries/Iridium/chat/types'
import iridium from '~/libraries/Iridium/IridiumManager'
import { User } from '~/libraries/Iridium/users/types'
Expand All @@ -7,12 +7,22 @@ import { TrackKind } from '~/libraries/WebRTC/types'
export function conversationHooks() {
// @ts-ignore
const $nuxt = useNuxtApp()
const conversationId: ComputedRef<string> = computed(() => {

const managers = reactive({
chat: iridium.chat,
users: iridium.users,
})

// todo - refactor so a conversationId param can be passed in rather than relying on route every time
const conversationId: ComputedRef<string | undefined> = computed(() => {
return $nuxt.$route.params.id
})

const conversation: ComputedRef<Conversation | undefined> = computed(() => {
return iridium.chat.state.conversations[conversationId.value]
if (!conversationId.value) {
return
}
return managers.chat.state.conversations[conversationId.value]
})

const isGroup: ComputedRef<boolean> = computed(() => {
Expand All @@ -27,26 +37,51 @@ export function conversationHooks() {
)
})

const otherParticipants: ComputedRef<(User | undefined)[]> = computed(() => {
return otherDids.value.map((did) => managers.users.getUser(did))
})

const otherTypingParticipants: ComputedRef<(User | undefined)[]> = computed(
() => {
if (!conversationId.value) {
return []
}
return (
managers.chat.ephemeral.typing[conversationId.value]?.map((did) =>
managers.users.getUser(did),
) ?? []
)
},
)

const allParticipantsAlphaSorted: ComputedRef<(User | undefined)[]> =
computed(() => {
if (!conversation.value) {
return []
}
const arr = conversation.value.participants.map((p) =>
managers.users.getUser(p),
) as User[]
return arr.sort((a, b) => a.name.localeCompare(b.name))
})

const enableRTC: ComputedRef<boolean> = computed(() => {
return Boolean(
otherDids.value?.filter(
(did) => iridium.users.ephemeral.status[did] === 'online',
(did) => managers.users.ephemeral.status[did] === 'online',
).length,
)
})

// for a DM
const userDetails: ComputedRef<User> = computed(() => {
return iridium.users.state[otherDids.value[0]]
})

return {
conversation,
conversationId,
isGroup,
otherDids,
otherParticipants,
otherTypingParticipants,
allParticipantsAlphaSorted,
enableRTC,
userDetails,
}
}

Expand All @@ -61,7 +96,7 @@ export async function call({
}) {
// @ts-ignore
const $nuxt = useNuxtApp()
const { enableRTC, otherDids } = conversationHooks()
const { enableRTC } = conversationHooks()

if (!enableRTC.value) {
return
Expand Down
32 changes: 11 additions & 21 deletions components/views/chat/UserList.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<template>
<div class="user-list hover-scroll">
<TypographyText>
{{ $t('pages.chat.members', { count: userDetails.length }) }}
{{
$t('pages.chat.members', { count: allParticipantsAlphaSorted.length })
}}
</TypographyText>
<div v-for="user in userDetails" :key="user.did" class="user">
<div
v-for="user in allParticipantsAlphaSorted"
:key="user.did"
class="user"
>
<UiUserState :user="user" :conversation-id="conversationId" />
<TypographyText class="ellipsis">
{{ user.name }}
Expand All @@ -13,30 +19,14 @@
</template>

<script lang="ts">
import Vue, { computed, ComputedRef, reactive } from 'vue'
import iridium from '~/libraries/Iridium/IridiumManager'
import Vue from 'vue'
import { conversationHooks } from '~/components/compositions/conversations'
import { User } from '~/libraries/Iridium/users/types'
export default Vue.extend({
setup() {
const { conversation, conversationId } = conversationHooks()
const { conversationId, allParticipantsAlphaSorted } = conversationHooks()
const state = reactive({
users: iridium.users,
})
const userDetails: ComputedRef<User[]> = computed(() => {
if (!conversation.value) {
return []
}
const arr = conversation.value.participants.map((p) =>
state.users.getUser(p),
) as User[]
return arr.sort((a, b) => a.name.localeCompare(b.name))
})
return { conversation, conversationId, userDetails }
return { conversationId, allParticipantsAlphaSorted }
},
})
</script>
Expand Down
6 changes: 3 additions & 3 deletions components/views/chat/chatbar/Chatbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
/>
</div>
<TypographyText
v-if="activeUpload"
class="no-select"
size="sm"
:color="'danger'"
v-if="activeUpload"
color="danger"
>
{{ $t('global.active_upload') }}
</TypographyText>
<ChatbarFooter v-else :charlimit="charlimit" />
<!-- <ChatbarFooter v-else :charlimit="charlimit" /> -->
</div>
4 changes: 2 additions & 2 deletions components/views/navigation/mobile/toolbar/Toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
>
<video-icon size="1.4x" />
</button>
<template v-if="userDetails">
<UiUserState v-if="!isGroup" :user="userDetails" />
<template v-if="otherParticipants.length">
<UiUserState v-if="!isGroup" :user="otherParticipants[0]" />
<UiGroupIcon v-else :members="conversation?.participants" />
</template>
</div>
7 changes: 3 additions & 4 deletions components/views/navigation/mobile/toolbar/Toolbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export default Vue.extend({
conversationId,
isGroup,
otherDids,
otherParticipants,
enableRTC,
userDetails,
} = conversationHooks()
async function handleCall() {
if (isGroup.value || !enableRTC.value) {
if (isGroup.value || !enableRTC.value || !conversationId.value) {
return
}
await call({
Expand All @@ -47,9 +47,8 @@ export default Vue.extend({
return {
conversation,
isGroup,
otherDids,
enableRTC,
userDetails,
otherParticipants,
handleCall,
}
},
Expand Down
2 changes: 1 addition & 1 deletion components/views/navigation/toolbar/Toolbar.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="toolbar">
<InteractablesSidebarToggle v-if="!ui.showSidebar" />
<div class="circle-container">
<UiUserState v-if="!isGroup" class="circle" :user="userDetails" />
<UiUserState v-if="!isGroup" class="circle" :user="otherParticipants[0]" />
<UiGroupIcon v-else :members="conversation.participants" />
</div>
<div class="user-info">
Expand Down
8 changes: 3 additions & 5 deletions components/views/navigation/toolbar/Toolbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
conversationHooks,
call,
} from '~/components/compositions/conversations'
import { User } from '~/libraries/Iridium/users/types'
export default Vue.extend({
components: {
Expand All @@ -46,8 +45,8 @@ export default Vue.extend({
conversationId,
isGroup,
otherDids,
otherParticipants,
enableRTC,
userDetails,
} = conversationHooks()
const subtitleText: ComputedRef<string | undefined> = computed(() => {
Expand All @@ -68,7 +67,7 @@ export default Vue.extend({
})
async function handleCall() {
if (isGroup.value || !enableRTC.value) {
if (isGroup.value || !enableRTC.value || !conversationId.value) {
return
}
await call({
Expand All @@ -81,12 +80,11 @@ export default Vue.extend({
return {
conversation,
isGroup,
otherDids,
enableRTC,
subtitleText,
callTooltipText,
userDetails,
handleCall,
otherParticipants,
}
},
data() {
Expand Down
4 changes: 2 additions & 2 deletions pages/chat/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default Vue.extend({
name: 'Chat',
layout: 'desktop',
setup() {
const { conversationId, conversation, isGroup } = conversationHooks()
const { conversationId, isGroup } = conversationHooks()
const state = reactive({
webrtc: iridium.webRTC,
Expand All @@ -19,7 +19,7 @@ export default Vue.extend({
return state.webrtc.isActiveCall(conversationId.value)
})
return { conversation, isGroup, isActiveCall }
return { isGroup, isActiveCall }
},
})
</script>
Expand Down

0 comments on commit 28f3ee9

Please sign in to comment.