2023-03-30 05:16:26 -04:00
|
|
|
import displayNameForUser from '../../../ide/history/util/displayNameForUser'
|
|
|
|
import moment from 'moment/moment'
|
|
|
|
import ColorManager from '../../../ide/colors/ColorManager'
|
|
|
|
import { DocDiffChunk, Highlight } from '../services/types/doc'
|
2023-05-17 06:55:30 -04:00
|
|
|
import { TFunction } from 'react-i18next'
|
2023-03-30 05:16:26 -04:00
|
|
|
|
2023-05-17 06:55:30 -04:00
|
|
|
export function highlightsFromDiffResponse(
|
|
|
|
chunks: DocDiffChunk[],
|
|
|
|
t: TFunction<'translation'> // Must be called `t` for i18next-scanner to find calls to it
|
|
|
|
) {
|
2023-03-30 05:16:26 -04:00
|
|
|
let pos = 0
|
|
|
|
const highlights: Highlight[] = []
|
|
|
|
let doc = ''
|
|
|
|
|
|
|
|
for (const entry of chunks) {
|
|
|
|
const content = entry.u || entry.i || entry.d || ''
|
|
|
|
doc += content
|
|
|
|
const from = pos
|
|
|
|
const to = doc.length
|
|
|
|
pos = to
|
|
|
|
const range = { from, to }
|
|
|
|
|
|
|
|
const isInsertion = typeof entry.i === 'string'
|
|
|
|
const isDeletion = typeof entry.d === 'string'
|
|
|
|
|
|
|
|
if (isInsertion || isDeletion) {
|
|
|
|
const meta = entry.meta
|
|
|
|
if (!meta) {
|
|
|
|
throw new Error('No meta found')
|
|
|
|
}
|
|
|
|
const user = meta.users?.[0]
|
|
|
|
const name = displayNameForUser(user)
|
|
|
|
const date = moment(meta.end_ts).format('Do MMM YYYY, h:mm a')
|
|
|
|
if (isInsertion) {
|
|
|
|
highlights.push({
|
|
|
|
type: 'addition',
|
|
|
|
// There doesn't seem to be a convenient way to make this translatable
|
2023-05-17 06:55:30 -04:00
|
|
|
label: t('added_by_on', { name, date }),
|
2023-03-30 05:16:26 -04:00
|
|
|
range,
|
2023-04-11 04:35:33 -04:00
|
|
|
hue: ColorManager.getHueForUserId(user?.id),
|
2023-03-30 05:16:26 -04:00
|
|
|
})
|
|
|
|
} else if (isDeletion) {
|
|
|
|
highlights.push({
|
|
|
|
type: 'deletion',
|
|
|
|
// There doesn't seem to be a convenient way to make this translatable
|
2023-05-17 06:55:30 -04:00
|
|
|
label: t('deleted_by_on', { name, date }),
|
2023-03-30 05:16:26 -04:00
|
|
|
range,
|
2023-04-11 04:35:33 -04:00
|
|
|
hue: ColorManager.getHueForUserId(user?.id),
|
2023-03-30 05:16:26 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { doc, highlights }
|
|
|
|
}
|