Files
homebox/frontend/composables/use-location-helpers.ts
Tonya d4e28e6f3b Upgrade frontend deps, including nuxt (#982)
* feat: begin upgrading deps, still very buggy

* feat: progress

* feat: sort all type issues

* fix: sort type issues

* fix: import sonner styles

* fix: nuxt is the enemy

* fix: try sorting issue with workflows

* fix: update vitest config for dynamic import of path and defineConfig

* fix: add missing import

* fix: add time out to try and fix issues

* fix: add ui:ci:preview task for frontend build in CI mode

* fix: i was silly

* feat: add go:ci:with-frontend task for CI mode and remove ui:ci:preview from e2e workflow

* fix: update baseURL in Playwright config for local testing to use port 7745

* fix: update E2E_BASE_URL and remove wait for timeout in login test for smoother execution
2025-09-04 09:00:25 +01:00

77 lines
1.7 KiB
TypeScript

import type { Ref } from "vue";
import type { LocationSummary, TreeItem } from "~~/lib/api/types/data-contracts";
export interface FlatTreeItem {
id: string;
name: string;
treeString: string;
}
function flatTree(tree: TreeItem[]): FlatTreeItem[] {
const v = [] as FlatTreeItem[];
// turns the nested items into a flat items array where
// the display is a string of the tree hierarchy separated by breadcrumbs
function flatten(items: TreeItem[], display: string) {
if (!items) {
return;
}
for (const item of items) {
v.push({
id: item.id,
name: item.name,
treeString: display + item.name,
});
if (item.children) {
flatten(item.children, display + item.name + " > ");
}
}
}
flatten(tree, "");
return v;
}
function filterOutSubtree(tree: TreeItem[], excludeId: string): TreeItem[] {
// Recursively filters out a subtree starting from excludeId
const result: TreeItem[] = [];
for (const item of tree) {
if (item.id === excludeId) {
continue;
}
const newItem = { ...item };
if (item.children) {
newItem.children = filterOutSubtree(item.children, excludeId);
}
result.push(newItem);
}
return result;
}
export function useFlatLocations(excludeSubtreeForLocation?: LocationSummary): Ref<FlatTreeItem[]> {
const locations = useLocationStore();
if (locations.tree === null) {
locations.refreshTree();
}
return computed(() => {
if (locations.tree === null) {
return [];
}
const filteredTree = excludeSubtreeForLocation
? filterOutSubtree(locations.tree, excludeSubtreeForLocation.id)
: locations.tree;
return flatTree(filteredTree);
});
}