|
| 1 | +// Stands in for staging a dependency in a change request (#8449) until the API |
| 2 | +// supports it. The API ignores ?change_request today and would publish the |
| 3 | +// change live, so a staged change never reaches it. Delete this file, and the |
| 4 | +// branches in useFeatureDependency that call it, once #8449 ships. |
| 5 | +import { |
| 6 | + DependencyConflictCode, |
| 7 | + DependencyEdge, |
| 8 | + DependencyFeature, |
| 9 | + StagedDependencyChange, |
| 10 | +} from 'common/types/responses' |
| 11 | +import { stagedDependencyEdge } from 'common/utils/stagedDependencyEdge' |
| 12 | + |
| 13 | +type StagedChanges = Record<string, StagedDependencyChange[]> |
| 14 | + |
| 15 | +// In local storage so the change request page can read what the feature modal |
| 16 | +// staged, across navigations and reloads. |
| 17 | +const STORAGE_KEY = 'fakeChangeRequestDependencies' |
| 18 | + |
| 19 | +const read = (): StagedChanges => { |
| 20 | + try { |
| 21 | + return JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}') |
| 22 | + } catch { |
| 23 | + return {} |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const write = (changes: StagedChanges) => { |
| 28 | + try { |
| 29 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(changes)) |
| 30 | + } catch { |
| 31 | + // Private mode or blocked storage: the change is lost on reload. |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const keyOf = (changeRequestId: number, featureId: number) => |
| 36 | + `${changeRequestId}-${featureId}` |
| 37 | + |
| 38 | +export const getStagedDependencyChanges = ( |
| 39 | + changeRequestId: number, |
| 40 | + featureId: number, |
| 41 | +): StagedDependencyChange[] => read()[keyOf(changeRequestId, featureId)] ?? [] |
| 42 | + |
| 43 | +export type FakeConflict = { code: DependencyConflictCode; message: string } |
| 44 | + |
| 45 | +type StageArgs = { |
| 46 | + changeRequestId: number |
| 47 | + featureId: number |
| 48 | + prerequisite: DependencyFeature |
| 49 | + // What is live today, read from the real API by the caller. |
| 50 | + liveEdges: DependencyEdge[] |
| 51 | + prerequisiteEdges: DependencyEdge[] |
| 52 | + dependentEdges: DependencyEdge[] |
| 53 | +} |
| 54 | + |
| 55 | +// The same refusals the API gives a direct add, checked against live data. |
| 56 | +export const stageAdd = ({ |
| 57 | + changeRequestId, |
| 58 | + dependentEdges, |
| 59 | + featureId, |
| 60 | + liveEdges, |
| 61 | + prerequisite, |
| 62 | + prerequisiteEdges, |
| 63 | +}: StageArgs): FakeConflict | null => { |
| 64 | + const changes = read() |
| 65 | + const key = keyOf(changeRequestId, featureId) |
| 66 | + const staged = changes[key] ?? [] |
| 67 | + const isLive = liveEdges.some((e) => e.prerequisite.id === prerequisite.id) |
| 68 | + if (isLive || staged.some((c) => c.prerequisite.id === prerequisite.id)) { |
| 69 | + return { |
| 70 | + code: 'dependency_exists', |
| 71 | + message: `"${prerequisite.name}" is already a prerequisite.`, |
| 72 | + } |
| 73 | + } |
| 74 | + if (dependentEdges.length) { |
| 75 | + const [edge] = dependentEdges |
| 76 | + return { |
| 77 | + code: 'feature_is_prerequisite', |
| 78 | + message: `The feature "${edge.prerequisite.name}" is already a prerequisite for the feature "${edge.feature.name}".`, |
| 79 | + } |
| 80 | + } |
| 81 | + const ownEdge = prerequisiteEdges.find( |
| 82 | + (e) => e.feature.id === prerequisite.id, |
| 83 | + ) |
| 84 | + if (ownEdge) { |
| 85 | + return { |
| 86 | + code: 'prerequisite_has_prerequisite', |
| 87 | + message: `The prerequisite "${prerequisite.name}" already has a prerequisite "${ownEdge.prerequisite.name}".`, |
| 88 | + } |
| 89 | + } |
| 90 | + changes[key] = [...staged, { action: 'add', prerequisite }] |
| 91 | + write(changes) |
| 92 | + return null |
| 93 | +} |
| 94 | + |
| 95 | +// Removing a staged add drops it rather than staging both, as #8449 specifies. |
| 96 | +export const stageRemove = ( |
| 97 | + changeRequestId: number, |
| 98 | + featureId: number, |
| 99 | + prerequisite: DependencyFeature, |
| 100 | +) => { |
| 101 | + const changes = read() |
| 102 | + const key = keyOf(changeRequestId, featureId) |
| 103 | + const staged = changes[key] ?? [] |
| 104 | + const stagedAdd = staged.some( |
| 105 | + (c) => c.action === 'add' && c.prerequisite.id === prerequisite.id, |
| 106 | + ) |
| 107 | + changes[key] = stagedAdd |
| 108 | + ? staged.filter((c) => c.prerequisite.id !== prerequisite.id) |
| 109 | + : [...staged, { action: 'remove', prerequisite }] |
| 110 | + write(changes) |
| 111 | +} |
| 112 | + |
| 113 | +// As #8720 answers GET ?change_request: the list as if the change request |
| 114 | +// were live. |
| 115 | +export const applyStagedChanges = ( |
| 116 | + changeRequestId: number, |
| 117 | + featureId: number, |
| 118 | + liveEdges: DependencyEdge[], |
| 119 | +): DependencyEdge[] => { |
| 120 | + const staged = getStagedDependencyChanges(changeRequestId, featureId) |
| 121 | + const removed = (id: number) => |
| 122 | + staged.some((c) => c.action === 'remove' && c.prerequisite.id === id) |
| 123 | + return [ |
| 124 | + ...liveEdges.filter((edge) => !removed(edge.prerequisite.id)), |
| 125 | + ...staged |
| 126 | + .filter((c) => c.action === 'add') |
| 127 | + .map((c) => stagedDependencyEdge(featureId, c.prerequisite)), |
| 128 | + ] |
| 129 | +} |
| 130 | + |
| 131 | +const stagedKeys = () => |
| 132 | + Object.entries(read()) |
| 133 | + .filter(([, changes]) => changes.length) |
| 134 | + .map(([key]) => key.split('-').map(Number)) |
| 135 | + |
| 136 | +// Stand in for listing change requests by feature, and a change request's |
| 137 | +// change sets, until real change sets carry the staged dependencies. |
| 138 | +export const getChangeRequestIdsStaging = (featureId: number) => |
| 139 | + stagedKeys() |
| 140 | + .filter(([, id]) => id === featureId) |
| 141 | + .map(([changeRequestId]) => changeRequestId) |
| 142 | + |
| 143 | +export const getFeatureIdsStaged = (changeRequestId: number) => |
| 144 | + stagedKeys() |
| 145 | + .filter(([id]) => id === changeRequestId) |
| 146 | + .map(([, featureId]) => featureId) |
| 147 | + |
| 148 | +// A deleted change request takes its staged changes with it. |
| 149 | +export const discardChangeRequest = (changeRequestId: number) => { |
| 150 | + const changes = read() |
| 151 | + Object.keys(changes) |
| 152 | + .filter((key) => key.startsWith(`${changeRequestId}-`)) |
| 153 | + .forEach((key) => delete changes[key]) |
| 154 | + write(changes) |
| 155 | +} |
0 commit comments