Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: added persistence to settings and ignores

Elijah Potter2d6e905b16e8
diff --git a/src/harper/HarperContext.tsx b/src/harper/HarperContext.tsx
deleted file mode 100644
index 574fd49da..000000000
--- a/src/harper/HarperContext.tsx
+++ /dev/null
@@ -1,140 +0,0 @@
-import { Lint, LintConfig, Linter, WorkerLinter } from 'harper.js';
-import React, {
- createContext,
- ReactNode,
- useContext,
- useEffect,
- useState,
-} from 'react';
-
-export type HarperConfig = {
- lintConfig: LintConfig;
- ignoredState?: string;
-};
-
-type HarperContextData = {
- linter: Linter;
- config: HarperConfig;
-};
-
-function createDefaultContextData(): HarperContextData {
- return {
- linter: new WorkerLinter(),
- config: {
- lintConfig: {},
- },
- };
-}
-
-const harperContext = createContext<
- [HarperContextData, React.Dispatch<HarperContextData>]
->([createDefaultContextData(), () => {}]);
-
-export function HarperProvider({
- children,
-}: {
- children: ReactNode | ReactNode[];
-}) {
- const dataReadWrite = useState<HarperContextData>(() =>
- createDefaultContextData()
- );
-
- return (
- <harperContext.Provider value={dataReadWrite}>
- {children}
- </harperContext.Provider>
- );
-}
-
-export function useLinter(): Linter {
- const [data, setData] = useContext(harperContext);
- const [config] = useLinterConfig();
- const [ignoredLintState] = useIgnoredLintState();
-
- useEffect(() => {
- data.linter.setLintConfig(config);
- }, [config, data, setData]);
-
- useEffect(() => {
- (async () => {
- await data.linter.clearIgnoredLints();
- if (ignoredLintState !== undefined) {
- await data.linter.importIgnoredLints(ignoredLintState);
- }
- })();
- }, [ignoredLintState, data, setData]);
-
- return data.linter;
-}
-
-export function useLintDescriptions(): Record<string, string> {
- const [data] = useContext(harperContext);
- const [descriptions, setDescriptions] = useState({});
-
- useEffect(() => {
- data.linter.getLintDescriptions().then(setDescriptions);
- }, [data]);
-
- return descriptions;
-}
-
-export function useDefaultLintConfig(): LintConfig {
- const [data] = useContext(harperContext);
- const [defaultConfig, setDefaultConfig] = useState({});
-
- useEffect(() => {
- data.linter.getDefaultLintConfig().then(setDefaultConfig);
- }, [data]);
-
- return defaultConfig;
-}
-
-function useConfigKey<T>(key: string): [T, (newV: T) => void] {
- const [data, setData] = useContext(harperContext);
-
- return [
- data.config[key],
- (v) => setData({ ...data, config: { ...data.config, [key]: v } }),
- ];
-}
-
-/**
- * Get the global linter's configuration, in addition to a callback to modify it.
- * This is the preferred method of modifying the lint configuration in this project, since this will trigger re-renders of
- * components that depend on the global linter.
- */
-export function useLinterConfig(): [
- LintConfig,
- (newConfig: LintConfig) => void,
-] {
- const [val, setVal] = useConfigKey('lintConfig');
- const defaultConfig = useDefaultLintConfig();
-
- useEffect(() => {
- if (Object.entries(val).length === 0) {
- setVal(defaultConfig);
- }
- }, [val, setVal, defaultConfig]);
-
- return [val as LintConfig, setVal];
-}
-
-export function useIgnoredLintState(): [
- string | undefined,
- (newState: string) => void,
-] {
- const [val, setVal] = useConfigKey('ignoredState');
-
- return [val as string, setVal];
-}
-
-/** Grab a callback that can be used to ignore a lint. */
-export function useIgnoreLint(): (lint: Lint) => Promise<void> {
- const linter = useLinter();
- const [_, setIgnoredLintState] = useIgnoredLintState();
-
- return async (lint) => {
- await linter.ignoreLint(lint);
- setIgnoredLintState(await linter.exportIgnoredLints());
- };
-}
diff --git a/src/harper/LintSettingList.tsx b/src/harper/LintSettingList.tsx
index eb2a8af79..442586a43 100644
--- a/src/harper/LintSettingList.tsx
+++ b/src/harper/LintSettingList.tsx
@@ -1,13 +1,10 @@
import React, { useEffect } from 'react';
-import {
- useDefaultLintConfig,
- useLintDescriptions,
- useLinterConfig,
-} from './HarperContext';
import LintSettingRow from './LintSettingRow';
+import useLintConfig, { useDefaultLintConfig } from './useLintConfig';
+import { useLintDescriptions } from './LinterProvider';
export default function LintSettingList() {
- const [lintConfig, setLintConfig] = useLinterConfig();
+ const [lintConfig, setLintConfig] = useLintConfig();
const defaultConfig = useDefaultLintConfig();
const descriptions = useLintDescriptions();
diff --git a/src/harper/LintSettingRow.tsx b/src/harper/LintSettingRow.tsx
index 879ab8673..f9f524cf3 100644
--- a/src/harper/LintSettingRow.tsx
+++ b/src/harper/LintSettingRow.tsx
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';
-import { useLinter } from './HarperContext';
import { Animate, CheckboxControl } from '@wordpress/components';
+import { useLinter } from './LinterProvider';
export default function LintSettingRow({
name,
diff --git a/src/harper/LinterProvider.tsx b/src/harper/LinterProvider.tsx
new file mode 100644
index 000000000..51b67b9ae
--- /dev/null
+++ b/src/harper/LinterProvider.tsx
@@ -0,0 +1,40 @@
+import { Linter, WorkerLinter } from 'harper.js';
+import React, {
+ createContext,
+ ReactNode,
+ useContext,
+ useEffect,
+ useRef,
+ useState,
+} from 'react';
+
+const linterContext = createContext<Linter>(new WorkerLinter());
+
+export default function LinterProvider({
+ children,
+}: {
+ children: ReactNode | ReactNode[];
+}) {
+ const linter = useRef(new WorkerLinter());
+
+ return (
+ <linterContext.Provider value={linter.current}>
+ {children}
+ </linterContext.Provider>
+ );
+}
+
+export function useLinter(): Linter {
+ return useContext(linterContext);
+}
+
+export function useLintDescriptions(): Record<string, string> {
+ const linter = useLinter();
+ const [descriptions, setDescriptions] = useState({});
+
+ useEffect(() => {
+ linter.getLintDescriptions().then(setDescriptions);
+ }, [linter]);
+
+ return descriptions;
+}
diff --git a/src/harper/SidebarControl.tsx b/src/harper/SidebarControl.tsx
index 6f21ae127..bae400d42 100644
--- a/src/harper/SidebarControl.tsx
+++ b/src/harper/SidebarControl.tsx
@@ -1,4 +1,3 @@
-import { uniqBy } from 'lodash-es';
import DataBlock from './DataBlock';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { createPortal } from 'react-dom';
diff --git a/src/harper/index.tsx b/src/harper/index.tsx
index cfbe24963..7a97b4357 100644
--- a/src/harper/index.tsx
+++ b/src/harper/index.tsx
@@ -4,7 +4,7 @@ import SidebarControl from './SidebarControl';
import Logo from './Logo';
import './index.css';
import React from 'react';
-import { HarperProvider } from './HarperContext';
+import LinterProvider from './LinterProvider';
function Sidebar() {
return (
@@ -13,9 +13,9 @@ function Sidebar() {
Harper
</PluginSidebarMoreMenuItem>
<PluginSidebar name="harper-sidebar" title="Harper" icon={Logo}>
- <HarperProvider>
+ <LinterProvider>
<SidebarControl />
- </HarperProvider>
+ </LinterProvider>
</PluginSidebar>
</>
);
diff --git a/src/harper/useIgnoredLintState.ts b/src/harper/useIgnoredLintState.ts
new file mode 100644
index 000000000..825c5f503
--- /dev/null
+++ b/src/harper/useIgnoredLintState.ts
@@ -0,0 +1,39 @@
+import { Lint } from 'harper.js';
+import { useDispatch, useSelect } from '@wordpress/data';
+import { useLinter } from './LinterProvider';
+
+const KEY = 'ignoredLints';
+
+export default function useIgnoredLintState(): [
+ string | undefined,
+ (newState: string) => void,
+] {
+ const ignoredLintState = useSelect(
+ (select) => select('core/preferences').get('harper-wp', KEY),
+ []
+ );
+
+ const { set } = useDispatch('core/preferences');
+
+ return [
+ ignoredLintState,
+ (newValue: string) => set('harper-wp', KEY, newValue),
+ ];
+}
+
+/** Get a callback that adds a lint to the global ignored lint state. */
+export function useIgnoreLint(): (lint: Lint) => Promise<void> {
+ const linter = useLinter();
+ const [ignoredLintState, setIgnoredLintState] = useIgnoredLintState();
+
+ return async (lint) => {
+ await linter.clearIgnoredLints();
+
+ if (ignoredLintState) {
+ await linter.importIgnoredLints(ignoredLintState);
+ }
+
+ await linter.ignoreLint(lint);
+ setIgnoredLintState(await linter.exportIgnoredLints());
+ };
+}
diff --git a/src/harper/useLintBoxes.ts b/src/harper/useLintBoxes.ts
index cc5d4588e..c2581930d 100644
--- a/src/harper/useLintBoxes.ts
+++ b/src/harper/useLintBoxes.ts
@@ -2,12 +2,9 @@ import { useCallback, useEffect, useState } from 'react';
import { IgnorableLintBox, LintBox } from './Box';
import RichText from './RichText';
import { Lint } from 'harper.js';
-import {
- useIgnoredLintState,
- useIgnoreLint,
- useLinter,
- useLinterConfig,
-} from './HarperContext';
+import { useLinter } from './LinterProvider';
+import useLintConfig from './useLintConfig';
+import useIgnoredLintState, { useIgnoreLint } from './useIgnoredLintState';
/**
* Lint given elements and return the resulting error targets.
@@ -18,7 +15,7 @@ export default function useLintBoxes(
richTexts: RichText[]
): [IgnorableLintBox[][], boolean] {
const linter = useLinter();
- const [config] = useLinterConfig();
+ const [config] = useLintConfig();
const [ignoreState] = useIgnoredLintState();
const ignoreLint = useIgnoreLint();
@@ -32,9 +29,14 @@ export default function useLintBoxes(
const newLints = await Promise.all(
richTexts.map(async (richText) => {
const contents = richText.getTextContent();
+
+ await linter.clearIgnoredLints();
+
if (ignoreState) {
await linter.importIgnoredLints(ignoreState);
}
+
+ await linter.setLintConfig(config);
return await linter.lint(contents);
})
);
diff --git a/src/harper/useLintConfig.ts b/src/harper/useLintConfig.ts
new file mode 100644
index 000000000..a167fa126
--- /dev/null
+++ b/src/harper/useLintConfig.ts
@@ -0,0 +1,50 @@
+import { useDispatch, useSelect } from '@wordpress/data';
+import { LintConfig } from 'harper.js';
+import { useLinter } from './LinterProvider';
+import useToggle from './useToggle';
+import { useCallback, useEffect, useState } from 'react';
+
+const KEY = 'lintConfig';
+
+export default function useLintConfig(): [
+ LintConfig,
+ (newState: LintConfig) => void,
+] {
+ const defaultConfig = useDefaultLintConfig();
+ let lintConfig = useSelect(
+ (select) => select('core/preferences').get('harper-wp', KEY),
+ []
+ );
+
+ const { set } = useDispatch('core/preferences');
+
+ const setConfig = useCallback(
+ (newValue) => {
+ set('harper-wp', KEY, newValue);
+ },
+ [set]
+ );
+
+ useEffect(() => {
+ if (lintConfig == null) {
+ setConfig(defaultConfig);
+ }
+ }, [defaultConfig, setConfig]);
+
+ if (lintConfig == null) {
+ lintConfig = {};
+ }
+
+ return [lintConfig, setConfig];
+}
+
+export function useDefaultLintConfig(): LintConfig {
+ const linter = useLinter();
+ const [defaultConfig, setDefaultConfig] = useState({});
+
+ useEffect(() => {
+ linter.getDefaultLintConfig().then(setDefaultConfig);
+ }, [linter]);
+
+ return defaultConfig;
+}
diff --git a/src/harper/useToggle.ts b/src/harper/useToggle.ts
new file mode 100644
index 000000000..c67c9a078
--- /dev/null
+++ b/src/harper/useToggle.ts
@@ -0,0 +1,7 @@
+import { useState } from 'react';
+
+export default function useToggle(): [boolean, () => void] {
+ const [value, setValue] = useState(false);
+
+ return [value, () => setValue(!value)];
+}