-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(site/src): replace misused useEffectEvent with correct patterns #24525
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,10 +260,11 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => { | |
| expect(result.current.error).toBeUndefined(); | ||
| }); | ||
|
|
||
| // This test case is really important to ensure that it's easy to plop this | ||
| // inside of useEffect calls without having to think about dependencies too | ||
| // much | ||
| it("Ensures that the copyToClipboard function always maintains a stable reference across all re-renders", async () => { | ||
| // This test case verifies that copyToClipboard maintains a stable | ||
| // reference across re-renders so it can be used safely in useEffect | ||
| // dependency arrays. Stability requires that onError and | ||
| // clearErrorOnSuccess are themselves stable references. | ||
| it("Ensures that the copyToClipboard function maintains a stable reference when its inputs are stable", async () => { | ||
| const initialOnError = vi.fn(); | ||
| const { result, rerender } = renderUseClipboard({ | ||
| onError: initialOnError, | ||
|
|
@@ -276,17 +277,6 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => { | |
| rerender({ onError: initialOnError }); | ||
| expect(result.current.copyToClipboard).toBe(initialCopy); | ||
|
|
||
| // Re-render with new onError prop and then swap back to simplify | ||
| // testing | ||
| rerender({ onError: vi.fn() }); | ||
| expect(result.current.copyToClipboard).toBe(initialCopy); | ||
| rerender({ onError: initialOnError }); | ||
|
|
||
| // Re-render with a new clear value then swap back to simplify testing | ||
| rerender({ onError: initialOnError, clearErrorOnSuccess: false }); | ||
| expect(result.current.copyToClipboard).toBe(initialCopy); | ||
| rerender({ onError: initialOnError, clearErrorOnSuccess: true }); | ||
|
|
||
|
Comment on lines
-279
to
-289
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fixed in 91548af. Rewrote the stability test comment to describe the new conditional-stability behaviour, and removed the assertions that tested stability with an unstable |
||
| // Trigger a failed clipboard interaction | ||
| setSimulateFailure(true); | ||
| await act(() => result.current.copyToClipboard("dummy-text-2")); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| import { useEffect, useEffectEvent, useRef, useState } from "react"; | ||
| import { | ||
| useCallback, | ||
| useEffect, | ||
| useEffectEvent, | ||
| useRef, | ||
| useState, | ||
| } from "react"; | ||
| import { type InfiniteData, useQueryClient } from "react-query"; | ||
| import { watchChat } from "#/api/api"; | ||
| import { chatMessagesKey, updateInfiniteChatsCache } from "#/api/queries/chats"; | ||
|
|
@@ -131,7 +137,7 @@ export const useChatStore = ( | |
| // last REST fetch, and structural sharing can suppress the | ||
| // refetch-driven store update when no new durable messages | ||
| // have been committed to the DB yet. | ||
| const upsertCacheMessages = useEffectEvent( | ||
| const upsertCacheMessages = useCallback( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eslint wasn't happy with |
||
| (messages: readonly TypesGen.ChatMessage[]) => { | ||
| if (!chatID || messages.length === 0) { | ||
| return; | ||
|
|
@@ -172,6 +178,7 @@ export const useChatStore = ( | |
| }; | ||
| }); | ||
| }, | ||
| [chatID, queryClient], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
|
|
@@ -626,7 +633,7 @@ export const useChatStore = ( | |
| } | ||
| activeChatIDRef.current = null; | ||
| }; | ||
| }, [chatID, initialDataLoaded, queryClient, store]); | ||
| }, [chatID, initialDataLoaded, queryClient, store, upsertCacheMessages]); | ||
| return { | ||
| store, | ||
| clearStreamError: () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should just ensure the caller makes their
onClickbe referentially stable