From c5254fe8daffa625bc5b10441a69c0f216b9ef0b Mon Sep 17 00:00:00 2001 From: Pedro Bonamin <46196328+pedrobonamin@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:16:52 +0200 Subject: [PATCH] chore(corel): rename bundle.name to bundle.slug (#7171) * chore(corel): rename bundle.name to bundle.slug * chore(corel): add migration script and handlers * fix(corel): update BundleDocument interface definition --- .../core/bundles/components/BundleMenu.tsx | 18 +-- .../components/__tests__/BundleMenu.test.tsx | 8 +- .../bundles/components/dialog/BundleForm.tsx | 14 +-- .../components/dialog/CreateBundleDialog.tsx | 6 +- .../dialog/__tests__/BundleForm.test.tsx | 4 +- .../__tests__/CreateBundleDialog.test.tsx | 6 +- .../components/panes/BundleActions.tsx | 14 +-- .../src/core/bundles/hooks/usePerspective.tsx | 10 +- .../sanity/src/core/bundles/util/const.ts | 5 +- .../src/core/bundles/util/dummyGetters.ts | 6 +- .../BundleMenuButton/BundleMenuButton.tsx | 4 +- .../__tests__/BundleMenuButton.test.tsx | 8 +- .../components/ReleaseDocumentPreview.tsx | 8 +- .../components/ReleasesTable/ReleaseRow.tsx | 5 +- .../ReleasesTable/ReleasesTable.tsx | 2 +- .../__tests__/ReleasesTable.test.tsx | 18 +-- .../sanity/src/core/releases/plugin/index.ts | 2 +- .../sanity/src/core/releases/tool/Migrate.tsx | 107 ++++++++++++++++++ .../core/releases/tool/ReleasesOverview.tsx | 4 +- .../src/core/releases/tool/ReleasesTool.tsx | 8 +- .../tool/__tests__/ReleaseReview.test.tsx | 4 +- .../tool/__tests__/ReleasesOverview.test.tsx | 10 +- .../releases/tool/detail/ReleaseDetail.tsx | 17 ++- .../releases/tool/detail/ReleaseOverview.tsx | 2 +- .../tool/detail/documentTable/DocumentRow.tsx | 2 +- .../documentTable/useDocumentPreviewValues.ts | 2 +- .../detail/documentTable/useReleaseHistory.ts | 10 +- .../detail/review/DocumentReviewHeader.tsx | 2 +- .../sanity/src/core/releases/types/router.ts | 2 +- .../__workshop__/BundlesStoreStory.tsx | 13 ++- .../sanity/src/core/store/bundles/types.ts | 5 +- .../core/store/bundles/useBundleOperations.ts | 2 +- .../perspective/DocumentPerspectiveMenu.tsx | 2 +- .../statusBar/DocumentStatusBarActions.tsx | 4 +- 34 files changed, 218 insertions(+), 116 deletions(-) create mode 100644 packages/sanity/src/core/releases/tool/Migrate.tsx diff --git a/packages/sanity/src/core/bundles/components/BundleMenu.tsx b/packages/sanity/src/core/bundles/components/BundleMenu.tsx index 5037e2cfe31..77fdb72937c 100644 --- a/packages/sanity/src/core/bundles/components/BundleMenu.tsx +++ b/packages/sanity/src/core/bundles/components/BundleMenu.tsx @@ -30,14 +30,14 @@ interface BundleListProps { */ export function BundleMenu(props: BundleListProps): JSX.Element { const {bundles, loading, actions, button} = props - const hasBundles = bundles && bundles.filter((b) => !isDraftOrPublished(b.name)).length > 0 + const hasBundles = bundles && bundles.filter((b) => !isDraftOrPublished(b.slug)).length > 0 const {currentGlobalBundle, setPerspective} = usePerspective() const handleBundleChange = useCallback( (bundle: Partial) => () => { - if (bundle.name) { - setPerspective(bundle.name) + if (bundle.slug) { + setPerspective(bundle.slug) } }, [setPerspective], @@ -58,7 +58,7 @@ export function BundleMenu(props: BundleListProps): JSX.Element { <> ) : undefined } @@ -72,14 +72,14 @@ export function BundleMenu(props: BundleListProps): JSX.Element { {bundles - .filter((b) => !isDraftOrPublished(b.name) && !b.archivedAt) + .filter((b) => !isDraftOrPublished(b.slug) && !b.archivedAt) .map((b) => ( @@ -103,8 +103,8 @@ export function BundleMenu(props: BundleListProps): JSX.Element { diff --git a/packages/sanity/src/core/bundles/components/__tests__/BundleMenu.test.tsx b/packages/sanity/src/core/bundles/components/__tests__/BundleMenu.test.tsx index 4ea68ce7ccc..4e94a4eb6cb 100644 --- a/packages/sanity/src/core/bundles/components/__tests__/BundleMenu.test.tsx +++ b/packages/sanity/src/core/bundles/components/__tests__/BundleMenu.test.tsx @@ -29,7 +29,7 @@ describe('BundleMenu', () => { hue: 'magenta', _id: 'db76c50e-358b-445c-a57c-8344c588a5d5', _type: 'bundle', - name: 'spring-drop', + slug: 'spring-drop', _rev: '6z08CvvPnPe5pWSKJ5zPRR', icon: 'heart-filled', description: 'What a spring drop, allergies galore 🌸', @@ -47,7 +47,7 @@ describe('BundleMenu', () => { _createdAt: '2024-07-02T11:37:06Z', _rev: '6z08CvvPnPe5pWSKJ5zJiK', _updatedAt: '2024-07-02T11:37:06Z', - name: 'autumn-drop', + slug: 'autumn-drop', authorId: '', }, { @@ -60,7 +60,7 @@ describe('BundleMenu', () => { _type: 'bundle', hue: 'red', _id: 'f6b2c2cc-1732-4465-bfb3-dd205b5d78e9', - name: 'summer-drop', + slug: 'summer-drop', authorId: '', }, ] @@ -133,7 +133,7 @@ describe('BundleMenu', () => { act(() => { expect(screen.getByText(mockBundles[0].title)).toBeInTheDocument() - expect(screen.getByTestId(`${mockBundles[0].name}-checkmark-icon`)).toBeInTheDocument() + expect(screen.getByTestId(`${mockBundles[0].slug}-checkmark-icon`)).toBeInTheDocument() }) }) diff --git a/packages/sanity/src/core/bundles/components/dialog/BundleForm.tsx b/packages/sanity/src/core/bundles/components/dialog/BundleForm.tsx index 86ce254e078..69f443a374f 100644 --- a/packages/sanity/src/core/bundles/components/dialog/BundleForm.tsx +++ b/packages/sanity/src/core/bundles/components/dialog/BundleForm.tsx @@ -55,15 +55,11 @@ export function BundleForm(props: { const handleBundleTitleChange = useCallback( (event: React.ChangeEvent) => { const pickedTitle = event.target.value - const pickedNameExists = - data && data.find((bundle) => bundle.name === speakingurl(pickedTitle)) + const newSlug = speakingurl(pickedTitle) + const slugExists = data && data.find((bundle) => bundle.slug === newSlug) const isEmptyTitle = pickedTitle.trim() === '' && !isInitialRender - if ( - isDraftOrPublished(pickedTitle) || - pickedNameExists || - (isEmptyTitle && !isInitialRender) - ) { + if (isDraftOrPublished(pickedTitle) || slugExists || (isEmptyTitle && !isInitialRender)) { if (isEmptyTitle && !isInitialRender) { // if the title is empty and it's not the first opening of the dialog, show an error // TODO localize text @@ -77,7 +73,7 @@ export function BundleForm(props: { {level: 'error', message: "Title cannot be 'drafts' or 'published'", path: []}, ]) } - if (pickedNameExists) { + if (slugExists) { // if the bundle already exists, show an error // TODO localize text setTitleErrors([{level: 'error', message: 'Bundle already exists', path: []}]) @@ -90,7 +86,7 @@ export function BundleForm(props: { } setIsInitialRender(false) - onChange({...value, title: pickedTitle, name: speakingurl(pickedTitle)}) + onChange({...value, title: pickedTitle, slug: newSlug}) }, [data, isInitialRender, onChange, onError, value], ) diff --git a/packages/sanity/src/core/bundles/components/dialog/CreateBundleDialog.tsx b/packages/sanity/src/core/bundles/components/dialog/CreateBundleDialog.tsx index 5ee6e606a6e..71de6d3ff91 100644 --- a/packages/sanity/src/core/bundles/components/dialog/CreateBundleDialog.tsx +++ b/packages/sanity/src/core/bundles/components/dialog/CreateBundleDialog.tsx @@ -18,7 +18,7 @@ export function CreateBundleDialog(props: CreateBundleDialogProps): JSX.Element const [hasErrors, setHasErrors] = useState(false) const [value, setValue] = useState>({ - name: '', + slug: '', title: '', hue: 'gray', icon: 'cube', @@ -31,7 +31,7 @@ export function CreateBundleDialog(props: CreateBundleDialogProps): JSX.Element const handleOnSubmit = useCallback( async (event: FormEvent) => { - if (value.name) { + if (value.slug) { try { event.preventDefault() setIsCreating(true) @@ -41,7 +41,7 @@ export function CreateBundleDialog(props: CreateBundleDialogProps): JSX.Element console.error(err) } finally { setIsCreating(false) - setPerspective(value.name) + setPerspective(value.slug) onCreate() } } diff --git a/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleForm.test.tsx b/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleForm.test.tsx index 620325324ca..1490ddfd31b 100644 --- a/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleForm.test.tsx +++ b/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleForm.test.tsx @@ -43,7 +43,7 @@ describe('BundleForm', () => { icon: 'heart-filled', _id: 'db76c50e-358b-445c-a57c-8344c588a5d5', _type: 'bundle', - name: 'spring-drop', + slug: 'spring-drop', hue: 'magenta', _createdAt: '2024-07-02T11:37:51Z', }, @@ -69,7 +69,7 @@ describe('BundleForm', () => { const titleInput = screen.getByTestId('bundle-form-title') fireEvent.change(titleInput, {target: {value: 'Bundle 1'}}) - expect(onChangeMock).toHaveBeenCalledWith({...valueMock, title: 'Bundle 1', name: 'bundle-1'}) + expect(onChangeMock).toHaveBeenCalledWith({...valueMock, title: 'Bundle 1', slug: 'bundle-1'}) }) it('should call onChange when description textarea value changes', () => { diff --git a/packages/sanity/src/core/bundles/components/dialog/__tests__/CreateBundleDialog.test.tsx b/packages/sanity/src/core/bundles/components/dialog/__tests__/CreateBundleDialog.test.tsx index e58bb8b43db..5d5b8e570f0 100644 --- a/packages/sanity/src/core/bundles/components/dialog/__tests__/CreateBundleDialog.test.tsx +++ b/packages/sanity/src/core/bundles/components/dialog/__tests__/CreateBundleDialog.test.tsx @@ -60,9 +60,9 @@ describe('CreateBundleDialog', () => { expect(onCancelMock).toHaveBeenCalled() }) - it('should call createBundle, setPerspective, and onCreate when form is submitted with a valid name', async () => { + it('should call createBundle, setPerspective, and onCreate when form is submitted with a valid slug', async () => { const value: Partial = { - name: 'bundle-1', + slug: 'bundle-1', title: 'Bundle 1', hue: 'gray', icon: 'cube', @@ -77,7 +77,7 @@ describe('CreateBundleDialog', () => { await expect(useBundleOperations().createBundle).toHaveBeenCalledWith(value) - expect(usePerspective().setPerspective).toHaveBeenCalledWith(value.name) + expect(usePerspective().setPerspective).toHaveBeenCalledWith(value.slug) expect(onCreateMock).toHaveBeenCalled() }) }) diff --git a/packages/sanity/src/core/bundles/components/panes/BundleActions.tsx b/packages/sanity/src/core/bundles/components/panes/BundleActions.tsx index 7980c0e8777..6b435e23824 100644 --- a/packages/sanity/src/core/bundles/components/panes/BundleActions.tsx +++ b/packages/sanity/src/core/bundles/components/panes/BundleActions.tsx @@ -25,7 +25,7 @@ interface BundleActionsProps { */ export function BundleActions(props: BundleActionsProps): JSX.Element { const {currentGlobalBundle, documentId, documentType} = props - const {name, title} = currentGlobalBundle + const {slug, title} = currentGlobalBundle const {data: bundles, loading} = useBundles() const documentStore = useDocumentStore() @@ -51,15 +51,15 @@ export function BundleActions(props: BundleActionsProps): JSX.Element { dummyFetch.current = fetchVersions() } await dummyFetch.current - setIsInVersion(versionDocumentExists(documentVersions, name)) + setIsInVersion(versionDocumentExists(documentVersions, slug)) } fetchVersionsInner() - }, [bundles, documentId, fetchVersions, documentVersions, name]) + }, [bundles, documentId, fetchVersions, documentVersions, slug]) const handleAddVersion = useCallback(async () => { // only add to version if there isn't already a version in that bundle of this doc - if (versionDocumentExists(documentVersions, name)) { + if (versionDocumentExists(documentVersions, slug)) { toast.push({ status: 'error', title: `There's already a version of this document in the bundle ${title}`, @@ -67,7 +67,7 @@ export function BundleActions(props: BundleActionsProps): JSX.Element { return } - const bundleId = `${name}.${documentId}` + const bundleId = `${slug}.${documentId}` setCreatingVersion(true) @@ -88,7 +88,7 @@ export function BundleActions(props: BundleActionsProps): JSX.Element { documentStore.pair, documentType, documentVersions, - name, + slug, newVersion, title, toast, @@ -98,7 +98,7 @@ export function BundleActions(props: BundleActionsProps): JSX.Element { return (