Repository navigation
Expand file tree
/
Copy pathStateInvalidation.php
More file actions
97 lines (87 loc) · 3 KB
/
Copy pathStateInvalidation.php
File metadata and controls
97 lines (87 loc) · 3 KB
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
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Support;
/**
* What a write to a table-state path invalidates.
*
* A decision, not an action: this says *what* must be reset, and the host does
* the resetting. The distinction is what makes the rules readable, because they
* are not uniform and the exceptions are the interesting part.
*
* Narrowing the set — a search, a filter — resets the page **and** the selection
* scope, because "everything the filter matches" was defined by the filter that
* was on screen. Redefining the set while a select-all-matching selection stands
* would silently change what a bulk action is about to touch.
*
* Re-sorting or resizing the page does **not** touch the selection: the same
* records still match, they are merely arranged differently, and a user who
* selected everything and then sorted has not changed their mind about what
* they selected.
*
* A cursor never survives any of it. It points into an ordering that no longer
* exists once the set is narrowed or re-sorted, unlike a page number, which is
* nominally still meaningful.
*/
final readonly class StateInvalidation
{
/**
* The paths that invalidate anything at all. A write below one of these —
* `filters.role.value` under `filters` — counts as a write to it.
*
* @var array<int, string>
*/
private const PATHS = [
'pagination.perPage',
'search',
'filters',
'columnFilters',
'sort.column',
'sort.direction',
];
/**
* The paths that rearrange rather than narrow, and so leave the selection
* alone.
*
* @var array<int, string>
*/
private const REARRANGING = [
'sort.column',
'sort.direction',
'pagination.perPage',
];
private function __construct(
public bool $resetsPage,
public bool $clearsCursor,
public bool $resetsSelectionScope,
public bool $normalisesPerPage,
public bool $marksViewChanged,
) {}
/**
* What this path invalidates, or null when it invalidates nothing.
*
* Null is the ordinary answer: most writes to table state — a modal form
* field, a selection toggle, an expanded row — leave the query alone.
*/
public static function forPath(string $path): ?self
{
$matched = null;
foreach (self::PATHS as $candidate) {
if ($path === $candidate || str_starts_with($path, $candidate.'.')) {
$matched = $candidate;
break;
}
}
if ($matched === null) {
return null;
}
return new self(
resetsPage: true,
clearsCursor: true,
resetsSelectionScope: ! in_array($matched, self::REARRANGING, true),
normalisesPerPage: $matched === 'pagination.perPage',
// The view this render must produce is not the one the poll checksum
// was taken for.
marksViewChanged: true,
);
}
}