-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLicenseSubPanel.tsx
142 lines (136 loc) · 4.4 KB
/
LicenseSubPanel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-FileCopyrightText: Meta Platforms, Inc. and its affiliates
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
import NotesIcon from '@mui/icons-material/Notes';
import { Badge, ToggleButton } from '@mui/material';
import MuiBox from '@mui/material/Box';
import { sortBy } from 'lodash';
import { useMemo, useState } from 'react';
import { PackageInfo } from '../../../../shared/shared-types';
import { text } from '../../../../shared/text';
import { setTemporaryDisplayPackageInfo } from '../../../state/actions/resource-actions/all-views-simple-actions';
import { useAppDispatch, useAppSelector } from '../../../state/hooks';
import {
getFrequentLicensesNameOrder,
getFrequentLicensesTexts,
} from '../../../state/selectors/resource-selectors';
import { Confirm } from '../../ConfirmationDialog/ConfirmationDialog';
import { TextBox } from '../../TextBox/TextBox';
import { AttributionFormConfig } from '../AttributionForm';
import { PackageAutocomplete } from '../PackageAutocomplete/PackageAutocomplete';
const classes = {
licenseText: {
marginTop: '12px',
},
endAdornment: {
paddingRight: '6px',
paddingTop: '2px',
},
};
interface LicenseSubPanelProps {
packageInfo: PackageInfo;
showHighlight?: boolean;
onEdit?: Confirm;
expanded?: boolean;
hidden?: boolean;
config?: AttributionFormConfig;
}
export function LicenseSubPanel({
packageInfo,
showHighlight,
onEdit,
expanded,
hidden,
config,
}: LicenseSubPanelProps) {
const [showLicenseText, setShowLicenseText] = useState(false);
const dispatch = useAppDispatch();
const frequentLicenseTexts = useAppSelector(getFrequentLicensesTexts);
const frequentLicensesNames = useAppSelector(getFrequentLicensesNameOrder);
const defaultLicenses = useMemo(
() =>
sortBy(
frequentLicensesNames.map<PackageInfo>(({ fullName, shortName }) => ({
id: shortName,
licenseName: fullName,
source: {
name: text.attributionColumn.commonLicenses,
},
suffix: `(${shortName})`,
})),
({ licenseName }) => licenseName?.toLowerCase(),
),
[frequentLicensesNames],
);
const defaultLicenseText = packageInfo.licenseText
? undefined
: frequentLicenseTexts[packageInfo.licenseName || ''];
return hidden ? null : (
<MuiBox>
<MuiBox display={'flex'} alignItems={'center'} gap={'8px'}>
<PackageAutocomplete
attribute={'licenseName'}
title={text.attributionColumn.licenseName}
packageInfo={packageInfo}
readOnly={!onEdit}
showHighlight={showHighlight}
onEdit={onEdit}
endAdornment={config?.licenseName?.endIcon}
defaults={defaultLicenses}
color={config?.licenseName?.color}
focused={config?.licenseName?.focused}
/>
{!expanded && (
<ToggleButton
value={'license-text'}
selected={showLicenseText}
onChange={() => setShowLicenseText((prev) => !prev)}
size={'small'}
aria-label="license-text-toggle-button"
>
<Badge
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
color={'info'}
variant={'dot'}
invisible={!packageInfo.licenseText}
>
<NotesIcon />
</Badge>
</ToggleButton>
)}
</MuiBox>
{(showLicenseText || expanded) && (
<TextBox
readOnly={!onEdit}
placeholder={defaultLicenseText}
sx={classes.licenseText}
maxRows={8}
minRows={3}
color={config?.licenseText?.color}
focused={config?.licenseText?.focused}
multiline
expanded={expanded}
title={
defaultLicenseText
? text.attributionColumn.licenseTextDefault
: text.attributionColumn.licenseText
}
text={packageInfo.licenseText}
handleChange={({ target: { value } }) =>
onEdit?.(() =>
dispatch(
setTemporaryDisplayPackageInfo({
...packageInfo,
licenseText: value,
wasPreferred: undefined,
}),
),
)
}
endIcon={config?.licenseText?.endIcon}
/>
)}
</MuiBox>
);
}