MCPcopy
hub / github.com/electerm/electerm / buildVisibleTreeRows

Function buildVisibleTreeRows

src/client/components/tree-list/tree-list-rows.js:7–109  ·  view source on GitHub ↗
({
  bookmarkGroups,
  bookmarkGroupTree,
  bookmarksMap,
  expandedKeys,
  keyword
})

Source from the content-addressed store, hash-verified

5}
6
7export function buildVisibleTreeRows ({
8 bookmarkGroups,
9 bookmarkGroupTree,
10 bookmarksMap,
11 expandedKeys,
12 keyword
13}) {
14 const groupTree = bookmarkGroupTree || {}
15 const rows = []
16 const matchedRowKeys = []
17 const expandedKeySet = new Set(expandedKeys || [])
18 const lowerKeyword = (keyword || '').toLowerCase()
19 const bookmarkMatchCache = new Map()
20 const groupMatchCache = new Map()
21
22 const bookmarkMatches = (bookmarkId) => {
23 if (bookmarkMatchCache.has(bookmarkId)) {
24 return bookmarkMatchCache.get(bookmarkId)
25 }
26 const item = bookmarksMap.get(bookmarkId)
27 const matched = Boolean(
28 item &&
29 (!lowerKeyword ||
30 createName(item).toLowerCase().includes(lowerKeyword) ||
31 (item.description || '').toLowerCase().includes(lowerKeyword))
32 )
33 bookmarkMatchCache.set(bookmarkId, matched)
34 return matched
35 }
36
37 const groupHasMatchedBookmarks = (group) => {
38 if (!lowerKeyword) {
39 return true
40 }
41 if (!group) {
42 return false
43 }
44 if (groupMatchCache.has(group.id)) {
45 return groupMatchCache.get(group.id)
46 }
47 const hasMatch = (group.bookmarkIds || []).some(bookmarkMatches) ||
48 (group.bookmarkGroupIds || []).some(id => {
49 return groupHasMatchedBookmarks(groupTree[id])
50 })
51 groupMatchCache.set(group.id, hasMatch)
52 return hasMatch
53 }
54
55 const visitGroup = (group, parentId = '', depth = 1) => {
56 if (!group || (lowerKeyword && !groupHasMatchedBookmarks(group))) {
57 return
58 }
59
60 rows.push({
61 key: `group:${group.id}`,
62 item: group,
63 isGroup: true,
64 parentId,

Callers 1

ItemListTreeClass · 0.90

Calls 2

isTopLevelGroupFunction · 0.85
visitGroupFunction · 0.85

Tested by

no test coverage detected