Last active
June 28, 2023 21:55
-
-
Save jonidelv/c813da85320a9b6490b502a20d9781b2 to your computer and use it in GitHub Desktop.
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
| window.currentUser = { id: '19', name: 'Jane', email: '[email protected]' }; | |
| export const ActiveProfiles({ profiles, onLaunchProfile }) => { | |
| var active = []; | |
| for(i=0; i < profiles.length; i++) { | |
| if(!profiles[i].disabled && profiles[i]['last_seen_time'] > new Date(new Date().getTime()-(24*60*1000)).toISOString()) { // within the last 24 hours | |
| active.push(profiles[i]); | |
| } | |
| } | |
| if(active.length == 1 && active[0].email === window.currentUser.email) { | |
| active.length = 0; | |
| } | |
| return ( | |
| <div> | |
| {active.map(function(a) { return <div onClick={() => onLaunchProfile(a.name, a.email)}>{a.name} - {a.email}</div> })} | |
| </div> | |
| ) | |
| } |
Author
Author
When rendering a list of elements in React, it's important to assign a unique key prop to each element. This helps React identify which items have changed, are added, or are removed, and can improve the performance of the component.
If the profiles array contains a unique identifier for each profile, such as an id, you could use that as the key prop.
return (
<div>
{active.map(({ id, name, email }) => (
<div key={id} onClick={() => onLaunchProfile(name, email)}>
{name} - {email}
</div>
))}
</div>
);
Author
This is how I would modify this component to utilize the Dropdown component created above.
import React, { useMemo, useCallback } from 'react';
import Dropdown from './Dropdown';
export const ActiveProfiles = ({ profiles, onLaunchProfile }) => {
const active = useMemo(() => {
const filteredProfiles = profiles.filter(
profile =>
!profile.disabled &&
profile.last_seen_time >
new Date(new Date().getTime() - 24 * 60 * 1000).toISOString()
);
if (
filteredProfiles.length === 1 &&
filteredProfiles[0].email === window.currentUser.email
) {
return [];
}
return filteredProfiles;
}, [profiles]);
const items = active.map(({ id, name, email }) => ({
value: id,
label: `${name} - ${email}`,
name,
email
}));
const handleSelect = useCallback((selectedItem) => {
onLaunchProfile(selectedItem.name, selectedItem.email);
}, [onLaunchProfile]);
return (
<div>
<Dropdown
label="Select a profile"
items={items}
onSelect={handleSelect}
/>
</div>
);
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could move the conditional statement that checks if the
activearray has only one element and if that element's email is equal towindow.currentUser.emailinside theuseMemohook. Instead of setting thelengthproperty of theactivearray to 0, you could return an empty array.Here's an example of how you could implement this:
This way, the
activearray will be an empty array if the conditional statement is true.