Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,29 @@
text-align: center;
}

.btnNewGroup {
margin: $sp-8 $sp-8 $sp-4 $sp-8;
.actions {
display: flex;
align-items: flex-end;
gap: $sp-4;
margin: $sp-8 $sp-8 $sp-4;

@include ltelg {
flex-direction: column;
align-items: stretch;
margin: $sp-4;
gap: $sp-3;
}
}

.searchField {
flex: 1;
}

.btnNewGroup {
margin: 0;
min-width: max-content;

@include ltelg {
width: 100%;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Permission groups page.
*/
import { FC, useContext, useState } from 'react'
import { ChangeEvent, FC, useContext, useMemo, useState } from 'react'
import classNames from 'classnames'

import { Button, LoadingSpinner, PageTitle } from '~/libs/ui'
import { Button, InputText, LoadingSpinner, PageTitle } from '~/libs/ui'
import { PlusIcon } from '@heroicons/react/solid'

import { DialogAddGroup } from '../../lib/components/DialogAddGroup'
Expand All @@ -24,6 +24,7 @@ const pageTitle = 'Groups'

export const PermissionGroupsPage: FC<Props> = (props: Props) => {
const [openDialogAddGroup, setOpenDialogAddGroup] = useState(false)
const [searchTerm, setSearchTerm] = useState('')
const { loadUser, cancelLoadUser, usersMapping }: AdminAppContextType
= useContext(AdminAppContext)
const {
Expand All @@ -37,6 +38,26 @@ export const PermissionGroupsPage: FC<Props> = (props: Props) => {
usersMapping,
)

const filteredGroups = useMemo(() => {

Choose a reason for hiding this comment

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

[⚠️ performance]
Using useMemo for filteredGroups is appropriate here to avoid unnecessary computations when groups or searchTerm change. However, ensure that groups is a stable reference (e.g., not recreated on every render) to fully benefit from memoization.

const normalized = searchTerm
.trim()
.toLowerCase()
if (!normalized) {
return groups
}

return groups.filter(group => {
const id = group.id ? group.id.toLowerCase() : ''
const name = group.name ? group.name.toLowerCase() : ''

return id.includes(normalized) || name.includes(normalized)
})
}, [groups, searchTerm])
const hasSearchTerm = useMemo(

Choose a reason for hiding this comment

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

[💡 performance]
The useMemo for hasSearchTerm is not necessary since the computation is trivial (just a string length check). Removing it could simplify the code without a performance hit.

() => searchTerm.trim().length > 0,
[searchTerm],
)

return (
<div className={classNames(styles.container, props.className)}>
<PageTitle>{pageTitle}</PageTitle>
Expand All @@ -51,24 +72,40 @@ export const PermissionGroupsPage: FC<Props> = (props: Props) => {
</div>
) : (
<>
<Button
primary
size='lg'
icon={PlusIcon}
iconToLeft
label='new group'
onClick={function onClick() {
setOpenDialogAddGroup(true)
}}
className={styles.btnNewGroup}
/>
{groups.length === 0 ? (
<div className={styles.actions}>
<InputText
name='groupSearch'
type='text'
label='Search groups'
placeholder='Search by name or ID'
value={searchTerm}
onChange={function onChange(event: ChangeEvent<HTMLInputElement>) {
setSearchTerm(event.target.value)
}}
forceUpdateValue
classNameWrapper={styles.searchField}
/>
<Button
primary
size='lg'
icon={PlusIcon}
iconToLeft
label='new group'
onClick={function onClick() {
setOpenDialogAddGroup(true)
}}
className={styles.btnNewGroup}
/>
</div>
{filteredGroups.length === 0 ? (
<p className={styles.noRecordFound}>
{MSG_NO_RECORD_FOUND}
{hasSearchTerm
? 'No groups match your search.'
: MSG_NO_RECORD_FOUND}
</p>
) : (
<GroupsTable
datas={groups}
datas={filteredGroups}
usersMapping={usersMapping}
/>
)}
Expand Down
Loading