Apply filters to nodes list.
This commit is contained in:
parent
5b703917b5
commit
cce665d149
6 changed files with 96 additions and 77 deletions
|
@ -1,37 +1,24 @@
|
|||
<script setup lang="ts">
|
||||
import {computed, defineProps} from "vue";
|
||||
import type {NodesFilter} from "@/types";
|
||||
|
||||
const props = defineProps({
|
||||
title: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
icon: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
variant: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
value: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
link: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
filter: {
|
||||
type: Object,
|
||||
required: false,
|
||||
},
|
||||
});
|
||||
interface Props {
|
||||
title: string;
|
||||
icon: string;
|
||||
variant: string;
|
||||
value: number;
|
||||
link: string;
|
||||
filter?: NodesFilter;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
|
||||
const linkTarget = computed(() => {
|
||||
if (props.filter) {
|
||||
const json = JSON.stringify(props.filter);
|
||||
return `${props.link}?search=${encodeURIComponent(json)}`;
|
||||
return {
|
||||
path: props.link,
|
||||
query: props.filter,
|
||||
}
|
||||
} else {
|
||||
return props.link;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import { createRouter, createWebHistory } from "vue-router";
|
|||
import AdminDashboardView from "@/views/AdminDashboardView.vue";
|
||||
import AdminNodesView from "@/views/AdminNodesView.vue";
|
||||
import HomeView from "@/views/HomeView.vue";
|
||||
import {isNodesFilter} from "@/types";
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
|
@ -20,6 +21,9 @@ const router = createRouter({
|
|||
path: "/admin/nodes",
|
||||
name: "admin-nodes",
|
||||
component: AdminNodesView,
|
||||
props: route => ({
|
||||
filter: isNodesFilter(route.query) ? route.query : {}
|
||||
})
|
||||
},
|
||||
],
|
||||
});
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {defineStore} from "pinia";
|
||||
import {isEnhancedNode, type EnhancedNode} from "@/types";
|
||||
import {type EnhancedNode, isEnhancedNode, type NodesFilter} from "@/types";
|
||||
import {internalApi} from "@/utils/Api";
|
||||
|
||||
interface NodesStoreState {
|
||||
|
@ -37,12 +37,13 @@ export const useNodesStore = defineStore({
|
|||
},
|
||||
},
|
||||
actions: {
|
||||
async refresh(page: number, nodesPerPage: number): Promise<void> {
|
||||
async refresh(page: number, nodesPerPage: number, filter: NodesFilter): Promise<void> {
|
||||
const result = await internalApi.getPagedList<EnhancedNode>(
|
||||
"nodes",
|
||||
isEnhancedNode,
|
||||
page,
|
||||
nodesPerPage,
|
||||
filter,
|
||||
);
|
||||
this.nodes = result.entries;
|
||||
this.totalNodes = result.total;
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import type {TypeGuard} from "@/types/shared";
|
||||
import {toIsArray} from "@/types/shared";
|
||||
import {toIsArray, type TypeGuard} from "@/types";
|
||||
import type {Headers} from "request";
|
||||
import {parseInteger} from "@/utils/Numbers";
|
||||
|
||||
|
@ -63,10 +62,12 @@ class Api {
|
|||
isT: TypeGuard<T>,
|
||||
page: number,
|
||||
itemsPerPage: number,
|
||||
filter?: object,
|
||||
): Promise<PagedListResult<T>> {
|
||||
const response = await this.doGet(path, toIsArray(isT), {
|
||||
_page: page,
|
||||
_perPage: itemsPerPage,
|
||||
...filter,
|
||||
});
|
||||
const totalStr = response.headers.get("x-total-count");
|
||||
const total = parseInteger(totalStr, 10);
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
<script setup lang="ts">
|
||||
import {useNodesStore} from "@/stores/nodes";
|
||||
import {onMounted, ref} from "vue";
|
||||
import type {EnhancedNode, MAC} from "@/types";
|
||||
import type {EnhancedNode, MAC, NodesFilter} from "@/types";
|
||||
import Pager from "@/components/Pager.vue";
|
||||
import LoadingContainer from "@/components/LoadingContainer.vue";
|
||||
|
||||
const NODE_PER_PAGE = 50;
|
||||
|
||||
interface Props {
|
||||
filter: NodesFilter;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
|
||||
type NodeRedactField = "nickname" | "email" | "token";
|
||||
type NodeRedactFieldsMap = Partial<Record<NodeRedactField, boolean>>;
|
||||
|
||||
|
@ -21,7 +27,7 @@ async function refresh(page: number): Promise<void> {
|
|||
loading.value = true;
|
||||
redactAllFields(true);
|
||||
try {
|
||||
await nodes.refresh(page, NODE_PER_PAGE);
|
||||
await nodes.refresh(page, NODE_PER_PAGE, props.filter);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,14 @@ export function isString(arg: unknown): arg is string {
|
|||
return typeof arg === "string"
|
||||
}
|
||||
|
||||
export function isNumber(arg: unknown): arg is number {
|
||||
return typeof arg === "number"
|
||||
}
|
||||
|
||||
export function isBoolean(arg: unknown): arg is boolean {
|
||||
return typeof arg === "boolean"
|
||||
}
|
||||
|
||||
export function toIsArray<T>(isT: TypeGuard<T>): TypeGuard<T[]> {
|
||||
return (arg): arg is T[] => isArray(arg, isT);
|
||||
}
|
||||
|
@ -31,6 +39,10 @@ export function toIsEnum<E>(enumDef: E): TypeGuard<E> {
|
|||
return (arg): arg is E => Object.values(enumDef).includes(arg as [keyof E]);
|
||||
}
|
||||
|
||||
export function isOptional<T>(arg: unknown, isT: TypeGuard<T>): arg is (T | undefined) {
|
||||
return arg === undefined || isT(arg);
|
||||
}
|
||||
|
||||
export type Version = string;
|
||||
|
||||
// Should be good enough for now.
|
||||
|
@ -51,14 +63,13 @@ export function isNodeStatistics(arg: unknown): arg is NodeStatistics {
|
|||
return false;
|
||||
}
|
||||
const stats = arg as NodeStatistics;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return (
|
||||
typeof stats.registered === "number" &&
|
||||
typeof stats.withVPN === "number" &&
|
||||
typeof stats.withCoords === "number" &&
|
||||
typeof stats.monitoring === "object" &&
|
||||
typeof stats.monitoring.active === "number" &&
|
||||
typeof stats.monitoring.pending === "number"
|
||||
isNumber(stats.registered) &&
|
||||
isNumber(stats.withVPN) &&
|
||||
isNumber(stats.withCoords) &&
|
||||
isObject(stats.monitoring) &&
|
||||
isNumber(stats.monitoring.active) &&
|
||||
isNumber(stats.monitoring.pending)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -85,11 +96,10 @@ export function isCommunityConfig(arg: unknown): arg is CommunityConfig {
|
|||
return false;
|
||||
}
|
||||
const cfg = arg as CommunityConfig;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return (
|
||||
typeof cfg.name === "string" &&
|
||||
typeof cfg.domain === "string" &&
|
||||
typeof cfg.contactEmail === "string" &&
|
||||
isString(cfg.name) &&
|
||||
isString(cfg.domain) &&
|
||||
isString(cfg.contactEmail) &&
|
||||
isArray(cfg.sites, isString) &&
|
||||
isArray(cfg.domains, isString)
|
||||
);
|
||||
|
@ -107,10 +117,9 @@ export function isLegalConfig(arg: unknown): arg is LegalConfig {
|
|||
return false;
|
||||
}
|
||||
const cfg = arg as LegalConfig;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return (
|
||||
(cfg.privacyUrl === undefined || typeof cfg.privacyUrl === "string") &&
|
||||
(cfg.imprintUrl === undefined || typeof cfg.imprintUrl === "string")
|
||||
isOptional(cfg.privacyUrl, isString) &&
|
||||
isOptional(cfg.imprintUrl, isString)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -125,8 +134,7 @@ export function isClientMapConfig(arg: unknown): arg is ClientMapConfig {
|
|||
return false;
|
||||
}
|
||||
const cfg = arg as ClientMapConfig;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return typeof cfg.mapUrl === "string";
|
||||
return isString(cfg.mapUrl);
|
||||
}
|
||||
|
||||
export class MonitoringConfig {
|
||||
|
@ -140,8 +148,7 @@ export function isMonitoringConfig(arg: unknown): arg is MonitoringConfig {
|
|||
return false;
|
||||
}
|
||||
const cfg = arg as MonitoringConfig;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return typeof cfg.enabled === "boolean";
|
||||
return isBoolean(cfg.enabled);
|
||||
}
|
||||
|
||||
export class Coords {
|
||||
|
@ -156,10 +163,9 @@ export function isCoords(arg: unknown): arg is Coords {
|
|||
return false;
|
||||
}
|
||||
const coords = arg as Coords;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return (
|
||||
typeof coords.lat === "number" &&
|
||||
typeof coords.lng === "number"
|
||||
isNumber(coords.lat) &&
|
||||
isNumber(coords.lng)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -177,11 +183,10 @@ export function isCoordsSelectorConfig(arg: unknown): arg is CoordsSelectorConfi
|
|||
return false;
|
||||
}
|
||||
const cfg = arg as CoordsSelectorConfig;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return (
|
||||
typeof cfg.lat === "number" &&
|
||||
typeof cfg.lng === "number" &&
|
||||
typeof cfg.defaultZoom === "number" &&
|
||||
isNumber(cfg.lat) &&
|
||||
isNumber(cfg.lng) &&
|
||||
isNumber(cfg.defaultZoom) &&
|
||||
isObject(cfg.layers) // TODO: Better types!
|
||||
);
|
||||
}
|
||||
|
@ -199,10 +204,9 @@ export function isOtherCommunityInfoConfig(arg: unknown): arg is OtherCommunityI
|
|||
return false;
|
||||
}
|
||||
const cfg = arg as OtherCommunityInfoConfig;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return (
|
||||
typeof cfg.showInfo === "boolean" &&
|
||||
typeof cfg.showBorderForDebugging === "boolean" &&
|
||||
isBoolean(cfg.showInfo) &&
|
||||
isBoolean(cfg.showBorderForDebugging) &&
|
||||
isArray(cfg.localCommunityPolygon, isCoords)
|
||||
);
|
||||
}
|
||||
|
@ -225,7 +229,6 @@ export function isClientConfig(arg: unknown): arg is ClientConfig {
|
|||
return false;
|
||||
}
|
||||
const cfg = arg as ClientConfig;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return (
|
||||
isCommunityConfig(cfg.community) &&
|
||||
isLegalConfig(cfg.legal) &&
|
||||
|
@ -233,7 +236,7 @@ export function isClientConfig(arg: unknown): arg is ClientConfig {
|
|||
isMonitoringConfig(cfg.monitoring) &&
|
||||
isCoordsSelectorConfig(cfg.coordsSelector) &&
|
||||
isOtherCommunityInfoConfig(cfg.otherCommunityInfo) &&
|
||||
typeof cfg.rootPath === "string"
|
||||
isString(cfg.rootPath)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -281,19 +284,18 @@ export function isNode(arg: unknown): arg is Node {
|
|||
return false;
|
||||
}
|
||||
const node = arg as Node;
|
||||
// noinspection SuspiciousTypeOfGuard
|
||||
return (
|
||||
isToken(node.token) &&
|
||||
typeof node.nickname === "string" &&
|
||||
typeof node.email === "string" &&
|
||||
typeof node.hostname === "string" &&
|
||||
(node.coords === undefined || typeof node.coords === "string") &&
|
||||
(node.key === undefined || isFastdKey(node.key)) &&
|
||||
isString(node.nickname) &&
|
||||
isString(node.email) &&
|
||||
isString(node.hostname) &&
|
||||
isOptional(node.coords, isString) &&
|
||||
isOptional(node.key, isFastdKey) &&
|
||||
isMAC(node.mac) &&
|
||||
typeof node.monitoring === "boolean" &&
|
||||
typeof node.monitoringConfirmed === "boolean" &&
|
||||
isBoolean(node.monitoring) &&
|
||||
isBoolean(node.monitoringConfirmed) &&
|
||||
isMonitoringState(node.monitoringState) &&
|
||||
typeof node.modifiedAt === "number"
|
||||
isNumber(node.modifiedAt)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -321,8 +323,26 @@ export function isEnhancedNode(arg: unknown): arg is EnhancedNode {
|
|||
}
|
||||
const node = arg as EnhancedNode;
|
||||
return (
|
||||
(node.site === undefined || isSite(node.site)) &&
|
||||
(node.domain === undefined || isDomain(node.domain)) &&
|
||||
(node.onlineState === undefined || isOnlineState(node.onlineState))
|
||||
isOptional(node.site, isSite) &&
|
||||
isOptional(node.domain, isDomain) &&
|
||||
isOptional(node.onlineState, isOnlineState)
|
||||
);
|
||||
}
|
||||
|
||||
export interface NodesFilter {
|
||||
hasKey?: boolean;
|
||||
hasCoords?: boolean;
|
||||
monitoringState?: MonitoringState;
|
||||
}
|
||||
|
||||
export function isNodesFilter(arg: unknown): arg is NodesFilter {
|
||||
if (!isObject(arg)) {
|
||||
return false;
|
||||
}
|
||||
const filter = arg as NodesFilter;
|
||||
return (
|
||||
isOptional(filter.hasKey, isBoolean) &&
|
||||
isOptional(filter.hasCoords, isBoolean) &&
|
||||
isOptional(filter.monitoringState, isMonitoringState)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue