refactor: add new tree adapter corresponding to search
This commit is contained in:
@@ -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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,19 +5,12 @@ import {
|
|||||||
import { Ref, ref, watchEffect } from "vue"
|
import { Ref, ref, watchEffect } from "vue"
|
||||||
import { NewWorkspaceService } from "~/services/new-workspace"
|
import { NewWorkspaceService } from "~/services/new-workspace"
|
||||||
import { HandleRef } from "~/services/new-workspace/handle"
|
import { HandleRef } from "~/services/new-workspace/handle"
|
||||||
import {
|
import { RESTCollectionViewItem } from "~/services/new-workspace/view"
|
||||||
RESTCollectionViewCollection,
|
|
||||||
RESTCollectionViewRequest,
|
|
||||||
} from "~/services/new-workspace/view"
|
|
||||||
import { Workspace } from "~/services/new-workspace/workspace"
|
import { Workspace } from "~/services/new-workspace/workspace"
|
||||||
import * as E from "fp-ts/Either"
|
import * as E from "fp-ts/Either"
|
||||||
|
|
||||||
type WorkspaceRESTCollectionListNode =
|
|
||||||
| { type: "collection"; value: RESTCollectionViewCollection }
|
|
||||||
| { type: "request"; value: RESTCollectionViewRequest }
|
|
||||||
|
|
||||||
export class WorkspaceRESTCollectionTreeAdapter
|
export class WorkspaceRESTCollectionTreeAdapter
|
||||||
implements SmartTreeAdapter<WorkspaceRESTCollectionListNode>
|
implements SmartTreeAdapter<RESTCollectionViewItem>
|
||||||
{
|
{
|
||||||
constructor(
|
constructor(
|
||||||
private workspaceHandle: HandleRef<Workspace>,
|
private workspaceHandle: HandleRef<Workspace>,
|
||||||
@@ -26,12 +19,12 @@ export class WorkspaceRESTCollectionTreeAdapter
|
|||||||
|
|
||||||
public getChildren(
|
public getChildren(
|
||||||
nodeID: string | null
|
nodeID: string | null
|
||||||
): Ref<ChildrenResult<WorkspaceRESTCollectionListNode>> {
|
): Ref<ChildrenResult<RESTCollectionViewItem>> {
|
||||||
if (this.workspaceHandle.value.type !== "ok") {
|
if (this.workspaceHandle.value.type !== "ok") {
|
||||||
throw new Error("Cannot issue children with invalid workspace handle")
|
throw new Error("Cannot issue children with invalid workspace handle")
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = ref<ChildrenResult<WorkspaceRESTCollectionListNode>>({
|
const result = ref<ChildrenResult<RESTCollectionViewItem>>({
|
||||||
status: "loading",
|
status: "loading",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
|
||||||
import { Service } from "dioc"
|
import { Service } from "dioc"
|
||||||
|
import * as E from "fp-ts/Either"
|
||||||
import {
|
import {
|
||||||
Component,
|
Component,
|
||||||
Ref,
|
Ref,
|
||||||
@@ -8,17 +10,15 @@ import {
|
|||||||
shallowRef,
|
shallowRef,
|
||||||
watch,
|
watch,
|
||||||
} from "vue"
|
} from "vue"
|
||||||
import { WorkspaceProvider } from "./provider"
|
|
||||||
import { HandleRef } from "./handle"
|
import { HandleRef } from "./handle"
|
||||||
import * as E from "fp-ts/Either"
|
import { WorkspaceProvider } from "./provider"
|
||||||
import { Workspace, WorkspaceCollection, WorkspaceRequest } from "./workspace"
|
|
||||||
import {
|
import {
|
||||||
RESTCollectionChildrenView,
|
RESTCollectionChildrenView,
|
||||||
RESTCollectionLevelAuthHeadersView,
|
RESTCollectionLevelAuthHeadersView,
|
||||||
RESTSearchResultsView,
|
RESTSearchResultsView,
|
||||||
RootRESTCollectionView,
|
RootRESTCollectionView,
|
||||||
} from "./view"
|
} from "./view"
|
||||||
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
|
import { Workspace, WorkspaceCollection, WorkspaceRequest } from "./workspace"
|
||||||
|
|
||||||
export type WorkspaceError<ServiceErr> =
|
export type WorkspaceError<ServiceErr> =
|
||||||
| { type: "SERVICE_ERROR"; error: ServiceErr }
|
| { type: "SERVICE_ERROR"; error: ServiceErr }
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Ref } from "vue"
|
import { Ref } from "vue"
|
||||||
import * as E from "fp-ts/Either"
|
import * as E from "fp-ts/Either"
|
||||||
|
|
||||||
import { HandleRef } from "./handle"
|
import { HandleRef } from "./handle"
|
||||||
import {
|
import {
|
||||||
Workspace,
|
Workspace,
|
||||||
|
|||||||
@@ -968,7 +968,7 @@ export class PersonalWorkspaceProviderService
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!searchQuery.value) {
|
if (!searchQuery.value) {
|
||||||
return markRaw({
|
return {
|
||||||
type: "ok" as const,
|
type: "ok" as const,
|
||||||
data: {
|
data: {
|
||||||
providerID: this.providerID,
|
providerID: this.providerID,
|
||||||
@@ -976,25 +976,9 @@ export class PersonalWorkspaceProviderService
|
|||||||
|
|
||||||
loading: ref(false),
|
loading: ref(false),
|
||||||
|
|
||||||
results: computed(() => {
|
results: ref(this.restCollectionState.value.state),
|
||||||
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,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return markRaw({
|
return markRaw({
|
||||||
@@ -1007,104 +991,39 @@ export class PersonalWorkspaceProviderService
|
|||||||
|
|
||||||
results: computed(() => {
|
results: computed(() => {
|
||||||
const filterText = searchQuery.value.toLowerCase()
|
const filterText = searchQuery.value.toLowerCase()
|
||||||
const filteredCollections: RESTCollectionViewItem[] = []
|
const filteredCollections = []
|
||||||
|
|
||||||
const isMatch = (text: string) =>
|
const isMatch = (text: string) =>
|
||||||
text.toLowerCase().includes(filterText)
|
text.toLowerCase().includes(filterText)
|
||||||
|
|
||||||
for (const collection of this.restCollectionState.value.state) {
|
for (const collection of this.restCollectionState.value.state) {
|
||||||
const filteredRequests: Extract<
|
const filteredRequests = []
|
||||||
RESTCollectionViewItem,
|
const filteredFolders = []
|
||||||
{ type: "request" }
|
for (const request of collection.requests) {
|
||||||
>[] = []
|
if (isMatch(request.name)) filteredRequests.push(request)
|
||||||
|
}
|
||||||
const filteredFolders: Extract<
|
for (const folder of collection.folders) {
|
||||||
RESTCollectionViewItem,
|
if (isMatch(folder.name)) filteredFolders.push(folder)
|
||||||
{ type: "collection" }
|
const filteredFolderRequests = []
|
||||||
>[] = []
|
for (const request of folder.requests) {
|
||||||
|
if (isMatch(request.name))
|
||||||
collection.requests.forEach((request, requestID) => {
|
filteredFolderRequests.push(request)
|
||||||
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,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
if (filteredFolderRequests.length > 0) {
|
||||||
|
const filteredFolder = Object.assign({}, folder)
|
||||||
collection.folders.forEach(
|
filteredFolder.requests = filteredFolderRequests
|
||||||
(childCollection, childCollectionID) => {
|
filteredFolders.push(filteredFolder)
|
||||||
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 (
|
if (
|
||||||
filteredRequests.length + filteredFolders.length > 0 ||
|
filteredRequests.length + filteredFolders.length > 0 ||
|
||||||
isMatch(collection.name)
|
isMatch(collection.name)
|
||||||
) {
|
) {
|
||||||
filteredCollections.push(
|
const filteredCollection = Object.assign({}, collection)
|
||||||
...filteredFolders,
|
filteredCollection.requests = filteredRequests
|
||||||
...filteredRequests
|
filteredCollection.folders = filteredFolders
|
||||||
)
|
filteredCollections.push(filteredCollection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { HoppRESTRequest } from "@hoppscotch/data"
|
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
|
||||||
import { Ref } from "vue"
|
import { Ref } from "vue"
|
||||||
import { HoppInheritedRESTProperty } from "~/helpers/types/HoppInheritedProperties"
|
import { HoppInheritedRESTProperty } from "~/helpers/types/HoppInheritedProperties"
|
||||||
|
|
||||||
@@ -53,5 +53,5 @@ export interface RESTSearchResultsView {
|
|||||||
|
|
||||||
loading: Ref<boolean>
|
loading: Ref<boolean>
|
||||||
|
|
||||||
results: Ref<RESTCollectionViewItem[]>
|
results: Ref<HoppCollection[]>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user