mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-02 12:25:44 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import React from 'react';
|
|
import { useStore } from '@tanstack/react-form';
|
|
import type { AnyFormApi } from '@tanstack/react-form';
|
|
import TitledGreyBox from '@/components/elements/TitledGreyBox';
|
|
import Checkbox from '@/components/ui/Checkbox';
|
|
import { setPermissionsSelected } from '@/components/server/users/permissionSelection';
|
|
|
|
interface Props {
|
|
form: AnyFormApi;
|
|
isEditable: boolean;
|
|
title: string;
|
|
editablePermissions: string[];
|
|
className?: string;
|
|
children?: React.ReactNode;
|
|
}
|
|
|
|
const PermissionTitleBox = ({ form, isEditable, title, editablePermissions, className, children }: Props) => {
|
|
const value = useStore(form.store, (state) => (state.values.permissions ?? []) as string[]);
|
|
const valueSet = new Set(value);
|
|
const canToggleGroup = isEditable && editablePermissions.length > 0;
|
|
const allChecked = editablePermissions.length > 0 && editablePermissions.every((p) => valueSet.has(p));
|
|
|
|
const onCheckedChange = (checked: boolean) => {
|
|
form.setFieldValue('permissions', setPermissionsSelected(value, editablePermissions, checked));
|
|
};
|
|
|
|
return (
|
|
<TitledGreyBox
|
|
title={
|
|
<div className={'flex items-center'}>
|
|
<p className={'text-sm uppercase flex-1'}>{title}</p>
|
|
{canToggleGroup && (
|
|
<Checkbox
|
|
aria-label={`Select all ${title} permissions`}
|
|
checked={allChecked}
|
|
indeterminate={!allChecked && editablePermissions.some((p) => valueSet.has(p))}
|
|
onChange={onCheckedChange}
|
|
className={'w-5 h-5'}
|
|
/>
|
|
)}
|
|
</div>
|
|
}
|
|
className={className}
|
|
>
|
|
{children}
|
|
</TitledGreyBox>
|
|
);
|
|
};
|
|
|
|
export default PermissionTitleBox;
|