Add eslint + prettier

This commit is contained in:
Tristan Edwards 2022-12-20 08:04:09 +00:00
parent 864ab6a8c7
commit ae1c36e6dc
6 changed files with 89 additions and 59 deletions

26
.eslintrc.json Normal file
View File

@ -0,0 +1,26 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint",
"prettier"
],
"rules": {
"prettier/prettier": "warn"
}
}

6
.prettierrc.json Normal file
View File

@ -0,0 +1,6 @@
{
"semi": false,
"tabWidth": 2,
"trailingComma": "all",
"arrowParens": "always"
}

View File

@ -24,12 +24,6 @@
"pre-commit": "dts lint"
}
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"jest": {
"testEnvironment": "jsdom"
},
@ -55,7 +49,13 @@
"@tsconfig/recommended": "^1.0.1",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@typescript-eslint/eslint-plugin": "^5.47.0",
"@typescript-eslint/parser": "^5.47.0",
"dts-cli": "^1.6.0",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.31.11",
"husky": "^8.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",

View File

@ -1,13 +1,13 @@
import {
import React, {
createContext,
ReactNode,
useContext,
useEffect,
useState,
useRef,
} from 'react';
} from "react"
import { uniqBy } from './utils';
import { uniqBy } from "./utils"
import {
OnConnectFunc,
@ -18,100 +18,98 @@ import {
NostrEvent,
SendMsgType,
SendEvent,
} from '@nostrgg/client';
} from "@nostrgg/client"
export * from '@nostrgg/client';
export * from "@nostrgg/client"
interface NostrContextType {
isLoading: boolean;
onConnect: (_onConnectCallback?: OnConnectFunc) => void;
onEvent: (_onEventCallback?: OnEventFunc) => void;
sendEvent?: (event: SendEvent) => void;
isLoading: boolean
onConnect: (_onConnectCallback?: OnConnectFunc) => void
onEvent: (_onEventCallback?: OnEventFunc) => void
sendEvent?: (event: SendEvent) => void
}
const NostrContext = createContext<NostrContextType>({
isLoading: true,
onConnect: () => null,
onEvent: () => null,
});
})
export function NostrProvider({
children,
relayUrls,
debug,
}: {
children: ReactNode;
relayUrls: string[];
debug?: boolean;
children: ReactNode
relayUrls: string[]
debug?: boolean
}) {
const [isLoading, setIsLoading] = useState(true);
const [isLoading, setIsLoading] = useState(true)
let onConnectCallback: null | OnConnectFunc = null;
let onEventCallback: null | OnEventFunc = null;
let onConnectCallback: null | OnConnectFunc = null
let onEventCallback: null | OnEventFunc = null
const sendEventRef = useRef<SendEventFunc>();
const sendEventRef = useRef<SendEventFunc>()
useEffect(() => {
const { sendEvent: _sendEvent } = initNostr({
relayUrls,
onConnect: (url: string, sendEvent) => {
setIsLoading(false);
setIsLoading(false)
if (onConnectCallback) {
onConnectCallback(url, sendEvent);
onConnectCallback(url, sendEvent)
}
},
onEvent: (relayUrl, event) => {
if (onEventCallback) {
onEventCallback(relayUrl, event);
onEventCallback(relayUrl, event)
}
},
debug,
});
})
sendEventRef.current = _sendEvent;
}, [onConnectCallback, onEventCallback, relayUrls]);
sendEventRef.current = _sendEvent
}, [onConnectCallback, onEventCallback, relayUrls])
const value: NostrContextType = {
isLoading,
sendEvent: sendEventRef.current,
onConnect: (_onConnectCallback?: OnConnectFunc) => {
if (_onConnectCallback) {
onConnectCallback = _onConnectCallback;
onConnectCallback = _onConnectCallback
}
},
onEvent: (_onEventCallback?: OnEventFunc) => {
if (_onEventCallback) {
onEventCallback = _onEventCallback;
onEventCallback = _onEventCallback
}
},
};
}
return (
<NostrContext.Provider value={value}>{children}</NostrContext.Provider>
);
return <NostrContext.Provider value={value}>{children}</NostrContext.Provider>
}
export function useNostr() {
return useContext(NostrContext);
return useContext(NostrContext)
}
export function useNostrEvents({ filter }: { filter: Filter }) {
const { isLoading, sendEvent, onConnect, onEvent } = useNostr();
const [events, setEvents] = useState<NostrEvent[]>([]);
const { isLoading, sendEvent, onConnect, onEvent } = useNostr()
const [events, setEvents] = useState<NostrEvent[]>([])
onConnect((url, _sendEvent) => {
_sendEvent([SendMsgType.REQ, filter], url);
});
_sendEvent([SendMsgType.REQ, filter], url)
})
onEvent((_relayUrl, event) => {
setEvents(_events => {
return [event, ..._events];
});
});
setEvents((_events) => {
return [event, ..._events]
})
})
const uniqEvents = events.length > 0 ? uniqBy(events, 'id') : [];
const sortedEvents = uniqEvents.sort((a, b) => b.created_at - a.created_at);
const uniqEvents = events.length > 0 ? uniqBy(events, "id") : []
const sortedEvents = uniqEvents.sort((a, b) => b.created_at - a.created_at)
return {
isLoading,
@ -119,5 +117,5 @@ export function useNostrEvents({ filter }: { filter: Filter }) {
onConnect,
onEvent,
sendEvent,
};
}
}

