-
-
Notifications
You must be signed in to change notification settings - Fork 888
Fixes #2986 - Multiple UI Updates #3165
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
Merged
palisadoes
merged 8 commits into
PalisadoesFoundation:develop-postgres
from
AceHunterr:Issue#2986
Jan 7, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
36d12b6
UI fixes on organisation pages
AceHunterr b4b8eba
UI fixes on organisation pages
AceHunterr 4882a61
Added TSDoc for Truncated Text
AceHunterr 6e6ed88
Added Debouncer
AceHunterr 744cf68
Update src/components/OrgListCard/OrgListCard.tsx
AceHunterr 7961826
Added code rabbit suggestions
AceHunterr c8dcc34
Added code rabbit suggestions
AceHunterr 558b366
Fixed test error
AceHunterr 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import useDebounce from './useDebounce'; | ||
|
||
/** | ||
* Props for the `TruncatedText` component. | ||
* | ||
* Includes the text to be displayed and an optional maximum width override. | ||
*/ | ||
interface InterfaceTruncatedTextProps { | ||
/** The full text to display. It may be truncated if it exceeds the maximum width. */ | ||
text: string; | ||
/** Optional: Override the maximum width for truncation. */ | ||
maxWidthOverride?: number; | ||
} | ||
|
||
/** | ||
* A React functional component that displays text and truncates it with an ellipsis (`...`) | ||
* if the text exceeds the available width or the `maxWidthOverride` value. | ||
* | ||
* The component adjusts the truncation dynamically based on the available space | ||
* or the `maxWidthOverride` value. It also listens for window resize events to reapply truncation. | ||
* | ||
* @param props - The props for the component. | ||
* @returns A heading element (`<h6>`) containing the truncated or full text. | ||
* | ||
* @example | ||
* ```tsx | ||
* <TruncatedText text="This is a very long text" maxWidthOverride={150} /> | ||
* ``` | ||
*/ | ||
const TruncatedText: React.FC<InterfaceTruncatedTextProps> = ({ | ||
text, | ||
maxWidthOverride, | ||
}) => { | ||
const [truncatedText, setTruncatedText] = useState<string>(''); | ||
const textRef = useRef<HTMLHeadingElement>(null); | ||
|
||
const { debouncedCallback, cancel } = useDebounce(() => { | ||
truncateText(); | ||
}, 100); | ||
|
||
/** | ||
* Truncate the text based on the available width or the `maxWidthOverride` value. | ||
*/ | ||
const truncateText = (): void => { | ||
const element = textRef.current; | ||
if (element) { | ||
const maxWidth = maxWidthOverride || element.offsetWidth; | ||
const fullText = text; | ||
|
||
const computedStyle = getComputedStyle(element); | ||
const fontSize = parseFloat(computedStyle.fontSize); | ||
const charPerPx = 0.065 + fontSize * 0.002; | ||
const maxChars = Math.floor(maxWidth * charPerPx); | ||
|
||
setTruncatedText( | ||
fullText.length > maxChars | ||
? `${fullText.slice(0, maxChars - 3)}...` | ||
: fullText, | ||
); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
truncateText(); | ||
window.addEventListener('resize', debouncedCallback); | ||
return () => { | ||
cancel(); | ||
window.removeEventListener('resize', debouncedCallback); | ||
}; | ||
}, [text, maxWidthOverride, debouncedCallback, cancel]); | ||
|
||
return ( | ||
<h6 ref={textRef} className="text-secondary"> | ||
{truncatedText} | ||
</h6> | ||
); | ||
}; | ||
|
||
export default TruncatedText; |
This file contains hidden or 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,42 @@ | ||
import { useRef, useCallback } from 'react'; | ||
|
||
/** | ||
* A custom React hook for debouncing a callback function. | ||
* It delays the execution of the callback until after a specified delay has elapsed | ||
* since the last time the debounced function was invoked. | ||
* | ||
* @param callback - The function to debounce. | ||
* @param delay - The delay in milliseconds to wait before invoking the callback. | ||
* @returns An object with the `debouncedCallback` function and a `cancel` method to clear the timeout. | ||
*/ | ||
function useDebounce<T extends (...args: unknown[]) => void>( | ||
callback: T, | ||
delay: number, | ||
): { debouncedCallback: (...args: Parameters<T>) => void; cancel: () => void } { | ||
const timeoutRef = useRef<number | undefined>(); | ||
|
||
/** | ||
* The debounced version of the provided callback function. | ||
* This function resets the debounce timer on each call, ensuring the callback | ||
* is invoked only after the specified delay has elapsed without further calls. | ||
* | ||
* @param args - The arguments to pass to the callback when invoked. | ||
*/ | ||
const debouncedCallback = useCallback( | ||
(...args: Parameters<T>) => { | ||
if (timeoutRef.current) clearTimeout(timeoutRef.current); | ||
timeoutRef.current = window.setTimeout(() => { | ||
callback(...args); | ||
}, delay); | ||
}, | ||
[callback, delay], | ||
); | ||
|
||
const cancel = useCallback(() => { | ||
if (timeoutRef.current) clearTimeout(timeoutRef.current); | ||
}, []); | ||
|
||
return { debouncedCallback, cancel }; | ||
} | ||
|
||
export default useDebounce; |
This file contains hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.