Skip to content

Commit

Permalink
Fix typing on sidebar menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
BusterNeece committed Jan 15, 2025
1 parent 19ddd8c commit d614784
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 13 additions & 9 deletions frontend/components/Common/SidebarMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class="nav-item"
>
<router-link
v-if="isRouteLink(category)"
v-if="isRouteLink(category.url)"
:class="getLinkClass(category)"
:to="category.url"
class="nav-link"
Expand Down Expand Up @@ -49,7 +49,7 @@
class="nav-item"
>
<router-link
v-if="isRouteLink(item)"
v-if="isRouteLink(item.url)"
:to="item.url"
class="nav-link ps-4 py-2"
:class="getLinkClass(item)"
Expand Down Expand Up @@ -85,25 +85,29 @@ import Icon from "~/components/Common/Icon.vue";
import {useRoute} from "vue-router";
import {some} from "lodash";
import {IconOpenInNew} from "~/components/Common/icons.ts";
import {MenuCategory, MenuSubCategory, ReactiveMenu} from "~/functions/filterMenu.ts";
import {MenuCategory, MenuRouteBasedUrl, MenuRouteUrl, MenuSubCategory} from "~/functions/filterMenu.ts";
const props = defineProps<{
menu: ReactiveMenu
menu: MenuCategory[]
}>();
const currentRoute = useRoute();
const isRouteLink = (item: MenuSubCategory) => {
return (typeof (item.url) !== 'undefined')
&& (typeof (item.url) !== 'string');
const isRouteLink = (url: MenuRouteUrl): url is MenuRouteBasedUrl => {
return (typeof (url) !== 'undefined')
&& (typeof (url) !== 'string');
};
const isCategory = (item: MenuCategory | MenuSubCategory): item is MenuCategory => {
return 'items' in item;
}
const isActiveItem = (item: MenuCategory | MenuSubCategory) => {
if ('items' in item && some(item.items, isActiveItem)) {
if (isCategory(item) && some(item.items ?? [], isActiveItem)) {
return true;
}
return isRouteLink(item) && !('params' in item.url) && item.url.name === currentRoute.name;
return isRouteLink(item.url) && !('params' in item.url) && item.url.name === currentRoute.name;
};
const getLinkClass = (item: MenuSubCategory) => {
Expand Down
6 changes: 4 additions & 2 deletions frontend/functions/filterMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import {reactiveComputed} from "@vueuse/core";

export type ReactiveMenu = Reactive<MenuCategory[]>;

interface RouteBasedUrl {
export interface MenuRouteBasedUrl {
name: string,
params?: Record<string, any>
}

export type MenuRouteUrl = string | MenuRouteBasedUrl;

export interface MenuSubCategory {
key: string,
label: ComputedRef<string>,
url?: string | RouteBasedUrl,
url?: MenuRouteUrl,
icon?: Icon,
visible?: boolean,
external?: boolean,
Expand Down

0 comments on commit d614784

Please sign in to comment.