-
-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* UI fixes on organisation pages * Added TSDoc for Truncated Text * Added Debouncer * Update src/components/OrgListCard/OrgListCard.tsx Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Added code rabbit suggestions * Fixed test error --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3b168ae
commit ef5a206
Showing
10 changed files
with
263 additions
and
25 deletions.
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 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
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 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 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
Oops, something went wrong.