-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLicenseSubPanel.tsx
117 lines (111 loc) · 3.45 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
// 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 MuiBox from '@mui/material/Box';
import { sortBy } from 'lodash';
import { useMemo } 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 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>
<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}
/>
<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>
);
}