Merge pull request #14492 from overleaf/ds-remake-compare

HIstory UI - Adding a new compare component for drop down

GitOrigin-RevId: 3e01e8fedd5966c1bc6253df0e8868e4a41b8504
This commit is contained in:
Jimmy Domagala-Tang 2023-09-05 10:00:19 -04:00 committed by Copybot
parent 3fa1245860
commit 0d932a226b
5 changed files with 126 additions and 68 deletions

View file

@ -1,10 +1,11 @@
import { LoadedUpdate, Version } from '../../../services/types/update' import { LoadedUpdate, Version } from '../../../services/types/update'
import { ReactNode, useCallback } from 'react' import { useCallback } from 'react'
import { ActiveDropdown } from '../../../hooks/use-dropdown-active-item' import { ActiveDropdown } from '../../../hooks/use-dropdown-active-item'
import { MenuItem } from 'react-bootstrap'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import CompareItems from './menu-item/compare-items'
import { updateRangeForUpdate } from '../../../utils/history-details' import { updateRangeForUpdate } from '../../../utils/history-details'
import CompareDropDownItem from './menu-item/compare-dropdown-item'
import { useHistoryContext } from '../../../context/history-context'
import MaterialIcon from '../../../../../shared/components/material-icon'
type VersionDropdownContentAllHistoryProps = { type VersionDropdownContentAllHistoryProps = {
update: LoadedUpdate update: LoadedUpdate
@ -22,25 +23,45 @@ function CompareVersionDropdownContentAllHistory({
}, [closeDropdownForItem, update]) }, [closeDropdownForItem, update])
const { t } = useTranslation() const { t } = useTranslation()
const { selection } = useHistoryContext()
const { updateRange: selRange } = selection
if (selRange === null) {
return null
}
return ( return (
<> <>
<DropdownOption> <CompareDropDownItem
<CompareItems comparisonRange={{
updateRange={updateRange} fromV: selRange.fromV,
selectionState="aboveSelected" toV: updateRange.toV,
text={t('history_compare_up_to_this_version')} fromVTimestamp: selRange.fromVTimestamp,
closeDropdown={closeDropdown} toVTimestamp: updateRange.toVTimestamp,
/> }}
</DropdownOption> closeDropdown={closeDropdown}
<DropdownOption> text={t('history_compare_up_to_this_version')}
<CompareItems icon={
updateRange={updateRange} <MaterialIcon
selectionState="belowSelected" type="align_start"
text={t('history_compare_from_this_version')} className="material-symbols-rounded history-dropdown-icon p-1"
closeDropdown={closeDropdown} />
/> }
</DropdownOption> />
<CompareDropDownItem
comparisonRange={{
fromV: updateRange.fromV,
toV: selRange.toV,
fromVTimestamp: updateRange.fromVTimestamp,
toVTimestamp: selRange.toVTimestamp,
}}
closeDropdown={closeDropdown}
text={t('history_compare_from_this_version')}
icon={
<MaterialIcon
type="align_end"
className="material-symbols-rounded history-dropdown-icon p-1"
/>
}
/>
</> </>
) )
} }
@ -61,47 +82,46 @@ function CompareVersionDropdownContentLabelsList({
}, [closeDropdownForItem, version]) }, [closeDropdownForItem, version])
const { t } = useTranslation() const { t } = useTranslation()
const { selection } = useHistoryContext()
const { updateRange: selRange } = selection
if (selRange === null) {
return null
}
return ( return (
<> <>
<DropdownOption> <CompareDropDownItem
<CompareItems comparisonRange={{
updateRange={{ fromV: selRange.fromV,
fromV: version, toV: version,
toV: version, fromVTimestamp: selRange.fromVTimestamp,
fromVTimestamp: versionTimestamp, toVTimestamp: versionTimestamp,
toVTimestamp: versionTimestamp, }}
}} closeDropdown={closeDropdownLabels}
selectionState="aboveSelected" text={t('history_compare_up_to_this_version')}
text={t('history_compare_up_to_this_version')} icon={
closeDropdown={closeDropdownLabels} <MaterialIcon
/> type="align_start"
</DropdownOption> className="material-symbols-rounded history-dropdown-icon p-1"
<DropdownOption> />
<CompareItems }
updateRange={{ />
fromV: version, <CompareDropDownItem
toV: version, comparisonRange={{
fromVTimestamp: versionTimestamp, fromV: version,
toVTimestamp: versionTimestamp, toV: selRange.toV,
}} fromVTimestamp: versionTimestamp,
selectionState="belowSelected" toVTimestamp: selRange.toVTimestamp,
text={t('history_compare_from_this_version')} }}
closeDropdown={closeDropdownLabels} closeDropdown={closeDropdownLabels}
/> text={t('history_compare_from_this_version')}
</DropdownOption> icon={
</> <MaterialIcon
) type="align_end"
} className="material-symbols-rounded history-dropdown-icon p-1"
/>
type DropdownOptionProps = { }
children: ReactNode />
}
function DropdownOption({ children, ...props }: DropdownOptionProps) {
return (
<>
<MenuItem {...props}>{children}</MenuItem>
</> </>
) )
} }

