Skip to content

Commit 1725195

Browse files
authored
fix(chat): desktop emoji picker stability and browser back navigation realtime sync (#201)
* fix(chat): ensure desktop emoji reaction picker stays open and interactive * fix(chat): fetch latest messages and reconnect realtime channel on back navigation
1 parent d1c7c44 commit 1725195

1 file changed

Lines changed: 49 additions & 13 deletions

File tree

‎resources/js/pages/Chat/Index.vue‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { Head, Link, usePage } from '@inertiajs/vue3';
2+
import { Head, Link, usePage, router } from '@inertiajs/vue3';
33
import {
44
Send,
55
Trash2,
@@ -288,6 +288,7 @@ const fetchLatestMessages = async (force = false) => {
288288
289289
if (data.messages && Array.isArray(data.messages)) {
290290
messages.value = data.messages;
291+
scrollToBottom(false);
291292
292293
if (typeof data.can_post === 'boolean') {
293294
canPost.value = data.can_post;
@@ -912,34 +913,55 @@ const openBanModal = (user: ChatUser) => {
912913
isBanModalOpen.value = true;
913914
};
914915
916+
let removeNavigateListener: (() => void) | null = null;
917+
915918
const handleVisibilityOrFocus = () => {
916919
if (document.visibilityState === 'visible') {
917-
fetchLatestMessages();
920+
fetchLatestMessages(true);
918921
}
919922
};
920923
924+
const handlePopState = () => {
925+
fetchLatestMessages(true);
926+
setupRealtime();
927+
};
928+
921929
const handleDocumentClick = () => {
922930
activeReactionPickerMessageId.value = null;
923931
};
924932
925933
onMounted(() => {
926934
initCooldown();
927935
setupRealtime();
936+
fetchLatestMessages(true);
928937
scrollToBottom(false);
929938
930-
window.addEventListener('focus', fetchLatestMessages);
931-
window.addEventListener('pageshow', fetchLatestMessages);
939+
window.addEventListener('focus', () => fetchLatestMessages(true));
940+
window.addEventListener('pageshow', () => fetchLatestMessages(true));
941+
window.addEventListener('popstate', handlePopState);
932942
document.addEventListener('visibilitychange', handleVisibilityOrFocus);
933943
window.addEventListener('click', handleDocumentClick);
944+
945+
removeNavigateListener = router.on('navigate', (event) => {
946+
if (event.detail.page.component === 'Chat/Index') {
947+
setupRealtime();
948+
fetchLatestMessages(true);
949+
}
950+
});
934951
});
935952
936953
onUnmounted(() => {
937954
if (cooldownInterval) {
938955
clearInterval(cooldownInterval);
939956
}
940957
941-
window.removeEventListener('focus', fetchLatestMessages);
942-
window.removeEventListener('pageshow', fetchLatestMessages);
958+
if (removeNavigateListener) {
959+
removeNavigateListener();
960+
}
961+
962+
window.removeEventListener('focus', () => fetchLatestMessages(true));
963+
window.removeEventListener('pageshow', () => fetchLatestMessages(true));
964+
window.removeEventListener('popstate', handlePopState);
943965
document.removeEventListener('visibilitychange', handleVisibilityOrFocus);
944966
window.removeEventListener('click', handleDocumentClick);
945967
@@ -1073,6 +1095,9 @@ onUnmounted(() => {
10731095
highlightedMessageId === msg.id
10741096
? 'bg-indigo-100/70 ring-2 ring-indigo-500/50 dark:bg-indigo-950/60'
10751097
: '',
1098+
activeReactionPickerMessageId === msg.id
1099+
? 'z-30'
1100+
: 'z-0',
10761101
isGroupedWithPrevious(idx) ? '!pt-0.5' : 'mt-1',
10771102
]"
10781103
>
@@ -1220,8 +1245,8 @@ onUnmounted(() => {
12201245
msg.is_deleted ||
12211246
!!msg.deleted_at
12221247
"
1223-
@click="reactToMessage(msg, r.emoji)"
1224-
class="inline-flex cursor-pointer items-center gap-1 rounded-full border px-2 py-0.5 text-[11px] font-medium transition active:scale-95 disabled:cursor-default"
1248+
@click.stop="reactToMessage(msg, r.emoji)"
1249+
class="inline-flex cursor-pointer items-center gap-1 rounded-full border px-2 py-0.5 text-[11px] font-medium transition select-none active:scale-95 disabled:cursor-default"
12251250
:class="
12261251
r.reacted
12271252
? 'border-indigo-300 bg-indigo-50 text-indigo-700 dark:border-indigo-800 dark:bg-indigo-950/70 dark:text-indigo-300'
@@ -1239,10 +1264,15 @@ onUnmounted(() => {
12391264
</div>
12401265
</div>
12411266

1242-
<!-- Floating Action Toolbar (Desktop on Hover) -->
1267+
<!-- Floating Action Toolbar (Desktop on Hover or Active Picker) -->
12431268
<div
12441269
v-if="!msg.is_deleted && !msg.deleted_at"
1245-
class="absolute -top-3 right-3 hidden items-center gap-0.5 rounded-xl border border-slate-200/90 bg-white p-0.5 shadow-sm sm:group-hover:flex dark:border-zinc-700 dark:bg-zinc-800"
1270+
class="absolute -top-3.5 right-3 items-center gap-0.5 rounded-xl border border-slate-200/90 bg-white p-0.5 shadow-md dark:border-zinc-700 dark:bg-zinc-800"
1271+
:class="[
1272+
activeReactionPickerMessageId === msg.id
1273+
? 'z-30 flex ring-1 ring-black/5 dark:ring-white/10'
1274+
: 'z-20 hidden sm:group-hover:flex',
1275+
]"
12461276
@click.stop
12471277
>
12481278
<!-- Reaction Trigger -->
@@ -1252,6 +1282,11 @@ onUnmounted(() => {
12521282
type="button"
12531283
@click.stop="toggleReactionPicker(msg.id)"
12541284
class="cursor-pointer rounded-lg p-1 text-slate-400 hover:bg-amber-50 hover:text-amber-600 dark:text-zinc-400 dark:hover:bg-amber-950/40 dark:hover:text-amber-400"
1285+
:class="
1286+
activeReactionPickerMessageId === msg.id
1287+
? 'bg-amber-50 text-amber-600 dark:bg-amber-950/40 dark:text-amber-400'
1288+
: ''
1289+
"
12551290
title="Add reaction"
12561291
>
12571292
<Smile class="h-3.5 w-3.5" />
@@ -1262,15 +1297,16 @@ onUnmounted(() => {
12621297
v-if="
12631298
activeReactionPickerMessageId === msg.id
12641299
"
1265-
class="absolute right-0 bottom-full z-30 mb-1 flex items-center gap-1 rounded-full border border-slate-200 bg-white px-2 py-1 shadow-lg dark:border-zinc-700 dark:bg-zinc-800"
1300+
class="absolute right-0 bottom-full z-40 mb-1.5 flex items-center gap-0.5 rounded-full border border-slate-200/90 bg-white px-2 py-1 shadow-xl ring-1 ring-black/5 dark:border-zinc-700 dark:bg-zinc-800 dark:ring-white/10"
12661301
@click.stop
12671302
>
12681303
<button
12691304
v-for="emoji in reactionEmojis"
12701305
:key="emoji"
12711306
type="button"
1272-
@click="reactToMessage(msg, emoji)"
1273-
class="cursor-pointer text-base transition hover:scale-125 active:scale-95"
1307+
@click.stop="reactToMessage(msg, emoji)"
1308+
class="flex h-7 w-7 cursor-pointer items-center justify-center rounded-lg text-base transition-transform select-none hover:scale-125 hover:bg-slate-100 active:scale-95 dark:hover:bg-zinc-700"
1309+
:title="`React with ${emoji}`"
12741310
>
12751311
{{ emoji }}
12761312
</button>

0 commit comments

Comments
 (0)