-
Notifications
You must be signed in to change notification settings - Fork 217
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
area/ui: Add a preference to render function names from the right #5382
Open
yomete
wants to merge
3
commits into
main
Choose a base branch
from
function-name-right-side
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
ui/packages/shared/profile/src/ProfileIcicleGraph/IcicleGraphArrow/TextWithEllipsis.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2022 The Parca Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {useEffect, useRef, useState} from 'react'; | ||
|
||
import {USER_PREFERENCES, useUserPreference} from '@parca/hooks'; | ||
|
||
interface Props { | ||
text: string; | ||
x: number; | ||
y: number; | ||
width: number; | ||
} | ||
|
||
function calculateTruncatedText( | ||
text: string, | ||
textElement: SVGTextElement, | ||
maxWidth: number | ||
): string { | ||
// Create a temporary element for measurement | ||
const tempElement = textElement.cloneNode() as SVGTextElement; | ||
tempElement.textContent = text; | ||
textElement.parentElement?.appendChild(tempElement); | ||
|
||
// If the text fits, return it | ||
const fullWidth = tempElement.getComputedTextLength(); | ||
if (fullWidth <= maxWidth) { | ||
textElement.parentElement?.removeChild(tempElement); | ||
return text; | ||
} | ||
|
||
// Binary search to find the maximum text that fits | ||
let start = 0; | ||
let end = text.length; | ||
let result = text; | ||
|
||
while (start < end) { | ||
const mid = Math.floor((start + end + 1) / 2); | ||
const truncated = text.slice(-mid); | ||
|
||
tempElement.textContent = truncated; | ||
const currentWidth = tempElement.getComputedTextLength(); | ||
|
||
if (currentWidth <= maxWidth) { | ||
result = truncated; | ||
start = mid; | ||
} else { | ||
end = mid - 1; | ||
} | ||
} | ||
|
||
textElement.parentElement?.removeChild(tempElement); | ||
return result; | ||
} | ||
|
||
function TextWithEllipsis({text, x, y, width}: Props): JSX.Element { | ||
const textRef = useRef<SVGTextElement>(null); | ||
const [displayText, setDisplayText] = useState(text); | ||
const [showFunctionNameFromLeft] = useUserPreference<boolean>( | ||
USER_PREFERENCES.SHOW_FUNCTION_NAME_FROM_LEFT.key | ||
); | ||
|
||
useEffect(() => { | ||
const textElement = textRef.current; | ||
if (textElement === null) return; | ||
|
||
const newText = showFunctionNameFromLeft | ||
? text | ||
: calculateTruncatedText(text, textElement, width); | ||
|
||
setDisplayText(newText); | ||
}, [text, width, showFunctionNameFromLeft]); | ||
|
||
return ( | ||
<text ref={textRef} x={x} y={y} className="text-xs"> | ||
{displayText} | ||
</text> | ||
); | ||
} | ||
|
||
export default TextWithEllipsis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This
useState
+useEffect
combo can be replaced with a singleuseMemo
that returns the text that can be used below to render.Technically,
useState
+useEffect
method has to render the component twice before it renders the expected output while theuseMemo
approach would do it in a single render. Not a biggie, but it improves the readability as well.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.
good catch, i'll make that change.
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.
hmm so I made changes based on the feedback above to now use useMemo.
but with this new change we don't get to see the truncated text when it should be shown.
I'm assuming this is related to the usage of the
textRef
ref and the fact that useMemo is running before the ref is attached to the DOM, therefore the initial render will always havetextRef.current
as null, and the memoization is preventing subsequent updates.