From 05f78d56ad01f8851cc2c7b374ede815ea2e0505 Mon Sep 17 00:00:00 2001 From: Thijs Daniels Date: Wed, 27 Sep 2023 00:32:13 +0200 Subject: [PATCH] feat(react-dictionary): add DictionaryProvider (#183) feat(react-dictionary): add DictionaryProvider --- .changeset/pink-news-jam.md | 5 + apps/infrastructure/package.json | 6 +- apps/storybook/package.json | 1 - package-lock.json | 592 +----------------- packages/react-dictionary/.eslintignore | 4 + packages/react-dictionary/LICENSE.md | 9 + packages/react-dictionary/README.md | 1 + .../components/DictionaryProvider.tsx | 46 ++ .../contexts/dictionaryContext.ts | 55 ++ .../react-dictionary/hooks/useDictionary.ts | 8 + .../react-dictionary/hooks/useTranslation.ts | 8 + packages/react-dictionary/index.ts | 4 + packages/react-dictionary/package.json | 33 + packages/react-dictionary/test/setup.ts | 7 + packages/react-dictionary/tsconfig.json | 3 + packages/react-dictionary/vitest.config.ts | 22 + packages/react-essentials/vitest.config.ts | 8 +- 17 files changed, 243 insertions(+), 569 deletions(-) create mode 100644 .changeset/pink-news-jam.md create mode 100644 packages/react-dictionary/.eslintignore create mode 100644 packages/react-dictionary/LICENSE.md create mode 100644 packages/react-dictionary/README.md create mode 100644 packages/react-dictionary/components/DictionaryProvider.tsx create mode 100644 packages/react-dictionary/contexts/dictionaryContext.ts create mode 100644 packages/react-dictionary/hooks/useDictionary.ts create mode 100644 packages/react-dictionary/hooks/useTranslation.ts create mode 100644 packages/react-dictionary/index.ts create mode 100644 packages/react-dictionary/package.json create mode 100644 packages/react-dictionary/test/setup.ts create mode 100644 packages/react-dictionary/tsconfig.json create mode 100644 packages/react-dictionary/vitest.config.ts diff --git a/.changeset/pink-news-jam.md b/.changeset/pink-news-jam.md new file mode 100644 index 00000000..264505bc --- /dev/null +++ b/.changeset/pink-news-jam.md @@ -0,0 +1,5 @@ +--- +"@codedazur/react-dictionary": minor +--- + +experimental release diff --git a/apps/infrastructure/package.json b/apps/infrastructure/package.json index 80a4fcc6..e5b3afa1 100644 --- a/apps/infrastructure/package.json +++ b/apps/infrastructure/package.json @@ -16,9 +16,7 @@ "@codedazur/cdk-static-site": "*", "@codedazur/essentials": "*", "aws-cdk-lib": "^2.89.0", - "constructs": "^10.2.69", - "eslint-config-next": "13.5.3", - "eslint-plugin-react": "7.33.2" + "constructs": "^10.2.69" }, "devDependencies": { "aws-cdk": "^2.89.0", @@ -27,4 +25,4 @@ "ts-node": "^10.9.1", "typescript": "^5.1.6" } -} \ No newline at end of file +} diff --git a/apps/storybook/package.json b/apps/storybook/package.json index 619e00ea..4dd4d8a0 100644 --- a/apps/storybook/package.json +++ b/apps/storybook/package.json @@ -23,7 +23,6 @@ "@storybook/jest": "^0.1.0 || ^0.2.0", "@types/styled-components": "^5.1.26", "color": "^4.2.3", - "eslint-config-next": "12.0.4", "react": "^18.2.0", "react-dom": "^18.2.0", "styled-components": "^5.3.6", diff --git a/package-lock.json b/package-lock.json index c5a297de..ef1a1c54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,9 +37,7 @@ "@codedazur/cdk-static-site": "*", "@codedazur/essentials": "*", "aws-cdk-lib": "^2.89.0", - "constructs": "^10.2.69", - "eslint-config-next": "13.5.3", - "eslint-plugin-react": "7.33.2" + "constructs": "^10.2.69" }, "devDependencies": { "aws-cdk": "^2.89.0", @@ -78,7 +76,6 @@ "@storybook/jest": "^0.1.0 || ^0.2.0", "@types/styled-components": "^5.1.26", "color": "^4.2.3", - "eslint-config-next": "12.0.4", "react": "^18.2.0", "react-dom": "^18.2.0", "styled-components": "^5.3.6", @@ -101,417 +98,6 @@ "vite": "4.4.9" } }, - "apps/storybook/node_modules/@babel/code-frame": { - "version": "7.12.11", - "license": "MIT", - "peer": true, - "dependencies": { - "@babel/highlight": "^7.10.4" - } - }, - "apps/storybook/node_modules/@eslint/eslintrc": { - "version": "0.4.3", - "license": "MIT", - "peer": true, - "dependencies": { - "ajv": "^6.12.4", - "debug": "^4.1.1", - "espree": "^7.3.0", - "globals": "^13.9.0", - "ignore": "^4.0.6", - "import-fresh": "^3.2.1", - "js-yaml": "^3.13.1", - "minimatch": "^3.0.4", - "strip-json-comments": "^3.1.1" - }, - "engines": { - "node": "^10.12.0 || >=12.0.0" - } - }, - "apps/storybook/node_modules/@humanwhocodes/config-array": { - "version": "0.5.0", - "license": "Apache-2.0", - "peer": true, - "dependencies": { - "@humanwhocodes/object-schema": "^1.2.0", - "debug": "^4.1.1", - "minimatch": "^3.0.4" - }, - "engines": { - "node": ">=10.10.0" - } - }, - "apps/storybook/node_modules/@typescript-eslint/parser": { - "version": "4.33.0", - "license": "BSD-2-Clause", - "dependencies": { - "@typescript-eslint/scope-manager": "4.33.0", - "@typescript-eslint/types": "4.33.0", - "@typescript-eslint/typescript-estree": "4.33.0", - "debug": "^4.3.1" - }, - "engines": { - "node": "^10.12.0 || >=12.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - }, - "peerDependencies": { - "eslint": "^5.0.0 || ^6.0.0 || ^7.0.0" - }, - "peerDependenciesMeta": { - "typescript": { - "optional": true - } - } - }, - "apps/storybook/node_modules/@typescript-eslint/scope-manager": { - "version": "4.33.0", - "license": "MIT", - "dependencies": { - "@typescript-eslint/types": "4.33.0", - "@typescript-eslint/visitor-keys": "4.33.0" - }, - "engines": { - "node": "^8.10.0 || ^10.13.0 || >=11.10.1" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, - "apps/storybook/node_modules/@typescript-eslint/types": { - "version": "4.33.0", - "license": "MIT", - "engines": { - "node": "^8.10.0 || ^10.13.0 || >=11.10.1" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, - "apps/storybook/node_modules/@typescript-eslint/typescript-estree": { - "version": "4.33.0", - "license": "BSD-2-Clause", - "dependencies": { - "@typescript-eslint/types": "4.33.0", - "@typescript-eslint/visitor-keys": "4.33.0", - "debug": "^4.3.1", - "globby": "^11.0.3", - "is-glob": "^4.0.1", - "semver": "^7.3.5", - "tsutils": "^3.21.0" - }, - "engines": { - "node": "^10.12.0 || >=12.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - }, - "peerDependenciesMeta": { - "typescript": { - "optional": true - } - } - }, - "apps/storybook/node_modules/@typescript-eslint/visitor-keys": { - "version": "4.33.0", - "license": "MIT", - "dependencies": { - "@typescript-eslint/types": "4.33.0", - "eslint-visitor-keys": "^2.0.0" - }, - "engines": { - "node": "^8.10.0 || ^10.13.0 || >=11.10.1" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/typescript-eslint" - } - }, - "apps/storybook/node_modules/acorn": { - "version": "7.4.1", - "license": "MIT", - "peer": true, - "bin": { - "acorn": "bin/acorn" - }, - "engines": { - "node": ">=0.4.0" - } - }, - "apps/storybook/node_modules/ansi-styles": { - "version": "4.3.0", - "license": "MIT", - "peer": true, - "dependencies": { - "color-convert": "^2.0.1" - }, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/chalk/ansi-styles?sponsor=1" - } - }, - "apps/storybook/node_modules/chalk": { - "version": "4.1.2", - "license": "MIT", - "peer": true, - "dependencies": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/chalk/chalk?sponsor=1" - } - }, - "apps/storybook/node_modules/color-convert": { - "version": "2.0.1", - "license": "MIT", - "peer": true, - "dependencies": { - "color-name": "~1.1.4" - }, - "engines": { - "node": ">=7.0.0" - } - }, - "apps/storybook/node_modules/color-name": { - "version": "1.1.4", - "license": "MIT", - "peer": true - }, - "apps/storybook/node_modules/escape-string-regexp": { - "version": "4.0.0", - "license": "MIT", - "peer": true, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "apps/storybook/node_modules/eslint": { - "version": "7.32.0", - "license": "MIT", - "peer": true, - "dependencies": { - "@babel/code-frame": "7.12.11", - "@eslint/eslintrc": "^0.4.3", - "@humanwhocodes/config-array": "^0.5.0", - "ajv": "^6.10.0", - "chalk": "^4.0.0", - "cross-spawn": "^7.0.2", - "debug": "^4.0.1", - "doctrine": "^3.0.0", - "enquirer": "^2.3.5", - "escape-string-regexp": "^4.0.0", - "eslint-scope": "^5.1.1", - "eslint-utils": "^2.1.0", - "eslint-visitor-keys": "^2.0.0", - "espree": "^7.3.1", - "esquery": "^1.4.0", - "esutils": "^2.0.2", - "fast-deep-equal": "^3.1.3", - "file-entry-cache": "^6.0.1", - "functional-red-black-tree": "^1.0.1", - "glob-parent": "^5.1.2", - "globals": "^13.6.0", - "ignore": "^4.0.6", - "import-fresh": "^3.0.0", - "imurmurhash": "^0.1.4", - "is-glob": "^4.0.0", - "js-yaml": "^3.13.1", - "json-stable-stringify-without-jsonify": "^1.0.1", - "levn": "^0.4.1", - "lodash.merge": "^4.6.2", - "minimatch": "^3.0.4", - "natural-compare": "^1.4.0", - "optionator": "^0.9.1", - "progress": "^2.0.0", - "regexpp": "^3.1.0", - "semver": "^7.2.1", - "strip-ansi": "^6.0.0", - "strip-json-comments": "^3.1.0", - "table": "^6.0.9", - "text-table": "^0.2.0", - "v8-compile-cache": "^2.0.3" - }, - "bin": { - "eslint": "bin/eslint.js" - }, - "engines": { - "node": "^10.12.0 || >=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/eslint" - } - }, - "apps/storybook/node_modules/eslint-config-next": { - "version": "12.0.4", - "license": "MIT", - "dependencies": { - "@next/eslint-plugin-next": "12.0.4", - "@rushstack/eslint-patch": "^1.0.6", - "@typescript-eslint/parser": "^4.20.0", - "eslint-import-resolver-node": "^0.3.4", - "eslint-import-resolver-typescript": "^2.4.0", - "eslint-plugin-import": "^2.22.1", - "eslint-plugin-jsx-a11y": "^6.4.1", - "eslint-plugin-react": "^7.23.1", - "eslint-plugin-react-hooks": "^4.2.0" - }, - "peerDependencies": { - "eslint": "^7.23.0", - "next": ">=10.2.0", - "typescript": ">=3.3.1" - }, - "peerDependenciesMeta": { - "typescript": { - "optional": true - } - } - }, - "apps/storybook/node_modules/eslint-import-resolver-typescript": { - "version": "2.7.1", - "license": "ISC", - "dependencies": { - "debug": "^4.3.4", - "glob": "^7.2.0", - "is-glob": "^4.0.3", - "resolve": "^1.22.0", - "tsconfig-paths": "^3.14.1" - }, - "engines": { - "node": ">=4" - }, - "peerDependencies": { - "eslint": "*", - "eslint-plugin-import": "*" - } - }, - "apps/storybook/node_modules/eslint-plugin-react-hooks": { - "version": "4.6.0", - "license": "MIT", - "engines": { - "node": ">=10" - }, - "peerDependencies": { - "eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0" - } - }, - "apps/storybook/node_modules/eslint-scope": { - "version": "5.1.1", - "license": "BSD-2-Clause", - "peer": true, - "dependencies": { - "esrecurse": "^4.3.0", - "estraverse": "^4.1.1" - }, - "engines": { - "node": ">=8.0.0" - } - }, - "apps/storybook/node_modules/eslint-visitor-keys": { - "version": "2.1.0", - "license": "Apache-2.0", - "engines": { - "node": ">=10" - } - }, - "apps/storybook/node_modules/espree": { - "version": "7.3.1", - "license": "BSD-2-Clause", - "peer": true, - "dependencies": { - "acorn": "^7.4.0", - "acorn-jsx": "^5.3.1", - "eslint-visitor-keys": "^1.3.0" - }, - "engines": { - "node": "^10.12.0 || >=12.0.0" - } - }, - "apps/storybook/node_modules/espree/node_modules/eslint-visitor-keys": { - "version": "1.3.0", - "license": "Apache-2.0", - "peer": true, - "engines": { - "node": ">=4" - } - }, - "apps/storybook/node_modules/estraverse": { - "version": "4.3.0", - "license": "BSD-2-Clause", - "peer": true, - "engines": { - "node": ">=4.0" - } - }, - "apps/storybook/node_modules/glob": { - "version": "7.2.3", - "license": "ISC", - "dependencies": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.1.1", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - }, - "engines": { - "node": "*" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, - "apps/storybook/node_modules/glob-parent": { - "version": "5.1.2", - "license": "ISC", - "peer": true, - "dependencies": { - "is-glob": "^4.0.1" - }, - "engines": { - "node": ">= 6" - } - }, - "apps/storybook/node_modules/has-flag": { - "version": "4.0.0", - "license": "MIT", - "peer": true, - "engines": { - "node": ">=8" - } - }, - "apps/storybook/node_modules/ignore": { - "version": "4.0.6", - "license": "MIT", - "peer": true, - "engines": { - "node": ">= 4" - } - }, - "apps/storybook/node_modules/supports-color": { - "version": "7.2.0", - "license": "MIT", - "peer": true, - "dependencies": { - "has-flag": "^4.0.0" - }, - "engines": { - "node": ">=8" - } - }, "apps/website": { "name": "@apps/website", "version": "0.0.0", @@ -3527,6 +3113,10 @@ "resolved": "packages/react-date-picker", "link": true }, + "node_modules/@codedazur/react-dictionary": { + "resolved": "packages/react-dictionary", + "link": true + }, "node_modules/@codedazur/react-essentials": { "resolved": "packages/react-essentials", "link": true @@ -4690,13 +4280,6 @@ "version": "13.5.3", "license": "MIT" }, - "node_modules/@next/eslint-plugin-next": { - "version": "12.0.4", - "license": "MIT", - "dependencies": { - "glob": "7.1.7" - } - }, "node_modules/@next/swc-darwin-x64": { "version": "13.5.3", "cpu": [ @@ -9875,6 +9458,7 @@ }, "node_modules/ansi-colors": { "version": "4.1.3", + "dev": true, "license": "MIT", "engines": { "node": ">=6" @@ -9964,6 +9548,7 @@ }, "node_modules/argparse": { "version": "1.0.10", + "dev": true, "license": "MIT", "dependencies": { "sprintf-js": "~1.0.2" @@ -10147,14 +9732,6 @@ "version": "0.0.7", "license": "ISC" }, - "node_modules/astral-regex": { - "version": "2.0.0", - "license": "MIT", - "peer": true, - "engines": { - "node": ">=8" - } - }, "node_modules/async": { "version": "3.2.4", "dev": true, @@ -13019,6 +12596,7 @@ }, "node_modules/enquirer": { "version": "2.4.1", + "dev": true, "license": "MIT", "dependencies": { "ansi-colors": "^4.1.1", @@ -13889,28 +13467,6 @@ "url": "https://opencollective.com/eslint" } }, - "node_modules/eslint-utils": { - "version": "2.1.0", - "license": "MIT", - "peer": true, - "dependencies": { - "eslint-visitor-keys": "^1.1.0" - }, - "engines": { - "node": ">=6" - }, - "funding": { - "url": "https://github.com/sponsors/mysticatea" - } - }, - "node_modules/eslint-utils/node_modules/eslint-visitor-keys": { - "version": "1.3.0", - "license": "Apache-2.0", - "peer": true, - "engines": { - "node": ">=4" - } - }, "node_modules/eslint-visitor-keys": { "version": "3.4.3", "license": "Apache-2.0", @@ -14083,6 +13639,7 @@ }, "node_modules/esprima": { "version": "4.0.1", + "dev": true, "license": "BSD-2-Clause", "bin": { "esparse": "bin/esparse.js", @@ -14891,11 +14448,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/functional-red-black-tree": { - "version": "1.0.1", - "license": "MIT", - "peer": true - }, "node_modules/functions-have-names": { "version": "1.2.3", "license": "MIT", @@ -15784,6 +15336,7 @@ }, "node_modules/is-fullwidth-code-point": { "version": "3.0.0", + "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -17890,6 +17443,7 @@ }, "node_modules/js-yaml": { "version": "3.14.1", + "dev": true, "license": "MIT", "dependencies": { "argparse": "^1.0.7", @@ -18358,11 +17912,6 @@ "dev": true, "license": "MIT" }, - "node_modules/lodash.truncate": { - "version": "4.4.2", - "license": "MIT", - "peer": true - }, "node_modules/log-symbols": { "version": "4.1.0", "dev": true, @@ -19901,6 +19450,7 @@ }, "node_modules/progress": { "version": "2.0.3", + "dev": true, "license": "MIT", "engines": { "node": ">=0.4.0" @@ -20529,17 +20079,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/regexpp": { - "version": "3.2.0", - "license": "MIT", - "peer": true, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/sponsors/mysticatea" - } - }, "node_modules/regexpu-core": { "version": "5.3.2", "dev": true, @@ -20700,6 +20239,7 @@ }, "node_modules/require-from-string": { "version": "2.0.2", + "dev": true, "license": "MIT", "engines": { "node": ">=0.10.0" @@ -21152,52 +20692,6 @@ "node": ">=8" } }, - "node_modules/slice-ansi": { - "version": "4.0.0", - "license": "MIT", - "peer": true, - "dependencies": { - "ansi-styles": "^4.0.0", - "astral-regex": "^2.0.0", - "is-fullwidth-code-point": "^3.0.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/chalk/slice-ansi?sponsor=1" - } - }, - "node_modules/slice-ansi/node_modules/ansi-styles": { - "version": "4.3.0", - "license": "MIT", - "peer": true, - "dependencies": { - "color-convert": "^2.0.1" - }, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/chalk/ansi-styles?sponsor=1" - } - }, - "node_modules/slice-ansi/node_modules/color-convert": { - "version": "2.0.1", - "license": "MIT", - "peer": true, - "dependencies": { - "color-name": "~1.1.4" - }, - "engines": { - "node": ">=7.0.0" - } - }, - "node_modules/slice-ansi/node_modules/color-name": { - "version": "1.1.4", - "license": "MIT", - "peer": true - }, "node_modules/smartwrap": { "version": "2.0.2", "dev": true, @@ -21423,6 +20917,7 @@ }, "node_modules/sprintf-js": { "version": "1.0.3", + "dev": true, "license": "BSD-3-Clause" }, "node_modules/stack-utils": { @@ -21539,6 +21034,7 @@ }, "node_modules/string-width": { "version": "4.2.3", + "dev": true, "license": "MIT", "dependencies": { "emoji-regex": "^8.0.0", @@ -21570,6 +21066,7 @@ }, "node_modules/string-width/node_modules/emoji-regex": { "version": "8.0.0", + "dev": true, "license": "MIT" }, "node_modules/string.prototype.matchall": { @@ -21883,41 +21380,6 @@ "url": "https://opencollective.com/unts" } }, - "node_modules/table": { - "version": "6.8.1", - "license": "BSD-3-Clause", - "peer": true, - "dependencies": { - "ajv": "^8.0.1", - "lodash.truncate": "^4.4.2", - "slice-ansi": "^4.0.0", - "string-width": "^4.2.3", - "strip-ansi": "^6.0.1" - }, - "engines": { - "node": ">=10.0.0" - } - }, - "node_modules/table/node_modules/ajv": { - "version": "8.12.0", - "license": "MIT", - "peer": true, - "dependencies": { - "fast-deep-equal": "^3.1.1", - "json-schema-traverse": "^1.0.0", - "require-from-string": "^2.0.2", - "uri-js": "^4.2.2" - }, - "funding": { - "type": "github", - "url": "https://github.com/sponsors/epoberezkin" - } - }, - "node_modules/table/node_modules/json-schema-traverse": { - "version": "1.0.0", - "license": "MIT", - "peer": true - }, "node_modules/tapable": { "version": "2.2.1", "license": "MIT", @@ -23255,11 +22717,6 @@ "uuid": "dist/bin/uuid" } }, - "node_modules/v8-compile-cache": { - "version": "2.4.0", - "license": "MIT", - "peer": true - }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "dev": true, @@ -24216,7 +23673,7 @@ }, "packages/cdk-mailer": { "name": "@codedazur/cdk-mailer", - "version": "0.0.0", + "version": "0.1.0", "license": "MIT", "dependencies": { "@aws-sdk/client-lambda": "^3.231.0", @@ -24484,6 +23941,21 @@ "react": ">=16.8.0" } }, + "packages/react-dictionary": { + "name": "@codedazur/react-dictionary", + "version": "0.0.0", + "license": "MIT", + "dependencies": { + "@codedazur/essentials": "^1.1.3" + }, + "devDependencies": { + "@codedazur/tsconfig": "^0.0.5" + }, + "peerDependencies": { + "react": ">=16.8.0", + "react-dom": ">=16.8.0" + } + }, "packages/react-essentials": { "name": "@codedazur/react-essentials", "version": "0.1.1", diff --git a/packages/react-dictionary/.eslintignore b/packages/react-dictionary/.eslintignore new file mode 100644 index 00000000..7b321ed3 --- /dev/null +++ b/packages/react-dictionary/.eslintignore @@ -0,0 +1,4 @@ +.turbo +coverage +dist +node_modules \ No newline at end of file diff --git a/packages/react-dictionary/LICENSE.md b/packages/react-dictionary/LICENSE.md new file mode 100644 index 00000000..811b1d9f --- /dev/null +++ b/packages/react-dictionary/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2022 Code d'Azur Interactive B.V. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/react-dictionary/README.md b/packages/react-dictionary/README.md new file mode 100644 index 00000000..f4fd2ff9 --- /dev/null +++ b/packages/react-dictionary/README.md @@ -0,0 +1 @@ +# @codedazur/react-dictionary diff --git a/packages/react-dictionary/components/DictionaryProvider.tsx b/packages/react-dictionary/components/DictionaryProvider.tsx new file mode 100644 index 00000000..24c49ea2 --- /dev/null +++ b/packages/react-dictionary/components/DictionaryProvider.tsx @@ -0,0 +1,46 @@ +import { FunctionComponent, ReactNode, useMemo } from "react"; +import { + Dictionary, + DictionaryKey, + Locale, + dictionaryContext, +} from "../contexts/dictionaryContext"; + +export interface DictionaryProviderProps { + locale?: Locale; + dictionaries: Record>; + children: ReactNode; +} + +export const DictionaryProvider: FunctionComponent = ({ + locale = Locale.en_US, + dictionaries, + children, +}) => { + const map = useMemo( + () => + new Map( + Object.entries(dictionaries).map(([dictionaryLocale, translations]) => [ + dictionaryLocale, + new Map( + Object.entries(translations) as unknown as [ + DictionaryKey, + string, + ][], + ), + ]) as [Locale, Dictionary][], + ), + [dictionaries], + ); + + return ( + + {children} + + ); +}; diff --git a/packages/react-dictionary/contexts/dictionaryContext.ts b/packages/react-dictionary/contexts/dictionaryContext.ts new file mode 100644 index 00000000..f2462cff --- /dev/null +++ b/packages/react-dictionary/contexts/dictionaryContext.ts @@ -0,0 +1,55 @@ +import { createContext } from "react"; + +export type Dictionaries = ReadonlyMap; +export type Dictionary = ReadonlyMap; + +/** + * This type can be overridden using module augmentation. + * + * @example + * // dictionary.d.ts + * + * import "@codedazur/react-dictionary"; + * + * declare module "@codedazur/react-dictionary" { + * export enum DictionaryKey { + * "foo", + * "bar", + * "baz", + * } + * } + */ +export enum DictionaryKey {} + +/** + * This enum can be overridden using module augmentation. + * + * @example + * // dictionary.d.ts + * + * import "@codedazur/react-dictionary"; + * + * declare module "@codedazur/react-dictionary" { + * export enum Locale { + * en_US = "en_US", + * en_GB = "en_GB", + * } + * } + */ +export enum Locale { + en_US = "en_US", +} + +export const DefaultLocale = Locale.en_US; + +export interface DictionaryContext { + locale: Locale | null; + entries: Dictionaries; +} + +export const dictionaryContext = createContext({ + locale: null, + entries: new Map([ + [null, new Map()], + ]), +}); diff --git a/packages/react-dictionary/hooks/useDictionary.ts b/packages/react-dictionary/hooks/useDictionary.ts new file mode 100644 index 00000000..2b681db2 --- /dev/null +++ b/packages/react-dictionary/hooks/useDictionary.ts @@ -0,0 +1,8 @@ +import { useContext } from "react"; +import { Locale, dictionaryContext } from "../contexts/dictionaryContext"; + +export const useDictionary = (locale?: Locale) => { + const { entries, locale: currentLocale } = useContext(dictionaryContext); + + return entries.get(locale ?? currentLocale)!; +}; diff --git a/packages/react-dictionary/hooks/useTranslation.ts b/packages/react-dictionary/hooks/useTranslation.ts new file mode 100644 index 00000000..94c476eb --- /dev/null +++ b/packages/react-dictionary/hooks/useTranslation.ts @@ -0,0 +1,8 @@ +import { DictionaryKey, Locale } from "../contexts/dictionaryContext"; +import { useDictionary } from "./useDictionary"; + +export const useTranslation = (key: DictionaryKey, locale?: Locale) => { + const dictionary = useDictionary(locale); + + return dictionary.get(key)!; +}; diff --git a/packages/react-dictionary/index.ts b/packages/react-dictionary/index.ts new file mode 100644 index 00000000..4fffcc3c --- /dev/null +++ b/packages/react-dictionary/index.ts @@ -0,0 +1,4 @@ +export * from "./components/DictionaryProvider"; +export * from "./contexts/dictionaryContext"; +export * from "./hooks/useDictionary"; +export * from "./hooks/useTranslation"; diff --git a/packages/react-dictionary/package.json b/packages/react-dictionary/package.json new file mode 100644 index 00000000..bb094b6c --- /dev/null +++ b/packages/react-dictionary/package.json @@ -0,0 +1,33 @@ +{ + "name": "@codedazur/react-dictionary", + "version": "0.0.0", + "main": ".dist/index.js", + "module": "./dist/index.mjs", + "types": "./dist/index.d.ts", + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.mjs" + } + }, + "publishConfig": { + "access": "public" + }, + "license": "MIT", + "scripts": { + "test": "vitest run --coverage", + "coverage": "vitest run --coverage", + "build": "tsup index.ts --format esm,cjs --dts", + "dev": "tsup index.ts --format esm,cjs --watch --external react" + }, + "peerDependencies": { + "react": ">=16.8.0", + "react-dom": ">=16.8.0" + }, + "dependencies": { + "@codedazur/essentials": "^1.1.3" + }, + "devDependencies": { + "@codedazur/tsconfig": "^0.0.5" + } +} diff --git a/packages/react-dictionary/test/setup.ts b/packages/react-dictionary/test/setup.ts new file mode 100644 index 00000000..d6ab365e --- /dev/null +++ b/packages/react-dictionary/test/setup.ts @@ -0,0 +1,7 @@ +import { cleanup } from "@testing-library/react"; +import { afterEach } from "vitest"; + +// Clears the JSDom after each "it" statement +afterEach(() => { + cleanup(); +}); diff --git a/packages/react-dictionary/tsconfig.json b/packages/react-dictionary/tsconfig.json new file mode 100644 index 00000000..786738c6 --- /dev/null +++ b/packages/react-dictionary/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@codedazur/tsconfig/react-library.json" +} diff --git a/packages/react-dictionary/vitest.config.ts b/packages/react-dictionary/vitest.config.ts new file mode 100644 index 00000000..488ec3dc --- /dev/null +++ b/packages/react-dictionary/vitest.config.ts @@ -0,0 +1,22 @@ +import react from "@vitejs/plugin-react"; +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + plugins: [react()], + test: { + environment: "jsdom", + setupFiles: "./test/setup.ts", + passWithNoTests: true, + coverage: { + provider: "v8", + reporter: ["text", "html", "clover", "json", "lcov"], + exclude: ["./test/setup.ts"], + thresholdAutoUpdate: true, + lines: 9.53, + functions: 33.33, + branches: 53.84, + statements: 9.53, + all: true, + }, + }, +}); diff --git a/packages/react-essentials/vitest.config.ts b/packages/react-essentials/vitest.config.ts index 488ec3dc..ab9c80a0 100644 --- a/packages/react-essentials/vitest.config.ts +++ b/packages/react-essentials/vitest.config.ts @@ -12,10 +12,10 @@ export default defineConfig({ reporter: ["text", "html", "clover", "json", "lcov"], exclude: ["./test/setup.ts"], thresholdAutoUpdate: true, - lines: 9.53, - functions: 33.33, - branches: 53.84, - statements: 9.53, + lines: 56.71, + functions: 44.44, + branches: 80, + statements: 56.71, all: true, }, },