generated from idea2app/Next-Bootstrap-ts
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlicense-filter.tsx
More file actions
191 lines (162 loc) · 5.55 KB
/
license-filter.tsx
File metadata and controls
191 lines (162 loc) · 5.55 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { FeatureAttitude, filterLicenses, InfectionRange, License } from 'license-filter';
import { observer } from 'mobx-react';
import { FC, useContext, useEffect, useState } from 'react';
import { Accordion, Button, ButtonGroup, Container, ProgressBar } from 'react-bootstrap';
import { licenseTips, optionValue } from '../components/License/helper';
import { PageHead } from '../components/PageHead';
import { i18n, I18nContext } from '../models/Translation';
interface List {
license: License;
score: number;
}
const choiceSteps = [
'popularity',
'reuseCondition',
'infectionIntensity',
'infectionRange',
'jurisdiction',
'patentStatement',
'patentRetaliation',
'enhancedAttribution',
'privacyLoophole',
'marketingEndorsement',
] as const;
const LicenseTool: FC = observer(() => {
const i18n = useContext(I18nContext);
const { t } = i18n;
const [stepIndex, setStepIndex] = useState(0);
const [keyIndex, setKeyIndex] = useState(0);
const [filterOption, setFilterOption] = useState({});
const [disableChoose, setDisableChoose] = useState(false);
const [lists, setLists] = useState<List[]>([]);
const now = Math.ceil(100 / choiceSteps.length);
useEffect(() => {
if (stepIndex === choiceSteps.length) setDisableChoose(true);
}, [stepIndex]);
const handleChoose = (value: string | null) => {
const choice = value ? +value : 0;
const key = choiceSteps[keyIndex];
const newObject = { ...filterOption, [key]: choice };
const tempLists = filterLicenses(newObject);
setFilterOption(newObject);
setLists(tempLists);
setStepIndex(stepIndex < choiceSteps.length ? stepIndex + 1 : stepIndex);
setKeyIndex(keyIndex < choiceSteps.length - 1 ? keyIndex + 1 : keyIndex);
};
const backToLast = () => {
const choice = 0;
const key = choiceSteps[keyIndex];
const newObject = { ...filterOption, [key]: choice };
const tempLists = filterLicenses(newObject);
setFilterOption(newObject);
setStepIndex(
stepIndex === choiceSteps.length ? stepIndex - 2 : stepIndex > 0 ? stepIndex - 1 : stepIndex,
);
setKeyIndex(keyIndex > 0 ? keyIndex - 1 : keyIndex);
if (disableChoose) setDisableChoose(false);
setLists(tempLists);
};
return (
<Container className="py-5">
<PageHead title={t('license_tool_headline')} />
<h1>{t('license_tool_headline')}</h1>
<p>{t('license_tool_description')}</p>
<p className="text-warning">{t('warn_info')}</p>
<h2>
{t('filter_option')}: {t(choiceSteps[keyIndex])}
</h2>
{licenseTips(i18n)[choiceSteps[keyIndex]].map(({ text }) => (
<p key={text}>{text}</p>
))}
<ProgressBar
className="mb-3"
variant="info"
now={(keyIndex + 1) * now}
label={t('step_x', { step: keyIndex + 1 })}
/>
<Button className="mb-2" variant="warning" onClick={backToLast}>
{t('last_step')}
</Button>
<ButtonGroup className="mb-2">
{optionValue(i18n)[choiceSteps[keyIndex]].map(({ value, text }) => (
<Button
key={value}
className="mx-1"
value={value}
id={`tb-${value}`}
disabled={disableChoose}
onClick={({ currentTarget: { value } }) => handleChoose(value)}
>
{text}
</Button>
))}
</ButtonGroup>
<Accordion defaultActiveKey="0">
{lists.map(({ license, score }, index) => (
<Accordion.Item key={license.name} eventKey={index + 1 + ''}>
<Accordion.Header>
{license.name} {t('license_score')}: {score * 10}
</Accordion.Header>
<Accordion.Body>{renderInfo(license, i18n)}</Accordion.Body>
</Accordion.Item>
))}
</Accordion>
</Container>
);
});
function renderInfo({ link, feature }: License, { t }: typeof i18n) {
const judge = (attitude: FeatureAttitude) =>
({
[FeatureAttitude.Positive]: t('attitude_positive'),
[FeatureAttitude.Negative]: t('attitude_negative'),
[FeatureAttitude.Undefined]: t('option_undefined'),
})[attitude] || t('option_undefined');
const judgeInfectionRange = (infectionRange: InfectionRange | undefined) =>
infectionRange !== undefined
? {
[InfectionRange.Library]: t('range_library'),
[InfectionRange.File]: t('range_file'),
[InfectionRange.Module]: t('range_module'),
}[infectionRange]
: t('option_undefined');
return (
<>
<ul>
<li>
{t('popularity')}: {judge(feature.popularity)}
</li>
<li>
{t('reuseCondition')}: {judge(feature.reuseCondition)}
</li>
<li>
{t('infectionIntensity')}: {judge(feature.infectionIntensity)}
</li>
<li>
{t('infectionRange')}: {judgeInfectionRange(feature.infectionRange)}
</li>
<li>
{t('jurisdiction')}: {judge(feature.jurisdiction)}
</li>
<li>
{t('patentStatement')}: {judge(feature.patentStatement)}
</li>
<li>
{t('patentRetaliation')}: {judge(feature.patentRetaliation)}
</li>
<li>
{t('enhancedAttribution')}: {judge(feature.enhancedAttribution)}
</li>
<li>
{t('privacyLoophole')}: {judge(feature.privacyLoophole)}
</li>
<li>
{t('marketingEndorsement')}: {judge(feature.marketingEndorsement)}
</li>
</ul>
<Button size="sm" target="_blank" href={link}>
{t('license_detail')}
</Button>
</>
);
}
export default LicenseTool;