View file

@ -0,0 +1,43 @@
import Icon from '../../../../../../shared/components/icon'
import { useHistoryContext } from '../../../../context/history-context'
import { UpdateRange } from '../../../../services/types/update'
import { ReactNode } from 'react'
import { Button, MenuItem } from 'react-bootstrap'
type CompareProps = {
comparisonRange: UpdateRange
icon: ReactNode
text: string
closeDropdown: () => void
}
function CompareDropDownItem({
comparisonRange,
text,
closeDropdown,
icon = <Icon type="exchange" fw />,
...props
}: CompareProps) {
const { setSelection } = useHistoryContext()
const handleCompareVersion = (e: React.MouseEvent<Button>) => {
e.stopPropagation()
closeDropdown()
setSelection(({ previouslySelectedPathname }) => ({
updateRange: comparisonRange,
comparing: true,
files: [],
previouslySelectedPathname,
}))
}
return (
<MenuItem {...props} onClick={handleCompareVersion}>
{icon}
<span className="">{text}</span>
</MenuItem>
)
}
export default CompareDropDownItem

View file

@ -8,14 +8,12 @@ import { ItemSelectionState } from '../../../../utils/history-details'
type CompareItemsProps = { type CompareItemsProps = {
updateRange: UpdateRange updateRange: UpdateRange
selectionState: ItemSelectionState selectionState: ItemSelectionState
text?: string
closeDropdown: () => void closeDropdown: () => void
} }
function CompareItems({ function CompareItems({
updateRange, updateRange,
selectionState, selectionState,
text,
closeDropdown, closeDropdown,
}: CompareItemsProps) { }: CompareItemsProps) {
const { t } = useTranslation() const { t } = useTranslation()
@ -37,7 +35,6 @@ function CompareItems({
}} }}
closeDropdown={closeDropdown} closeDropdown={closeDropdown}
toolTipDescription={t('history_compare_from_this_version')} toolTipDescription={t('history_compare_from_this_version')}
text={text}
icon={ icon={
<MaterialIcon <MaterialIcon
type="align_end" type="align_end"
@ -56,7 +53,6 @@ function CompareItems({
}} }}
closeDropdown={closeDropdown} closeDropdown={closeDropdown}
toolTipDescription={t('history_compare_up_to_this_version')} toolTipDescription={t('history_compare_up_to_this_version')}
text={text}
icon={ icon={
<MaterialIcon <MaterialIcon
type="align_start" type="align_start"

View file

@ -8,14 +8,12 @@ import { Button } from 'react-bootstrap'
type CompareProps = { type CompareProps = {
comparisonRange: UpdateRange comparisonRange: UpdateRange
icon?: ReactNode icon?: ReactNode
text?: string
toolTipDescription?: string toolTipDescription?: string
closeDropdown: () => void closeDropdown: () => void
} }
function Compare({ function Compare({
comparisonRange, comparisonRange,
text,
closeDropdown, closeDropdown,
toolTipDescription, toolTipDescription,
icon = <Icon type="exchange" fw />, icon = <Icon type="exchange" fw />,
@ -48,7 +46,6 @@ function Compare({
> >
<span className="sr-only">{toolTipDescription}</span> <span className="sr-only">{toolTipDescription}</span>
{icon} {icon}
{text ?? <span className="px-1">{text}</span>}
</Button> </Button>
</Tooltip> </Tooltip>
) )

View file

@ -363,9 +363,11 @@ describe('change list', function () {
.eq(1) .eq(1)
.within(() => { .within(() => {
cy.findByRole('button', { name: /compare drop down/i }).click() cy.findByRole('button', { name: /compare drop down/i }).click()
cy.findByRole('button', { cy.findByRole('menu').within(() => {
name: /compare up to this version/i, cy.findByRole('menuitem', {
}).click() name: /compare up to this version/i,
}).click()
})
}) })
cy.findAllByTestId('history-version-details').should($versions => { cy.findAllByTestId('history-version-details').should($versions => {