/
nav.ts
102 lines (90 loc) · 3.36 KB
/
nav.ts
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
/* Copyright 2018 Spotify AB. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { matchPath } from 'react-router'
import { LOCATION_CHANGE, LocationChangeAction } from 'react-router-redux'
import { Action } from 'redux'
import { isType } from 'typescript-fsa'
import actions from '../actions'
export interface INav {
// The currently selected type
readonly type: string | null
// Types that are expanded in the browse tree
readonly expandedTypes: ReadonlyArray<string>
// The current search query, if any
readonly query: null | string
// Whether to show the browse tree
readonly showTree: boolean
}
const INITIAL_STATE: INav = {
expandedTypes: [],
query: null,
showTree: true,
type: null
}
// Generates the IDs of all parent types for a given type (including the type itself).
const withParentTypes = (type: string): string[] => {
const newExpandedTypes = []
for (let i = type.length; i > 0; i = type.lastIndexOf('.', i - 1)) {
newExpandedTypes.push(type.substr(0, i))
}
return newExpandedTypes
}
// Makes the specified type the selected type, making sure to expand any parent nodes.
const makeSelected = (type: string, state: INav): INav => {
const newExpandedTypes = withParentTypes(type)
// explicitly don't include self; only expand the path to self
newExpandedTypes.splice(newExpandedTypes.indexOf(type), 1)
return { ...state, type, expandedTypes: state.expandedTypes.concat(newExpandedTypes), query: null }
}
// A reducer that handles user navigation
export default (state: INav = INITIAL_STATE, action: Action): INav => {
if (isType(action, actions.nav.selectType)) {
return makeSelected(action.payload.type, state)
}
if (isType(action, actions.nav.expandType)) {
const newExpandedTypes = withParentTypes(action.payload.type)
return { ...state, expandedTypes: state.expandedTypes.concat(newExpandedTypes) }
}
if (isType(action, actions.nav.collapseType)) {
// This makes sure to also collapse any child nodes
const expandedTypes = state.expandedTypes.filter((t) => !t.startsWith(action.payload.type))
return {
...state,
expandedTypes
}
}
if (isType(action, actions.nav.search)) {
return { ...state, query: action.payload.query }
}
if (isType(action, actions.nav.showTree)) {
return { ...state, showTree: true }
}
if (isType(action, actions.nav.hideTree)) {
return { ...state, showTree: false }
}
if (action.type === LOCATION_CHANGE) {
const locationChangeAction = action as LocationChangeAction
const match = matchPath(locationChangeAction.payload.pathname, { path: '/:fullName' })
if (match) {
const params = match.params
if ('fullName' in params) {
const typedParams = params as {fullName: string}
const type = '.' + typedParams.fullName
return makeSelected(type, state)
}
}
}
return state
}