Skip to content

Commit

Permalink
refactor(releases): don't track deleted releases
Browse files Browse the repository at this point in the history
Releases can't be deleted, only archived
  • Loading branch information
bjoerge authored and RitaDias committed Oct 28, 2024
1 parent 1e0d055 commit 1fd7f1d
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 260 deletions.
22 changes: 3 additions & 19 deletions packages/sanity/src/core/releases/components/ReleasesMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import {styled} from 'styled-components'
import {MenuButton, Tooltip} from '../../../ui-components'
import {useTranslation} from '../../i18n'
import {type ReleaseDocument} from '../../store/release/types'
import {useReleases} from '../../store/release/useReleases'
import {usePerspective} from '../hooks'
import {LATEST} from '../util/const'
import {isDraftOrPublished} from '../util/util'
import {ReleaseBadge} from './ReleaseBadge'

const StyledMenu = styled(Menu)`
Expand All @@ -35,17 +33,14 @@ interface BundleListProps {
*/
export const ReleasesMenu = memo(function ReleasesMenu(props: BundleListProps): ReactElement {
const {releases, loading, actions, button, perspective} = props
const {deletedReleases} = useReleases()
const {currentGlobalBundle, setPerspectiveFromRelease} = usePerspective(perspective)
const {t} = useTranslation()

const sortedBundlesToDisplay = useMemo(() => {
if (!releases) return []

return releases
.filter(({_id, state}) => !isDraftOrPublished(_id) && state !== 'archived')
.sort(({_id: aId}, {_id: bId}) => Number(deletedReleases[aId]) - Number(deletedReleases[bId]))
}, [releases, deletedReleases])
return releases.filter(({state}) => state !== 'archived')
}, [releases])
const hasBundles = sortedBundlesToDisplay.length > 0

const handleReleaseChange = useCallback(
Expand All @@ -55,11 +50,6 @@ export const ReleasesMenu = memo(function ReleasesMenu(props: BundleListProps):
[setPerspectiveFromRelease],
)

const isBundleDeleted = useCallback(
(releaseId: string) => Boolean(deletedReleases[releaseId]),
[deletedReleases],
)

return (
<>
<MenuButton
Expand Down Expand Up @@ -94,20 +84,14 @@ export const ReleasesMenu = memo(function ReleasesMenu(props: BundleListProps):
onClick={handleReleaseChange(release._id)}
padding={1}
pressed={false}
disabled={isBundleDeleted(release._id)}
data-testid={`release-${release._id}`}
>
<Tooltip
disabled={!isBundleDeleted(release._id)}
content={t('release.deleted-tooltip')}
placement="bottom-start"
>
<Tooltip content={t('release.deleted-tooltip')} placement="bottom-start">
<Flex>
<ReleaseBadge
hue={release.metadata.hue}
icon={release.metadata.icon}
padding={2}
isDisabled={isBundleDeleted(release._id)}
/>

<Box flex={1} padding={2} style={{minWidth: 100}}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ vi.mock('../../util/util', () => ({
}))

vi.mock('../../../store/release/useReleases', () => ({
useReleases: vi.fn().mockReturnValue({deletedReleases: {}}),
useReleases: vi.fn().mockReturnValue({}),
}))

const mockUseReleases = useReleases as Mock<typeof useReleases>
Expand Down Expand Up @@ -234,20 +234,20 @@ describe('ReleasesMenu', () => {
})
})

it('should not show deleted releases when not included in the list', async () => {
it('should not show archived releases when not included in the list', async () => {
mockUseReleases.mockReturnValue({
dispatch: vi.fn(),
loading: false,
data: [],
deletedReleases: {
'mock-deleted-release': {
data: [
{
_id: 'mock-deleted-release',
_type: 'system-tmp.release',
state: 'archived',
metadata: {
title: 'Mock Deleted Bundle',
title: 'Mock Archived Bundle',
},
} as ReleaseDocument,
},
],
})
const wrapper = await createTestProvider()
render(<ReleasesMenu button={ButtonTest} releases={mockReleases} loading={false} />, {
Expand All @@ -257,53 +257,7 @@ describe('ReleasesMenu', () => {
fireEvent.click(screen.getByRole('button', {name: 'Button Test'}))

expect(
within(screen.getByTestId('releases-list')).queryByText('Mock Deleted Bundle'),
within(screen.getByTestId('releases-list')).queryByText('Mock Archived Bundle'),
).not.toBeInTheDocument()
})

it('should show deleted releases that are included in the list', async () => {
mockUseReleases.mockReturnValue({
dispatch: vi.fn(),
loading: false,
data: [],
deletedReleases: {
'mock-deleted-release': {
_id: 'mock-deleted-release',
_type: 'system-tmp.release',
metadata: {
title: 'Mock Deleted Bundle',
},
} as ReleaseDocument,
},
})
const wrapper = await createTestProvider()
render(
<ReleasesMenu
button={ButtonTest}
releases={[
...mockReleases,
{
_id: 'mock-deleted-release',
_type: 'system-tmp.release',
metadata: {
title: 'Mock Deleted Bundle',
},
} as ReleaseDocument,
]}
loading={false}
/>,
{
wrapper,
},
)

fireEvent.click(screen.getByRole('button', {name: 'Button Test'}))

const allMenuBundles = within(screen.getByTestId('releases-list')).getAllByRole('menuitem')
// deleted should be at the end of the release list
const [deletedBundle] = allMenuBundles.reverse()

within(deletedBundle).getByText('Mock Deleted Bundle')
expect(deletedBundle).toBeDisabled()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('ReleaseDetailsDialog', () => {
loading: true,
dispatch: vi.fn(),
error: undefined,
deletedReleases: {},
stack: [],
})

//mockUseDateTimeFormat.mockReturnValue({format: vi.fn().mockReturnValue('Mocked date')})
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('ReleaseDetailsDialog', () => {
loading: true,
dispatch: vi.fn(),
error: undefined,
deletedReleases: {},
stack: [],
})

//mockUseDateTimeFormat.mockReturnValue({format: vi.fn().mockReturnValue('Mocked date')})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('ReleaseForm', () => {
loading: false,
dispatch: vi.fn(),
error: undefined,
deletedReleases: {},
stack: [],
})

mockUseDateTimeFormat.mockReturnValue({format: vi.fn().mockReturnValue('Mocked date')})
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('ReleaseForm', () => {
data: mockData,
loading: false,
dispatch: vi.fn(),
deletedReleases: {} as Record<string, ReleaseDocument>,
stack: [],
})

mockUseDateTimeFormat.mockReturnValue({format: vi.fn().mockReturnValue('Mocked date')})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ async function setupMocks({
data: releases,
loading: false,
dispatch: vi.fn(),
deletedReleases: {},
stack: [],
})

Expand Down
31 changes: 9 additions & 22 deletions packages/sanity/src/core/releases/navbar/GlobalPerspectiveMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {useReleases} from '../../store/release/useReleases'
import {ReleaseDetailsDialog} from '../components/dialog/ReleaseDetailsDialog'
import {usePerspective} from '../hooks'
import {LATEST} from '../util/const'
import {isDraftOrPublished} from '../util/util'

const StyledMenu = styled(Menu)`
min-width: 200px;
Expand All @@ -22,21 +21,20 @@ const StyledBox = styled(Box)`
`

