Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: hooksのロジックを整理する #5361

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

AtsushiM
Copy link
Member

@AtsushiM AtsushiM commented Feb 4, 2025

関連URL

概要

変更内容

確認方法

@AtsushiM AtsushiM changed the title chore: hooksno chore: hooksのロジックを整理する Feb 4, 2025
Copy link

pkg-pr-new bot commented Feb 4, 2025

Open in Stackblitz

npm i https://pkg.pr.new/kufu/smarthr-ui@5361

commit: aa9b87c

Comment on lines +8 to +24
useEffect(() => {
const handleClick = (e: MouseEvent) => {
if (innerRefs.some((target) => isEventIncludedParent(e, target.current))) {
innerCallback(e)

return
}

outerCallback(e)
},
// spread innerRefs to compare deps one by one
// eslint-disable-next-line react-hooks/exhaustive-deps
[...innerRefs, innerCallback, outerCallback],
)
}

useEffect(() => {
window.addEventListener('click', handleClick)

return () => {
window.removeEventListener('click', handleClick)
}
}, [handleClick])
}, [innerRefs, innerCallback, outerCallback])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleClickはuseEffectの中でしか利用されていないため、ひとまとめにしています。
依存関係の配列も展開は微妙なのでmemo化したものが渡されてくるように修正しています

Comment on lines +28 to +32
if (!parent) return false

const path = e.composedPath()
if (path.length === 0 || !parent) return false

if (path.length === 0) return false
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pathの生成が不要なパターンに対するチェックを最適化しています

Comment on lines 8 to 18
useEffect(() => {
const handleKeyPress = (e: KeyboardEvent) => {
if (ESCAPE_KEY_REGEX.test(e.key)) {
cb()
}
},
[cb],
)
useEffect(() => {
}

document.addEventListener('keydown', handleKeyPress)

return () => document.removeEventListener('keydown', handleKeyPress)
}, [handleKeyPress])
}, [cb])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleKeyPressはuseEffect中でしか利用されていないため、ひとまとめにしています

Comment on lines -7 to +12
const handleOuterClick = useCallback(
(e: MouseEvent) => {
if (targets.some((target) => isEventIncludedParent(e, target.current))) {
return
useEffect(() => {
const handleOuterClick = (e: MouseEvent) => {
if (targets.every((target) => isEventExcludedParent(e, target.current))) {
callback(e)
}
callback(e)
},
// spread targets to compare deps one by one
// eslint-disable-next-line react-hooks/exhaustive-deps
[...targets, callback],
)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

早期returnが直後の処理の逆転条件でしかないため、理解がワンテンポ遅れることになり、デメリットしかない状態になっています。
条件を逆転させることでわかりやすくしています

Comment on lines +29 to +36
const calculatedSeqs = useMemo(() => {
const parentSeqs = parent.seqs.concat(currentSeq)

return {
parentSeqs,
portalChildOf: parentSeqs.join(','),
}
}, [currentSeq, parent.seqs])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このファイルがやっていることはややこしいのですが、ざっくりいうと以下のとおりです

  • アプリ全体でのPortalにシーケンスnoを振ります
  • Portalを開く毎に紐づくnoをdata属性に追加し、現在開かれているportalが分かる状態にします
  • 上記を利用して、特定のPortalがなにかのPortalの子になっているか?をチェックできるようにします

このチェックのため、data属性を利用していますが、data属性は基本stringしか突っ込めないので、joinでつなげるようにしています

Comment on lines -90 to +102
const childOf = element.dataset?.portalChildOf || ''
const includesSeq = childOf.split(',').includes(String(parentPortalSeq))
return includesSeq || _isChildPortal(element.parentElement, parentPortalSeq)

let includesSeq = false
const childOf = element.dataset?.portalChildOf

if (childOf) {
includesSeq = seqRegex.test(childOf)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これまでのロジックでは ,つなぎの数値をsplitして比較していましたが、シーケンス番号は数値を,でつなげたものであるため、正規表現でsplitしなくてもチェック可能にしました。

https://github.com/kufu/smarthr-ui/pull/5361/files#diff-606e53788a9b6453997da495ed36be27a5678880d9c1151e4cc056400907a121R57

@AtsushiM AtsushiM marked this pull request as ready for review February 13, 2025 00:33
@AtsushiM AtsushiM requested a review from a team as a code owner February 13, 2025 00:33
@AtsushiM AtsushiM requested review from moshisora and yuzuru-akiyama and removed request for a team February 13, 2025 00:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant