From 554b86d22327f425245c880a7392f069c960f46f Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Thu, 7 May 2026 16:02:54 -0300 Subject: [PATCH] feat: useWorkspaceEcho / usePostEcho composables and realtime list refresh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two new composables under resources/js/composables/echo/ that abstract channel naming for the two Reverb subscription patterns we now use: - useWorkspaceEcho — list/dashboard views: 1 subscription per workspace delivers events for many entities. Resolves currentWorkspace.id from Inertia shared props; no-ops when there is no workspace yet. - usePostEcho — focused views (Show, Edit): per-post channel. Posts index now subscribes via useWorkspaceEcho to the 3 lifecycle events (post.created / post.deleted / post.platform.status.updated) and reloads the posts prop on receipt. `reset: ['posts']` is required on the reload — without it, InfiniteScroll's merge prop appends every broadcast onto the existing list and produces duplicates. Show / Edit migrated from raw useEcho to usePostEcho; AiGenerateDialog, NotificationBell and AiPostWizard updated to the new event names from the namespaced convention. AiStream composable moved into the same echo/ folder for organisation; its bound event names (TextDelta / StreamEnd / Error) come from the laravel/ai package and stay as-is. Bonus: comments tab send button was overriding shadcn's size="icon" (40px) with class="size-9" (36px), which left it visibly shorter than the textarea. Removed the override and matched the textarea's min-h to size-10 so both render at 40px. --- resources/js/components/NotificationBell.vue | 2 +- .../components/posts/ai/AiGenerateDialog.vue | 2 +- .../components/posts/create/AiPostWizard.vue | 2 +- resources/js/composables/echo/usePostEcho.ts | 9 +++++++++ .../js/composables/echo/useWorkspaceEcho.ts | 18 ++++++++++++++++++ resources/js/pages/posts/Edit.vue | 6 +++--- resources/js/pages/posts/Index.vue | 11 +++++++++++ resources/js/pages/posts/Show.vue | 4 ++-- 8 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 resources/js/composables/echo/usePostEcho.ts create mode 100644 resources/js/composables/echo/useWorkspaceEcho.ts diff --git a/resources/js/components/NotificationBell.vue b/resources/js/components/NotificationBell.vue index 17182e9b..527c4825 100644 --- a/resources/js/components/NotificationBell.vue +++ b/resources/js/components/NotificationBell.vue @@ -44,7 +44,7 @@ const channelName = computed(() => ); if (channelName.value) { - useEcho(channelName.value, '.NotificationCreated', (e: { notification: Notification }) => { + useEcho(channelName.value, '.notification.created', (e: { notification: Notification }) => { const exists = notifications.value.some((n) => n.id === e.notification.id); if (exists) return; diff --git a/resources/js/components/posts/ai/AiGenerateDialog.vue b/resources/js/components/posts/ai/AiGenerateDialog.vue index f081de88..788190c1 100644 --- a/resources/js/components/posts/ai/AiGenerateDialog.vue +++ b/resources/js/components/posts/ai/AiGenerateDialog.vue @@ -6,7 +6,7 @@ import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; -import { useAiStream } from '@/composables/useAiStream'; +import { useAiStream } from '@/composables/echo/useAiStream'; import { generate as generatePostAi } from '@/routes/app/posts/ai'; const props = defineProps<{ diff --git a/resources/js/components/posts/create/AiPostWizard.vue b/resources/js/components/posts/create/AiPostWizard.vue index 86dea005..8770abed 100644 --- a/resources/js/components/posts/create/AiPostWizard.vue +++ b/resources/js/components/posts/create/AiPostWizard.vue @@ -229,7 +229,7 @@ const subscribeToCreation = (userId: string, creationId: string) => { const channelName = `users.${userId}.ai-creation.${creationId}`; subscribedChannelName = channelName; - echoChannel = echo().private(channelName).listen('.PostCreationReady', (e: any) => { + echoChannel = echo().private(channelName).listen('.ai.creation.completed', (e: any) => { if (e.error) { previewStatus.value = 'error'; previewError.value = e.error; diff --git a/resources/js/composables/echo/usePostEcho.ts b/resources/js/composables/echo/usePostEcho.ts new file mode 100644 index 00000000..8daa46ac --- /dev/null +++ b/resources/js/composables/echo/usePostEcho.ts @@ -0,0 +1,9 @@ +import { useEcho } from '@laravel/echo-vue'; + +export const usePostEcho = ( + postId: string, + event: string | string[], + callback: (payload: T) => void, +) => { + return useEcho(`post.${postId}`, event, callback); +}; diff --git a/resources/js/composables/echo/useWorkspaceEcho.ts b/resources/js/composables/echo/useWorkspaceEcho.ts new file mode 100644 index 00000000..51ce7d87 --- /dev/null +++ b/resources/js/composables/echo/useWorkspaceEcho.ts @@ -0,0 +1,18 @@ +import { usePage } from '@inertiajs/vue3'; +import { useEcho } from '@laravel/echo-vue'; + +import type { Auth } from '@/types'; + +export const useWorkspaceEcho = ( + event: string | string[], + callback: (payload: T) => void, +) => { + const page = usePage(); + const workspaceId = (page.props.auth as Auth | undefined)?.currentWorkspace?.id; + + if (!workspaceId) { + return; + } + + return useEcho(`workspace.${workspaceId}`, event, callback); +}; diff --git a/resources/js/pages/posts/Edit.vue b/resources/js/pages/posts/Edit.vue index 25025849..1ca1b293 100644 --- a/resources/js/pages/posts/Edit.vue +++ b/resources/js/pages/posts/Edit.vue @@ -1,6 +1,5 @@