Просмотр исходного кода

refactor: add new tree adapter corresponding to search

jamesgeorge007 1 год назад
Родитель
Сommit
1a442659b1

+ 93 - 0
packages/hoppscotch-common/src/helpers/adapters/WorkspaceRESTCollectionSearchTreeAdapter.ts

@@ -0,0 +1,93 @@
+import { HoppCollection } from "@hoppscotch/data"
+import { ChildrenResult, SmartTreeAdapter } from "@hoppscotch/ui"
+import { Ref, computed, ref } from "vue"
+import { navigateToFolderWithIndexPath } from "~/newstore/collections"
+import { RESTCollectionViewItem } from "~/services/new-workspace/view"
+
+export class WorkspaceRESTSearchCollectionTreeAdapter
+  implements SmartTreeAdapter<RESTCollectionViewItem>
+{
+  constructor(public data: Ref<HoppCollection[]>) {}
+
+  getChildren(
+    nodeID: string | null
+  ): Ref<ChildrenResult<RESTCollectionViewItem>> {
+    const result = ref<ChildrenResult<RESTCollectionViewItem>>({
+      status: "loading",
+    })
+
+    return computed(() => {
+      if (nodeID === null) {
+        result.value = {
+          status: "loaded",
+          data: this.data.value.map((item, index) => ({
+            id: index.toString(),
+
+            data: <RESTCollectionViewItem>{
+              type: "collection",
+              value: {
+                collectionID: index.toString(),
+                isLastItem: index === this.data.value.length - 1,
+                name: item.name,
+                parentCollectionID: null,
+              },
+            },
+          })),
+        }
+      } else {
+        const indexPath = nodeID.split("/").map((x) => parseInt(x))
+
+        const item = navigateToFolderWithIndexPath(this.data.value, indexPath)
+
+        if (item) {
+          const collections = item.folders.map(
+            (childCollection, childCollectionID) => {
+              return {
+                id: `${nodeID}/${childCollectionID}`,
+                data: <RESTCollectionViewItem>{
+                  type: "collection",
+                  value: {
+                    isLastItem:
+                      item.folders?.length > 1
+                        ? childCollectionID === item.folders.length - 1
+                        : false,
+                    collectionID: `${nodeID}/${childCollectionID}`,
+                    name: childCollection.name,
+                    parentCollectionID: nodeID,
+                  },
+                },
+              }
+            }
+          )
+
+          const requests = item.requests.map((request, requestID) => {
+            // TODO: Replace `parentCollectionID` with `collectionID`
+            return {
+              id: `${nodeID}/${requestID}`,
+              data: <RESTCollectionViewItem>{
+                type: "request",
+                value: {
+                  isLastItem:
+                    item.requests?.length > 1
+                      ? requestID === item.requests.length - 1
+                      : false,
+                  parentCollectionID: nodeID,
+                  collectionID: nodeID,
+                  requestID: `${nodeID}/${requestID}`,
+                  request,
+                },
+              },
+            }
+          })
+
+          result.value = {
+            status: "loaded",
+            data: [...collections, ...requests],
+          }
+        }
+      }
+
+      return result.value
+    })
+  }
+}

+ 4 - 11
packages/hoppscotch-common/src/helpers/adapters/WorkspaceRESTCollectionTreeAdapter.ts

