|
| 1 | +import * as React from "react"; |
| 2 | +import { |
| 3 | + TagPicker, |
| 4 | + IBasePicker, |
| 5 | + ITag, |
| 6 | + IBasePickerSuggestionsProps, |
| 7 | + IPickerItemProps, |
| 8 | + ISuggestionItemProps, |
| 9 | +} from "@fluentui/react/lib/Pickers"; |
| 10 | + |
| 11 | +import { useGroups } from "../../hooks"; |
| 12 | +import { IGroup } from "../../common/model/IGroup"; |
| 13 | +import { IGroupPickerProps, GroupTypeFilter } from "./IGroupPickerProps"; |
| 14 | +import { IGroupPickerState } from "./IGroupPickerState"; |
| 15 | +import { useGroupPickerStyles } from "./GroupPickerStyles"; |
| 16 | +import { IconButton } from "@fluentui/react/lib/Button"; |
| 17 | +import { Text } from "@fluentui/react/lib/Text"; |
| 18 | +import { Stack } from "@fluentui/react/lib/Stack"; |
| 19 | +import { Label } from "@fluentui/react/lib/Label"; |
| 20 | +import { FontIcon } from "@fluentui/react/lib/Icon"; |
| 21 | +import { Customizer } from "@fluentui/react/lib/Utilities"; |
| 22 | + |
| 23 | +import pullAllBy from "lodash/pullAllBy"; |
| 24 | +import find from "lodash/find"; |
| 25 | +import strings from "ControlStrings"; |
| 26 | + |
| 27 | +const pickerSuggestionsProps: IBasePickerSuggestionsProps = { |
| 28 | + suggestionsHeaderText: strings.GroupPickerSuggestionsHeaderText, |
| 29 | + noResultsFoundText: strings.genericNoResultsFoundText, |
| 30 | +}; |
| 31 | + |
| 32 | +const initialState: IGroupPickerState = { |
| 33 | + savedSelectedGroups: [], |
| 34 | +}; |
| 35 | + |
| 36 | +const getTextFromItem = (item: ITag): string => item.name; |
| 37 | + |
| 38 | +const matchGroupType = ( |
| 39 | + group: IGroup, |
| 40 | + groupType?: GroupTypeFilter |
| 41 | +): boolean => { |
| 42 | + if (!groupType || groupType === "All") return true; |
| 43 | + |
| 44 | + const isUnified = group.groupTypes?.includes("Unified"); |
| 45 | + const isSecurity = !!group.securityEnabled && !isUnified; |
| 46 | + |
| 47 | + if (groupType === "M365") return isUnified; |
| 48 | + if (groupType === "Security") return isSecurity; |
| 49 | + return true; |
| 50 | +}; |
| 51 | + |
| 52 | +const reducer = ( |
| 53 | + state: IGroupPickerState, |
| 54 | + action: { type: string; payload: any } // eslint-disable-line @typescript-eslint/no-explicit-any |
| 55 | +): IGroupPickerState => { |
| 56 | + switch (action.type) { |
| 57 | + case "UPDATE_SELECTEDITEM": |
| 58 | + return { ...state, savedSelectedGroups: action.payload }; |
| 59 | + default: |
| 60 | + return state; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +export const GroupPicker: React.FunctionComponent<IGroupPickerProps> = ( |
| 65 | + props: IGroupPickerProps |
| 66 | +) => { |
| 67 | + const [state, dispatch] = React.useReducer(reducer, initialState); |
| 68 | + const picker = React.useRef<IBasePicker<ITag>>(null); |
| 69 | + const { serviceScope } = props.appcontext; |
| 70 | + const { getGroups } = useGroups(serviceScope); |
| 71 | + const { |
| 72 | + onSelectedGroups, |
| 73 | + selectedGroups, |
| 74 | + itemLimit, |
| 75 | + multiSelect, |
| 76 | + label, |
| 77 | + styles, |
| 78 | + themeVariant, |
| 79 | + groupType, |
| 80 | + } = props; |
| 81 | + |
| 82 | + const { |
| 83 | + pickerStylesMulti, |
| 84 | + pickerStylesSingle, |
| 85 | + renderItemStylesMulti, |
| 86 | + renderItemStylesSingle, |
| 87 | + renderIconButtonRemoveStyles, |
| 88 | + componentClasses, |
| 89 | + } = useGroupPickerStyles(themeVariant); |
| 90 | + |
| 91 | + const groupTypeById = React.useRef<Record<string, "m365" | "security">>({}); |
| 92 | + |
| 93 | + const getGroupTypeIcon = React.useCallback( |
| 94 | + (groupId: string | number | undefined): JSX.Element | null => { |
| 95 | + if (!groupId) return null; |
| 96 | + const groupType = groupTypeById.current[groupId.toString()]; |
| 97 | + if (!groupType) return null; |
| 98 | + if (groupType === "m365") { |
| 99 | + return ( |
| 100 | + <FontIcon |
| 101 | + iconName="OfficeLogo" |
| 102 | + className={componentClasses.groupTypeIconM365} |
| 103 | + title={strings.GroupPickerGroupTypeM365Label} |
| 104 | + aria-label={strings.GroupPickerGroupTypeM365Label} |
| 105 | + /> |
| 106 | + ); |
| 107 | + } |
| 108 | + return ( |
| 109 | + <FontIcon |
| 110 | + iconName="LockSolid" |
| 111 | + className={componentClasses.groupTypeIconSecurity} |
| 112 | + title={strings.GroupPickerGroupTypeSecurityLabel} |
| 113 | + aria-label={strings.GroupPickerGroupTypeSecurityLabel} |
| 114 | + /> |
| 115 | + ); |
| 116 | + }, |
| 117 | + [componentClasses.groupTypeIconM365, componentClasses.groupTypeIconSecurity] |
| 118 | + ); |
| 119 | + |
| 120 | + const useFilterSuggestedGroups = React.useCallback( |
| 121 | + async (filterText: string, groupsList: ITag[]): Promise<ITag[]> => { |
| 122 | + const tags: ITag[] = []; |
| 123 | + try { |
| 124 | + const groups: IGroup[] = await getGroups(filterText); |
| 125 | + if (groups?.length) { |
| 126 | + for (const group of groups) { |
| 127 | + if (!matchGroupType(group, groupType)) continue; |
| 128 | + const isUnified = group.groupTypes?.includes("Unified"); |
| 129 | + const groupTypeKey: "m365" | "security" | undefined = isUnified |
| 130 | + ? "m365" |
| 131 | + : group.securityEnabled |
| 132 | + ? "security" |
| 133 | + : undefined; |
| 134 | + if (groupTypeKey) { |
| 135 | + groupTypeById.current[group.id] = groupTypeKey; |
| 136 | + } |
| 137 | + const checkExists = find(groupsList, { key: group.id }); |
| 138 | + if (checkExists) continue; |
| 139 | + tags.push({ key: group.id, name: group.displayName }); |
| 140 | + } |
| 141 | + } |
| 142 | + return tags; |
| 143 | + } catch (error) { |
| 144 | + console.log(error); |
| 145 | + return tags; |
| 146 | + } |
| 147 | + }, |
| 148 | + [groupType, getGroups] |
| 149 | + ); |
| 150 | + |
| 151 | + React.useEffect(() => { |
| 152 | + dispatch({ |
| 153 | + type: "UPDATE_SELECTEDITEM", |
| 154 | + payload: selectedGroups, |
| 155 | + }); |
| 156 | + }, [props]); |
| 157 | + |
| 158 | + const _onRenderItem = React.useCallback( |
| 159 | + (itemProps: IPickerItemProps<ITag>) => { |
| 160 | + const { savedSelectedGroups } = state; |
| 161 | + if (itemProps.item) { |
| 162 | + return ( |
| 163 | + <Stack |
| 164 | + horizontal |
| 165 | + horizontalAlign="start" |
| 166 | + verticalAlign="center" |
| 167 | + tokens={{ childrenGap: 7 }} |
| 168 | + styles={ |
| 169 | + (multiSelect ?? true) && (itemLimit && itemLimit > 1) |
| 170 | + ? renderItemStylesMulti |
| 171 | + : renderItemStylesSingle |
| 172 | + } |
| 173 | + > |
| 174 | + <FontIcon iconName="Group" /> |
| 175 | + <Text variant="medium">{itemProps.item.name}</Text> |
| 176 | + {getGroupTypeIcon(itemProps.item.key)} |
| 177 | + <IconButton |
| 178 | + styles={renderIconButtonRemoveStyles} |
| 179 | + iconProps={{ iconName: "Cancel" }} |
| 180 | + title={strings.TeamPickerButtonRemoveTitle} |
| 181 | + onClick={() => { |
| 182 | + const _newSelectedGroups = pullAllBy(savedSelectedGroups, [ |
| 183 | + itemProps.item, |
| 184 | + ]); |
| 185 | + dispatch({ |
| 186 | + type: "UPDATE_SELECTEDITEM", |
| 187 | + payload: _newSelectedGroups, |
| 188 | + }); |
| 189 | + onSelectedGroups(_newSelectedGroups); |
| 190 | + }} |
| 191 | + /> |
| 192 | + </Stack> |
| 193 | + ); |
| 194 | + } |
| 195 | + return null; |
| 196 | + }, |
| 197 | + [ |
| 198 | + state.savedSelectedGroups, |
| 199 | + renderItemStylesMulti, |
| 200 | + renderItemStylesSingle, |
| 201 | + renderIconButtonRemoveStyles, |
| 202 | + itemLimit, |
| 203 | + onSelectedGroups, |
| 204 | + ] |
| 205 | + ); |
| 206 | + |
| 207 | + const _onRenderSuggestionsItem = React.useCallback( |
| 208 | + (propsTag: ITag, _itemProps: ISuggestionItemProps<ITag>) => { |
| 209 | + return ( |
| 210 | + <Stack |
| 211 | + horizontal |
| 212 | + horizontalAlign="start" |
| 213 | + verticalAlign="center" |
| 214 | + tokens={{ childrenGap: 5, padding: 10 }} |
| 215 | + > |
| 216 | + <FontIcon iconName="Group" /> |
| 217 | + <Text variant="smallPlus">{propsTag.name}</Text> |
| 218 | + {getGroupTypeIcon(propsTag.key)} |
| 219 | + </Stack> |
| 220 | + ); |
| 221 | + }, |
| 222 | + [] |
| 223 | + ); |
| 224 | + |
| 225 | + |
| 226 | + return ( |
| 227 | + <Customizer settings={{ theme: props.themeVariant }}> |
| 228 | + <div style={{ width: "100%" }}> |
| 229 | + {label && <Label>{label}</Label>} |
| 230 | + <TagPicker |
| 231 | + styles={ |
| 232 | + styles ?? |
| 233 | + ((multiSelect ?? true) && (itemLimit && itemLimit > 1) |
| 234 | + ? pickerStylesMulti |
| 235 | + : pickerStylesSingle) |
| 236 | + } |
| 237 | + selectedItems={state.savedSelectedGroups} |
| 238 | + onRenderItem={_onRenderItem} |
| 239 | + onRenderSuggestionsItem={_onRenderSuggestionsItem} |
| 240 | + onResolveSuggestions={useFilterSuggestedGroups} |
| 241 | + getTextFromItem={getTextFromItem} |
| 242 | + pickerSuggestionsProps={pickerSuggestionsProps} |
| 243 | + onEmptyResolveSuggestions={(selectGroups) => { |
| 244 | + return useFilterSuggestedGroups("", selectGroups); |
| 245 | + }} |
| 246 | + itemLimit={(multiSelect ?? true) ? (props.itemLimit ?? undefined) : 1} |
| 247 | + onChange={(items) => { |
| 248 | + dispatch({ type: "UPDATE_SELECTEDITEM", payload: items }); |
| 249 | + props.onSelectedGroups(items); |
| 250 | + }} |
| 251 | + componentRef={picker} |
| 252 | + /> |
| 253 | + </div> |
| 254 | + </Customizer> |
| 255 | + ); |
| 256 | +}; |
0 commit comments