Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

chore: type and ux improvement for SmartTree (#3126)

Anwarul Islam610538ca0266
diff --git a/packages/hoppscotch-common/src/components/collections/Collection.vue b/packages/hoppscotch-common/src/components/collections/Collection.vue
index 0c069a545..44caea729 100644
--- a/packages/hoppscotch-common/src/components/collections/Collection.vue
+++ b/packages/hoppscotch-common/src/components/collections/Collection.vue
@@ -193,7 +193,7 @@ import IconTrash2 from "~icons/lucide/trash-2"
import IconEdit from "~icons/lucide/edit"
import IconFolder from "~icons/lucide/folder"
import IconFolderOpen from "~icons/lucide/folder-open"
-import { PropType, ref, computed, watch } from "vue"
+import { ref, computed, watch } from "vue"
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
import { useI18n } from "@composables/i18n"
import { TippyComponent } from "vue-tippy"
@@ -209,67 +209,36 @@ type FolderType = "collection" | "folder"
const t = useI18n()
-const props = defineProps({
- id: {
- type: String,
- default: "",
- required: true,
- },
- parentID: {
- type: String as PropType<string | null>,
- default: null,
- required: false,
- },
- data: {
- type: Object as PropType<HoppCollection<HoppRESTRequest> | TeamCollection>,
- default: () => ({}),
- required: true,
- },
- collectionsType: {
- type: String as PropType<CollectionType>,
- default: "my-collections",
- required: true,
- },
- /**
- * Collection component can be used for both collections and folders.
- * folderType is used to determine which one it is.
- */
- folderType: {
- type: String as PropType<FolderType>,
- default: "collection",
- required: true,
- },
- isOpen: {
- type: Boolean,
- default: false,
- required: true,
- },
- isSelected: {
- type: Boolean as PropType<boolean | null>,
- default: false,
- required: false,
- },
- exportLoading: {
- type: Boolean,
- default: false,
- required: false,
- },
- hasNoTeamAccess: {
- type: Boolean,
- default: false,
- required: false,
- },
- collectionMoveLoading: {
- type: Array as PropType<string[]>,
- default: () => [],
- required: false,
- },
- isLastItem: {
- type: Boolean,
- default: false,
- required: false,
- },
-})
+const props = withDefaults(
+ defineProps<{
+ id: string
+ parentID?: string | null
+ data: HoppCollection<HoppRESTRequest> | TeamCollection
+ /**
+ * Collection component can be used for both collections and folders.
+ * folderType is used to determine which one it is.
+ */
+ collectionsType: CollectionType
+ folderType: FolderType
+ isOpen: boolean
+ isSelected?: boolean | null
+ exportLoading?: boolean
+ hasNoTeamAccess?: boolean
+ collectionMoveLoading?: string[]
+ isLastItem?: boolean
+ }>(),
+ {
+ id: "",
+ parentID: null,
+ collectionsType: "my-collections",
+ folderType: "collection",
+ isOpen: false,
+ isSelected: false,
+ exportLoading: false,
+ hasNoTeamAccess: false,
+ isLastItem: false,
+ }
+)
const emit = defineEmits<{
(event: "toggle-children"): void
@@ -448,8 +417,13 @@ const notSameDestination = computed(() => {
})
const isCollLoading = computed(() => {
- if (props.collectionMoveLoading.length > 0 && props.data.id) {
- return props.collectionMoveLoading.includes(props.data.id)
+ const { collectionMoveLoading } = props
+ if (
+ collectionMoveLoading &&
+ collectionMoveLoading.length > 0 &&
+ props.data.id
+ ) {
+ return collectionMoveLoading.includes(props.data.id)
} else {
return false
}
diff --git a/packages/hoppscotch-common/src/components/smart/Tree.vue b/packages/hoppscotch-common/src/components/smart/Tree.vue
index 3c9a07459..781459254 100644
--- a/packages/hoppscotch-common/src/components/smart/Tree.vue
+++ b/packages/hoppscotch-common/src/components/smart/Tree.vue
@@ -10,6 +10,7 @@
class="flex flex-col flex-1"
>
<SmartTreeBranch
+ :root-nodes-length="rootNodes.data.length"
:node-item="rootNode"
:adapter="adapter as SmartTreeAdapter<T>"
>
diff --git a/packages/hoppscotch-common/src/components/smart/TreeBranch.vue b/packages/hoppscotch-common/src/components/smart/TreeBranch.vue
index 45366755a..9fcac990e 100644
--- a/packages/hoppscotch-common/src/components/smart/TreeBranch.vue
+++ b/packages/hoppscotch-common/src/components/smart/TreeBranch.vue
@@ -85,19 +85,25 @@ const props = defineProps<{
* The node item that will be used to render the tree branch content
*/
nodeItem: TreeNode<T>
+ /**
+ * Total number of rootNode
+ */
+ rootNodesLength?: number
}>()
const CHILD_SLOT_NAME = "default"
const t = useI18n()
+const isOnlyRootChild = computed(() => props.rootNodesLength === 1)
+
/**
* Marks whether the children on this branch were ever rendered
* See the usage inside '<template>' for more info
*/
-const childrenRendered = ref(false)
+const childrenRendered = ref(isOnlyRootChild.value)
-const showChildren = ref(false)
-const isNodeOpen = ref(false)
+const showChildren = ref(isOnlyRootChild.value)
+const isNodeOpen = ref(isOnlyRootChild.value)
const highlightNode = ref(false)