Conversation
ページ読み込み時にURLのハッシュが#comment_で始まる場合、 対象コメントが折り畳まれていれば全コメントを展開し、 対象コメントにスクロールするようにした。
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 WalkthroughWalkthroughURLハッシュ( Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@test/system/comment/product_comments_test.rb`:
- Around line 45-55: テスト product_comments_test.rb の `comments(:comment20)`
参照が存在しないためフィクスチャロードエラーが発生しています;`oldest_comment = comments(:comment20)`
を修正するには、test/fixtures/comments.yml に comment17〜comment20 を追加して `:comment20`
を定義するか、既存の折り畳まれたコメントのフィクスチャ(例: `:comment12` など)を使うようテスト内の `comments(:comment20)`
を該当の既存キーに置き換えてください(テスト名 "anchor link to folded comment unfolds and scrolls to
it" と変数 `oldest_comment` を更新)。
🧹 Nitpick comments (1)
app/javascript/comments.js (1)
45-66: アンカーリンク処理のロジックは概ね良好です。全体的な設計は正しく、折り畳まれたコメントの展開→スクロールの流れがきちんと実装されています。いくつか細かい点を挙げます:
setTimeout300ms はやや脆弱: レンダリングに300ms以上かかった場合、スクロール先の位置が不正確になる可能性があります。requestAnimationFrameのネストやMutationObserverを使うとより堅牢になりますが、現状のユースケースでは実用上問題ない範囲だと思います。
moreCommentsSectionの非表示方法: Line 58 でstyle.display = 'none'を使っていますが、Line 39 ではclassList.remove('is-hidden')で表示制御しています。ただし、既存コード(Line 80)でもstyle.display = 'none'が使われているので、既存パターンとは一貫性があります。
ERB生成のフィクスチャキー(:comment20)の参照でエラーが 発生するため、product.comments.order(:created_at).firstで 最古のコメントを取得するように修正した。
全コメントを一気に展開するのではなく、アンカー対象の コメントからそれより新しいコメントまでを表示し、 対象より古いコメントは「前のコメント」ボタンで 折り畳んだまま残すようにした。
固定の300ms遅延ではなくブラウザの描画サイクルに合わせて スクロールするようにし、レンダリング時間に依存しない 堅牢な実装にした。comments.jsとanswer.jsの両方を修正。
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
app/javascript/comments.js (1)
82-86:⚠️ Potential issue | 🟡 Minor
commentTotalCount <= initialLimitの場合、moreCommentButtonがnullになり得るLine 24 の分岐で
commentTotalCount <= initialLimitの場合、「前のコメント」ボタンがDOMに存在しない可能性があります。その場合、Line 86 のaddEventListenerでTypeError: Cannot read properties of nullが発生します。これはこのPRで導入された問題ではありませんが、Line 45-80 のアンカーロジックが先に実行される流れになったため、認識しておく価値があります。🛡️ 修正案
const moreCommentButton = document.querySelector( '.a-button.is-lg.is-text.is-block' ) const moreComments = document.querySelector('.thread-comments-more') - moreCommentButton.addEventListener('click', () => { + if (!moreCommentButton) return + moreCommentButton.addEventListener('click', () => {
🧹 Nitpick comments (2)
app/javascript/answer.js (1)
6-13:querySelectorの代わりにgetElementByIdを使用すべき
comments.jsではdocument.getElementById(commentAnchor.slice(1))を使用しているのに対し、ここではdocument.querySelector(answerAnchor)を使用しています。querySelectorはCSSセレクタとして解釈するため、IDに.や:などの特殊文字が含まれる場合にエラーが発生します。getElementByIdの方が安全で、comments.jsとの一貫性も保てます。♻️ 修正案
if (answerAnchor) { requestAnimationFrame(() => { requestAnimationFrame(() => { - const anchorElement = document.querySelector(answerAnchor) + const anchorElement = document.getElementById(answerAnchor.slice(1)) if (anchorElement) { anchorElement.scrollIntoView({ behavior: 'instant' }) } }) }) }app/javascript/comments.js (1)
45-80: アンカー展開ロジックは正しく実装されています。対象コメント以降を展開し、古いコメントは折り畳んだまま残すロジックは、主要なケース(中間コメント、最古コメント、既に表示済みのコメント)を正しく処理できています。
ただし、
.a-button.is-lg.is-text.is-blockセレクタでのボタン取得が Line 34、Line 58、Line 82 と3箇所で重複しています。ボタン要素の取得を1箇所にまとめることを検討してください。
IDに特殊文字が含まれる場合のエラーを防ぎ、 comments.jsとの一貫性を保つ。
moreCommentButtonとmoreCommentsの取得が3箇所に 重複していたのをトップレベルで1回だけ取得するように整理した。
Issue
概要
ページ読み込み時にURLのハッシュが#comment_で始まる場合、
対象コメントが折り畳まれていれば全コメントを展開し、
対象コメントにスクロールするようにした。
変更確認方法
確認1: 折り畳まれたコメントへのアンカーリンク(新規ウィンドウ)
」ボタンは表示されない
確認2: 折り畳まれていないコメントへのアンカーリンク(デグレ確認)
確認3: アンカーリンクなしでのアクセス(デグレ確認)
確認4: 他の箇所でも同様に動作するか(任意)
Issue では日報コメント・相談でも同じ現象が報告されています。同じ comments.js
を共有しているため、コメントが9件以上ある日報や相談ページでも同様に確認できます。
Summary by CodeRabbit
新機能
改善
テスト