Skip to content

Commit ceeb07f

Browse files
committed
feat(router): use typed router
1 parent 69969ad commit ceeb07f

File tree

13 files changed

+261
-88
lines changed

13 files changed

+261
-88
lines changed

eslint.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const SORT_IMPORT_CUSTOM_GROUP = {
1010
export default antfu({
1111
toml: false,
1212
isInEditor: false,
13+
ignores: ["**/typed-router.ts"]
14+
}, {
1315
rules: {
1416
"antfu/consistent-list-newline": "off",
1517
"antfu/if-newline": "off",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"stylelint-rem-over-px": "^1.0.2",
5353
"type-fest": "^4.41.0",
5454
"typescript": "^5.9.2",
55+
"unplugin-vue-router": "^0.15.0",
5556
"vue-router": "^4.5.1",
5657
"vue-tsc": "^3.0.6",
5758
"workbox-build": "^7.3.0",

pnpm-lock.yaml

Lines changed: 124 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.vue

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
11
<template>
2-
<router-view />
2+
<main class="container main-container">
3+
<main-header />
4+
<main-placeholder v-if="!repositories.length" />
5+
<template v-else>
6+
<tab-selector v-model="selectedTab" :items="tabs" />
7+
<router-view v-slot="{ Component }">
8+
<keep-alive>
9+
<component :is="Component" />
10+
</keep-alive>
11+
</router-view>
12+
</template>
13+
</main>
314
</template>
415
<script setup lang="ts">
5-
import { RouterView } from "vue-router";
16+
import { computed } from "vue";
17+
import { RouterView, useRoute, useRouter } from "vue-router";
18+
import { useEventsStore } from "@/store/events";
19+
import { useRepositoriesStore } from "@/store/repositories";
20+
import MainHeader from "@/components/header/main-header.vue";
21+
import MainPlaceholder from "@/components/main-placeholder.vue";
22+
import TabSelector from "@/components/tab-selector.vue";
23+
24+
const route = useRoute("Repositories");
25+
const router = useRouter();
26+
27+
const { repositories } = useRepositoriesStore();
28+
const { isFeedAvailable, amount } = useEventsStore();
29+
30+
type Tab = "Repositories" | "Dependencies" | "Feed";
31+
const tabs = computed(() => {
32+
const entries: { value: Tab, text: string }[] = [
33+
{ value: "Repositories", text: "Repositories" },
34+
{ value: "Dependencies", text: "Dependencies" }
35+
];
36+
if (isFeedAvailable.value) entries.push({ value: "Feed", text: `Feed (${amount.value})` });
37+
return entries;
38+
});
39+
40+
const selectedTab = computed<Tab>({
41+
get: () => route.name?.toString() as Tab ?? tabs.value[0].value,
42+
set: (name) => { router.push({ name }); }
43+
});
644
</script>
45+
<style lang="scss">
46+
.main-container {
47+
display: grid;
48+
gap: 1.5rem;
49+
}
50+
</style>
File renamed without changes.
File renamed without changes.

src/pages/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script setup lang="ts">
2+
definePage({ name: "Home", redirect: { name: "Repositories", params: {} } });
3+
</script>
File renamed without changes.

src/router.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
11
import { createRouter, createWebHistory } from "vue-router";
2+
import { routes } from "vue-router/auto-routes";
23
import { useSettingsStore } from "./store/settings";
34

4-
const HomeView = () => import("@/views/HomeView.vue");
5-
const RepositoriesView = () => import("@/views/RepositoriesView.vue");
6-
const DependenciesView = () => import("@/views/DependenciesView.vue");
7-
const FeedView = () => import("@/views/FeedView.vue");
8-
95
const router = createRouter({
106
history: createWebHistory(import.meta.env.BASE_URL),
11-
routes: [
12-
{
13-
path: "/",
14-
name: "Home",
15-
component: HomeView,
16-
redirect: { name: "Repositories" },
17-
children: [
18-
{ name: "Repositories", path: "/repositories", component: RepositoriesView },
19-
{ name: "Dependencies", path: "/dependencies", component: DependenciesView },
20-
{ name: "Feed", path: "/feed", component: FeedView }
21-
]
22-
},
23-
{
24-
path: "/:pathMatch(.*)*",
25-
redirect: { name: "Home", params: {} }
26-
}
27-
]
7+
routes
288
});
299

3010
router.beforeEach(() => {

0 commit comments

Comments
 (0)