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>
|
||||
|
||||
<script setup>
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { computed, watch } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
|
||||
import { useArtistStore } from '../stores/artist.js'
|
||||
import ArtistHeader from '../components/artist/ArtistHeader.vue'
|
||||
import ArtistPostsTab from '../components/artist/ArtistPostsTab.vue'
|
||||
import ArtistGalleryTab from '../components/artist/ArtistGalleryTab.vue'
|
||||
import ArtistManagementTab from '../components/artist/ArtistManagementTab.vue'
|
||||
import { useTabQuery } from '../composables/useTabQuery.js'
|
||||
|
||||
const VALID_TABS = ['posts', 'gallery', 'management']
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const store = useArtistStore()
|
||||
|
||||
const slug = computed(() => route.params.slug)
|
||||
const tab = ref('posts')
|
||||
|
||||
function resolveDefaultTab () {
|
||||
const fromUrl = route.query.tab
|
||||
if (VALID_TABS.includes(fromUrl)) return fromUrl
|
||||
if ((store.postCount ?? 0) > 0) return 'posts'
|
||||
return 'gallery'
|
||||
}
|
||||
const { tab, resolve } = useTabQuery(
|
||||
VALID_TABS,
|
||||
() => ((store.postCount ?? 0) > 0 ? 'posts' : 'gallery'),
|
||||
)
|
||||
|
||||
watch(slug, async (s) => {
|
||||
if (!s) return
|
||||
@@ -70,23 +66,8 @@ watch(slug, async (s) => {
|
||||
document.title = store.overview
|
||||
? `${store.overview.name} — FabledCurator`
|
||||
: 'FabledCurator'
|
||||
tab.value = resolveDefaultTab()
|
||||
tab.value = resolve()
|
||||
}, { 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>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -36,32 +36,14 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, watch } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
|
||||
import SubscriptionsTab from '../components/subscriptions/SubscriptionsTab.vue'
|
||||
import DownloadsTab from '../components/subscriptions/DownloadsTab.vue'
|
||||
import SettingsTab from '../components/subscriptions/SettingsTab.vue'
|
||||
import { useTabQuery } from '../composables/useTabQuery.js'
|
||||
|
||||
const VALID_TABS = ['subscriptions', 'downloads', 'settings']
|
||||
|
||||
const route = useRoute()
|
||||
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
|
||||
}
|
||||
})
|
||||
const { tab } = useTabQuery(VALID_TABS, 'subscriptions')
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
Reference in New Issue
Block a user