From d928675405f15702c0513c1fd94778d5799211a5 Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Thu, 28 Nov 2024 07:12:40 +0000 Subject: [PATCH] Yarn plugin diffing improvements (#19) * Sort yarn-related changes to the bottom of the diff view * Hide yarn plugin diff by default --- package.json | 1 + src/components/common/Diff/Diff.js | 87 +++++++++++++++++++----------- src/hooks/fetch-diff.js | 21 +++++--- yarn.lock | 2 +- 4 files changed, 73 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index 268e2c1a..6f5d8e36 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "antd": "4.0.3", "date-fns": "^2.23.0", "framer-motion": "^2.0.0-beta.52", + "lodash": "^4.17.21", "markdown-to-jsx": "6.11.0", "react": "16.13.0", "react-content-loader": "5.0.2", diff --git a/src/components/common/Diff/Diff.js b/src/components/common/Diff/Diff.js index 5fc6ac61..aaabde18 100644 --- a/src/components/common/Diff/Diff.js +++ b/src/components/common/Diff/Diff.js @@ -7,6 +7,7 @@ import { tokenize, Decoration as DiffDecoration } from 'react-diff-view' +import { Button, Card, Typography } from 'antd' import DiffHeader from './DiffHeader' import { getComments } from './DiffComment' import { replaceWithProvidedAppName } from '../../../utils' @@ -86,6 +87,30 @@ const DiffView = styled(RDiff)` const isDiffCollapsedByDefault = ({ type, hunks }) => type === 'delete' || hunks.length > 5 ? true : undefined +const Placeholder = ({ newPath, children }) => { + const [showDiff, setShowDiff] = useState(false) + + if (!showDiff && newPath === '.yarn/plugins/@yarnpkg/plugin-backstage.cjs') { + return ( + + + + The diff for the Backstage yarn plugin is hidden by default. + + + ) + } + + return children +} + const Diff = ({ packageName, oldPath, @@ -185,36 +210,38 @@ const Diff = ({ /> {!isDiffCollapsed && ( - - {originalHunks => { - const updatedHunks = getHunksWithAppName(originalHunks) - - const options = { - enhancers: [markEdits(updatedHunks)] - } - - const tokens = tokenize(updatedHunks, options) - - return updatedHunks.map(hunk => [ - - {hunk.content} - , - - ]) - }} - + + + {originalHunks => { + const updatedHunks = getHunksWithAppName(originalHunks) + + const options = { + enhancers: [markEdits(updatedHunks)] + } + + const tokens = tokenize(updatedHunks, options) + + return updatedHunks.map(hunk => [ + + {hunk.content} + , + + ]) + }} + + )} ) diff --git a/src/hooks/fetch-diff.js b/src/hooks/fetch-diff.js index aee50b79..224fd911 100644 --- a/src/hooks/fetch-diff.js +++ b/src/hooks/fetch-diff.js @@ -1,5 +1,6 @@ import { useEffect, useState } from 'react' import { parseDiff } from 'react-diff-view' +import sortBy from 'lodash/sortBy' import { getDiffURL, USE_YARN_PLUGIN } from '../utils' import { useSettings } from '../SettingsProvider' @@ -8,8 +9,18 @@ const delay = ms => new Promise(res => setTimeout(res, ms)) const excludeYarnLock = ({ oldPath, newPath, ...rest }) => !(oldPath.includes('yarn.lock') || newPath.includes('yarn.lock')) -const packageJsonFirst = ({ newPath }) => - newPath.includes('package.json') ? -1 : 1 +const applyCustomSort = parsedDiff => + sortBy(parsedDiff, ({ newPath }) => { + if (newPath.includes('package.json')) { + return -1 + } else if (newPath === '.yarnrc.yml') { + return 1 + } else if (newPath.startsWith('.yarn/')) { + return 2 + } + + return 0 + }) export const useFetchDiff = ({ shouldShowDiff, @@ -45,11 +56,7 @@ export const useFetchDiff = ({ const diff = await response.text() - setDiff( - parseDiff(diff) - .filter(excludeYarnLock) - .sort(packageJsonFirst) - ) + setDiff(applyCustomSort(parseDiff(diff).filter(excludeYarnLock))) setIsLoading(false) setIsDone(true) diff --git a/yarn.lock b/yarn.lock index a9034b6a..7eee8377 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7516,7 +7516,7 @@ lodash.uniq@^4.5.0: resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548" integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A== -lodash@^4.17.4: +lodash@^4.17.21, lodash@^4.17.4: version "4.17.21" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==