@@ -5,19 +5,12 @@ import {
 import { Ref, ref, watchEffect } from "vue"
 import { NewWorkspaceService } from "~/services/new-workspace"
 import { HandleRef } from "~/services/new-workspace/handle"
-import {
-  RESTCollectionViewCollection,
-  RESTCollectionViewRequest,
-} from "~/services/new-workspace/view"
+import { RESTCollectionViewItem } from "~/services/new-workspace/view"
 import { Workspace } from "~/services/new-workspace/workspace"
 import * as E from "fp-ts/Either"
 
-type WorkspaceRESTCollectionListNode =
-  | { type: "collection"; value: RESTCollectionViewCollection }
-  | { type: "request"; value: RESTCollectionViewRequest }
-
 export class WorkspaceRESTCollectionTreeAdapter
-  implements SmartTreeAdapter<WorkspaceRESTCollectionListNode>
+  implements SmartTreeAdapter<RESTCollectionViewItem>
 {
   constructor(
     private workspaceHandle: HandleRef<Workspace>,
@@ -26,12 +19,12 @@ export class WorkspaceRESTCollectionTreeAdapter
 
   public getChildren(
     nodeID: string | null
-  ): Ref<ChildrenResult<WorkspaceRESTCollectionListNode>> {
+  ): Ref<ChildrenResult<RESTCollectionViewItem>> {
     if (this.workspaceHandle.value.type !== "ok") {
       throw new Error("Cannot issue children with invalid workspace handle")
     }
 
-    const result = ref<ChildrenResult<WorkspaceRESTCollectionListNode>>({
+    const result = ref<ChildrenResult<RESTCollectionViewItem>>({
       status: "loading",
     })
 

+ 4 - 4
packages/hoppscotch-common/src/services/new-workspace/index.ts

@@ -1,4 +1,6 @@
+import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
 import { Service } from "dioc"
+import * as E from "fp-ts/Either"
 import {
   Component,
   Ref,
@@ -8,17 +10,15 @@ import {
   shallowRef,
   watch,
 } from "vue"
-import { WorkspaceProvider } from "./provider"
 import { HandleRef } from "./handle"
-import * as E from "fp-ts/Either"
-import { Workspace, WorkspaceCollection, WorkspaceRequest } from "./workspace"
+import { WorkspaceProvider } from "./provider"
 import {
   RESTCollectionChildrenView,
   RESTCollectionLevelAuthHeadersView,
   RESTSearchResultsView,
   RootRESTCollectionView,
 } from "./view"
-import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
+import { Workspace, WorkspaceCollection, WorkspaceRequest } from "./workspace"
 
 export type WorkspaceError<ServiceErr> =
   | { type: "SERVICE_ERROR"; error: ServiceErr }

+ 1 - 0
packages/hoppscotch-common/src/services/new-workspace/provider.ts

@@ -1,5 +1,6 @@
 import { Ref } from "vue"
 import * as E from "fp-ts/Either"
+
 import { HandleRef } from "./handle"
 import {
   Workspace,

+ 24 - 105
packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts

@@ -968,7 +968,7 @@ export class PersonalWorkspaceProviderService
           }
 
           if (!searchQuery.value) {
-            return markRaw({
+            return {
               type: "ok" as const,
               data: {
                 providerID: this.providerID,
@@ -976,25 +976,9 @@ export class PersonalWorkspaceProviderService
 
                 loading: ref(false),
 
-                results: computed(() => {
-                  return this.restCollectionState.value.state.map(
-                    (coll, id) => {
-                      return <RESTCollectionViewItem>{
-                        type: "collection",
-                        value: {
-                          collectionID: id.toString(),
-                          isLastItem:
-                            id ===
-                            this.restCollectionState.value.state.length - 1,
-                          name: coll.name,
-                          parentCollectionID: null,
-                        },
-                      }
-                    }
-                  )
-                }),
+                results: ref(this.restCollectionState.value.state),
               },
-            })
+            }
           }
 
           return markRaw({
@@ -1007,104 +991,39 @@ export class PersonalWorkspaceProviderService
 
               results: computed(() => {
                 const filterText = searchQuery.value.toLowerCase()
-                const filteredCollections: RESTCollectionViewItem[] = []
+                const filteredCollections = []
 
                 const isMatch = (text: string) =>
                   text.toLowerCase().includes(filterText)
 
                 for (const collection of this.restCollectionState.value.state) {
-                  const filteredRequests: Extract<
-                    RESTCollectionViewItem,
-                    { type: "request" }
-                  >[] = []
-
-                  const filteredFolders: Extract<
-                    RESTCollectionViewItem,
-                    { type: "collection" }
-                  >[] = []
-
-                  collection.requests.forEach((request, requestID) => {
-                    if (isMatch(request.name)) {
-                      filteredRequests.push({
-                        type: "request",
-                        value: {
-                          collectionID: collection.id!,
-                          isLastItem:
-                            collection.requests?.length > 1
-                              ? requestID === collection.requests.length - 1
-                              : false,
-                          // TODO: Replace `parentCollectionID` with `collectionID`
-                          parentCollectionID: collection.id!,
-                          requestID: requestID.toString(),
-                          request: request as HoppRESTRequest,
-                        },
-                      })
+                  const filteredRequests = []
+                  const filteredFolders = []
+                  for (const request of collection.requests) {
+                    if (isMatch(request.name)) filteredRequests.push(request)
+                  }
+                  for (const folder of collection.folders) {
+                    if (isMatch(folder.name)) filteredFolders.push(folder)
+                    const filteredFolderRequests = []
+                    for (const request of folder.requests) {
+                      if (isMatch(request.name))
+                        filteredFolderRequests.push(request)
                     }
-                  })
-
-                  collection.folders.forEach(
-                    (childCollection, childCollectionID) => {
-                      if (isMatch(childCollection.name)) {
-                        filteredFolders.push({
-                          type: "collection",
-                          value: {
-                            collectionID: `${collection.id}/${childCollectionID}`,
-                            isLastItem:
-                              collection.folders?.length > 1
-                                ? childCollectionID ===
-                                  collection.folders.length - 1
-                                : false,
-                            name: childCollection.name,
-                            parentCollectionID: collection.id!,
-                          },
-                        })
-                      }
-
-                      const filteredFolderRequests: Extract<
-                        RESTCollectionViewItem,
-                        { type: "request" }
-                      >[] = ([] = [])
-
-                      childCollection.requests.forEach(
-                        (request: HoppRESTRequest, requestID: number) => {
-                          if (isMatch(request.name))
-                            filteredFolderRequests.push({
-                              type: "request",
-                              value: {
-                                collectionID: childCollection.id!,
-                                isLastItem:
-                                  childCollection.requests?.length > 1
-                                    ? requestID ===
-                                      childCollection.requests.length - 1
-                                    : false,
-                                // TODO: Replace `parentCollectionID` with `collectionID`
-                                parentCollectionID: childCollection.id!,
-                                requestID: requestID.toString(),
-                                request,
-                              },
-                            })
-                        }
-                      )
-
-                      if (filteredFolderRequests.length > 0) {
-                        const filteredFolder = Object.assign(
-                          {},
-                          childCollection
-                        )
-                        filteredFolder.requests = filteredFolderRequests
-                        filteredFolders.push(filteredFolder)
-                      }
+                    if (filteredFolderRequests.length > 0) {
+                      const filteredFolder = Object.assign({}, folder)
+                      filteredFolder.requests = filteredFolderRequests
+                      filteredFolders.push(filteredFolder)
                     }
-                  )
+                  }
 
                   if (
                     filteredRequests.length + filteredFolders.length > 0 ||
                     isMatch(collection.name)
                   ) {
-                    filteredCollections.push(
-                      ...filteredFolders,
-                      ...filteredRequests
-                    )
+                    const filteredCollection = Object.assign({}, collection)
+                    filteredCollection.requests = filteredRequests
+                    filteredCollection.folders = filteredFolders
+                    filteredCollections.push(filteredCollection)
                   }
                 }
 

+ 2 - 2
packages/hoppscotch-common/src/services/new-workspace/view.ts

@@ -1,4 +1,4 @@
-import { HoppRESTRequest } from "@hoppscotch/data"
+import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
 import { Ref } from "vue"
 import { HoppInheritedRESTProperty } from "~/helpers/types/HoppInheritedProperties"
 
@@ -53,5 +53,5 @@ export interface RESTSearchResultsView {
 
   loading: Ref<boolean>
 
-  results: Ref<RESTCollectionViewItem[]>
+  results: Ref<HoppCollection[]>
 }