|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-present, Parse, LLC |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the LICENSE file in |
| 6 | + * the root directory of this source tree. |
| 7 | + */ |
| 8 | +import React from 'react'; |
| 9 | +import { diffLines, diffChars } from 'diff'; |
| 10 | +import styles from 'dashboard/Data/Config/ConfigConflictDiff.scss'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Serialize a config value to a string suitable for diffing. |
| 14 | + * Both server and user values go through this to ensure consistent formatting. |
| 15 | + */ |
| 16 | +function serializeForDiff(value, type, isUserValue = false) { |
| 17 | + if (value === null || value === undefined) { |
| 18 | + return 'null'; |
| 19 | + } |
| 20 | + |
| 21 | + switch (type) { |
| 22 | + case 'Object': |
| 23 | + case 'Array': { |
| 24 | + // User values from ConfigDialog are already JSON strings for Object/Array |
| 25 | + if (isUserValue && typeof value === 'string') { |
| 26 | + try { |
| 27 | + return JSON.stringify(JSON.parse(value), null, 2); |
| 28 | + } catch { |
| 29 | + return value; |
| 30 | + } |
| 31 | + } |
| 32 | + return JSON.stringify(value, null, 2); |
| 33 | + } |
| 34 | + case 'Boolean': |
| 35 | + return String(value); |
| 36 | + case 'Number': |
| 37 | + return String(value); |
| 38 | + case 'Date': { |
| 39 | + if (typeof value === 'string') { |
| 40 | + return value; |
| 41 | + } |
| 42 | + if (value instanceof Date) { |
| 43 | + return value.toISOString(); |
| 44 | + } |
| 45 | + if (value && value.iso) { |
| 46 | + return value.iso; |
| 47 | + } |
| 48 | + return String(value); |
| 49 | + } |
| 50 | + case 'GeoPoint': { |
| 51 | + if (value && typeof value.toJSON === 'function') { |
| 52 | + const json = value.toJSON(); |
| 53 | + return JSON.stringify({ latitude: json.latitude, longitude: json.longitude }, null, 2); |
| 54 | + } |
| 55 | + if (value && (value.latitude !== undefined || value.longitude !== undefined)) { |
| 56 | + return JSON.stringify({ latitude: value.latitude, longitude: value.longitude }, null, 2); |
| 57 | + } |
| 58 | + return JSON.stringify(value, null, 2); |
| 59 | + } |
| 60 | + case 'File': { |
| 61 | + if (value && typeof value.toJSON === 'function') { |
| 62 | + const json = value.toJSON(); |
| 63 | + return JSON.stringify({ name: json.name, url: json.url }, null, 2); |
| 64 | + } |
| 65 | + if (value && (value._name !== undefined || value.name !== undefined)) { |
| 66 | + return JSON.stringify({ name: value._name || value.name, url: value._url || value.url }, null, 2); |
| 67 | + } |
| 68 | + return JSON.stringify(value, null, 2); |
| 69 | + } |
| 70 | + case 'String': |
| 71 | + default: |
| 72 | + return String(value); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Render a line with character-level highlighting. |
| 78 | + * charDiffs is the result of diffChars() for this line pair. |
| 79 | + * side is 'removed' or 'added'. |
| 80 | + */ |
| 81 | +function renderCharHighlightedContent(charDiffs, side) { |
| 82 | + return charDiffs.map((part, i) => { |
| 83 | + if (part.added && side === 'added') { |
| 84 | + return <span key={i} className={styles.charAdded}>{part.value}</span>; |
| 85 | + } |
| 86 | + if (part.removed && side === 'removed') { |
| 87 | + return <span key={i} className={styles.charRemoved}>{part.value}</span>; |
| 88 | + } |
| 89 | + if (!part.added && !part.removed) { |
| 90 | + return <span key={i}>{part.value}</span>; |
| 91 | + } |
| 92 | + return null; |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * ConfigConflictDiff displays a GitHub-style unified diff between |
| 98 | + * the server's latest value and the user's edited value. |
| 99 | + */ |
| 100 | +const ConfigConflictDiff = ({ serverValue, userValue, type }) => { |
| 101 | + const serverStr = serializeForDiff(serverValue, type, false); |
| 102 | + const userStr = serializeForDiff(userValue, type, true); |
| 103 | + |
| 104 | + const lineDiffs = diffLines(serverStr, userStr); |
| 105 | + |
| 106 | + // Build diff lines with character-level highlighting |
| 107 | + const rows = []; |
| 108 | + let oldLineNum = 1; |
| 109 | + let newLineNum = 1; |
| 110 | + |
| 111 | + for (let i = 0; i < lineDiffs.length; i++) { |
| 112 | + const part = lineDiffs[i]; |
| 113 | + const lines = part.value.replace(/\n$/, '').split('\n'); |
| 114 | + |
| 115 | + if (part.removed) { |
| 116 | + // Check if next part is an addition (paired change for char-level diff) |
| 117 | + const nextPart = lineDiffs[i + 1]; |
| 118 | + const hasCharDiff = nextPart && nextPart.added; |
| 119 | + let charDiffResult = null; |
| 120 | + |
| 121 | + if (hasCharDiff) { |
| 122 | + charDiffResult = diffChars(part.value, nextPart.value); |
| 123 | + } |
| 124 | + |
| 125 | + for (const line of lines) { |
| 126 | + rows.push({ |
| 127 | + type: 'removed', |
| 128 | + oldNum: oldLineNum++, |
| 129 | + newNum: null, |
| 130 | + prefix: '-', |
| 131 | + content: line, |
| 132 | + charDiffs: charDiffResult, |
| 133 | + charSide: 'removed', |
| 134 | + singleLineDiff: lines.length === 1, |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + if (hasCharDiff) { |
| 139 | + const addedLines = nextPart.value.replace(/\n$/, '').split('\n'); |
| 140 | + for (const line of addedLines) { |
| 141 | + rows.push({ |
| 142 | + type: 'added', |
| 143 | + oldNum: null, |
| 144 | + newNum: newLineNum++, |
| 145 | + prefix: '+', |
| 146 | + content: line, |
| 147 | + charDiffs: charDiffResult, |
| 148 | + charSide: 'added', |
| 149 | + singleLineDiff: addedLines.length === 1, |
| 150 | + }); |
| 151 | + } |
| 152 | + i++; // Skip the next (added) part since we handled it |
| 153 | + } |
| 154 | + } else if (part.added) { |
| 155 | + for (const line of lines) { |
| 156 | + rows.push({ |
| 157 | + type: 'added', |
| 158 | + oldNum: null, |
| 159 | + newNum: newLineNum++, |
| 160 | + prefix: '+', |
| 161 | + content: line, |
| 162 | + charDiffs: null, |
| 163 | + charSide: null, |
| 164 | + singleLineDiff: false, |
| 165 | + }); |
| 166 | + } |
| 167 | + } else { |
| 168 | + for (const line of lines) { |
| 169 | + rows.push({ |
| 170 | + type: 'context', |
| 171 | + oldNum: oldLineNum++, |
| 172 | + newNum: newLineNum++, |
| 173 | + prefix: ' ', |
| 174 | + content: line, |
| 175 | + charDiffs: null, |
| 176 | + charSide: null, |
| 177 | + singleLineDiff: false, |
| 178 | + }); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if (rows.length === 0 || (rows.length === 1 && rows[0].type === 'context' && rows[0].content === '')) { |
| 184 | + return <div className={styles.emptyDiff}>Values are identical — no differences found.</div>; |
| 185 | + } |
| 186 | + |
| 187 | + // For character-level highlighting within a single line, |
| 188 | + // we need to map charDiffs to individual lines. Since diffChars |
| 189 | + // operates on the full block text, for single-line values we can |
| 190 | + // highlight directly. For multi-line, we show line-level coloring only. |
| 191 | + const renderContent = (row) => { |
| 192 | + if (row.charDiffs && row.singleLineDiff) { |
| 193 | + return renderCharHighlightedContent(row.charDiffs, row.charSide); |
| 194 | + } |
| 195 | + return row.content; |
| 196 | + }; |
| 197 | + |
| 198 | + const lineStyle = (row) => { |
| 199 | + if (row.type === 'removed') { |
| 200 | + return styles.lineRemoved; |
| 201 | + } |
| 202 | + if (row.type === 'added') { |
| 203 | + return styles.lineAdded; |
| 204 | + } |
| 205 | + return styles.lineContext; |
| 206 | + }; |
| 207 | + |
| 208 | + return ( |
| 209 | + <div className={styles.container}> |
| 210 | + <table className={styles.table}> |
| 211 | + <tbody> |
| 212 | + {rows.map((row, idx) => ( |
| 213 | + <tr key={idx} className={lineStyle(row)}> |
| 214 | + <td className={styles.lineNumber}>{row.oldNum ?? ''}</td> |
| 215 | + <td className={styles.lineNumber}>{row.newNum ?? ''}</td> |
| 216 | + <td className={styles.prefix}>{row.prefix}</td> |
| 217 | + <td className={styles.content}>{renderContent(row)}</td> |
| 218 | + </tr> |
| 219 | + ))} |
| 220 | + </tbody> |
| 221 | + </table> |
| 222 | + </div> |
| 223 | + ); |
| 224 | +}; |
| 225 | + |
| 226 | +export default ConfigConflictDiff; |
0 commit comments