|
| 1 | +import { reduceArray, restoreArray } from "@mendix/filter-commons/condition-utils"; |
| 2 | +import { disposeBatch } from "@mendix/widget-plugin-mobx-kit/disposeBatch"; |
| 3 | +import { FilterCondition } from "mendix/filters"; |
| 4 | +import { autorun, reaction } from "mobx"; |
| 5 | +import { fnv1aHash } from "../../utils/fnv-1a-hash"; |
| 6 | + |
| 7 | +type ConditionWithMeta = { |
| 8 | + cond: FilterCondition | undefined; |
| 9 | + meta: string; |
| 10 | +}; |
| 11 | + |
| 12 | +interface ObservableInput { |
| 13 | + condWithMeta: ConditionWithMeta; |
| 14 | + metaKey: string; |
| 15 | + hydrate(value: ConditionWithMeta): void; |
| 16 | +} |
| 17 | + |
| 18 | +type MetaBag = Record<string, string>; |
| 19 | + |
| 20 | +export class CombinedFilter { |
| 21 | + private _inputs: ObservableInput[]; |
| 22 | + readonly stableKey: string; |
| 23 | + readonly ownMetaKey = "CombinedFilter"; |
| 24 | + |
| 25 | + constructor(spec: { stableKey: string; inputs: ObservableInput[] }) { |
| 26 | + this._inputs = spec.inputs; |
| 27 | + this.stableKey = spec.stableKey; |
| 28 | + } |
| 29 | + |
| 30 | + storageKey(hash: string): string { |
| 31 | + return `${this.stableKey}-${hash}`; |
| 32 | + } |
| 33 | + |
| 34 | + readMetaFromStorage(key: string): MetaBag | null { |
| 35 | + const item = sessionStorage.getItem(key); |
| 36 | + if (!item) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + try { |
| 40 | + return JSON.parse(item) as MetaBag; |
| 41 | + } catch (e) { |
| 42 | + console.error(`CombinedFilter.readFilterMeta: Error parsing meta for key ${key}`, e); |
| 43 | + return null; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + clearFilterMeta(hash: string): void { |
| 48 | + sessionStorage.removeItem(this.storageKey(hash)); |
| 49 | + } |
| 50 | + |
| 51 | + filterHash(filter: FilterCondition): string { |
| 52 | + return fnv1aHash(JSON.stringify(filter)).toString(); |
| 53 | + } |
| 54 | + |
| 55 | + restoreMeta(filter: FilterCondition): MetaBag | null { |
| 56 | + const hash = this.filterHash(filter); |
| 57 | + const key = this.storageKey(hash); |
| 58 | + return this.readMetaFromStorage(key); |
| 59 | + } |
| 60 | + |
| 61 | + hydrate(filter: FilterCondition | undefined): void { |
| 62 | + if (!filter) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const meta = this.restoreMeta(filter); |
| 67 | + if (!meta) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const conditions = restoreArray(filter, meta[this.ownMetaKey]); |
| 72 | + if (conditions.length !== this._inputs.length) { |
| 73 | + console.error( |
| 74 | + `CombinedFilter.hydrate: Number of conditions (${conditions.length}) does not match number of inputs (${this._inputs.length})` |
| 75 | + ); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + for (let i = 0; i < this._inputs.length; i++) { |
| 80 | + const input = this._inputs[i]; |
| 81 | + const condWithMeta: ConditionWithMeta = { |
| 82 | + cond: conditions[i], |
| 83 | + meta: meta[input.metaKey] |
| 84 | + }; |
| 85 | + |
| 86 | + input.hydrate(condWithMeta); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + get filter(): FilterCondition | undefined { |
| 91 | + return this.filterWithBag.filter; |
| 92 | + } |
| 93 | + |
| 94 | + get filterWithBag(): { |
| 95 | + filter: FilterCondition | undefined; |
| 96 | + bag: MetaBag; |
| 97 | + hash: string | null; |
| 98 | + } { |
| 99 | + const bag: MetaBag = {}; |
| 100 | + const conditions: Array<FilterCondition | undefined> = []; |
| 101 | + |
| 102 | + for (const { condWithMeta: data, metaKey } of this._inputs) { |
| 103 | + bag[metaKey] = data.meta; |
| 104 | + conditions.push(data.cond); |
| 105 | + } |
| 106 | + |
| 107 | + const [filter, meta] = reduceArray(conditions); |
| 108 | + bag[this.ownMetaKey] = meta; |
| 109 | + |
| 110 | + return { filter, bag, hash: filter ? this.filterHash(filter) : null }; |
| 111 | + } |
| 112 | + |
| 113 | + saveFilterMeta(hash: string | null, bag: MetaBag): void { |
| 114 | + if (!hash) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + sessionStorage.setItem(this.storageKey(hash), JSON.stringify(bag)); |
| 119 | + } |
| 120 | + |
| 121 | + setup(): () => void { |
| 122 | + const [add, disposeAll] = disposeBatch(); |
| 123 | + |
| 124 | + add(autorun(() => console.dir(this.filter))); |
| 125 | + |
| 126 | + add( |
| 127 | + reaction( |
| 128 | + () => this.filterWithBag, |
| 129 | + (next, prev) => { |
| 130 | + if (prev && prev.hash) { |
| 131 | + this.clearFilterMeta(prev.hash); |
| 132 | + } |
| 133 | + if (next.hash) { |
| 134 | + this.saveFilterMeta(next.hash, next.bag); |
| 135 | + } |
| 136 | + } |
| 137 | + ) |
| 138 | + ); |
| 139 | + |
| 140 | + return disposeAll; |
| 141 | + } |
| 142 | +} |
0 commit comments