Skip to content

Commit

Permalink
fix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov committed Oct 7, 2024
1 parent 58ab62d commit 45dc646
Showing 1 changed file with 77 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,98 +36,6 @@ const Loader = () => (
</div>
)

const NewDiffRenderer = ({
headName,
hashedPath,
segments,
ignoredUploadIds,
}) => {
return segments?.map((segment, segmentIndex) => {
let content = ''
const lineData = []
segment.lines.forEach((line, lineIndex) => {
content += line.content

if (lineIndex < segment.lines.length - 1) {
content += '\n'
}

lineData.push({
headNumber: line?.headNumber,
baseNumber: line?.baseNumber,
headCoverage: line?.headCoverage,
baseCoverage: line?.baseCoverage,
hitCount: without(line?.coverageInfo?.hitUploadIds, ...ignoredUploadIds)
.length,
})
})

return (
<VirtualDiffRenderer
key={segmentIndex}
code={content}
fileName={headName}
hashedPath={hashedPath}
lineData={lineData}
/>
)
})
}

const OldDiffRenderer = ({
headName,
fileLabel,
fullFilePath,
segments,
ignoredUploadIds,
stickyPadding,
comparisonData,
}) => {
return segments?.map((segment, segmentIndex) => {
const content = segment.lines.map((line) => line.content).join('\n')
return (
<Fragment key={`${headName}-${segmentIndex}`}>
<CodeRendererInfoRow>
<div className="flex w-full justify-between">
<div className="flex gap-1">
<span data-testid="patch">{segment?.header}</span>
{fileLabel && (
<span className="border-l-2 pl-2">{fileLabel}</span>
)}
</div>
<A href={fullFilePath} isExternal hook="commit full file">
View full file
</A>
</div>
</CodeRendererInfoRow>
<CodeRenderer
code={content}
fileName={headName}
rendererType={CODE_RENDERER_TYPE.DIFF}
LineComponent={({ i, line, ...props }) => (
<DiffLine
// If this line one of the first 3 or last three lines of the segment
key={i + 1}
lineContent={line}
edgeOfFile={i <= 2 || i >= segment.lines.length - 3}
path={comparisonData?.hashedPath}
hitCount={
without(
segment?.lines?.[i]?.coverageInfo?.hitUploadIds,
...ignoredUploadIds
).length
}
stickyPadding={stickyPadding}
{...props}
{...segment.lines[i]}
/>
)}
/>
</Fragment>
)
})
}

function CommitFileDiff({ path }) {
const { commitFileDiff } = useNavLinks()
const { owner, repo, provider, commit } = useParams()
Expand Down Expand Up @@ -172,24 +80,83 @@ function CommitFileDiff({ path }) {
return (
<>
{isCriticalFile && <CriticalFileLabel variant="borderTop" />}
{virtualDiffRenderer ? (
<NewDiffRenderer
headName={headName}
hashedPath={comparisonData.hashedPath}
segments={segments}
ignoredUploadIds={ignoredUploadIds}
/>
) : (
<OldDiffRenderer
headName={headName}
fileLabel={fileLabel}
fullFilePath={fullFilePath}
segments={segments}
ignoredUploadIds={ignoredUploadIds}
stickyPadding={stickyPadding}
comparisonData={comparisonData}
/>
)}
{segments?.map((segment, segmentIndex) => {
const content = segment.lines.map((line) => line.content).join('\n')

let newDiffContent = ''
const lineData = []
if (virtualDiffRenderer) {
segment.lines.forEach((line, lineIndex) => {
newDiffContent += line.content

if (lineIndex < segment.lines.length - 1) {
newDiffContent += '\n'
}

lineData.push({
headNumber: line?.headNumber,
baseNumber: line?.baseNumber,
headCoverage: line?.headCoverage,
baseCoverage: line?.baseCoverage,
hitCount: without(
line?.coverageInfo?.hitUploadIds,
...ignoredUploadIds
).length,
})
})
}

return (
<Fragment key={`${headName}-${segmentIndex}`}>
<CodeRendererInfoRow>
<div className="flex w-full justify-between">
<div className="flex gap-1">
<span data-testid="patch">{segment?.header}</span>
{fileLabel && (
<span className="border-l-2 pl-2">{fileLabel}</span>
)}
</div>
<A href={fullFilePath} isExternal hook="commit full file">
View full file
</A>
</div>
</CodeRendererInfoRow>
{virtualDiffRenderer ? (
<VirtualDiffRenderer
key={segmentIndex}
code={newDiffContent}
fileName={headName}
hashedPath={comparisonData?.hashedPath}
lineData={lineData}
/>
) : (
<CodeRenderer
code={content}
fileName={headName}
rendererType={CODE_RENDERER_TYPE.DIFF}
LineComponent={({ i, line, ...props }) => (
<DiffLine
// If this line one of the first 3 or last three lines of the segment
key={i + 1}
lineContent={line}
edgeOfFile={i <= 2 || i >= segment.lines.length - 3}
path={comparisonData?.hashedPath}
hitCount={
without(
segment?.lines?.[i]?.coverageInfo?.hitUploadIds,
...ignoredUploadIds
).length
}
stickyPadding={stickyPadding}
{...props}
{...segment.lines[i]}
/>
)}
/>
)}
</Fragment>
)
})}
</>
)
}
Expand Down

0 comments on commit 45dc646

Please sign in to comment.