Skip to content

Commit

Permalink
feat: use tree selector to select project (#174)
Browse files Browse the repository at this point in the history
* feat: use tree selector to select project

* fix: fix eslint eror

* fix: fix lint error

* fix: remove conole

* fix: fix change error
  • Loading branch information
DiamondYuan authored Nov 21, 2024
1 parent 5ec7be4 commit 34db046
Showing 1 changed file with 73 additions and 15 deletions.
88 changes: 73 additions & 15 deletions src/components/ProjectSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Select } from 'antd'
import { TreeSelect } from 'antd'
import React from 'react'
import ProjectDefinition from '../models/ProjectDefinition'
import { localize } from '../utils/Language'
Expand All @@ -10,39 +10,97 @@ interface ProjectSelectorProps {
excludeProjectId?: string
}

interface ProjectSelectorTreeNode {
title: string
key: string
value: string
disabled?: boolean
parentProjectId?: string
children?: ProjectSelectorTreeNode[]
}

class ProjectSelector extends React.Component<ProjectSelectorProps> {
render() {
const { allProjects, selectedProjectId, onChange, excludeProjectId } =
this.props

const projectOptions = [
const projectsMap: {
[key: string]: ProjectSelectorTreeNode
} = {}
let root: ProjectSelectorTreeNode[] = []
// Create a map of all projects
allProjects.forEach((item) => {
projectsMap[item.id] = {
title: item.name,
parentProjectId: item.parentProjectId,
key: item.id,
value: item.id,
}
})

// Function to check if a project or its ancestors are excluded
const isProjectOrAncestorExcluded = (projectId: string): boolean => {
let current: string | undefined = projectId
while (current) {
if (current === excludeProjectId) {
return true
}
// eslint-disable-next-line no-loop-func
current = projectsMap[current].parentProjectId
}
return false
}

// Distribute projects and apps to their respective parents or to the root
allProjects.forEach((item) => {
const project = projectsMap[item.id]
if (isProjectOrAncestorExcluded(item.id)) {
project.disabled = true
}
if (item.parentProjectId && projectsMap[item.parentProjectId]) {
const children =
projectsMap[item.parentProjectId].children || []
children.push(project)
projectsMap[item.parentProjectId].children = children
} else {
root.push(project)
}
})

root.forEach((project) => {
if (project.children)
project.children =
project.children.length > 0 ? project.children : undefined
})
root = [
{
value: '',
label: localize(
title: localize(
'projects.parent_project_selector_default',
'root <no parent project>'
),
key: '',
value: '',
children: root,
},
...allProjects
.filter((project) => project.id !== excludeProjectId)
.map((project) => ({
value: project.id,
label: project.name,
})),
]

const handleChange = (value: string) => {
onChange(value ?? '')
}

return (
<Select
showSearch
<TreeSelect
allowClear
style={{ width: '100%' }}
treeDefaultExpandAll
placeholder={localize(
'apps.select_parent_project',
'Select a parent project'
)}
optionFilterProp="label"
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
value={selectedProjectId || ''}
onChange={onChange}
options={projectOptions}
onChange={handleChange}
treeData={root}
/>
)
}
Expand Down

0 comments on commit 34db046

Please sign in to comment.