Skip to content

Commit

Permalink
wip typeguard etc
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Mar 25, 2024
1 parent c3442d7 commit 96ae8dc
Show file tree
Hide file tree
Showing 43 changed files with 254 additions and 194 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
dist
dist-ssr
*.local
docs

# Editor directories and files
.vscode/*
Expand Down
6 changes: 2 additions & 4 deletions iris-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"engine": "node >=18.0.0",
"scripts": {
"dev": "vite",
"build": "tsc && vite build && npm run docs",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"docs": "typedoc --out dist/irisdb ../irisdb/index.ts",
"test": "vitest"
},
"dependencies": {
Expand Down Expand Up @@ -48,7 +47,6 @@
"daisyui": "^4.7.2",
"jsdom": "^24.0.0",
"postcss": "^8.4.35",
"tailwindcss": "^3.4.1",
"typedoc": "^0.25.12"
"tailwindcss": "^3.4.1"
}
}
2 changes: 1 addition & 1 deletion iris-docs/src/pages/canvas/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PlusIcon } from '@heroicons/react/24/solid';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import publicState from 'irisdb-ndk/PublicState';
import useAuthors from 'irisdb-ndk/useAuthors';
Expand Down
2 changes: 1 addition & 1 deletion iris-docs/src/pages/canvas/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useLocalState } from 'irisdb';
import { useParams } from 'react-router-dom';

import { useLocalState } from 'irisdb/useNodeState';
import Canvas from '@/pages/canvas/Canvas';
import { FileList } from '@/shared/components/FileList';
import LoginDialog from '@/shared/components/LoginDialog';
Expand Down
2 changes: 1 addition & 1 deletion iris-docs/src/pages/document/Document.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import publicState from 'irisdb-ndk/PublicState';
import useAuthors from 'irisdb-ndk/useAuthors';
Expand Down
2 changes: 1 addition & 1 deletion iris-docs/src/pages/document/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useLocalState } from 'irisdb';
import { useParams } from 'react-router-dom';

import { useLocalState } from 'irisdb/useNodeState';
import Document from '@/pages/document/Document';
import { FileList } from '@/shared/components/FileList';
import LoginDialog from '@/shared/components/LoginDialog';
Expand Down
2 changes: 1 addition & 1 deletion iris-docs/src/pages/explorer/Explorer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import localState from 'irisdb/LocalState';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import publicState from 'irisdb-ndk/PublicState';
import { useParams } from 'react-router-dom';
Expand Down
4 changes: 2 additions & 2 deletions iris-docs/src/pages/explorer/ExplorerNode.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChevronRightIcon } from '@heroicons/react/20/solid';
import { useEffect, useState } from 'react';

import Node, { DIR_VALUE } from 'irisdb/Node';
import { JsonValue } from 'irisdb/types';
import { useEffect, useState } from 'react';

import Show from '@/shared/components/Show';
import { SortedMap } from '@/utils/SortedMap/SortedMap';

Expand Down
3 changes: 1 addition & 2 deletions iris-docs/src/pages/explorer/ExplorerNodeEditRow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ChangeEvent, FormEvent, useState } from 'react';

import Node, { DIR_VALUE } from 'irisdb/Node';
import { ChangeEvent, FormEvent, useState } from 'react';

type EditRowProps = {
level: number;
Expand Down
2 changes: 1 addition & 1 deletion iris-docs/src/shared/components/FileList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FolderIcon } from '@heroicons/react/24/outline';
import { FolderOpenIcon, PlusIcon, TrashIcon, UserPlusIcon } from '@heroicons/react/24/solid';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import publicState from 'irisdb-ndk/PublicState';
import useAuthors from 'irisdb-ndk/useAuthors';
Expand Down
2 changes: 1 addition & 1 deletion iris-docs/src/shared/components/LoginDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hexToBytes } from '@noble/hashes/utils';
import classNames from 'classnames';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { newUserLogin, privateKeyLogin } from 'irisdb-ndk/ndk';
import { nip19 } from 'nostr-tools';
import { ChangeEvent, FormEvent, useEffect, useState } from 'react';
Expand Down
6 changes: 3 additions & 3 deletions iris-docs/src/shared/components/UserButton.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useLocalState } from 'irisdb';
import { useCallback, useEffect, useRef } from 'react';

import { useLocalState } from 'irisdb/useNodeState';
import LoginDialog from '@/shared/components/LoginDialog';
import Show from '@/shared/components/Show';
import { Avatar } from '@/shared/components/user/Avatar';

export default function UserButton() {
const [pubKey] = useLocalState('user/publicKey', '');
const [pubKey] = useLocalState('user/publicKey', '', String);
const userModal = useRef<HTMLDialogElement>(null);

const showModal = useCallback(() => {
Expand All @@ -19,7 +19,7 @@ export default function UserButton() {

return (
<>
<Show when={pubKey}>
<Show when={!!pubKey}>
<div className="ml-2 rounded-full cursor-pointer" onClick={showModal}>
<Avatar pubKey={pubKey} />
</div>
Expand Down
7 changes: 3 additions & 4 deletions iris-docs/src/shared/components/ViewingUsers.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useLocalState } from 'irisdb';
import { PublicKey, publicState } from 'irisdb-ndk';
import { useEffect, useState } from 'react';

import publicState from 'irisdb-ndk/PublicState';
import { useLocalState } from 'irisdb/useNodeState';
import { Avatar } from '@/shared/components/user/Avatar';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';

interface ViewingUsersProps {
file: string;
Expand Down Expand Up @@ -50,7 +49,7 @@ export function ViewingUsers({ file, authors }: ViewingUsersProps) {
publicState(authors.map((k) => new PublicKey(k)))
.get(file)
.get('viewing')
.get(myPubKey)
.get(String(myPubKey))
.put(isViewing, undefined, expiresAt);
};
setViewing();
Expand Down
6 changes: 3 additions & 3 deletions iris-docs/src/shared/components/share/AddUserForm.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import classNames from 'classnames';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import publicState from 'irisdb-ndk/PublicState';
import { FormEvent, useMemo, useState } from 'react';

import { UserRow } from '@/shared/components/user/UserRow';

export const AddUserForm = ({ file }: { file: string }) => {
const [myPubKey] = useLocalState('user/publicKey', '');
const [myPubKey] = useLocalState('user/publicKey', '', String);
const [userToAdd, setUserToAdd] = useState('');
const userToAddValid = useMemo(() => {
if (userToAdd === myPubKey) return false;
Expand All @@ -22,7 +22,7 @@ export const AddUserForm = ({ file }: { file: string }) => {
const onSubmit = (e: FormEvent) => {
e.preventDefault();
if (userToAddValid) {
publicState([myPubKey])
publicState([new PublicKey(myPubKey)])
.get(`${file}/writers/${new PublicKey(userToAdd).toString()}`)
.put(true);
setUserToAdd('');
Expand Down
4 changes: 2 additions & 2 deletions iris-docs/src/shared/components/share/FollowUserForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import ndk from 'irisdb-ndk/ndk';
import { FormEvent, useMemo, useState } from 'react';
Expand All @@ -22,7 +22,7 @@ export const FollowUserForm = () => {
const onSubmit = (e: FormEvent) => {
e.preventDefault();
if (userToAddValid) {
const me = ndk.getUser({ pubkey: myPubKey });
const me = ndk.getUser({ pubkey: String(myPubKey) });
const userToFollow = ndk.getUser({ pubkey: new PublicKey(userToAdd).toString() });
me.follow(userToFollow);
setUserToAdd('');
Expand Down
4 changes: 2 additions & 2 deletions iris-docs/src/shared/components/share/ShareMenuModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import { nip19 } from 'nostr-tools';
import { RefObject, useMemo } from 'react';
Expand All @@ -19,7 +19,7 @@ export function ShareMenuModal({
}) {
const user = useSearchParam('user', 'follows');
const [myPubKey] = useLocalState('user/publicKey', '');
const myNpub = useMemo(() => nip19.npubEncode(myPubKey), [myPubKey]);
const myNpub = useMemo(() => nip19.npubEncode(String(myPubKey)), [myPubKey]);
const userPublicKey = useMemo(() => {
if (!user || user === 'follows') return;
return new PublicKey(user);
Expand Down
4 changes: 2 additions & 2 deletions iris-docs/src/shared/components/share/WriteAccessUsers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TrashIcon } from '@heroicons/react/24/solid';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import publicState from 'irisdb-ndk/PublicState';
import useAuthors from 'irisdb-ndk/useAuthors';
Expand Down Expand Up @@ -62,7 +62,7 @@ export const WriteAccessUsers = ({ user, isMine, file }: WriteAccessUsersProps)
className="btn btn-circle btn-sm btn-outline"
type="button"
onClick={() => {
publicState([new PublicKey(myPubKey)])
publicState([new PublicKey(String(myPubKey))])
.get(`${file}/writers/${pubKey}`)
.put(false);
}}
Expand Down
3 changes: 1 addition & 2 deletions irisdb-ndk/Hex/Hex.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from 'vitest';

import { EventID, PublicKey } from 'irisdb-ndk/Hex/Hex';
import { describe, expect, it } from 'vitest';

describe('PublicKey', () => {
it('should convert npub bech32 to hex', () => {
Expand Down
2 changes: 1 addition & 1 deletion irisdb-ndk/NDKAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NDK, { NDKEvent, NostrEvent } from '@nostr-dev-kit/ndk';
import debug from 'debug';
import { Adapter, Callback, NodeValue, Unsubscribe } from 'irisdb/types';
import { Adapter, Callback, NodeValue, Unsubscribe } from 'irisdb';

import { PublicKey } from './Hex/PublicKey';

Expand Down
3 changes: 1 addition & 2 deletions irisdb-ndk/PublicState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import MemoryAdapter from 'irisdb/adapters/MemoryAdapter';
import { MemoryAdapter, Node } from 'irisdb';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';

import Node from '../irisdb/Node';
import ndk from './ndk';
import NDKAdapter from './NDKAdapter';

Expand Down
6 changes: 6 additions & 0 deletions irisdb-ndk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { EventID } from './Hex/EventID';
export { Hex } from './Hex/Hex';
export { PublicKey } from './Hex/PublicKey';
export { default as NDKAdapter } from './NDKAdapter';
export { default as publicState } from './PublicState';
export { default as useAuthors } from './useAuthors';
3 changes: 1 addition & 2 deletions irisdb-ndk/ndk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
import NDK, { NDKNip07Signer, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk';
import { NDKEvent } from '@nostr-dev-kit/ndk';
import NDKCacheAdapterDexie from '@nostr-dev-kit/ndk-cache-dexie';
import { localState } from 'irisdb';
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';

import localState from 'irisdb/LocalState';

const dexieAdapter = new NDKCacheAdapterDexie({ dbName: 'nostree-db' });

const ndk = new NDK({
Expand Down
10 changes: 5 additions & 5 deletions irisdb-ndk/useAuthors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NDKTag } from '@nostr-dev-kit/ndk';
import { useLocalState } from 'irisdb/useNodeState';
import { useLocalState } from 'irisdb';
import { Hex } from 'irisdb-ndk/Hex/Hex';
import { PublicKey } from 'irisdb-ndk/Hex/PublicKey';
import publicState from 'irisdb-ndk/PublicState';
Expand All @@ -9,10 +9,10 @@ import ndk from './ndk';

export default function useAuthors(ownerOrGroup?: string, groupPath?: string): string[] {
const [myPubKey] = useLocalState('user/publicKey', '');
const initialAuthors = useMemo(() => {
const initialAuthors = useMemo((): string[] => {
if (!ownerOrGroup) return [];
if (ownerOrGroup === 'follows') {
return myPubKey ? [myPubKey] : [];
return myPubKey ? [String(myPubKey)] : [];
} else {
const k = new PublicKey(ownerOrGroup);
return [k.toString()];
Expand All @@ -22,10 +22,10 @@ export default function useAuthors(ownerOrGroup?: string, groupPath?: string): s

useEffect(() => {
if (ownerOrGroup === 'follows') {
const sub = ndk.subscribe({ kinds: [3], authors: [myPubKey] });
const sub = ndk.subscribe({ kinds: [3], authors: [String(myPubKey)] });
sub.on('event', (event) => {
if (event.kind === 3) {
const newAuthors = new Set([myPubKey]);
const newAuthors = new Set([String(myPubKey)]);
let updated = false;
event.tags.forEach((tag: NDKTag) => {
if (tag[0] === 'p') {
Expand Down
12 changes: 0 additions & 12 deletions irisdb/LocalState.ts

This file was deleted.

9 changes: 0 additions & 9 deletions irisdb/index.ts

This file was deleted.

11 changes: 8 additions & 3 deletions irisdb/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "irisdb",
"version": "1.0.0",
"type": "module",
"description": "object-like data structure that can be subscribed to and easily synced over different transports",
"main": "index.ts",
"main": "src/index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"docs": "typedoc --out docs ./src/index.ts"
},
"repository": {
"type": "git",
Expand All @@ -15,5 +17,8 @@
"bugs": {
"url": "https://github.com/irislib/iris-docs/issues"
},
"homepage": "https://github.com/irislib/iris/iris-docs"
"homepage": "https://github.com/irislib/iris/iris-docs",
"dependencies": {
"typedoc": "^0.25.12"
}
}
11 changes: 11 additions & 0 deletions irisdb/src/LocalState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import BroadcastChannelAdapter from './adapters/BroadcastChannelAdapter';
import LocalStorageMemoryAdapter from './adapters/LocalStorageMemoryAdapter';
import Node from './Node';

const NAME = 'localState';
const localState = new Node({
id: NAME,
adapters: [new LocalStorageMemoryAdapter(), new BroadcastChannelAdapter(NAME)],
});

export { localState };
4 changes: 1 addition & 3 deletions irisdb/Node.test.ts → irisdb/src/Node.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Callback, MemoryAdapter, Unsubscribe } from 'irisdb';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import MemoryAdapter from 'irisdb/adapters/MemoryAdapter';
import { Callback, Unsubscribe } from 'irisdb/types';

import Node, { DIR_VALUE } from './Node';

describe('Node', () => {
Expand Down
Loading

0 comments on commit 96ae8dc

Please sign in to comment.