File size: 3,938 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useSyncExternalStore } from 'react'
import type { SegmentNodeState } from '../userspace/app/segment-explorer-node'

/**
 * Trie data structure for storing and searching paths
 *
 * This can be used to store app router paths and search for them efficiently.
 * e.g.
 *
 * [trie root]
 *   β”œβ”€β”€ layout.js
 *   β”œβ”€β”€ page.js
 *   β”œβ”€β”€ blog
 *       β”œβ”€β”€ layout.js
 *       β”œβ”€β”€ page.js
 *       β”œβ”€β”€ [slug]
 *          β”œβ”€β”€ layout.js
 *          β”œβ”€β”€ page.js
 **/

type TrieNode<Value = string> = {
  value: Value | undefined
  children: {
    [key: string]: TrieNode<Value> | undefined
  }
}

type Trie<Value = string> = {
  insert: (value: Value) => void
  remove: (value: Value) => void
  getRoot: () => TrieNode<Value>
}

const listeners = new Set<() => void>()
const createSegmentTreeStore = (): {
  subscribe: (callback: () => void) => () => void
  getSnapshot: () => SegmentTrieNode
  getServerSnapshot: () => SegmentTrieNode
} => {
  // return a store that can be used by useSyncExternalStore
  return {
    subscribe: (callback) => {
      listeners.add(callback)
      return () => listeners.delete(callback)
    },
    getSnapshot: () => {
      return trie.getRoot()
    },
    getServerSnapshot: () => {
      return trie.getRoot()
    },
  }
}

// TODO: Move the Segment Tree into React State
const { subscribe, getSnapshot, getServerSnapshot } = createSegmentTreeStore()

function createTrie<Value = string>({
  getCharacters = (item: Value) => [item] as string[],
  compare = (a: Value | undefined, b: Value | undefined) => a === b,
}: {
  getCharacters?: (item: Value) => string[]
  compare?: (a: Value | undefined, b: Value | undefined) => boolean
}): Trie<Value> {
  let root: TrieNode<Value> = {
    value: undefined,
    children: {},
  }

  function markUpdated() {
    for (const listener of listeners) {
      listener()
    }
  }

  function insert(value: Value) {
    let currentNode = root
    const segments = getCharacters(value)

    for (const segment of segments) {
      if (!currentNode.children[segment]) {
        currentNode.children[segment] = {
          value: undefined,
          // Skip value for intermediate nodes
          children: {},
        }
      }
      currentNode = currentNode.children[segment]
    }

    currentNode.value = value

    root = { ...root }
    markUpdated()
  }

  function remove(value: Value) {
    let currentNode = root
    const segments = getCharacters(value)

    const stack: TrieNode<Value>[] = []
    let found = true
    for (const segment of segments) {
      if (!currentNode.children[segment]) {
        found = false
        break
      }
      stack.push(currentNode)
      currentNode = currentNode.children[segment]!
    }
    // If the value is not found, skip removal
    if (!found || !compare(currentNode.value, value)) {
      return
    }
    currentNode.value = undefined
    for (let i = stack.length - 1; i >= 0; i--) {
      const parentNode = stack[i]
      const segment = segments[i]
      if (Object.keys(parentNode.children[segment]!.children).length === 0) {
        delete parentNode.children[segment]
      }
    }

    root = { ...root }
    markUpdated()
  }

  function getRoot(): TrieNode<Value> {
    return root
  }

  return { insert, remove, getRoot }
}

export type SegmentTrie = Trie<SegmentNodeState>
export type SegmentTrieNode = TrieNode<SegmentNodeState>

const trie: SegmentTrie = createTrie({
  compare: (a, b) => {
    if (!a || !b) return false
    return (
      a.pagePath === b.pagePath &&
      a.type === b.type &&
      a.boundaryType === b.boundaryType
    )
  },
  getCharacters: (item) => item.pagePath.split('/'),
})
export const insertSegmentNode = trie.insert
export const removeSegmentNode = trie.remove

export function useSegmentTree(): SegmentTrieNode {
  const state = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
  return state
}