Track the site with the URL

This commit is contained in:
Eli Ribble 2026-04-17 22:04:24 +00:00
parent efd6f59fca
commit 55cb4ca962
No known key found for this signature in database
3 changed files with 63 additions and 3 deletions

View file

@ -0,0 +1,43 @@
import { computed, ComputedRef } from "vue";
import { useRoute, useRouter, LocationQueryValue } from "vue-router";
export function useQueryParam(paramName: string) {
const route = useRoute();
const router = useRouter();
// Returns string | null for easier handling
const value = computed<string | null>(() => {
const param = route.query[paramName];
// Handle arrays by taking first value, or return null
if (Array.isArray(param)) {
return param[0] ?? null;
}
return param ?? null;
});
const setValue = (newValue: string | number) => {
router.replace({
name: route.name,
query: {
...route.query,
[paramName]: String(newValue),
},
});
};
const removeValue = () => {
const { [paramName]: _, ...rest } = route.query;
router.replace({
name: route.name,
query: rest,
});
};
return {
value,
setValue,
removeValue,
};
}