Skip to content

Commit

Permalink
Merge branch 'feature/deprecated-fields-support' into vite
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorezz committed Dec 5, 2024
2 parents 76f8291 + 185050c commit 644b15a
Show file tree
Hide file tree
Showing 24 changed files with 507 additions and 71 deletions.
17 changes: 17 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@
"postCreateCommand": "sh .devcontainer/postCreateCommand.sh",
"customizations": {
"vscode": {
"settings": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[go]": {
"editor.insertSpaces": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
}
}
},
"extensions": [
"dbaeumer.vscode-eslint",
"github.vscode-pull-request-github",
"golang.go",
"waderyan.gitblame"
]
}
Expand Down
7 changes: 7 additions & 0 deletions .devcontainer/githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

echo "Git pre-push hook"
echo "Run Build"
npm run build
echo "Run Test"
npm run test
10 changes: 9 additions & 1 deletion .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#!/bin/bash

echo PostCreateCommand

echo "NodeJs version"
node -v
go s
echo "GO version"
go version

echo "Set Git hooks"
echo "Set pre-push hook"
cp .devcontainer/githooks/pre-push .git/hooks
chmod +x .git/hooks/pre-push

echo "Install Global Deps"
npm i -g http-server
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
Expand Down
27 changes: 23 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"js-yaml": "^4.1.0",
"locale-codes": "^1.3.1",
"lodash": "^4.17.21",
"mime-db": "^1.53.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.45.0",
Expand All @@ -70,6 +71,7 @@
"@types/golang-wasm-exec": "^1.15.0",
"@types/js-yaml": "^4.0.9",
"@types/lodash": "^4.14.195",
"@types/mime-db": "^1.43.5",
"@types/node": "^20.3.1",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
Expand Down
128 changes: 101 additions & 27 deletions src/app/components/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldErrors, FormProvider, Resolver, useForm } from "react-hook-form";
import { FieldErrors, FieldPathByValue, FormProvider, Resolver, useForm } from "react-hook-form";
import PubliccodeYmlLanguages from "./PubliccodeYmlLanguages";

import { Col, Container, notify, Row } from "design-react-kit";
Expand All @@ -14,7 +14,7 @@ import * as countrySection from "../contents/countrySpecificSection";
import developmentStatus from "../contents/developmentStatus";
import maintenanceTypes from "../contents/maintenanceTypes";
import platforms from "../contents/platforms";
import PublicCode, { defaultItaly } from "../contents/publiccode";
import PublicCode, { defaultItaly, LATEST_VERSION, PublicCodeWithDeprecatedFields } from "../contents/publiccode";
import softwareTypes from "../contents/softwareTypes";
import linter from "../linter";
import { useAppDispatch, useAppSelector } from "../store";
Expand All @@ -28,17 +28,22 @@ import EditorFeatures from "./EditorFeatures";
import EditorInput from "./EditorInput";
import EditorMultiselect from "./EditorMultiselect";
import EditorRadio from "./EditorRadio";
import EditorScreenshots from "./EditorScreenshots";
import EditorSelect from "./EditorSelect";
import { Footer } from "./Foot";
import Head from "./Head";
import InfoBox from "./InfoBox";
import { YamlModal } from "./YamlModal";

import useFormPersist from "react-hook-form-persist";
import { RequiredDeep } from "type-fest";
import mimeTypes from "../contents/mime-types";
import { getPubliccodeYmlVersionList } from "../contents/publiccode-yml-version";
import { isMinorThanLatest, toSemVerObject } from "../semver";
import { resetPubliccodeYmlLanguages, setPubliccodeYmlLanguages } from "../store/publiccodeYmlLanguages";
import yamlSerializer from "../yaml-serializer";

const validatorFn = async (values: PublicCode) => await validator(JSON.stringify(values), "main");
const validatorFn = async (values: PublicCode) => await validator(JSON.stringify(values), "main", values.url);