export function GlobalPerspectiveMenu(): JSX.Element {
const {deletedReleases, loading, data: releases} = useReleases()
const {loading, data: releases} = useReleases()
const {currentGlobalBundle, setPerspectiveFromRelease, setPerspective} = usePerspective()
const [createBundleDialogOpen, setCreateBundleDialogOpen] = useState(false)
const styledMenuRef = useRef<HTMLDivElement>(null)

const {t} = useTranslation()

const sortedBundlesToDisplay = useMemo(() => {
const filteredReleases = useMemo(() => {
if (!releases) return []

return [...(releases || []), ...Object.values(deletedReleases)].filter(
({_id, state}) => !isDraftOrPublished(_id) && state !== 'archived',
)
}, [releases, deletedReleases])
const hasBundles = sortedBundlesToDisplay.length > 0
return releases.filter(({_id, state}) => state !== 'archived')
}, [releases])

const hasBundles = filteredReleases.length > 0

const handleBundleChange = useCallback(
(releaseId: string) => () => {
Expand All @@ -45,11 +43,6 @@ export function GlobalPerspectiveMenu(): JSX.Element {
[setPerspectiveFromRelease],
)

const isBundleDeleted = useCallback(
(releaseId: string) => Boolean(deletedReleases[releaseId]),
[deletedReleases],
)

/* create new release */
const handleCreateBundleClick = useCallback(() => {
setCreateBundleDialogOpen(true)
Expand Down Expand Up @@ -85,20 +78,15 @@ export function GlobalPerspectiveMenu(): JSX.Element {
<>
<MenuDivider />
<StyledBox data-testid="releases-list">
{sortedBundlesToDisplay.map(({_id, ...release}) => (
{filteredReleases.map(({_id, ...release}) => (
<MenuItem
key={_id}
onClick={handleBundleChange(_id)}
padding={1}
pressed={false}
disabled={isBundleDeleted(_id)}
data-testid={`release-${_id}`}
>
<Tooltip
disabled={!isBundleDeleted(_id)}
content={t('release.deleted-tooltip')}
placement="bottom-start"
>
<Tooltip content={t('release.deleted-tooltip')} placement="bottom-start">
<Flex>
<Box flex={1} padding={2} style={{minWidth: 100}}>
<Text size={1} weight="medium">
Expand Down Expand Up @@ -139,9 +127,8 @@ export function GlobalPerspectiveMenu(): JSX.Element {
setPerspective,
handleCreateBundleClick,
hasBundles,
isBundleDeleted,
loading,
sortedBundlesToDisplay,
filteredReleases,
t,
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type ReleaseMenuButtonProps = {

export const ReleaseMenuButton = ({disabled, release}: ReleaseMenuButtonProps) => {
const {updateRelease} = useReleaseOperations()
const isBundleArchived = !!release?.metadata.archivedAt
const isBundleArchived = release?.state === 'archived'
const [isPerformingOperation, setIsPerformingOperation] = useState(false)
const [selectedAction, setSelectedAction] = useState<'edit'>()

Expand All @@ -34,6 +34,7 @@ export const ReleaseMenuButton = ({disabled, release}: ReleaseMenuButtonProps) =
setIsPerformingOperation(true)
await updateRelease({
...release,
state: isBundleArchived ? 'active' : 'archived',
metadata: {
...release.metadata,
archivedAt: isBundleArchived ? undefined : new Date().toISOString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import {type DocumentInBundleResult} from './useBundleDocuments'
export function ReleaseDashboardFooter(props: {
documents: DocumentInBundleResult[]
release: ReleaseDocument
isBundleDeleted: boolean
}) {
const {documents, release, isBundleDeleted} = props
const {documents, release} = props

return (
<Card flex="none">
Expand All @@ -24,14 +23,14 @@ export function ReleaseDashboardFooter(props: {

<Flex flex="none" gap={1}>
{/* TODO: Replace this with the real actions. */}
{!isBundleDeleted && !release.publishAt && (
{!release.publishAt && (
<ReleasePublishAllButton
release={release}
releaseDocuments={documents}
disabled={!documents.length}
/>
)}
<ReleaseMenuButton disabled={isBundleDeleted} release={release} />
<ReleaseMenuButton release={release} />
</Flex>
</Flex>
</Card>
Expand Down
Loading

0 comments on commit 1fd7f1d

Please sign in to comment.