Files
Ashley Harrison 47f8717149 React: Use new JSX transform (#88802)
* update eslint, tsconfig + esbuild to handle new jsx transform

* remove thing that breaks the new jsx transform

* remove react imports

* adjust grafana-icons build

* is this the correct syntax?

* try this

* well this was much easier than expected...

* change grafana-plugin-configs webpack config

* fixes

* fix lockfile

* fix 2 more violations

* use path.resolve instead of require.resolve

* remove react import

* fix react imports

* more fixes

* remove React import

* remove import React from docs

* remove another react import
2024-06-25 12:43:47 +01:00

114 lines
3.0 KiB
TypeScript

import { useEffect } from 'react';
import { useAsyncFn } from 'react-use';
import { contextSrv } from 'app/core/core';
import { Role, OrgRole, AccessControlAction } from 'app/types';
import { RolePicker } from './RolePicker';
import { fetchUserRoles, updateUserRoles } from './api';
export interface Props {
basicRole: OrgRole;
roles?: Role[];
userId: number;
orgId?: number;
onBasicRoleChange: (newRole: OrgRole) => void;
roleOptions: Role[];
disabled?: boolean;
basicRoleDisabled?: boolean;
basicRoleDisabledMessage?: string;
/**
* Set whether the component should send a request with the new roles to the
* backend in UserRolePicker.onRolesChange (apply=false), or call {@link onApplyRoles}
* with the updated list of roles (apply=true).
*
* Besides it sets the RolePickerMenu's Button title to
* * `Update` in case apply equals false
* * `Apply` in case apply equals true
*
* @default false
*/
apply?: boolean;
onApplyRoles?: (newRoles: Role[], userId: number, orgId: number | undefined) => void;
pendingRoles?: Role[];
maxWidth?: string | number;
width?: string | number;
isLoading?: boolean;
}
export const UserRolePicker = ({
basicRole,
roles,
userId,
orgId,
onBasicRoleChange,
roleOptions,
disabled,
basicRoleDisabled,
basicRoleDisabledMessage,
apply = false,
onApplyRoles,
pendingRoles,
maxWidth,
width,
isLoading,
}: Props) => {
const [{ loading, value: appliedRoles = roles || [] }, getUserRoles] = useAsyncFn(async () => {
try {
if (roles) {
return roles;
}
if (apply && Boolean(pendingRoles?.length)) {
return pendingRoles;
}
if (contextSrv.hasPermission(AccessControlAction.ActionUserRolesList) && userId > 0) {
return await fetchUserRoles(userId, orgId);
}
} catch (e) {
// TODO handle error
console.error('Error loading options');
}
return [];
}, [orgId, userId, pendingRoles, roles]);
useEffect(() => {
// only load roles when there is an Org selected
if (orgId) {
getUserRoles();
}
}, [getUserRoles, orgId]);
const onRolesChange = async (roles: Role[]) => {
if (!apply) {
await updateUserRoles(roles, userId, orgId);
await getUserRoles();
} else if (onApplyRoles) {
onApplyRoles(roles, userId, orgId);
}
};
const canUpdateRoles =
contextSrv.hasPermission(AccessControlAction.ActionUserRolesAdd) &&
contextSrv.hasPermission(AccessControlAction.ActionUserRolesRemove);
return (
<RolePicker
appliedRoles={appliedRoles}
basicRole={basicRole}
onRolesChange={onRolesChange}
onBasicRoleChange={onBasicRoleChange}
roleOptions={roleOptions}
isLoading={loading || isLoading}
disabled={disabled}
basicRoleDisabled={basicRoleDisabled}
basicRoleDisabledMessage={basicRoleDisabledMessage}
showBasicRole
apply={apply}
canUpdateRoles={canUpdateRoles}
maxWidth={maxWidth}
width={width}
/>
);
};