Portal/rsconcept/frontend/src/features/library/components/SelectAccessPolicy.tsx

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import { MiniButton } from '@/components/Control';
2025-02-18 19:39:54 +03:00
import { DomIconProps } from '@/components/DomainIcons';
import { Dropdown, DropdownButton, useDropdown } from '@/components/Dropdown';
2025-02-18 19:39:54 +03:00
import { IconPrivate, IconProtected, IconPublic } from '@/components/Icons';
2025-01-28 23:23:03 +03:00
import { CProps } from '@/components/props';
2024-06-07 20:17:03 +03:00
import { prefixes } from '@/utils/constants';
import { describeAccessPolicy, labelAccessPolicy } from '@/utils/labels';
2025-02-18 19:39:54 +03:00
import { AccessPolicy } from '../backend/types';
interface SelectAccessPolicyProps extends CProps.Styling {
2024-06-07 20:17:03 +03:00
value: AccessPolicy;
onChange: (value: AccessPolicy) => void;
2024-06-07 20:17:03 +03:00
disabled?: boolean;
stretchLeft?: boolean;
}
2025-02-12 15:12:59 +03:00
export function SelectAccessPolicy({ value, disabled, stretchLeft, onChange, ...restProps }: SelectAccessPolicyProps) {
2024-06-07 20:17:03 +03:00
const menu = useDropdown();
2025-02-05 01:27:32 +03:00
function handleChange(newValue: AccessPolicy) {
menu.hide();
if (newValue !== value) {
onChange(newValue);
}
}
2024-06-07 20:17:03 +03:00
return (
<div ref={menu.ref} {...restProps}>
2024-06-07 20:17:03 +03:00
<MiniButton
title={`Доступ: ${labelAccessPolicy(value)}`}
hideTitle={menu.isOpen}
2024-06-21 17:13:34 +03:00
className='h-full'
2024-06-07 20:17:03 +03:00
icon={<PolicyIcon value={value} size='1.25rem' />}
onClick={menu.toggle}
disabled={disabled}
/>
<Dropdown isOpen={menu.isOpen} stretchLeft={stretchLeft}>
{Object.values(AccessPolicy).map((item, index) => (
<DropdownButton
key={`${prefixes.policy_list}${index}`}
text={labelAccessPolicy(item)}
title={describeAccessPolicy(item)}
icon={<PolicyIcon value={item} size='1rem' />}
onClick={() => handleChange(item)}
/>
))}
</Dropdown>
</div>
);
}
2025-02-18 19:39:54 +03:00
/** Icon for access policy. */
function PolicyIcon({ value, size = '1.25rem', className }: DomIconProps<AccessPolicy>) {
switch (value) {
case AccessPolicy.PRIVATE:
return <IconPrivate size={size} className={className ?? 'text-warn-600'} />;
case AccessPolicy.PROTECTED:
return <IconProtected size={size} className={className ?? 'text-sec-600'} />;
case AccessPolicy.PUBLIC:
return <IconPublic size={size} className={className ?? 'text-ok-600'} />;
}
}