mirror of
https://github.com/XFox111/PasswordGeneratorExtension.git
synced 2026-04-22 08:08:01 +03:00
Major 4.0 (#380)
- Migrated to WXT - Migrated to NPM - Added Insert & copy action - Added ESLint
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
export default class ExtensionOptions
|
||||
{
|
||||
public MinLength: number = 4;
|
||||
public MaxLength: number = 32;
|
||||
public ContextMenu: boolean = true;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
export default class GeneratorOptions
|
||||
{
|
||||
public Length: number = 8;
|
||||
|
||||
public Special: boolean = true;
|
||||
public Numeric: boolean = true;
|
||||
public Lowercase: boolean = true;
|
||||
public Uppercase: boolean = true;
|
||||
|
||||
public ExcludeSimilar: boolean = true;
|
||||
public ExcludeAmbiguous: boolean = true;
|
||||
|
||||
public ExcludeRepeating: boolean = false;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import React, { createContext, useContext, useEffect, useState } from "react";
|
||||
import ExtensionOptions from "./ExtensionOptions";
|
||||
import GeneratorOptions from "./GeneratorOptions";
|
||||
|
||||
const defaultOptions: IStorage =
|
||||
{
|
||||
generatorOptions: new GeneratorOptions(),
|
||||
extOptions: new ExtensionOptions(),
|
||||
updateStorage: async () => { }
|
||||
};
|
||||
|
||||
const Storage = createContext<IStorage>(defaultOptions);
|
||||
|
||||
export const useStorage = () => useContext<IStorage>(Storage);
|
||||
|
||||
export const StorageProvider: React.FC<IStorageProviderProps> = (props) =>
|
||||
{
|
||||
const [storage, setStorage] = useState<IStorage | null>(null);
|
||||
|
||||
const getStorage = async () =>
|
||||
setStorage({
|
||||
extOptions: await browser.storage.sync.get(defaultOptions.extOptions) as ExtensionOptions,
|
||||
generatorOptions: await browser.storage.sync.get(defaultOptions.generatorOptions) as GeneratorOptions,
|
||||
updateStorage: async (options) => await browser.storage.sync.set(options)
|
||||
});
|
||||
|
||||
useEffect(() =>
|
||||
{
|
||||
getStorage();
|
||||
browser.storage.sync.onChanged.addListener(getStorage);
|
||||
return () => browser.storage.sync.onChanged.removeListener(getStorage);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Storage.Provider value={ storage ?? defaultOptions }>
|
||||
{ storage ? props.children : props.loader }
|
||||
</Storage.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
// #region Types
|
||||
interface IStorage
|
||||
{
|
||||
generatorOptions: GeneratorOptions;
|
||||
extOptions: ExtensionOptions;
|
||||
updateStorage: (options: Partial<GeneratorOptions | ExtensionOptions>) => Promise<void>;
|
||||
}
|
||||
|
||||
interface IStorageProviderProps extends React.PropsWithChildren
|
||||
{
|
||||
loader?: JSX.Element;
|
||||
}
|
||||
// #endregion
|
||||
@@ -0,0 +1,5 @@
|
||||
import ExtensionOptions from "./ExtensionOptions";
|
||||
import GeneratorOptions from "./GeneratorOptions";
|
||||
|
||||
export { useStorage, StorageProvider } from "./Storage";
|
||||
export { ExtensionOptions, GeneratorOptions };
|
||||
Reference in New Issue
Block a user