Skip to content

Commit

Permalink
fix: remove react-hooks linter suppression
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Dec 18, 2024
1 parent 05c9e74 commit a76e797
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {TrashIcon} from '@sanity/icons'
import {useTelemetry} from '@sanity/telemetry/react'
import {Box, Flex, Switch, Text, useToast} from '@sanity/ui'
import {useCallback, useEffect, useState} from 'react'
import {useEffectEvent} from 'use-effect-event'

import {Button} from '../../../../../ui-components'
import {type ObjectInputProps, set} from '../../../../form'
Expand Down Expand Up @@ -47,46 +48,48 @@ export function FormCreate(props: ObjectInputProps) {
const {data} = useTasks()
const savedTask = data.find((task) => task._id === value._id)

const handleCreatingSuccess = useEffectEvent(() => {
telemetry.log(TaskCreated)
toast.push({
closable: true,
status: 'success',
title: t('form.status.success'),
})

setCreating(false)
if (createMore) {
setViewMode({type: 'create'})
} else {
setActiveTab('subscribed')
}
})
useEffect(() => {
// This useEffect takes care of closing the form when a task entered the "creation" state.
// That action is async and we don't have access to the promise, once the value is updated in the form we will close the form.
if (creating && savedTask?.createdByUser) {
telemetry.log(TaskCreated)
toast.push({
closable: true,
status: 'success',
title: t('form.status.success'),
})

setCreating(false)
if (createMore) {
setViewMode({type: 'create'})
} else {
setActiveTab('subscribed')
}
handleCreatingSuccess()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [creating, savedTask?.createdByUser])
}, [creating, handleCreatingSuccess, savedTask?.createdByUser])

const handleCreatingTimeout = useEffectEvent(() => {
setCreating(false)
toast.push({
closable: true,
status: 'error',
title: t('form.status.error.creation-failed'),
})
})
useEffect(() => {
// If after 10 seconds the task is still in the "creating" state, show an error and reset the creating state.
let timeoutId: ReturnType<typeof setTimeout> | null = null
if (creating) {
timeoutId = setTimeout(() => {
setCreating(false)
toast.push({
closable: true,
status: 'error',
title: t('form.status.error.creation-failed'),
})
}, 10000)
timeoutId = setTimeout(() => handleCreatingTimeout(), 10000)
}
// Cleanup function to clear the timeout
return () => {
if (timeoutId) clearTimeout(timeoutId)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [creating])
}, [creating, handleCreatingTimeout])

const handleCreate = useCallback(async () => {
setCreating(true)
Expand Down
9 changes: 6 additions & 3 deletions packages/sanity/src/core/tasks/hooks/useActivityLog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {type TransactionLogEventWithEffects} from '@sanity/types'
import {useCallback, useEffect, useState} from 'react'
import {useEffectEvent} from 'use-effect-event'

import {useClient} from '../../hooks'
import {getJsonStream} from '../../store/_legacy/history/history/getJsonStream'
Expand Down Expand Up @@ -65,10 +66,12 @@ export function useActivityLog(task: TaskDocument): {
[transactionsUrl, token, publishedId],
)

const handleFetchAndParse = useEffectEvent(() => fetchAndParse(task))
useEffect(() => {
fetchAndParse(task)
// Task is updated on every change, wait until the revision changes to update the activity log.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetchAndParse, task._rev])
if (task._rev) {
handleFetchAndParse()
}
}, [handleFetchAndParse, task._rev])
return {changes}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
useDocumentStore,
useTranslation,
} from 'sanity'
import {useEffectEvent} from 'use-effect-event'

import {Delay} from '../../../../components'
import {structureLocaleNamespace} from '../../../../i18n'
Expand Down Expand Up @@ -102,21 +103,23 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
}, [documentId, documentStore, documentType, patchChannel])

const hasRev = Boolean(value?._rev)
const handleInitialValue = useEffectEvent(() => {
// this is a workaround for an issue that caused the document pushed to withDocument to get
// stuck at the first initial value.
// This effect is triggered only when the document goes from not having a revision, to getting one
// so it will kick in as soon as the document is received from the backend
patchChannel.publish({
type: 'mutation',
patches: [],
snapshot: value,
})
})
useEffect(() => {
if (hasRev) {
// this is a workaround for an issue that caused the document pushed to withDocument to get
// stuck at the first initial value.
// This effect is triggered only when the document goes from not having a revision, to getting one
// so it will kick in as soon as the document is received from the backend
patchChannel.publish({
type: 'mutation',
patches: [],
snapshot: value,
})
handleInitialValue()
}
// React to changes in hasRev only
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasRev])
}, [handleInitialValue, hasRev])

const [formRef, setFormRef] = useState<null | HTMLDivElement>(null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,6 @@ export function DocumentListPaneContent(props: DocumentListPaneContentProps) {
</CommandListBox>
</RootBox>
)
// Explicitly don't include `noDocumentsContent` in the deps array, as it's
// causing a visual bug where the "No documents" message is shown for a split second
// when clearing a search query with no results
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
collapsed,
error,
Expand All @@ -260,11 +256,13 @@ export function DocumentListPaneContent(props: DocumentListPaneContentProps) {
items,
layout,
loadingVariant,
// noDocumentsContent,
noDocumentsContent,
onRetry,
paneTitle,
renderItem,
searchInputElement,
shouldRender,
t,
])

return (
Expand Down

0 comments on commit a76e797

Please sign in to comment.