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

feat: useMemoWithPrevious React hook #2820

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

yomybaby
Copy link
Member

@yomybaby yomybaby commented Nov 6, 2024

Resolves #2819

Changes:
Introduces a new useMemoWithPrevious hook to manage previous and current values with reset capability, replacing the manual useRef implementation in ContainerLogModal. This hook provides a more robust way to track state changes while maintaining previous values.

Implementation Details:

  • Created new useMemoWithPrevious hook that returns both current and previous values along with a reset function
  • Updated ContainerLogModal to use the new hook for tracking log line numbers
  • Replaced manual previousLastLineNumber ref management with the new hook's resetPrevious functionality
  • Improved state management when switching kernel IDs or changing log sizes

Testing Requirements:

  • Verify log highlighting works correctly when switching between different kernels
  • Confirm previous line numbers are properly reset when changing log sizes
  • Check that log display maintains proper highlighting of new content

Copy link

graphite-app bot commented Nov 6, 2024

Your org requires the Graphite merge queue for merging into main

Add the label “flow:merge-queue” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “flow:hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@github-actions github-actions bot added the size:M 30~100 LoC label Nov 6, 2024
Copy link
Member Author

yomybaby commented Nov 6, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @yomybaby and the rest of your teammates on Graphite Graphite

@yomybaby yomybaby marked this pull request as ready for review November 6, 2024 01:31
@yomybaby yomybaby force-pushed the feature/use-memo-with-previouse branch from 8c18df8 to 9f79883 Compare November 6, 2024 01:47
Copy link

github-actions bot commented Nov 6, 2024

Coverage report for ./react

St.
Category Percentage Covered / Total
🔴 Statements
4.96% (+0.15% 🔼)
360/7253
🔴 Branches
4.43% (+0.02% 🔼)
221/4992
🔴 Functions
2.97% (+0.16% 🔼)
71/2393
🔴 Lines
4.86% (+0.15% 🔼)
345/7093
Show new covered files 🐣
St.
File Statements Branches Functions Lines
🟢
... / useMemoWithPrevious.tsx
100% 100% 100% 100%

Test suite run success

100 tests passing in 13 suites.

Report generated by 🧪jest coverage report action from 5702e0a

@yomybaby yomybaby marked this pull request as draft November 6, 2024 02:24
@yomybaby yomybaby marked this pull request as ready for review November 6, 2024 02:24
useEffect(() => {
prevRef.current = current;
// Only update when deps change
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// eslint-disable-next-line react-hooks/exhaustive-deps

const memoizedPrev = useMemo(() => {
return prevRef.current;
// Only update when the reset key changes and deps change
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// eslint-disable-next-line react-hooks/exhaustive-deps

Copy link
Member Author

Choose a reason for hiding this comment

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

This useMemo does not include prevRef as a dependency item because, in this case, only deps and prevResetKey are intentionally considered as dependency items.
Without this comment, ESLint complains about the missing dependency.

const prevRef = useRef(initialPrev);
const [prevResetKey, setPrevResetKey] = useState({});

// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// eslint-disable-next-line react-hooks/exhaustive-deps

Copy link
Member Author

Choose a reason for hiding this comment

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

Without this eslint complained `React Hook useMemo has a missing dependency: 'factory'. Either include it or remove the dependency array.eslintreact-hooks/exhaustive-deps

@yomybaby yomybaby force-pushed the feature/use-memo-with-previouse branch from 9f79883 to 01ea18e Compare November 6, 2024 06:57
@yomybaby yomybaby requested a review from rapsealk November 6, 2024 06:58
const prevRef = useRef(initialPrev);
const [prevResetKey, setPrevResetKey] = useState({});

const current = useMemo(factory, deps);
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know why it was deleted. 😢 I just added it again.

@yomybaby yomybaby force-pushed the feature/use-memo-with-previouse branch from 01ea18e to 2f93b41 Compare November 6, 2024 07:48
@github-actions github-actions bot added size:L 100~500 LoC and removed size:M 30~100 LoC labels Nov 6, 2024
@yomybaby yomybaby force-pushed the feature/use-memo-with-previouse branch 2 times, most recently from c3224fc to a3efc59 Compare November 7, 2024 07:21
@yomybaby yomybaby requested a review from rapsealk November 7, 2024 07:24
Copy link
Member

@rapsealk rapsealk left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

graphite-app bot commented Nov 8, 2024

Merge activity

Resolves #2819

**Changes:**
Introduces a new `useMemoWithPrevious` hook to manage previous and current values with reset capability, replacing the manual `useRef` implementation in ContainerLogModal. This hook provides a more robust way to track state changes while maintaining previous values.

**Implementation Details:**
- Created new `useMemoWithPrevious` hook that returns both current and previous values along with a reset function
- Updated ContainerLogModal to use the new hook for tracking log line numbers
- Replaced manual `previousLastLineNumber` ref management with the new hook's `resetPrevious` functionality
- Improved state management when switching kernel IDs or changing log sizes

**Testing Requirements:**
- Verify log highlighting works correctly when switching between different kernels
- Confirm previous line numbers are properly reset when changing log sizes
- Check that log display maintains proper highlighting of new content
@yomybaby yomybaby force-pushed the feature/use-memo-with-previouse branch from a3efc59 to 5702e0a Compare November 8, 2024 04:31
@graphite-app graphite-app bot merged commit 5702e0a into main Nov 8, 2024
6 checks passed
@graphite-app graphite-app bot deleted the feature/use-memo-with-previouse branch November 8, 2024 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L 100~500 LoC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hooks: useMemoWithPrevious
2 participants