diff options
Diffstat (limited to 'node_modules/@phntms/use-local-state')
30 files changed, 1070 insertions, 0 deletions
diff --git a/node_modules/@phntms/use-local-state/.babelrc b/node_modules/@phntms/use-local-state/.babelrc new file mode 100644 index 0000000..3e7174a --- /dev/null +++ b/node_modules/@phntms/use-local-state/.babelrc @@ -0,0 +1,6 @@ +{ + "presets": [ + "@babel/preset-env", + "@babel/preset-typescript" + ] +}
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/.eslintrc b/node_modules/@phntms/use-local-state/.eslintrc new file mode 100644 index 0000000..ec7d069 --- /dev/null +++ b/node_modules/@phntms/use-local-state/.eslintrc @@ -0,0 +1,57 @@ +{ + "env": { + "browser": true, + "node": true, + "es6": true, + "jest": true + }, + "parser": "@typescript-eslint/parser", + "parserOptions": { + "jsx": true, + "useJSXTextNode": true + }, + "settings": { + "react": { + "version": "detect" + } + }, + "plugins": ["prettier", "eslint-plugin-import"], + "extends": [ + "plugin:react/recommended", + "plugin:react-hooks/recommended", + "plugin:@typescript-eslint/recommended", + "plugin:prettier/recommended" + ], + "rules": { + "react-hooks/rules-of-hooks": "error", + "react-hooks/exhaustive-deps": "error", + "react/react-in-jsx-scope": "off", + "@typescript-eslint/explicit-function-return-type": "off", + "@typescript-eslint/explicit-module-boundary-types": "off", + "@typescript-eslint/ban-ts-comment": "off", + "react/prop-types": "off", + "import/order": [ + "error", + { + "groups": ["builtin", "external", "internal"], + "pathGroups": [ + { + "pattern": "react", + "group": "external", + "position": "before" + } + ], + "pathGroupsExcludedImportTypes": ["react"], + "newlines-between": "always", + "alphabetize": { + "order": "asc", + "caseInsensitive": true + } + } + ], + "prettier/prettier": "error" + }, + "globals": { + "React": "writable" + } +} diff --git a/node_modules/@phntms/use-local-state/.github/CODEOWNERS b/node_modules/@phntms/use-local-state/.github/CODEOWNERS new file mode 100644 index 0000000..6e3be23 --- /dev/null +++ b/node_modules/@phntms/use-local-state/.github/CODEOWNERS @@ -0,0 +1 @@ +* @digitaljohn @PauloMFJ
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REPORT_A_BUG.md b/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REPORT_A_BUG.md new file mode 100644 index 0000000..aed65f3 --- /dev/null +++ b/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REPORT_A_BUG.md @@ -0,0 +1,41 @@ +--- +name: "\U0001F41B Report a bug" +about: "Is something not working as you expect?" +--- + +<!-- Please answer the following. Issues that do not will be closed. --> + +> Clearly describe the bug + +e.g. "Data does not persist upon refresh." + +> Which version of use-local-state are you using? + +e.g. `0.1.0` + +> What version of React are you using use-local-state within? + +e.g. `16.9.0` + +> What build/platform are you using? + +e.g. "Next.js 10" + +> What did you expect to happen? + +e.g. "Updated value to be persisted." + +> What actually happened (e.g. what warnings or errors did you get)? + +e.g. "Hook always returns default state." + +<!-- +Before posting, please check that the bug hasn't already been discussed previously (https://github.com/phantomstudios/use-local-state/search) + +You can help us fix the bug more quickly by: +1. Figuring out what needs to be done and proposing it +2. Submitting a PR with failing tests. + +Once the bug has been confirmed, you can help out further by: +1. Writing the code and submitting a PR. +--> diff --git a/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REPORT_A_DOCS_ISSUE.md b/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REPORT_A_DOCS_ISSUE.md new file mode 100644 index 0000000..bcdd4be --- /dev/null +++ b/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REPORT_A_DOCS_ISSUE.md @@ -0,0 +1,14 @@ +--- +name: "\U0001F4DD Report a docs issue" +about: "Is something wrong, confusing or missing in the docs?" +--- + +<!-- Please answer the following. Issues that do not will be closed. --> + +> Clearly describe the documentation issue + +e.g. "Documentation for setting updated state is required." + +> What solution would you like to see? + +e.g. "I suggest rewriting it so it covers the following topics in detail..." diff --git a/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REQUEST_A_FEATURE.md b/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REQUEST_A_FEATURE.md new file mode 100644 index 0000000..bcf02d4 --- /dev/null +++ b/node_modules/@phntms/use-local-state/.github/ISSUE_TEMPLATE/REQUEST_A_FEATURE.md @@ -0,0 +1,24 @@ +--- +name: "\U0001F680 Request a feature" +about: "Do you want to suggest a new feature?" +--- + +<!-- Please answer the following. Issues that do not will be closed. --> + +> What is the problem you're trying to solve? + +e.g. "We need the ability reset and or clear the state..." + +> What solution would you like to see? + +e.g. "Expose a new method that..." + +<!-- +Before posting, please check that the feature hasn't already been discussed previously (https://github.com/phantomstudios/use-local-state/search) + +You can help us add the feature more quickly by: +1. Providing as much detail as possible in this issue + +Once the feature is approved, you can help out further by: +1. Writing the code and submitting a PR. +--> diff --git a/node_modules/@phntms/use-local-state/.github/PULL_REQUEST_TEMPLATE.md b/node_modules/@phntms/use-local-state/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..0aaafb8 --- /dev/null +++ b/node_modules/@phntms/use-local-state/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,12 @@ +<!--- +Please read the following. Pull requests that do not adhere to these guidelines will very likely be closed. + +Each pull request should, with the exception of minor documentation fixes, be associated with an open issue. If there is an associated open issue, then please continue by answering these two questions: --> + +> Which issue, if any, is this issue related to? + +e.g. "Closes #000" or "None, as it's a documentation fix." + +> Is there anything in the PR that needs further explanation? + +e.g. "No, it's self explanatory." diff --git a/node_modules/@phntms/use-local-state/.github/workflows/npmpublish.yml b/node_modules/@phntms/use-local-state/.github/workflows/npmpublish.yml new file mode 100644 index 0000000..6fb9e41 --- /dev/null +++ b/node_modules/@phntms/use-local-state/.github/workflows/npmpublish.yml @@ -0,0 +1,30 @@ +name: Node.js Package + +on: + release: + types: [created] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + - run: npm ci + - run: npm test + + publish-npm: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + - run: npm ci + - run: npm publish --access=public + env: + NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} diff --git a/node_modules/@phntms/use-local-state/.github/workflows/test.yml b/node_modules/@phntms/use-local-state/.github/workflows/test.yml new file mode 100644 index 0000000..35f72b1 --- /dev/null +++ b/node_modules/@phntms/use-local-state/.github/workflows/test.yml @@ -0,0 +1,28 @@ +name: test + +on: [pull_request] + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [12.x, 14.x, 16.x, 18.x] + + steps: + - uses: actions/checkout@v1 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: npm install, build, and test + run: | + npm ci + npm run build --if-present + npm run lint + npm test + env: + CI: true diff --git a/node_modules/@phntms/use-local-state/.prettierignore b/node_modules/@phntms/use-local-state/.prettierignore new file mode 100644 index 0000000..879f612 --- /dev/null +++ b/node_modules/@phntms/use-local-state/.prettierignore @@ -0,0 +1,5 @@ +node_modules +coverage +dist +public +tmp diff --git a/node_modules/@phntms/use-local-state/CODE_OF_CONDUCT.md b/node_modules/@phntms/use-local-state/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..a5ce8d8 --- /dev/null +++ b/node_modules/@phntms/use-local-state/CODE_OF_CONDUCT.md @@ -0,0 +1,76 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, sex characteristics, gender identity and expression, +level of experience, education, socio-economic status, nationality, personal +appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +- The use of sexualized language or imagery and unwelcome sexual attention or + advances +- Trolling, insulting/derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or electronic + address, without explicit permission +- Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at developers@phntms.com. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see +https://www.contributor-covenant.org/faq diff --git a/node_modules/@phntms/use-local-state/CONTRIBUTING.md b/node_modules/@phntms/use-local-state/CONTRIBUTING.md new file mode 100644 index 0000000..fb742b7 --- /dev/null +++ b/node_modules/@phntms/use-local-state/CONTRIBUTING.md @@ -0,0 +1,48 @@ +# How to contribute + +I'm really happy that you're interested in helping out with this little project. + +As this is very early days for the project there's not a lot in the way of +resources, but please check out the [documentation](./README.md), and also the +[list of issues](https://github.com/phantomstudios/use-local-state/issues). + +Please submit an issue if you need help with anything. + +We have a [code of conduct](./CODE_OF_CONDUCT.md) so please make sure you follow +it. + +## Testing + +Testing is performed using `jest` and is tested on Windows, Linux and MacOS with +Node 10 and 12 runtimes. For a PR to be considered all these environments need to +pass. Testing is automated using GitHub Actions CI. + +## Submitting changes + +Please send a +[GitHub Pull Request to use-local-state](https://github.com/phantomstudios/use-local-state/pull/new/master) +with a clear list of what you've done (read more about +[pull requests](https://help.github.com/en/articles/about-pull-requests)). When you send a pull +request, please make sure you've covered off all the points in the template. + +Make sure you've read about our workflow (below); in essence make sure each Pull +Request is atomic but don't worry too much about the commits themselves as we use +squash-and-merge. + +## Our workflow + +We use [GitHub flow](https://guides.github.com/introduction/flow/); it's a lot +like git-flow but simpler and more forgiving. We use the `squash and merge` +strategy to merge Pull Requests. + +In effect this means: + +- Don't worry about individual commits. They will be preserved, but not on the + main `master` branch history, so feel free to commit early and often, using + git as a save mechanism. +- Your Pull Request title and description become very important; they are the + history of the master branch and explain all the changes. +- You ought to be able to find any previous version easily using GitHub tabs, or + [Releases](https://github.com/phantomstudios/use-local-state/releases) + +Thanks, John Chipps-Harding diff --git a/node_modules/@phntms/use-local-state/LICENSE b/node_modules/@phntms/use-local-state/LICENSE new file mode 100755 index 0000000..ee31b07 --- /dev/null +++ b/node_modules/@phntms/use-local-state/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Phantom Studios Ltd + +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/node_modules/@phntms/use-local-state/README.md b/node_modules/@phntms/use-local-state/README.md new file mode 100755 index 0000000..0b015bf --- /dev/null +++ b/node_modules/@phntms/use-local-state/README.md @@ -0,0 +1,103 @@ +# use-local-state + +[![NPM version][npm-image]][npm-url] +[![Actions Status][ci-image]][ci-url] +[![PR Welcome][npm-downloads-image]][npm-downloads-url] + +A simple React hook that allows you to store data within `LocalStorage` with the same interface as the standard `React.useState` hook. + +## Introduction + +Just swap out your `useState` calls with `useLocalState` to persist the data between refreshes. This hook is also SSR safe and does not break when used without `window` existing. + +```javascript +import useLocalState from "@phntms/use-local-state"; + +const [value, setValue, resetValue] = useLocalState("KEY", ""); +``` + +## Installation + +Install this package with `npm`. + +```bash +npm i @phntms/use-local-state +``` + +## Usage + +Store a boolean to track if a user has accepted terms of use. + +```JSX +import React from 'react'; +import useLocalState from '@phntms/use-local-state'; + +const TermsExample = () = { + const [accepted, setAccepted] = useLocalState("TERMS_ACCEPTED", false); + + return ( + <> + <h1>Welcome</h2> + {!accepted && <> + <p>Do you accept our terms?</p> + <button onClick={()=>setAccepted(true)}>Accept</button> + </>} + </> + ); +} +``` + +Store a list of bookmarks. + +```JSX +import React from 'react'; +import useLocalState from '@phntms/use-local-state'; + +interface Bookmark { + title: string; + url: string; +} + +const BookmarkExample = () = { + const [bookmarks, setBookmarks, clearBookmarks] = useLocalState<Bookmark[]>("BOOKMARKS", []); + + const addBookmark = (bookmark: Bookmark) => setBookmarks([...bookmarks, bookmark]); + + return ( + <> + <h1>Bookmarks</h2> + <NewBookmark add={addBookmark} /> + <button onClick={clearBookmarks}>Clear Bookmarks</button> + <ul> + {bookmarks.map(((bookmark, i) => ( + <li key={i}> + <a href={bookmark.url}>{bookmark.title}</a> + </li> + )))} + </ul> + </> + ); +} +``` + +## API + +### Input + +- `key` : Required - The key of type `string` to store within `LocalStorage`. +- `initialValue` : Required - The default/initial value of type `T` if the key is not found within the `LocalStorage` object. + +### Output + +An array containing the value, a function to set the value and another function to reset the value. + +- `[0]` : The value of the data stored in LocalStorage or the defaultValue if not set. +- `[1]` : A function to set the value stored in LocalStorage. Signature is exactly like the standard [React.useState](https://reactjs.org/docs/hooks-state.html) hook. +- `[2]` : A function to reset the data stored in the LocalStorage. + +[npm-image]: https://img.shields.io/npm/v/@phntms/use-local-state.svg?style=flat-square&logo=react +[npm-url]: https://npmjs.org/package/@phntms/use-local-state +[npm-downloads-image]: https://img.shields.io/npm/dm/@phntms/use-local-state.svg +[npm-downloads-url]: https://npmcharts.com/compare/@phntms/use-local-state?minimal=true +[ci-image]: https://github.com/phantomstudios/use-local-state/workflows/test/badge.svg +[ci-url]: https://github.com/phantomstudios/use-local-state/actions diff --git a/node_modules/@phntms/use-local-state/SUPPORT.md b/node_modules/@phntms/use-local-state/SUPPORT.md new file mode 100644 index 0000000..bb68ccb --- /dev/null +++ b/node_modules/@phntms/use-local-state/SUPPORT.md @@ -0,0 +1,4 @@ +# use-local-state Support + +For _questions_ on how to use `use-local-state` or what went wrong when you tried something, our primary resource is by opening a +[GitHub Issue](https://github.com/phantomstudios/use-local-state/issues), where you can get help from developers. diff --git a/node_modules/@phntms/use-local-state/jest.config.js b/node_modules/@phntms/use-local-state/jest.config.js new file mode 100644 index 0000000..5df035f --- /dev/null +++ b/node_modules/@phntms/use-local-state/jest.config.js @@ -0,0 +1,15 @@ +/** + * @type {jest.ProjectConfig} + */ +module.exports = { + roots: ["<rootDir>/test"], + transform: { + "^.+\\.tsx?$": "ts-jest", + }, + moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], + globals: { + "ts-jest": { + tsconfig: "tsconfig.test.json", + }, + }, +}; diff --git a/node_modules/@phntms/use-local-state/lib/index.d.ts b/node_modules/@phntms/use-local-state/lib/index.d.ts new file mode 100644 index 0000000..7c9c492 --- /dev/null +++ b/node_modules/@phntms/use-local-state/lib/index.d.ts @@ -0,0 +1,5 @@ +declare type Dispatch<A> = (value: A) => void; +declare type SetStateAction<S> = S | ((prevState: S) => S); +declare const useLocalState: <S>(key: string, defaultValue: S | (() => S)) => [S, Dispatch<SetStateAction<S>>, () => void]; +export default useLocalState; +//# sourceMappingURL=index.d.ts.map
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/lib/index.d.ts.map b/node_modules/@phntms/use-local-state/lib/index.d.ts.map new file mode 100644 index 0000000..b9df483 --- /dev/null +++ b/node_modules/@phntms/use-local-state/lib/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,aAAK,QAAQ,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;AACtC,aAAK,cAAc,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AAEnD,QAAA,MAAM,aAAa,WACZ,MAAM,yEAE6B,IAAI,CAuC7C,CAAC;AAEF,eAAe,aAAa,CAAC"}
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/lib/index.js b/node_modules/@phntms/use-local-state/lib/index.js new file mode 100644 index 0000000..12a0de2 --- /dev/null +++ b/node_modules/@phntms/use-local-state/lib/index.js @@ -0,0 +1,46 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var react_1 = require("react"); +var utils_1 = require("./utils"); +var useLocalState = function (key, defaultValue) { + var _a = (0, react_1.useState)(function () { + var isCallable = function (value) { + return typeof value === "function"; + }; + var toStore = isCallable(defaultValue) ? defaultValue() : defaultValue; + if (!utils_1.SUPPORTED) + return toStore; + var item = window.localStorage.getItem(key); + try { + return item ? JSON.parse(item) : toStore; + } + catch (error) { + return toStore; + } + }), value = _a[0], setValue = _a[1]; + var lastValue = (0, react_1.useRef)(value); + lastValue.current = value; + var setLocalStateValue = (0, react_1.useCallback)(function (newValue) { + var isCallable = function (value) { + return typeof value === "function"; + }; + var toStore = isCallable(newValue) + ? newValue(lastValue.current) + : newValue; + if (utils_1.SUPPORTED) + window.localStorage.setItem(key, JSON.stringify(toStore)); + setValue(toStore); + }, [key]); + var reset = (0, react_1.useCallback)(function () { + var isCallable = function (value) { + return typeof value === "function"; + }; + var toStore = isCallable(defaultValue) ? defaultValue() : defaultValue; + setValue(toStore); + if (utils_1.SUPPORTED) + window.localStorage.removeItem(key); + }, [defaultValue, key]); + return [value, setLocalStateValue, reset]; +}; +exports.default = useLocalState; +//# sourceMappingURL=index.js.map
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/lib/index.js.map b/node_modules/@phntms/use-local-state/lib/index.js.map new file mode 100644 index 0000000..1750353 --- /dev/null +++ b/node_modules/@phntms/use-local-state/lib/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,+BAAsD;AAEtD,iCAAoC;AAKpC,IAAM,aAAa,GAAG,UACpB,GAAW,EACX,YAA2B;IAErB,IAAA,KAAoB,IAAA,gBAAQ,EAAI;QACpC,IAAM,UAAU,GAAG,UAAC,KAAc;YAChC,OAAA,OAAO,KAAK,KAAK,UAAU;QAA3B,CAA2B,CAAC;QAC9B,IAAM,OAAO,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC;QACzE,IAAI,CAAC,iBAAS;YAAE,OAAO,OAAO,CAAC;QAC/B,IAAM,IAAI,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC9C,IAAI;YACF,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;SAC1C;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,OAAO,CAAC;SAChB;IACH,CAAC,CAAC,EAXK,KAAK,QAAA,EAAE,QAAQ,QAWpB,CAAC;IAEH,IAAM,SAAS,GAAG,IAAA,cAAM,EAAC,KAAK,CAAC,CAAC;IAChC,SAAS,CAAC,OAAO,GAAG,KAAK,CAAC;IAE1B,IAAM,kBAAkB,GAAG,IAAA,mBAAW,EACpC,UAAC,QAA2B;QAC1B,IAAM,UAAU,GAAG,UAAC,KAAc;YAChC,OAAA,OAAO,KAAK,KAAK,UAAU;QAA3B,CAA2B,CAAC;QAC9B,IAAM,OAAO,GAAG,UAAU,CAAC,QAAQ,CAAC;YAClC,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC;YAC7B,CAAC,CAAC,QAAQ,CAAC;QACb,IAAI,iBAAS;YAAE,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;QACzE,QAAQ,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC,EACD,CAAC,GAAG,CAAC,CACN,CAAC;IAEF,IAAM,KAAK,GAAG,IAAA,mBAAW,EAAC;QACxB,IAAM,UAAU,GAAG,UAAC,KAAc;YAChC,OAAA,OAAO,KAAK,KAAK,UAAU;QAA3B,CAA2B,CAAC;QAC9B,IAAM,OAAO,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC;QACzE,QAAQ,CAAC,OAAO,CAAC,CAAC;QAClB,IAAI,iBAAS;YAAE,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IACrD,CAAC,EAAE,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC,CAAC;IAExB,OAAO,CAAC,KAAK,EAAE,kBAAkB,EAAE,KAAK,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,kBAAe,aAAa,CAAC"}
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/lib/utils.d.ts b/node_modules/@phntms/use-local-state/lib/utils.d.ts new file mode 100644 index 0000000..74f40d9 --- /dev/null +++ b/node_modules/@phntms/use-local-state/lib/utils.d.ts @@ -0,0 +1,3 @@ +export declare const IS_BROWSER: boolean; +export declare const SUPPORTED: false | Storage; +//# sourceMappingURL=utils.d.ts.map
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/lib/utils.d.ts.map b/node_modules/@phntms/use-local-state/lib/utils.d.ts.map new file mode 100644 index 0000000..d9f31f3 --- /dev/null +++ b/node_modules/@phntms/use-local-state/lib/utils.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,SAAgC,CAAC;AAExD,eAAO,MAAM,SAAS,iBAAoC,CAAC"}
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/lib/utils.js b/node_modules/@phntms/use-local-state/lib/utils.js new file mode 100644 index 0000000..c1c445c --- /dev/null +++ b/node_modules/@phntms/use-local-state/lib/utils.js @@ -0,0 +1,6 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.SUPPORTED = exports.IS_BROWSER = void 0; +exports.IS_BROWSER = typeof window !== "undefined"; +exports.SUPPORTED = exports.IS_BROWSER && window.localStorage; +//# sourceMappingURL=utils.js.map
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/lib/utils.js.map b/node_modules/@phntms/use-local-state/lib/utils.js.map new file mode 100644 index 0000000..9213404 --- /dev/null +++ b/node_modules/@phntms/use-local-state/lib/utils.js.map @@ -0,0 +1 @@ +{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAG,OAAO,MAAM,KAAK,WAAW,CAAC;AAE3C,QAAA,SAAS,GAAG,kBAAU,IAAI,MAAM,CAAC,YAAY,CAAC"}
\ No newline at end of file diff --git a/node_modules/@phntms/use-local-state/package.json b/node_modules/@phntms/use-local-state/package.json new file mode 100755 index 0000000..910aad5 --- /dev/null +++ b/node_modules/@phntms/use-local-state/package.json @@ -0,0 +1,73 @@ +{ + "name": "@phntms/use-local-state", + "description": "A simple React hook that allows you to store data within LocalStorage with the same interface as the standard React.useState hook.", + "version": "0.1.4", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "homepage": "https://github.com/phantomstudios/use-local-state#readme", + "repository": { + "type": "git", + "url": "https://github.com/phantomstudios/use-local-state.git" + }, + "bugs": { + "url": "https://github.com/phantomstudios/use-local-state/issues" + }, + "keywords": [ + "react", + "hooks", + "hook", + "react-hook", + "localstorage", + "state" + ], + "scripts": { + "build": "tsc", + "build:types": "tsc --emitDeclarationOnly", + "prepublishOnly": "npm run build", + "test": "jest --verbose", + "test:watch": "jest --verbose --watch", + "coverage": "jest --coverage", + "lint": "NODE_ENV=test npm-run-all --parallel lint:*", + "lint:js": "eslint \"src/**/*.{js,jsx,ts,tsx}\"", + "lint:format": "prettier \"**/*.{md,html,yaml,yml}\" --check", + "lint:type-check": "tsc --noEmit", + "fix": "npm-run-all --sequential fix:*", + "fix:js": "eslint \"src/**/*.{js,jsx,ts,tsx}\" --fix", + "fix:format": "prettier \"**/*.{md,html,yaml,yml}\" --write", + "depcheck": "npx npm-check --update" + }, + "author": "John Chipps-Harding (john.chipps-harding@phntms.com)", + "license": "MIT", + "peerDependencies": { + "react": ">=16.8.0" + }, + "devDependencies": { + "@babel/preset-env": "^7.18.2", + "@babel/preset-typescript": "^7.17.12", + "@testing-library/react": "^13.3.0", + "@testing-library/react-hooks": "^8.0.1", + "@types/jest": "^28.1.3", + "@types/react": "^18.0.14", + "@typescript-eslint/eslint-plugin": "^5.29.0", + "@typescript-eslint/parser": "^5.29.0", + "babel-eslint": "^10.1.0", + "babel-jest": "^28.1.1", + "browser-env": "^3.2.6", + "eslint": "^8.18.0", + "eslint-config-prettier": "^8.5.0", + "eslint-plugin-import": "^2.26.0", + "eslint-plugin-prettier": "^4.0.0", + "eslint-plugin-react": "^7.30.0", + "eslint-plugin-react-hooks": "^4.6.0", + "jest": "^28.1.1", + "jest-environment-jsdom": "^28.1.1", + "npm-run-all": "^4.1.5", + "prettier": "^2.7.1", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-test-renderer": "^18.2.0", + "ts-jest": "^28.0.5", + "typescript": "^4.7.4" + }, + "dependencies": {} +} diff --git a/node_modules/@phntms/use-local-state/src/index.ts b/node_modules/@phntms/use-local-state/src/index.ts new file mode 100755 index 0000000..face65b --- /dev/null +++ b/node_modules/@phntms/use-local-state/src/index.ts @@ -0,0 +1,52 @@ +import { useState, useCallback, useRef } from "react"; + +import { SUPPORTED } from "./utils"; + +type Dispatch<A> = (value: A) => void; +type SetStateAction<S> = S | ((prevState: S) => S); + +const useLocalState = <S>( + key: string, + defaultValue: S | (() => S) +): [S, Dispatch<SetStateAction<S>>, () => void] => { + const [value, setValue] = useState<S>(() => { + const isCallable = (value: unknown): value is () => S => + typeof value === "function"; + const toStore = isCallable(defaultValue) ? defaultValue() : defaultValue; + if (!SUPPORTED) return toStore; + const item = window.localStorage.getItem(key); + try { + return item ? JSON.parse(item) : toStore; + } catch (error) { + return toStore; + } + }); + + const lastValue = useRef(value); + lastValue.current = value; + + const setLocalStateValue = useCallback( + (newValue: SetStateAction<S>) => { + const isCallable = (value: unknown): value is (prevState: S) => S => + typeof value === "function"; + const toStore = isCallable(newValue) + ? newValue(lastValue.current) + : newValue; + if (SUPPORTED) window.localStorage.setItem(key, JSON.stringify(toStore)); + setValue(toStore); + }, + [key] + ); + + const reset = useCallback(() => { + const isCallable = (value: unknown): value is (prevState: S) => S => + typeof value === "function"; + const toStore = isCallable(defaultValue) ? defaultValue() : defaultValue; + setValue(toStore); + if (SUPPORTED) window.localStorage.removeItem(key); + }, [defaultValue, key]); + + return [value, setLocalStateValue, reset]; +}; + +export default useLocalState; diff --git a/node_modules/@phntms/use-local-state/src/utils.ts b/node_modules/@phntms/use-local-state/src/utils.ts new file mode 100644 index 0000000..67d8ce6 --- /dev/null +++ b/node_modules/@phntms/use-local-state/src/utils.ts @@ -0,0 +1,3 @@ +export const IS_BROWSER = typeof window !== "undefined"; + +export const SUPPORTED = IS_BROWSER && window.localStorage; diff --git a/node_modules/@phntms/use-local-state/test/index.test.tsx b/node_modules/@phntms/use-local-state/test/index.test.tsx new file mode 100755 index 0000000..6c01e95 --- /dev/null +++ b/node_modules/@phntms/use-local-state/test/index.test.tsx @@ -0,0 +1,363 @@ +/** + * @jest-environment jsdom + */ + +import { renderHook, act } from "@testing-library/react-hooks"; + +import useLocalState from "../src"; + +beforeEach(() => { + localStorage.clear(); +}); + +describe("useLocalState()", () => { + it("stores initial value as string", async () => { + const key = "key"; + const value = "something"; + const { result } = renderHook(() => useLocalState(key, value)); + + const [values] = result.current; + expect(values).toEqual(value); + }); + + it(`initial value isn't stored into localStorage`, async () => { + const key = "key"; + const value = "something"; + renderHook(() => useLocalState(key, value)); + + expect(localStorage.getItem(key)).toEqual(null); + }); + + it("stores initial value as boolean", async () => { + const key = "key"; + const value = false; + const { result } = renderHook(() => useLocalState(key, value)); + + const [values] = result.current; + expect(values).toEqual(value); + }); + + it("stores initial value as object", async () => { + const key = "key"; + const value = { + something: "else", + }; + const { result } = renderHook(() => useLocalState(key, value)); + + const [values] = result.current; + expect(values).toEqual(value); + }); + + it("stores initial value as array", async () => { + const key = "todos"; + const values = ["first", "second"]; + const { result } = renderHook(() => useLocalState(key, values)); + + const [todos] = result.current; + expect(todos).toEqual(values); + }); + + it("accepts callback as an initial value", async () => { + const key = "todos"; + const values = ["first", "second"]; + const callback = () => values; + const { result } = renderHook(() => useLocalState(key, callback)); + + const [todos] = result.current; + expect(todos).toEqual(values); + }); + + it("accepts callback as a initial value and can be updated", async () => { + const key = "todos"; + const values = ["first", "second"]; + const callback = () => values; + const { result } = renderHook(() => useLocalState(key, callback)); + + const [initialValues] = result.current; + expect(initialValues).toEqual(values); + + const newValue = ["third", "fourth"]; + act(() => { + const setValue = result.current[1]; + setValue(newValue); + }); + + const [changedValues] = result.current; + expect(changedValues).toEqual(newValue); + }); + + it("can update value as string", async () => { + const key = "key"; + const value = "something"; + const { result } = renderHook(() => useLocalState(key, value)); + + const [initialValue] = result.current; + expect(initialValue).toEqual(value); + + const newValue = "something else"; + act(() => { + const setValue = result.current[1]; + + setValue(newValue); + }); + + const [values] = result.current; + expect(values).toEqual(newValue); + }); + + it("can update value as function", async () => { + const key = "key"; + const value = "something"; + const { result } = renderHook(() => useLocalState(key, value)); + + const [initialValue] = result.current; + expect(initialValue).toEqual(value); + + const newValue = " else"; + act(() => { + const setValue = result.current[1]; + setValue((v) => v + newValue); + }); + + const [values] = result.current; + expect(values).toEqual("something else"); + }); + + it("can update value as function multiple times", async () => { + const key = "key"; + const value = "something"; + const { result } = renderHook(() => useLocalState(key, value)); + + const [initialValue] = result.current; + expect(initialValue).toEqual(value); + + const newValue = " else"; + act(() => { + const setValue = result.current[1]; + setValue((v) => v + newValue); + }); + + const newValue2 = " again"; + act(() => { + const setValue = result.current[1]; + setValue((v) => v + newValue2); + }); + + const [values] = result.current; + expect(values).toEqual("something else again"); + }); + + it("can update value as object", async () => { + const key = "key"; + const value = { something: "something" }; + const { result } = renderHook(() => useLocalState(key, value)); + + const [initialValue] = result.current; + expect(initialValue).toEqual(value); + + const newValue = { something: "else" }; + act(() => { + const setValue = result.current[1]; + + setValue(newValue); + }); + + const [values] = result.current; + expect(values).toEqual(newValue); + }); + + it("can update value as array", async () => { + const key = "todos"; + const values = ["first", "second"]; + const { result } = renderHook(() => useLocalState(key, values)); + + const newValues = ["third", "fourth"]; + act(() => { + const setValues = result.current[1]; + + setValues(newValues); + }); + + const [todos] = result.current; + expect(todos).toEqual(newValues); + }); + + it("updates state with callback function", async () => { + const key = "todos"; + const values = ["first", "second"]; + const { result } = renderHook(() => useLocalState(key, values)); + + const newValues = ["first", "second"]; + act(() => { + const setTodos = result.current[1]; + + setTodos((current) => [...current, ...newValues]); + }); + + const [todos] = result.current; + expect(todos).toEqual([...values, ...newValues]); + }); + + it("does not fail even if invalid data is stored into localStorage", async () => { + const key = "todos"; + const value = "something"; + + localStorage.setItem(key, value); + + const newValues = ["first", "second"]; + const { result } = renderHook(() => useLocalState(key, newValues)); + + const [todos] = result.current; + expect(todos).toEqual(newValues); + }); + + it("gets initial value from localStorage if there is a value", async () => { + const key = "todos"; + const values = ["first", "second"]; + + localStorage.setItem(key, JSON.stringify(values)); + + const newValues = ["third", "fourth"]; + const { result } = renderHook(() => useLocalState(key, newValues)); + + const [todos] = result.current; + expect(todos).toEqual(values); + }); + + it("does not throw an error with two states with different keys", async () => { + const keyA = "todos"; + const keyB = "todos"; + const valuesA = ["first", "second"]; + const valuesB = ["third", "fourth"]; + + expect(() => { + renderHook(() => { + useLocalState(keyA, valuesA); + useLocalState(keyB, valuesB); + }); + }).not.toThrow(); + }); + + it("can handle `undefined` values", async () => { + const key = "todos"; + const values = ["first", "second"]; + const { result } = renderHook(() => + useLocalState<string[] | undefined>(key, values) + ); + + act(() => { + const [, setValues] = result.current; + setValues(undefined); + }); + + const [value] = result.current; + expect(value).toBe(undefined); + }); + + it("can handle `null` values", async () => { + const key = "todos"; + const values = ["first", "second"]; + const { result } = renderHook(() => + useLocalState<string[] | null>(key, values) + ); + + act(() => { + const [, setValues] = result.current; + setValues(null); + }); + + const [value] = result.current; + expect(value).toBe(null); + }); + + it("can reset to default value", async () => { + const key = "key"; + const value = "something"; + const { result } = renderHook(() => useLocalState(key, value)); + + const [initialValue] = result.current; + expect(initialValue).toEqual(value); + + const newValue = "something else"; + act(() => { + const setValue = result.current[1]; + setValue(newValue); + }); + + const [changedValue] = result.current; + expect(changedValue).toEqual(newValue); + + act(() => { + const resetValue = result.current[2]; + resetValue(); + }); + + const [resetValue] = result.current; + expect(resetValue).toEqual(value); + expect(localStorage.getItem(key)).toEqual(null); + }); + + it("can reset to default value as callback", async () => { + const key = "todos"; + const values = ["first", "second"]; + const callback = () => values; + const { result } = renderHook(() => useLocalState(key, callback)); + + const [initialValues] = result.current; + expect(initialValues).toEqual(values); + + const newValues = ["third", "fourth"]; + act(() => { + const setValues = result.current[1]; + setValues(newValues); + }); + + const [changedValues] = result.current; + expect(changedValues).toEqual(newValues); + + act(() => { + const resetValues = result.current[2]; + resetValues(); + }); + + const [resetValues] = result.current; + expect(resetValues).toEqual(values); + expect(localStorage.getItem(key)).toEqual(null); + }); + + it("does not change the setter when value is updated", async () => { + const key = "key"; + const value = "something"; + const { result } = renderHook(() => useLocalState(key, value)); + + const setterOne = result.current[1]; + + const newValue = "something else"; + act(() => { + const setValue = result.current[1]; + setValue(newValue); + }); + + const setterTwo = result.current[1]; + + expect(setterOne).toEqual(setterTwo); + }); + + it("does not change the setter when value is updated via function", async () => { + const key = "key"; + const value = "something"; + const { result } = renderHook(() => useLocalState(key, value)); + + const setterOne = result.current[1]; + + const newValue = " else"; + act(() => { + const setValue = result.current[1]; + setValue((v) => v + newValue); + }); + + const setterTwo = result.current[1]; + + expect(setterOne).toEqual(setterTwo); + }); +}); diff --git a/node_modules/@phntms/use-local-state/tsconfig.json b/node_modules/@phntms/use-local-state/tsconfig.json new file mode 100644 index 0000000..a956764 --- /dev/null +++ b/node_modules/@phntms/use-local-state/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "lib": ["dom", "es2015"], + "jsx": "react", + "declaration": true, + "declarationMap": true, + "sourceMap": true, + "outDir": "./lib", + "rootDir": "./src", + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + "noImplicitReturns": true, + "allowSyntheticDefaultImports": true, + "esModuleInterop": true + }, + "include": ["src/**/*.ts"], + "exclude": ["test/**/*"] +} diff --git a/node_modules/@phntms/use-local-state/tsconfig.test.json b/node_modules/@phntms/use-local-state/tsconfig.test.json new file mode 100644 index 0000000..76c3269 --- /dev/null +++ b/node_modules/@phntms/use-local-state/tsconfig.test.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "target": "es6" + } +} |