diff --git a/packages/hoppscotch-common/src/components/http/test/Runner.vue b/packages/hoppscotch-common/src/components/http/test/Runner.vue index 96195724af2..aeb2d9ad776 100644 --- a/packages/hoppscotch-common/src/components/http/test/Runner.vue +++ b/packages/hoppscotch-common/src/components/http/test/Runner.vue @@ -33,7 +33,7 @@
- +
diff --git a/packages/hoppscotch-common/src/helpers/runner/adapter.ts b/packages/hoppscotch-common/src/helpers/runner/adapter.ts new file mode 100644 index 00000000000..ab22151d81c --- /dev/null +++ b/packages/hoppscotch-common/src/helpers/runner/adapter.ts @@ -0,0 +1,110 @@ +import { + ChildrenResult, + SmartTreeAdapter, +} from "@hoppscotch/ui/dist/helpers/treeAdapter" +import { Ref } from "vue" +import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data" +import { computed } from "vue" + +export class TestRunnerCollectionsAdapter implements SmartTreeAdapter { + constructor(public data: Ref[]>) {} + + navigateToFolderWithIndexPath( + collections: HoppCollection[], + indexPaths: number[] + ) { + if (indexPaths.length === 0) return null + + let target = collections[indexPaths.shift() as number] + + while (indexPaths.length > 0) + target = target.folders[indexPaths.shift() as number] + + return target !== undefined ? target : null + } + + getChildren(id: string | null): Ref> { + return computed(() => { + if (id === null) { + const data = this.data.value.map((item, index) => ({ + id: `folder-${index.toString()}`, + data: { + type: "collections", + isLastItem: index === this.data.value.length - 1, + data: { + parentIndex: null, + data: item, + }, + }, + })) + return { + status: "loaded", + data: data, + } as ChildrenResult + } + + const childType = id.split("-")[0] + + if (childType === "request") { + return { + status: "loaded", + data: [], + } + } + + const folderId = id.split("-")[1] + + const indexPath = folderId.split("/").map((x) => parseInt(x)) + + const item = this.navigateToFolderWithIndexPath( + this.data.value, + indexPath + ) + + if (item) { + const data = [ + ...item.folders.map((folder, index) => ({ + id: `folder-${folderId}/${index}`, + data: { + isLastItem: + item.folders && item.folders.length > 1 + ? index === item.folders.length - 1 + : false, + type: "folders", + isSelected: true, + data: { + parentIndex: id, + data: folder, + }, + }, + })), + ...item.requests.map((requests, index) => ({ + id: `request-${id}/${index}`, + data: { + isLastItem: + item.requests && item.requests.length > 1 + ? index === item.requests.length - 1 + : false, + type: "requests", + isSelected: true, + data: { + parentIndex: id, + data: requests, + }, + }, + })), + ] + + return { + status: "loaded", + data: data, + } as ChildrenResult + } else { + return { + status: "loaded", + data: [], + } + } + }) + } +} diff --git a/packages/hoppscotch-ui/src/components/smart/TreeBranch.vue b/packages/hoppscotch-ui/src/components/smart/TreeBranch.vue index c9cdc5f75b9..e1fff5131e1 100644 --- a/packages/hoppscotch-ui/src/components/smart/TreeBranch.vue +++ b/packages/hoppscotch-ui/src/components/smart/TreeBranch.vue @@ -81,25 +81,31 @@ import { SmartTreeAdapter, TreeNode } from "~/helpers/treeAdapter" import { HOPP_UI_OPTIONS, HoppUIPluginOptions } from "./../../plugin" const { t } = inject(HOPP_UI_OPTIONS) ?? {} -const props = defineProps<{ - /** - * The node item that will be used to render the tree branch - * @template T The type of the data passed to the tree branch - */ - adapter: SmartTreeAdapter - /** - * The node item that will be used to render the tree branch content - */ - nodeItem: TreeNode - /** - * Total number of rootNode - */ - rootNodesLength?: number - /** - * open by default - */ - expandAll?: boolean -}>() +const props = withDefaults( + defineProps<{ + /** + * The node item that will be used to render the tree branch + * @template T The type of the data passed to the tree branch + */ + adapter: SmartTreeAdapter + /** + * The node item that will be used to render the tree branch content + */ + nodeItem: TreeNode + /** + * Total number of rootNode + */ + rootNodesLength?: number + /** + * open by default + */ + expandAll?: boolean + }>(), + { + rootNodesLength: 0, + expandAll: false, + } +) const CHILD_SLOT_NAME = "default"