-
Notifications
You must be signed in to change notification settings - Fork 9
[add] Class version of License Filter #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TechQuery
merged 13 commits into
Open-Source-Bazaar:main
from
luojiyin1987:feat/license-filter
Jul 16, 2025
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d16c5b0
i18n: add license filter
luojiyin1987 8f38ca2
dependencise: add license-filter
luojiyin1987 ce3a499
feat: license filter
luojiyin1987 421ec6b
fix: isHydrated error
luojiyin1987 f62981b
i18n: format
luojiyin1987 45885c7
feat: add license filter to router
luojiyin1987 87554f7
remove Hydration code
luojiyin1987 36e4560
i18n: license filter
luojiyin1987 5168ec2
fix: Hydration error
luojiyin1987 a2de38f
fix from review
luojiyin1987 b32866d
feat: show Progress
luojiyin1987 4d8b780
Merge branch 'main' into feat/license-filter
TechQuery 3473afa
[refactor] replace React hooks function with MobX observable class to…
TechQuery File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { FeatureAttitude, InfectionRange } from 'license-filter'; | ||
|
|
||
| import { i18n } from '../../models/Translation'; | ||
|
|
||
| type OptionValue = Record<string, { value: number; text: string }[]>; | ||
|
|
||
| type LicenseTips = Record<string, { text: string }[]>; | ||
|
|
||
| const options: string[] = [ | ||
| 'popularity', | ||
| 'reuseCondition', | ||
| 'infectionIntensity', | ||
| 'jurisdiction', | ||
| 'patentStatement', | ||
| 'patentRetaliation', | ||
| 'enhancedAttribution', | ||
| 'privacyLoophole', | ||
| 'marketingEndorsement', | ||
| ]; | ||
|
|
||
| export const optionValue = ({ t }: typeof i18n) => { | ||
| const optionValue = options.reduce((optionValue, option) => { | ||
| optionValue[option] = [ | ||
| { value: FeatureAttitude.Undefined, text: t('feature_attitude_undefined') }, | ||
| { value: FeatureAttitude.Positive, text: t('feature_attitude_positive') }, | ||
| { value: FeatureAttitude.Negative, text: t('feature_attitude_negative') }, | ||
| ]; | ||
|
|
||
| return optionValue; | ||
| }, {} as OptionValue); | ||
|
|
||
| optionValue.infectionRange = [ | ||
| { value: 0, text: t('infection_range_undefined') }, | ||
| { value: InfectionRange.Library, text: t('infection_range_library') }, | ||
| { value: InfectionRange.File, text: t('infection_range_file') }, | ||
| { value: InfectionRange.Module, text: t('infection_range_module') }, | ||
| ]; | ||
|
|
||
| return optionValue; | ||
| }; | ||
|
|
||
| export const licenseTips = ({ t }: typeof i18n): LicenseTips => ({ | ||
| popularity: [{ text: t('tip_popularity_0') }, { text: t('tip_popularity_1') }], | ||
| reuseCondition: [{ text: t('tip_reuse_condition') }], | ||
| infectionIntensity: [{ text: t('tip_infection_intensity') }], | ||
| jurisdiction: [{ text: t('tip_jurisdiction') }], | ||
| patentStatement: [{ text: t('tip_patent_statement') }], | ||
| patentRetaliation: [{ text: t('tip_patent_retaliation') }], | ||
| enhancedAttribution: [{ text: t('tip_enhanced_attribution') }], | ||
| privacyLoophole: [{ text: t('tip_privacy_loophole') }], | ||
| marketingEndorsement: [{ text: t('tip_marketing_endorsement') }], | ||
| infectionRange: [{ text: t('tip_infection_range') }], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import { | ||
| FeatureAttitude, | ||
| filterLicenses, | ||
| InfectionRange, | ||
| License, | ||
| } from 'license-filter'; | ||
| import { observer } from 'mobx-react'; | ||
| import { GetServerSidePropsContext } from 'next'; | ||
| import { cache, compose, errorLogger } from 'next-ssr-middleware'; | ||
| import { FC, useContext, useEffect, useState } from 'react'; | ||
| import { | ||
| Accordion, | ||
| Button, | ||
| ButtonGroup, | ||
| Container, | ||
| ProgressBar, | ||
| } from 'react-bootstrap'; | ||
|
|
||
| import { PageHead } from '../components/Layout/PageHead'; | ||
| import { licenseTips, optionValue } from '../components/License/helper'; | ||
| import { i18n, I18nContext, I18nProps, loadSSRLanguage } from '../models/Translation'; | ||
|
|
||
| export const getServerSideProps = compose(cache(), errorLogger, async (context: GetServerSidePropsContext) => { | ||
| const ssrData = await loadSSRLanguage(context as any); | ||
|
|
||
| return { | ||
| props: JSON.parse(JSON.stringify({ | ||
| ...ssrData, | ||
| })), | ||
| }; | ||
| }); | ||
|
|
||
| interface List { | ||
| license: License; | ||
| score: number; | ||
| } | ||
|
|
||
| const choiceSteps = [ | ||
| 'popularity', | ||
| 'reuseCondition', | ||
| 'infectionIntensity', | ||
| 'infectionRange', | ||
| 'jurisdiction', | ||
| 'patentStatement', | ||
| 'patentRetaliation', | ||
| 'enhancedAttribution', | ||
| 'privacyLoophole', | ||
| 'marketingEndorsement', | ||
| ] as const; | ||
|
|
||
| const LicenseTool: FC<I18nProps> = 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={(() => { | ||
| const currentLang = i18n.currentLanguage; | ||
| const stepNumber = keyIndex + 1; | ||
|
|
||
| switch (currentLang) { | ||
| case 'zh-CN': | ||
| case 'zh-TW': | ||
| return `第 ${stepNumber} 步`; | ||
| case 'en-US': | ||
| default: | ||
| return `step ${stepNumber}`; | ||
| } | ||
| })()} | ||
|
luojiyin1987 marked this conversation as resolved.
Outdated
|
||
| /> | ||
| <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; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.