View File

@ -5,7 +5,7 @@ export const uniqBy = <T>(arr: T[], key: keyof T): T[] => {
...map,
[`${item[key]}`]: item,
}),
{}
)
);
};
{},
),
)
}

View File

@ -1566,9 +1566,9 @@
dependencies:
"@types/yargs-parser" "*"
"@typescript-eslint/eslint-plugin@^5.30.0":
"@typescript-eslint/eslint-plugin@^5.30.0", "@typescript-eslint/eslint-plugin@^5.47.0":
version "5.47.0"
resolved "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.47.0.tgz"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.47.0.tgz#dadb79df3b0499699b155839fd6792f16897d910"
integrity sha512-AHZtlXAMGkDmyLuLZsRpH3p4G/1iARIwc/T0vIem2YB+xW6pZaXYXzCBnZSF/5fdM97R9QqZWZ+h3iW10XgevQ==
dependencies:
"@typescript-eslint/scope-manager" "5.47.0"
@ -1581,9 +1581,9 @@
semver "^7.3.7"
tsutils "^3.21.0"
"@typescript-eslint/parser@^5.30.0":
"@typescript-eslint/parser@^5.30.0", "@typescript-eslint/parser@^5.47.0":
version "5.47.0"
resolved "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.47.0.tgz"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.47.0.tgz#62e83de93499bf4b500528f74bf2e0554e3a6c8d"
integrity sha512-udPU4ckK+R1JWCGdQC4Qa27NtBg7w020ffHqGyAK8pAgOVuNw7YaKXGChk+udh+iiGIJf6/E/0xhVXyPAbsczw==
dependencies:
"@typescript-eslint/scope-manager" "5.47.0"
@ -2785,7 +2785,7 @@ escodegen@^2.0.0:
eslint-config-prettier@^8.5.0:
version "8.5.0"
resolved "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.5.0.tgz"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.5.0.tgz#5a81680ec934beca02c7b1a61cf8ca34b66feab1"
integrity sha512-obmWKLUNCnhtQRKc+tmnYuQl0pFU1ibYJQ5BGhTVB08bHe9wC8qUeG7c08dj9XX+AuPj1YSGSQIHl1pnDHZR0Q==
eslint-import-resolver-node@^0.3.6:
@ -2858,7 +2858,7 @@ eslint-plugin-jsx-a11y@^6.6.0:
eslint-plugin-prettier@^4.2.1:
version "4.2.1"
resolved "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-4.2.1.tgz"
resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.2.1.tgz#651cbb88b1dab98bfd42f017a12fa6b2d993f94b"
integrity sha512-f/0rXLXUt0oFYs8ra4w49wYZBG5GKZpAYsJSm6rnYL5uVDjd+zowwMwVZHnAjf4edNrKpCDYfXDgmRE/Ak7QyQ==
dependencies:
prettier-linter-helpers "^1.0.0"
@ -2868,7 +2868,7 @@ eslint-plugin-react-hooks@^4.6.0:
resolved "https://registry.npmjs.org/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.0.tgz"
integrity sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==
eslint-plugin-react@^7.30.1:
eslint-plugin-react@^7.30.1, eslint-plugin-react@^7.31.11:
version "7.31.11"
resolved "https://registry.npmjs.org/eslint-plugin-react/-/eslint-plugin-react-7.31.11.tgz"
integrity sha512-TTvq5JsT5v56wPa9OYHzsrOlHzKZKjV+aLgS+55NJP/cuzdiQPC7PfYoUjMoxlffKtvijpk7vA/jmuqRb9nohw==
@ -2929,7 +2929,7 @@ eslint-visitor-keys@^3.3.0:
resolved "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.3.0.tgz"
integrity sha512-mQ+suqKJVyeuwGYHAdjMFqjCyfl8+Ldnxuyp3ldiMBFKkvytrXUZWaiPCEav8qDHKty44bD+qV1IP4T+w+xXRA==
eslint@^8.7.0:
eslint@^8.30.0, eslint@^8.7.0:
version "8.30.0"
resolved "https://registry.npmjs.org/eslint/-/eslint-8.30.0.tgz"
integrity sha512-MGADB39QqYuzEGov+F/qb18r4i7DohCDOfatHaxI2iGlPuC65bwG2gxgO+7DkyL38dRFaRH7RaRAgU6JKL9rMQ==