refactor(dry-F5): useTabQuery composable for ?tab= query-param routing
ArtistView and SubscriptionsView both two-way-bound a tab ref with the ?tab= query param via identical tab->URL and URL->tab watchers. Extracted useTabQuery(validTabs, defaultTab) — defaultTab accepts a string or a function (ArtistView's default is postCount-dependent), and resolve() is exposed so ArtistView can re-apply it after the artist loads. Both views drop their now-orphaned ref/useRouter imports. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
import { ref, watch } from 'vue'
|
||||||
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
|
|
||||||
|
// Two-way bind a tab ref with the ?tab= query param: the initial value comes
|
||||||
|
// from the URL (when it names a valid tab) else `defaultTab` — a string, or a
|
||||||
|
// function for a state-dependent default. Tab changes push to the URL via
|
||||||
|
// router.replace (no history spam); URL changes (back/forward) flow back into
|
||||||
|
// the ref. Returns { tab, resolve } where resolve() re-applies the URL-or-
|
||||||
|
// default rule (callers re-run it when their default inputs change).
|
||||||
|
export function useTabQuery(validTabs, defaultTab) {
|
||||||
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
|
const resolveDefault = () =>
|
||||||
|
(typeof defaultTab === 'function' ? defaultTab() : defaultTab)
|
||||||
|
const resolve = () =>
|
||||||
|
(validTabs.includes(route.query.tab) ? route.query.tab : resolveDefault())
|
||||||
|
|
||||||
|
const tab = ref(resolve())
|
||||||
|
|
||||||
|
watch(tab, (t) => {
|
||||||
|
if (route.query.tab === t) return
|
||||||
|
router.replace({ query: { ...route.query, tab: t } })
|
||||||
|
})
|
||||||
|
watch(() => route.query.tab, (q) => {
|
||||||
|
if (q && validTabs.includes(q) && tab.value !== q) tab.value = q
|
||||||
|
})
|
||||||
|
|
||||||
|
return { tab, resolve }
|
||||||
|
}
|
||||||
@@ -39,30 +39,26 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { computed, ref, watch } from 'vue'
|
import { computed, watch } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute } from 'vue-router'
|
||||||
|
|
||||||
import { useArtistStore } from '../stores/artist.js'
|
import { useArtistStore } from '../stores/artist.js'
|
||||||
import ArtistHeader from '../components/artist/ArtistHeader.vue'
|
import ArtistHeader from '../components/artist/ArtistHeader.vue'
|
||||||
import ArtistPostsTab from '../components/artist/ArtistPostsTab.vue'
|
import ArtistPostsTab from '../components/artist/ArtistPostsTab.vue'
|
||||||
import ArtistGalleryTab from '../components/artist/ArtistGalleryTab.vue'
|
import ArtistGalleryTab from '../components/artist/ArtistGalleryTab.vue'
|
||||||
import ArtistManagementTab from '../components/artist/ArtistManagementTab.vue'
|
import ArtistManagementTab from '../components/artist/ArtistManagementTab.vue'
|
||||||
|
import { useTabQuery } from '../composables/useTabQuery.js'
|
||||||
|
|
||||||
const VALID_TABS = ['posts', 'gallery', 'management']
|
const VALID_TABS = ['posts', 'gallery', 'management']
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
|
||||||
const store = useArtistStore()
|
const store = useArtistStore()
|
||||||
|
|
||||||
const slug = computed(() => route.params.slug)
|
const slug = computed(() => route.params.slug)
|
||||||
const tab = ref('posts')
|
const { tab, resolve } = useTabQuery(
|
||||||
|
VALID_TABS,
|
||||||
function resolveDefaultTab () {
|
() => ((store.postCount ?? 0) > 0 ? 'posts' : 'gallery'),
|
||||||
const fromUrl = route.query.tab
|
)
|
||||||
if (VALID_TABS.includes(fromUrl)) return fromUrl
|
|
||||||
if ((store.postCount ?? 0) > 0) return 'posts'
|
|
||||||
return 'gallery'
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(slug, async (s) => {
|
watch(slug, async (s) => {
|
||||||
if (!s) return
|
if (!s) return
|
||||||
@@ -70,23 +66,8 @@ watch(slug, async (s) => {
|
|||||||
document.title = store.overview
|
document.title = store.overview
|
||||||
? `${store.overview.name} — FabledCurator`
|
? `${store.overview.name} — FabledCurator`
|
||||||
: 'FabledCurator'
|
: 'FabledCurator'
|
||||||
tab.value = resolveDefaultTab()
|
tab.value = resolve()
|
||||||
}, { immediate: true })
|
}, { immediate: true })
|
||||||
|
|
||||||
// Reflect tab changes back into the URL so refresh/back/forward work.
|
|
||||||
watch(tab, (newTab) => {
|
|
||||||
if (route.query.tab === newTab) return
|
|
||||||
router.replace({
|
|
||||||
query: { ...route.query, tab: newTab },
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// React to URL-tab changes (e.g., back/forward).
|
|
||||||
watch(() => route.query.tab, (q) => {
|
|
||||||
if (q && VALID_TABS.includes(q) && tab.value !== q) {
|
|
||||||
tab.value = q
|
|
||||||
}
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -36,32 +36,14 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, watch } from 'vue'
|
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
|
||||||
|
|
||||||
import SubscriptionsTab from '../components/subscriptions/SubscriptionsTab.vue'
|
import SubscriptionsTab from '../components/subscriptions/SubscriptionsTab.vue'
|
||||||
import DownloadsTab from '../components/subscriptions/DownloadsTab.vue'
|
import DownloadsTab from '../components/subscriptions/DownloadsTab.vue'
|
||||||
import SettingsTab from '../components/subscriptions/SettingsTab.vue'
|
import SettingsTab from '../components/subscriptions/SettingsTab.vue'
|
||||||
|
import { useTabQuery } from '../composables/useTabQuery.js'
|
||||||
|
|
||||||
const VALID_TABS = ['subscriptions', 'downloads', 'settings']
|
const VALID_TABS = ['subscriptions', 'downloads', 'settings']
|
||||||
|
|
||||||
const route = useRoute()
|
const { tab } = useTabQuery(VALID_TABS, 'subscriptions')
|
||||||
const router = useRouter()
|
|
||||||
|
|
||||||
const tab = ref(
|
|
||||||
VALID_TABS.includes(route.query.tab) ? route.query.tab : 'subscriptions',
|
|
||||||
)
|
|
||||||
|
|
||||||
watch(tab, (t) => {
|
|
||||||
if (route.query.tab === t) return
|
|
||||||
router.replace({ query: { ...route.query, tab: t } })
|
|
||||||
})
|
|
||||||
|
|
||||||
watch(() => route.query.tab, (q) => {
|
|
||||||
if (q && VALID_TABS.includes(q) && tab.value !== q) {
|
|
||||||
tab.value = q
|
|
||||||
}
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
Reference in New Issue
Block a user