const checkWarnings = async (values: PublicCode) => {

Expand All @@ -56,8 +61,8 @@ const checkWarnings = async (values: PublicCode) => {
}


const resolver: Resolver<PublicCode> = async (values) => {
const res = await validatorFn(values);
const resolver: Resolver<PublicCode | PublicCodeWithDeprecatedFields> = async (values) => {
const res = await validatorFn(values as PublicCode);

if (res.errors.length === 0)
return {
Expand All @@ -78,7 +83,7 @@ const resolver: Resolver<PublicCode> = async (values) => {
};

const defaultValues = {
publiccodeYmlVersion: "0.4",
publiccodeYmlVersion: LATEST_VERSION,
legal: {},
localisation: { availableLanguages: [] },
maintenance: { contacts: [], contractors: [] },
Expand All @@ -97,29 +102,60 @@ export default function Editor() {
const { t } = useTranslation();
const languages = useAppSelector((state) => state.language.languages);
const configCountrySections = countrySection.parse(DEFAULT_COUNTRY_SECTIONS);

const [currentPublicodeYmlVersion, setCurrentPubliccodeYmlVersion] = useState('');
const [isYamlModalVisible, setYamlModalVisibility] = useState(false);

const getNestedValue = (obj: PublicCodeWithDeprecatedFields, path: string) => {
return path.split('.').reduce((acc, key) => (acc as never)?.[key], obj);
}

type PublicCodeDeprecatedField = FieldPathByValue<RequiredDeep<PublicCodeWithDeprecatedFields>, string | Array<string>>

const isDeprecatedFieldVisible = (fieldName: PublicCodeDeprecatedField) => {
const values = getValues() as PublicCodeWithDeprecatedFields;

if (!values) {
return false
}

const fieldValue = getNestedValue(values, fieldName);//values[fieldName]

if (fieldValue === null || fieldValue === undefined) {
return false
}

return true
}
//#endregion

//#region form definition
const methods = useForm<PublicCode>({
const methods = useForm<PublicCode | PublicCodeWithDeprecatedFields>({
defaultValues,
resolver,
});
const { getValues, handleSubmit, watch, setValue, reset } = methods;

const setLanguages = useCallback((publicCode: PublicCode) => {
console.log(Object.keys(publicCode.description));
dispatch(setPubliccodeYmlLanguages(Object.keys(publicCode.description)));
}, [dispatch])

const checkPubliccodeYmlVersion = useCallback((publicCode: PublicCode) => {
const { publiccodeYmlVersion } = publicCode

if (isMinorThanLatest(toSemVerObject(publiccodeYmlVersion))) {
setCurrentPubliccodeYmlVersion(publiccodeYmlVersion)
} else {
setCurrentPubliccodeYmlVersion('')
}
}, [])

useFormPersist("form-values", {
watch,
setValue,
onDataRestored: useCallback((pc: PublicCode) => {
console.log('onDataRestored', pc)
setLanguages(pc);
}, [setLanguages]),
checkPubliccodeYmlVersion(pc);
}, [setLanguages, checkPubliccodeYmlVersion]),
storage: window?.localStorage, // default window.sessionStorage
exclude: [],
});
Expand Down Expand Up @@ -155,23 +191,21 @@ export default function Editor() {
setLanguages(publicCode)
reset(values)

const res = await checkWarnings(values)
checkPubliccodeYmlVersion(publicCode);

console.log(res.warnings)
const res = await checkWarnings(values)

if (res.warnings.size) {
let body = ''
const body = Array
.from(res.warnings)
.reduce((p, [key, { message }]) => p + `${key}: ${message}`, '')

for (const item of res.warnings) {
const key = item[0];
const value = item[1].message;
body = body + `${key}: ${value}\n\n\n`;
}
const _1_MINUTE = 60 * 1 * 1000

notify('Warnings', body, {
dismissable: true,
state: 'warning',
duration: 60 * 2 * 1000
duration: _1_MINUTE
})
}
}
Expand All @@ -198,6 +232,17 @@ export default function Editor() {
<PubliccodeYmlLanguages />
<FormProvider {...methods}>
<form>
{currentPublicodeYmlVersion &&
<Row xs="1" md="1">
<Col>
<EditorSelect<"publiccodeYmlVersion">
fieldName="publiccodeYmlVersion"
data={getPubliccodeYmlVersionList(currentPublicodeYmlVersion)}
required
/>
</Col>
</Row>
}
<Row xs="1" md="2">
<Col>
<EditorInput<"name"> fieldName="name" required />
Expand All @@ -209,12 +254,15 @@ export default function Editor() {
{languages.map((lang) => (
<div key={`description.${lang}`}>
<Row xs="1" md="2">
<Col>
<EditorDescriptionInput<"genericName">
fieldName="genericName"
lang={lang}
/>
</Col>
{isDeprecatedFieldVisible((`description.${lang}.genericName` as never)) &&
<Col md={{ size: 12 }} xxl={{ size: 12 }}>
<EditorDescriptionInput<"genericName">
fieldName="genericName"
lang={lang}
deprecated
/>
</Col>
}
<Col>
<EditorDescriptionInput<"localisedName">
fieldName="localisedName"
Expand All @@ -231,6 +279,9 @@ export default function Editor() {
<Col>
<EditorFeatures lang={lang} />
</Col>
<Col>
<EditorScreenshots lang={lang} />
</Col>
</Row>
<Row>
<EditorDescriptionInput<"longDescription">
Expand Down Expand Up @@ -265,6 +316,23 @@ export default function Editor() {
required
/>
</Col>
{isDeprecatedFieldVisible('inputTypes') && <Col md={{ size: 12 }} xxl={{ size: 12 }}>
<EditorMultiselect<"inputTypes">
fieldName="inputTypes"
data={Object.keys(mimeTypes).map(o => ({ text: o, value: o }))}
/>
</Col>}
{isDeprecatedFieldVisible('outputTypes') && <Col md={{ size: 12 }} xxl={{ size: 12 }}>
<EditorMultiselect<"outputTypes">
fieldName="outputTypes"
data={Object.keys(mimeTypes).map(o => ({ text: o, value: o }))}
/>
</Col>}
{isDeprecatedFieldVisible('monochromeLogo') &&
<Col md={{ size: 12 }} xxl={{ size: 12 }}>
<EditorInput<"monochromeLogo"> fieldName="monochromeLogo" deprecated />
</Col>
}
<Col>
<EditorInput<"logo"> fieldName="logo" />
</Col>
Expand Down Expand Up @@ -300,6 +368,7 @@ export default function Editor() {
filter="contains"
/>
</Col>

<Col>
<EditorSelect<"legal.license">
fieldName="legal.license"
Expand All @@ -311,6 +380,11 @@ export default function Editor() {
}
/>
</Col>
{isDeprecatedFieldVisible("legal.authorsFile") &&
<Col md={{ size: 12 }} xxl={{ size: 12 }}>
<EditorInput<"legal.authorsFile"> fieldName="legal.authorsFile" deprecated />
</Col>
}
<Col>
<EditorRadio<"softwareType">
fieldName="softwareType"
Expand Down Expand Up @@ -361,7 +435,7 @@ export default function Editor() {
/>
<InfoBox />
<YamlModal
yaml={YAML.stringify(linter(getValues()))}
yaml={YAML.stringify(linter(getValues() as PublicCode))}
display={isYamlModalVisible}
toggle={() => setYamlModalVisibility(!isYamlModalVisible)}
/>
Expand Down
Loading

0 comments on commit 644b15a

Please sign in to comment.