Skip to content

Commit

Permalink
fix: remove react hooks linter suppressions in PT hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jul 31, 2024
1 parent 37ef088 commit ed927e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"check:deps": "pnpm --recursive --parallel exec depcheck",
"check:format": "prettier . --check",
"check:lint": "turbo run lint --continue -- --quiet",
"check:react-compiler": "eslint --no-inline-config --no-eslintrc --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --rule 'react-compiler/react-compiler: [warn]' --ignore-path .eslintignore.react-compiler --max-warnings 23 .",
"check:react-compiler": "eslint --no-inline-config --no-eslintrc --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --rule 'react-compiler/react-compiler: [warn]' --ignore-path .eslintignore.react-compiler --max-warnings 7 .",
"check:test": "run-s test -- --silent",
"check:types": "tsc && turbo run check:types --filter='./packages/*' --filter='./packages/@sanity/*'",
"chore:format:fix": "prettier --cache --write .",
Expand Down
61 changes: 29 additions & 32 deletions packages/sanity/src/core/form/inputs/PortableText/toolbar/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ export function useFocusBlock(): PortableTextBlock | undefined {
const editor = usePortableTextEditor()
const selection = usePortableTextEditorSelection()

// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(() => PortableTextEditor.focusBlock(editor), [editor, selection]) // selection must be an additional dep here
return useMemo(
() => (selection ? PortableTextEditor.focusBlock(editor) : undefined),
[editor, selection],
)
}

export function useFocusChild(): PortableTextChild | undefined {
const editor = usePortableTextEditor()
const selection = usePortableTextEditorSelection()

// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(() => PortableTextEditor.focusChild(editor), [editor, selection]) // selection must be an additional dep here
return useMemo(
() => (selection ? PortableTextEditor.focusChild(editor) : undefined),
[editor, selection],
)
}

export function useActionGroups({
Expand Down Expand Up @@ -75,29 +79,23 @@ export function useActiveActionKeys({
const selection = usePortableTextEditorSelection()

return useUnique(
useMemo(
() => {
return actions
.filter((a) => {
if (a.type === 'annotation') {
return PortableTextEditor.isAnnotationActive(editor, a.key)
}
useMemo(() => {
return selection
? actions
.filter((a) => {
if (a.type === 'annotation') {
return PortableTextEditor.isAnnotationActive(editor, a.key)
}

if (a.type === 'listStyle') {
return PortableTextEditor.hasListStyle(editor, a.key)
}
if (a.type === 'listStyle') {
return PortableTextEditor.hasListStyle(editor, a.key)
}

return PortableTextEditor.isMarkActive(editor, a.key)
})
.map((a) => a.key)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[
editor,
// This is needed so that active actions update as `selection` changes
selection,
],
),
return PortableTextEditor.isMarkActive(editor, a.key)
})
.map((a) => a.key)
: []
}, [actions, editor, selection]),
)
}

Expand All @@ -109,13 +107,12 @@ export function useActiveStyleKeys({items}: {items: BlockStyleItem[]}): string[]
return useUnique(
useMemo(
() =>
items.filter((i) => PortableTextEditor.hasBlockStyle(editor, i.style)).map((i) => i.style),
// eslint-disable-next-line react-hooks/exhaustive-deps
[
focusBlock,
// This is needed so that active styles update as `selection` changes
selection,
],
focusBlock && selection
? items
.filter((i) => PortableTextEditor.hasBlockStyle(editor, i.style))
.map((i) => i.style)
: [],
[editor, focusBlock, items, selection],
),
)
}

0 comments on commit ed927e5

Please sign in to comment.