Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { TreeSelect } from "antd";
import type React from "react";
import { type ReactNode, useCallback, useEffect, useRef, useState } from "react";
 
import { Tooltip } from "@humansignal/ui";
 
import "./NewTaxonomy.scss";
import { TaxonomySearch, type TaxonomySearchRef } from "./TaxonomySearch";
 
export type TaxonomyPath = string[];
type onAddLabelCallback = (path: string[]) => any;
type onDeleteLabelCallback = (path: string[]) => any;
 
export type TaxonomyItem = {
  label: string;
  path: TaxonomyPath;
  depth: number;
  isLeaf?: boolean; // only in new async taxonomy
  children?: TaxonomyItem[];
  origin?: "config" | "user" | "session";
  hint?: string;
  color?: string;
};
 
export type AntTaxonomyItem = {
  title: string | JSX.Element;
  value: string;
  key: string;
  isLeaf?: boolean;
  children?: AntTaxonomyItem[];
  disableCheckbox?: boolean;
};
 
type TaxonomyOptions = {
  leafsOnly?: boolean;
  showFullPath?: boolean;
  pathSeparator: string;
  maxUsages?: number;
  maxWidth?: number;
  minWidth?: number;
  dropdownWidth?: number;
  placeholder?: string;
};
 
export type SelectedItem = {
  label: string;
  value: string;
}[];
 
type TaxonomyProps = {
  items: TaxonomyItem[];
  selected: SelectedItem[];
  onChange: (node: any, selected: TaxonomyPath[]) => any;
  onLoadData?: (item: TaxonomyPath) => any;
  onAddLabel?: onAddLabelCallback;
  onDeleteLabel?: onDeleteLabelCallback;
  options: TaxonomyOptions;
  isEditable?: boolean;
  defaultSearch?: boolean;
};
 
type TaxonomyExtendedOptions = TaxonomyOptions & {
  maxUsagesReached?: boolean;
};
 
const convert = (
  items: TaxonomyItem[],
  options: TaxonomyExtendedOptions,
  selectedPaths: string[],
): AntTaxonomyItem[] => {
  // generate string or component to be the `title` of the item
  const enrich = (item: TaxonomyItem) => {
    const color = (item: TaxonomyItem) => (
      // no BEM here to make it more lightweight
      // global classname to allow to change it in Style tag
      <span className="htx-taxonomy-item-color" style={{ background: item.color }}>
        {item.label}
      </span>
    );
 
    if (!item.hint) return item.color ? color(item) : item.label;
 
    return <Tooltip title={item.hint}>{item.color ? color(item) : <span>{item.label}</span>}</Tooltip>;
  };
 
  const convertItem = (item: TaxonomyItem): AntTaxonomyItem => {
    const value = item.path.join(options.pathSeparator);
    const isLeaf = item.isLeaf !== false && !item.children?.length;
    const disabledNode = options.leafsOnly && !isLeaf;
    const maxUsagesReached = options.maxUsagesReached && !selectedPaths.includes(value);
 
    return {
      title: enrich(item),
      value,
      key: value,
      isLeaf,
      disableCheckbox: disabledNode || maxUsagesReached,
      children: item.children?.map(convertItem),
    };
  };
 
  return items.map(convertItem);
};
 
const NewTaxonomy = ({
  items,
  selected,
  onChange,
  onLoadData,
  defaultSearch = true,
  // @todo implement user labels
  // onAddLabel,
  // onDeleteLabel,
  options,
  isEditable = true,
}: TaxonomyProps) => {
  const refInput = useRef<TaxonomySearchRef>(null);
  const [treeData, setTreeData] = useState<AntTaxonomyItem[]>([]);
  const [filteredTreeData, setFilteredTreeData] = useState<AntTaxonomyItem[]>([]);
  const [expandedKeys, setExpandedKeys] = useState<React.Key[] | undefined>([]);
  const separator = options.pathSeparator;
  const style = { minWidth: options.minWidth ?? 200, maxWidth: options.maxWidth };
  const dropdownWidth = options.dropdownWidth === undefined ? true : +options.dropdownWidth;
  const maxUsagesReached = !!options.maxUsages && selected.length >= options.maxUsages;
  const value = selected.map((path) => path.map((p) => p.value).join(separator));
  const displayed = selected.map((path) => ({
    value: path.map((p) => p.value).join(separator),
    label: options.showFullPath ? path.map((p) => p.label).join(separator) : path.at(-1).label,
  }));
 
  useEffect(() => {
    setTreeData(convert(items, { ...options, maxUsagesReached }, value));
  }, [items, maxUsagesReached]);
 
  const loadData = useCallback(async (node: any) => {
    return onLoadData?.(node.value.split(separator));
  }, []);
 
  const handleSearch = useCallback((list: AntTaxonomyItem[], expandedKeys: React.Key[] | null) => {
    setFilteredTreeData(list);
    if (expandedKeys?.length) setExpandedKeys(expandedKeys);
    else setExpandedKeys(undefined);
  }, []);
 
  const renderDropdown = useCallback(
    (origin: ReactNode) => {
      return (
        <>
          {!defaultSearch && <TaxonomySearch ref={refInput} treeData={treeData} onChange={handleSearch} />}
          {origin}
        </>
      );
    },
    [treeData],
  );
 
  const handleDropdownChange = useCallback(
    (open: boolean) => {
      if (open) {
        // handleDropdownChange is being called before the dropdown is rendered,
        // 200ms is the time that we have to wait to dropdown be rendered and animated
        setTimeout(() => {
          refInput.current?.focus();
        }, 200);
      } else {
        refInput.current?.resetValue();
      }
    },
    [refInput],
  );
 
  return (
    <TreeSelect
      treeData={defaultSearch ? treeData : filteredTreeData}
      value={displayed}
      labelInValue={true}
      onChange={(items) =>
        onChange(
          null,
          items.map((item) => item.value.split(separator)),
        )
      }
      loadData={loadData}
      treeCheckable
      showSearch={defaultSearch}
      showArrow={!defaultSearch}
      dropdownRender={renderDropdown}
      onDropdownVisibleChange={handleDropdownChange}
      treeExpandedKeys={!defaultSearch ? expandedKeys : undefined}
      onTreeExpand={(expandedKeys: React.Key[]) => {
        setExpandedKeys(expandedKeys);
      }}
      treeCheckStrictly
      showCheckedStrategy={TreeSelect.SHOW_ALL}
      treeExpandAction={false}
      dropdownMatchSelectWidth={dropdownWidth}
      placeholder={options.placeholder || "Click to add..."}
      style={style}
      className="htx-taxonomy"
      popupClassName="htx-taxonomy-dropdown"
      disabled={!isEditable}
    />
  );
};
 
export { NewTaxonomy };