From 3580d2eaf6ce686d23c6fc0b1ed6b6d0877322d8 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Tue, 19 May 2026 15:15:13 -0300 Subject: [PATCH] refactor(posts): flatten togglePlatform with snapToCompatibleVariant helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pull the variant-snap branch out of togglePlatform into a named helper with guard clauses. togglePlatform now reads top-down: lock check → deselect short-circuit → snap → select. --- resources/js/pages/posts/Edit.vue | 34 +++++++++++++++++-------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/resources/js/pages/posts/Edit.vue b/resources/js/pages/posts/Edit.vue index 3e95f0f1..61e9128f 100644 --- a/resources/js/pages/posts/Edit.vue +++ b/resources/js/pages/posts/Edit.vue @@ -202,24 +202,28 @@ const activeTab = ref(initialTabFromQuery); const deleteModal = ref | null>(null); const editorSidebarRef = ref | null>(null); +const snapToCompatibleVariant = (platformId: string) => { + const pp = post.value.post_platforms.find((p) => p.id === platformId); + const current = platformContentTypes.value[platformId]; + if (!pp || !current) return; + if (!getMediaIncompatibilityReason(current, media.value)) return; + + const fallback = firstCompatibleVariant(pp.platform, media.value); + if (!fallback) return; + + platformContentTypes.value = { ...platformContentTypes.value, [platformId]: fallback }; +}; + const togglePlatform = (platformId: string) => { if (isLocked.value) return; - const index = selectedPlatformIds.value.indexOf(platformId); - if (index === -1) { - const pp = post.value.post_platforms.find((p) => p.id === platformId); - const currentVariant = platformContentTypes.value[platformId]; - const currentIncompatible = pp && currentVariant - && getMediaIncompatibilityReason(currentVariant, media.value) !== null; - if (pp && currentIncompatible) { - const fallback = firstCompatibleVariant(pp.platform, media.value); - if (fallback) { - platformContentTypes.value = { ...platformContentTypes.value, [platformId]: fallback }; - } - } - selectedPlatformIds.value.push(platformId); - } else { - selectedPlatformIds.value.splice(index, 1); + + if (selectedPlatformIds.value.includes(platformId)) { + selectedPlatformIds.value = selectedPlatformIds.value.filter((id) => id !== platformId); + return; } + + snapToCompatibleVariant(platformId); + selectedPlatformIds.value.push(platformId); }; // Save logic