Delete assets/js/dashboard/stats/modals/filter-modal.js with huggingface_hub
Browse files
assets/js/dashboard/stats/modals/filter-modal.js
DELETED
|
@@ -1,261 +0,0 @@
|
|
| 1 |
-
import React from 'react'
|
| 2 |
-
import { useParams } from 'react-router-dom'
|
| 3 |
-
|
| 4 |
-
import { ModalLayout, ModalFooter } from '../../components/modal-layout'
|
| 5 |
-
import {
|
| 6 |
-
EVENT_PROPS_PREFIX,
|
| 7 |
-
FILTER_GROUP_TO_MODAL_TYPE,
|
| 8 |
-
formatFilterGroup,
|
| 9 |
-
FILTER_OPERATIONS,
|
| 10 |
-
getFilterGroup,
|
| 11 |
-
FILTER_MODAL_TO_FILTER_GROUP,
|
| 12 |
-
cleanLabels,
|
| 13 |
-
getAvailableFilterModals
|
| 14 |
-
} from '../../util/filters'
|
| 15 |
-
import { useDashboardStateContext } from '../../dashboard-state-context'
|
| 16 |
-
import { useSiteContext } from '../../site-context'
|
| 17 |
-
import { isModifierPressed, isTyping } from '../../keybinding'
|
| 18 |
-
import FilterModalGroup from './filter-modal-group'
|
| 19 |
-
import { rootRoute } from '../../router'
|
| 20 |
-
import { useAppNavigate } from '../../navigation/use-app-navigate'
|
| 21 |
-
import { SegmentModal } from '../../segments/segment-modals'
|
| 22 |
-
import { findAppliedSegmentFilter } from '../../filtering/segments'
|
| 23 |
-
import { Button } from '../../components/button'
|
| 24 |
-
|
| 25 |
-
function partitionFilters(modalType, filters) {
|
| 26 |
-
const otherFilters = []
|
| 27 |
-
const filterState = {}
|
| 28 |
-
let hasRelevantFilters = false
|
| 29 |
-
|
| 30 |
-
filters.forEach((filter, index) => {
|
| 31 |
-
const filterGroup = getFilterGroup(filter)
|
| 32 |
-
if (FILTER_GROUP_TO_MODAL_TYPE[filterGroup] === modalType) {
|
| 33 |
-
const key = filterState[filterGroup]
|
| 34 |
-
? `${filterGroup}:${index}`
|
| 35 |
-
: filterGroup
|
| 36 |
-
filterState[key] = filter
|
| 37 |
-
hasRelevantFilters = true
|
| 38 |
-
} else {
|
| 39 |
-
otherFilters.push(filter)
|
| 40 |
-
}
|
| 41 |
-
})
|
| 42 |
-
|
| 43 |
-
FILTER_MODAL_TO_FILTER_GROUP[modalType].forEach((filterGroup) => {
|
| 44 |
-
if (!filterState[filterGroup]) {
|
| 45 |
-
filterState[filterGroup] = emptyFilter(filterGroup)
|
| 46 |
-
}
|
| 47 |
-
})
|
| 48 |
-
|
| 49 |
-
return { filterState, otherFilters, hasRelevantFilters }
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
function emptyFilter(key) {
|
| 53 |
-
const filterKey = key === 'props' ? EVENT_PROPS_PREFIX : key
|
| 54 |
-
|
| 55 |
-
return [FILTER_OPERATIONS.is, filterKey, []]
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
class FilterModal extends React.Component {
|
| 59 |
-
constructor(props) {
|
| 60 |
-
super(props)
|
| 61 |
-
|
| 62 |
-
const modalType = this.props.modalType
|
| 63 |
-
|
| 64 |
-
const dashboardState = this.props.dashboardState
|
| 65 |
-
const { filterState, otherFilters, hasRelevantFilters } = partitionFilters(
|
| 66 |
-
modalType,
|
| 67 |
-
dashboardState.filters
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
this.handleKeydown = this.handleKeydown.bind(this)
|
| 71 |
-
this.closeModal = this.closeModal.bind(this)
|
| 72 |
-
this.state = {
|
| 73 |
-
dashboardState,
|
| 74 |
-
filterState,
|
| 75 |
-
labelState: dashboardState.labels,
|
| 76 |
-
otherFilters,
|
| 77 |
-
hasRelevantFilters
|
| 78 |
-
}
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
componentDidMount() {
|
| 82 |
-
document.addEventListener('keydown', this.handleKeydown)
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
componentWillUnmount() {
|
| 86 |
-
document.removeEventListener('keydown', this.handleKeydown)
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
handleKeydown(e) {
|
| 90 |
-
if (isTyping(e) || isModifierPressed(e)) return
|
| 91 |
-
|
| 92 |
-
if (e.target.tagName === 'BODY' && e.key === 'Enter') {
|
| 93 |
-
this.handleSubmit()
|
| 94 |
-
}
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
handleSubmit(e) {
|
| 98 |
-
const filters = Object.values(this.state.filterState)
|
| 99 |
-
.filter(([_op, _key, clauses]) => clauses.length > 0)
|
| 100 |
-
.concat(this.state.otherFilters)
|
| 101 |
-
|
| 102 |
-
this.selectFiltersAndCloseModal(filters)
|
| 103 |
-
e.preventDefault()
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
isDisabled() {
|
| 107 |
-
return Object.values(this.state.filterState).every(
|
| 108 |
-
([_operation, _key, clauses]) => clauses.length === 0
|
| 109 |
-
)
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
closeModal() {
|
| 113 |
-
this.props.navigate({
|
| 114 |
-
path: rootRoute.path,
|
| 115 |
-
search: (search) => search
|
| 116 |
-
})
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
selectFiltersAndCloseModal(filters) {
|
| 120 |
-
this.props.navigate({
|
| 121 |
-
path: rootRoute.path,
|
| 122 |
-
search: (searchRecord) => ({
|
| 123 |
-
...searchRecord,
|
| 124 |
-
filters: filters,
|
| 125 |
-
labels: cleanLabels(filters, this.state.labelState)
|
| 126 |
-
}),
|
| 127 |
-
replace: true
|
| 128 |
-
})
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
onUpdateRowValue(id, newFilter, newLabels) {
|
| 132 |
-
this.setState((prevState) => {
|
| 133 |
-
const [_operation, filterKey, _clauses] = newFilter
|
| 134 |
-
return {
|
| 135 |
-
filterState: {
|
| 136 |
-
...prevState.filterState,
|
| 137 |
-
[id]: newFilter
|
| 138 |
-
},
|
| 139 |
-
labelState: cleanLabels(
|
| 140 |
-
Object.values(this.state.filterState).concat(
|
| 141 |
-
this.state.dashboardState.filters
|
| 142 |
-
),
|
| 143 |
-
prevState.labelState,
|
| 144 |
-
filterKey,
|
| 145 |
-
newLabels
|
| 146 |
-
)
|
| 147 |
-
}
|
| 148 |
-
})
|
| 149 |
-
}
|
| 150 |
-
|
| 151 |
-
onAddRow(filterGroup) {
|
| 152 |
-
this.setState((prevState) => {
|
| 153 |
-
const filter = emptyFilter(filterGroup)
|
| 154 |
-
const id = `${filterGroup}${Object.keys(this.state.filterState).length}`
|
| 155 |
-
|
| 156 |
-
return {
|
| 157 |
-
filterState: {
|
| 158 |
-
...prevState.filterState,
|
| 159 |
-
[id]: filter
|
| 160 |
-
}
|
| 161 |
-
}
|
| 162 |
-
})
|
| 163 |
-
}
|
| 164 |
-
|
| 165 |
-
onDeleteRow(id) {
|
| 166 |
-
this.setState((prevState) => {
|
| 167 |
-
const filterState = { ...prevState.filterState }
|
| 168 |
-
delete filterState[id]
|
| 169 |
-
return { filterState }
|
| 170 |
-
})
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
-
getFilterGroups() {
|
| 174 |
-
const groups = FILTER_MODAL_TO_FILTER_GROUP[this.props.modalType]
|
| 175 |
-
return groups
|
| 176 |
-
}
|
| 177 |
-
|
| 178 |
-
render() {
|
| 179 |
-
return (
|
| 180 |
-
<ModalLayout
|
| 181 |
-
title={`Filter by ${formatFilterGroup(this.props.modalType)}`}
|
| 182 |
-
onClose={this.closeModal}
|
| 183 |
-
>
|
| 184 |
-
<form
|
| 185 |
-
className="flex flex-col gap-y-6"
|
| 186 |
-
onSubmit={this.handleSubmit.bind(this)}
|
| 187 |
-
>
|
| 188 |
-
<div className="flex flex-col gap-y-3 mb-2">
|
| 189 |
-
{this.getFilterGroups().map((filterGroup) => (
|
| 190 |
-
<FilterModalGroup
|
| 191 |
-
key={filterGroup}
|
| 192 |
-
filterGroup={filterGroup}
|
| 193 |
-
filterState={this.state.filterState}
|
| 194 |
-
labels={this.state.labelState}
|
| 195 |
-
onUpdateRowValue={this.onUpdateRowValue.bind(this)}
|
| 196 |
-
onAddRow={this.onAddRow.bind(this)}
|
| 197 |
-
onDeleteRow={this.onDeleteRow.bind(this)}
|
| 198 |
-
/>
|
| 199 |
-
))}
|
| 200 |
-
</div>
|
| 201 |
-
|
| 202 |
-
<ModalFooter>
|
| 203 |
-
{this.state.hasRelevantFilters ? (
|
| 204 |
-
<Button
|
| 205 |
-
theme="secondary"
|
| 206 |
-
size="sm"
|
| 207 |
-
onClick={() => {
|
| 208 |
-
this.selectFiltersAndCloseModal(this.state.otherFilters)
|
| 209 |
-
}}
|
| 210 |
-
>
|
| 211 |
-
{FILTER_MODAL_TO_FILTER_GROUP[this.props.modalType].length > 1
|
| 212 |
-
? 'Remove filters'
|
| 213 |
-
: 'Remove filter'}
|
| 214 |
-
</Button>
|
| 215 |
-
) : (
|
| 216 |
-
<Button
|
| 217 |
-
type="button"
|
| 218 |
-
theme="secondary"
|
| 219 |
-
size="sm"
|
| 220 |
-
onClick={this.closeModal}
|
| 221 |
-
>
|
| 222 |
-
Cancel
|
| 223 |
-
</Button>
|
| 224 |
-
)}
|
| 225 |
-
|
| 226 |
-
<Button type="submit" size="sm" disabled={this.isDisabled()}>
|
| 227 |
-
Apply filter
|
| 228 |
-
</Button>
|
| 229 |
-
</ModalFooter>
|
| 230 |
-
</form>
|
| 231 |
-
</ModalLayout>
|
| 232 |
-
)
|
| 233 |
-
}
|
| 234 |
-
}
|
| 235 |
-
|
| 236 |
-
export default function FilterModalWithRouter(props) {
|
| 237 |
-
const navigate = useAppNavigate()
|
| 238 |
-
const { field } = useParams()
|
| 239 |
-
const { dashboardState } = useDashboardStateContext()
|
| 240 |
-
const site = useSiteContext()
|
| 241 |
-
if (!Object.keys(getAvailableFilterModals(site)).includes(field)) {
|
| 242 |
-
return null
|
| 243 |
-
}
|
| 244 |
-
const appliedSegmentFilter =
|
| 245 |
-
field === 'segment'
|
| 246 |
-
? findAppliedSegmentFilter({ filters: dashboardState.filters })
|
| 247 |
-
: null
|
| 248 |
-
if (appliedSegmentFilter) {
|
| 249 |
-
const [_operation, _dimension, [segmentId]] = appliedSegmentFilter
|
| 250 |
-
return <SegmentModal id={segmentId} />
|
| 251 |
-
}
|
| 252 |
-
return (
|
| 253 |
-
<FilterModal
|
| 254 |
-
{...props}
|
| 255 |
-
modalType={field || 'page'}
|
| 256 |
-
dashboardState={dashboardState}
|
| 257 |
-
navigate={navigate}
|
| 258 |
-
site={site}
|
| 259 |
-
/>
|
| 260 |
-
)
|
| 261 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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