@nostrify/react 0.2.13 → 0.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +14 -20
- package/CHANGELOG.md +18 -0
- package/dist/NostrContext.d.ts +13 -0
- package/dist/NostrContext.d.ts.map +1 -0
- package/dist/NostrContext.js +7 -0
- package/dist/NostrContext.ts +17 -0
- package/dist/example/test-helpers.js +33 -0
- package/dist/example/test-helpers.ts +33 -0
- package/dist/example/useAuthor.js +31 -0
- package/dist/example/useAuthor.ts +38 -0
- package/dist/example/useCurrentUser.js +42 -0
- package/dist/example/useCurrentUser.ts +48 -0
- package/dist/example/useLoginActions.js +23 -0
- package/dist/example/useLoginActions.ts +22 -0
- package/dist/example/useSocialFeed.js +15 -0
- package/dist/example/useSocialFeed.ts +18 -0
- package/dist/example/vite-env.d.js +0 -0
- package/dist/example/vite-env.d.ts +1 -0
- package/dist/login/NLogin.d.ts +46 -0
- package/dist/login/NLogin.d.ts.map +1 -0
- package/dist/login/NLogin.js +73 -0
- package/dist/login/NLogin.ts +122 -0
- package/dist/login/NUser.d.ts +23 -0
- package/dist/login/NUser.d.ts.map +1 -0
- package/dist/login/NUser.js +41 -0
- package/dist/login/NUser.ts +58 -0
- package/dist/login/NostrLoginContext.d.ts +24 -0
- package/dist/login/NostrLoginContext.d.ts.map +1 -0
- package/dist/login/NostrLoginContext.js +5 -0
- package/dist/login/NostrLoginContext.ts +28 -0
- package/dist/login/NostrLoginProvider.d.ts +15 -0
- package/dist/login/NostrLoginProvider.d.ts.map +1 -0
- package/dist/login/NostrLoginProvider.js +17 -0
- package/dist/login/NostrLoginProvider.ts +34 -0
- package/dist/login/mod.d.ts +5 -0
- package/dist/login/mod.d.ts.map +1 -0
- package/dist/login/mod.js +10 -0
- package/dist/login/mod.ts +4 -0
- package/dist/login/nostrLoginReducer.d.ts +16 -0
- package/dist/login/nostrLoginReducer.d.ts.map +1 -0
- package/dist/login/nostrLoginReducer.js +28 -0
- package/dist/login/nostrLoginReducer.ts +42 -0
- package/dist/login/useNostrLogin.d.ts +3 -0
- package/dist/login/useNostrLogin.d.ts.map +1 -0
- package/dist/login/useNostrLogin.js +12 -0
- package/dist/login/useNostrLogin.ts +13 -0
- package/dist/login/useNostrLoginReducer.d.ts +4 -0
- package/dist/login/useNostrLoginReducer.d.ts.map +1 -0
- package/dist/login/useNostrLoginReducer.js +15 -0
- package/dist/login/useNostrLoginReducer.ts +20 -0
- package/dist/mod.d.ts +4 -0
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +7 -0
- package/dist/mod.ts +3 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/useNostr.d.ts +3 -0
- package/dist/useNostr.d.ts.map +1 -0
- package/dist/useNostr.js +12 -0
- package/dist/useNostr.ts +13 -0
- package/package.json +5 -6
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { NPool } from '@nostrify/nostrify';
|
|
2
|
+
import type { NostrSigner } from '@nostrify/types';
|
|
3
|
+
import type { NLoginBunker, NLoginExtension, NLoginNsec } from './NLogin.ts';
|
|
4
|
+
/** Represents a Nostr user with authentication credentials. */
|
|
5
|
+
export declare class NUser {
|
|
6
|
+
/** The authentication method used for this user */
|
|
7
|
+
readonly method: 'nsec' | 'bunker' | 'extension' | `x-${string}`;
|
|
8
|
+
/** The public key of the user in hex format. */
|
|
9
|
+
readonly pubkey: string;
|
|
10
|
+
/** The signer that can sign events on behalf of this user. */
|
|
11
|
+
readonly signer: NostrSigner;
|
|
12
|
+
constructor(
|
|
13
|
+
/** The authentication method used for this user */
|
|
14
|
+
method: 'nsec' | 'bunker' | 'extension' | `x-${string}`,
|
|
15
|
+
/** The public key of the user in hex format. */
|
|
16
|
+
pubkey: string,
|
|
17
|
+
/** The signer that can sign events on behalf of this user. */
|
|
18
|
+
signer: NostrSigner);
|
|
19
|
+
static fromNsecLogin(login: NLoginNsec): NUser;
|
|
20
|
+
static fromBunkerLogin(login: NLoginBunker, pool: NPool): NUser;
|
|
21
|
+
static fromExtensionLogin(login: NLoginExtension): NUser;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=NUser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NUser.d.ts","sourceRoot":"","sources":["../../login/NUser.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE7E,+DAA+D;AAC/D,qBAAa,KAAK;IAEd,mDAAmD;IACnD,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,WAAW,GAAG,KAAK,MAAM,EAAE;IAChE,gDAAgD;IAChD,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,8DAA8D;IAC9D,QAAQ,CAAC,MAAM,EAAE,WAAW;;IAL5B,mDAAmD;IAC1C,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,WAAW,GAAG,KAAK,MAAM,EAAE;IAChE,gDAAgD;IACvC,MAAM,EAAE,MAAM;IACvB,8DAA8D;IACrD,MAAM,EAAE,WAAW;IAG9B,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,KAAK;IAa9C,MAAM,CAAC,eAAe,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,GAAG,KAAK;IAmB/D,MAAM,CAAC,kBAAkB,CAAC,KAAK,EAAE,eAAe,GAAG,KAAK;CAOzD"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { NBrowserSigner, NConnectSigner, NSecSigner } from "@nostrify/nostrify";
|
|
2
|
+
import { nip19 } from "nostr-tools";
|
|
3
|
+
class NUser {
|
|
4
|
+
constructor(method, pubkey, signer) {
|
|
5
|
+
this.method = method;
|
|
6
|
+
this.pubkey = pubkey;
|
|
7
|
+
this.signer = signer;
|
|
8
|
+
}
|
|
9
|
+
static fromNsecLogin(login) {
|
|
10
|
+
const sk = nip19.decode(login.data.nsec);
|
|
11
|
+
return new NUser(
|
|
12
|
+
login.type,
|
|
13
|
+
login.pubkey,
|
|
14
|
+
new NSecSigner(sk.data)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
static fromBunkerLogin(login, pool) {
|
|
18
|
+
const clientSk = nip19.decode(login.data.clientNsec);
|
|
19
|
+
const clientSigner = new NSecSigner(clientSk.data);
|
|
20
|
+
return new NUser(
|
|
21
|
+
login.type,
|
|
22
|
+
login.pubkey,
|
|
23
|
+
new NConnectSigner({
|
|
24
|
+
relay: pool.group(login.data.relays),
|
|
25
|
+
pubkey: login.pubkey,
|
|
26
|
+
signer: clientSigner,
|
|
27
|
+
timeout: 6e4
|
|
28
|
+
})
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
static fromExtensionLogin(login) {
|
|
32
|
+
return new NUser(
|
|
33
|
+
login.type,
|
|
34
|
+
login.pubkey,
|
|
35
|
+
new NBrowserSigner()
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
NUser
|
|
41
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { NBrowserSigner, NConnectSigner, NSecSigner } from '@nostrify/nostrify';
|
|
2
|
+
import { nip19 } from 'nostr-tools';
|
|
3
|
+
import type { NPool } from '@nostrify/nostrify';
|
|
4
|
+
import type { NostrSigner } from '@nostrify/types';
|
|
5
|
+
|
|
6
|
+
import type { NLoginBunker, NLoginExtension, NLoginNsec } from './NLogin.ts';
|
|
7
|
+
|
|
8
|
+
/** Represents a Nostr user with authentication credentials. */
|
|
9
|
+
export class NUser {
|
|
10
|
+
constructor(
|
|
11
|
+
/** The authentication method used for this user */
|
|
12
|
+
readonly method: 'nsec' | 'bunker' | 'extension' | `x-${string}`,
|
|
13
|
+
/** The public key of the user in hex format. */
|
|
14
|
+
readonly pubkey: string,
|
|
15
|
+
/** The signer that can sign events on behalf of this user. */
|
|
16
|
+
readonly signer: NostrSigner,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
static fromNsecLogin(login: NLoginNsec): NUser {
|
|
20
|
+
const sk = nip19.decode(login.data.nsec) as {
|
|
21
|
+
type: 'nsec';
|
|
22
|
+
data: Uint8Array;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return new NUser(
|
|
26
|
+
login.type,
|
|
27
|
+
login.pubkey,
|
|
28
|
+
new NSecSigner(sk.data),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static fromBunkerLogin(login: NLoginBunker, pool: NPool): NUser {
|
|
33
|
+
const clientSk = nip19.decode(login.data.clientNsec) as {
|
|
34
|
+
type: 'nsec';
|
|
35
|
+
data: Uint8Array;
|
|
36
|
+
};
|
|
37
|
+
const clientSigner = new NSecSigner(clientSk.data);
|
|
38
|
+
|
|
39
|
+
return new NUser(
|
|
40
|
+
login.type,
|
|
41
|
+
login.pubkey,
|
|
42
|
+
new NConnectSigner({
|
|
43
|
+
relay: pool.group(login.data.relays),
|
|
44
|
+
pubkey: login.pubkey,
|
|
45
|
+
signer: clientSigner,
|
|
46
|
+
timeout: 60_000,
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static fromExtensionLogin(login: NLoginExtension): NUser {
|
|
52
|
+
return new NUser(
|
|
53
|
+
login.type,
|
|
54
|
+
login.pubkey,
|
|
55
|
+
new NBrowserSigner(),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Context } from 'react';
|
|
2
|
+
import type { NLoginType } from './NLogin.ts';
|
|
3
|
+
/**
|
|
4
|
+
* NostrLoginContextType defines the shape of the context that will be provided
|
|
5
|
+
* to components that need access to the Nostr login state.
|
|
6
|
+
*/
|
|
7
|
+
export type NostrLoginContextType = {
|
|
8
|
+
/** The list of Nostr logins. */
|
|
9
|
+
logins: readonly NLoginType[];
|
|
10
|
+
/** Dispatch an action to add a login to the state. */
|
|
11
|
+
addLogin: (login: NLoginType) => void;
|
|
12
|
+
/** Dispatch an action to remove a login from the state. */
|
|
13
|
+
removeLogin: (loginId: string) => void;
|
|
14
|
+
/** Dispatch an action to set the user's current login (by moving it to the top of the state). */
|
|
15
|
+
setLogin: (loginId: string) => void;
|
|
16
|
+
/** Dispatch an action to clear the login state. */
|
|
17
|
+
clearLogins: () => void;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* NostrLoginContext is a React context that provides access to the Nostr login state and
|
|
21
|
+
* a dispatch function to update the state.
|
|
22
|
+
*/
|
|
23
|
+
export declare const NostrLoginContext: Context<NostrLoginContextType | undefined>;
|
|
24
|
+
//# sourceMappingURL=NostrLoginContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NostrLoginContext.d.ts","sourceRoot":"","sources":["../../login/NostrLoginContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAiB,MAAM,OAAO,CAAC;AAEpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,gCAAgC;IAChC,MAAM,EAAE,SAAS,UAAU,EAAE,CAAC;IAC9B,sDAAsD;IACtD,QAAQ,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;IACtC,2DAA2D;IAC3D,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACvC,iGAAiG;IACjG,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACpC,mDAAmD;IACnD,WAAW,EAAE,MAAM,IAAI,CAAC;CACzB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAE7D,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Context, createContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import type { NLoginType } from './NLogin.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* NostrLoginContextType defines the shape of the context that will be provided
|
|
7
|
+
* to components that need access to the Nostr login state.
|
|
8
|
+
*/
|
|
9
|
+
export type NostrLoginContextType = {
|
|
10
|
+
/** The list of Nostr logins. */
|
|
11
|
+
logins: readonly NLoginType[];
|
|
12
|
+
/** Dispatch an action to add a login to the state. */
|
|
13
|
+
addLogin: (login: NLoginType) => void;
|
|
14
|
+
/** Dispatch an action to remove a login from the state. */
|
|
15
|
+
removeLogin: (loginId: string) => void;
|
|
16
|
+
/** Dispatch an action to set the user's current login (by moving it to the top of the state). */
|
|
17
|
+
setLogin: (loginId: string) => void;
|
|
18
|
+
/** Dispatch an action to clear the login state. */
|
|
19
|
+
clearLogins: () => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* NostrLoginContext is a React context that provides access to the Nostr login state and
|
|
24
|
+
* a dispatch function to update the state.
|
|
25
|
+
*/
|
|
26
|
+
export const NostrLoginContext: Context<NostrLoginContextType | undefined> = createContext<
|
|
27
|
+
NostrLoginContextType | undefined
|
|
28
|
+
>(undefined);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FC, ReactNode } from 'react';
|
|
2
|
+
/** Props for `NostrLoginProvider`. */
|
|
3
|
+
interface NostrLoginProviderProps {
|
|
4
|
+
/** The child components that will have access to the context. */
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
/** The key used to store (and revive) the logins in localStorage. */
|
|
7
|
+
storageKey: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* NostrLoginProvider is a React component that provides a context for managing Nostr logins.
|
|
11
|
+
* It uses a reducer to handle the state of logins and stores them in localStorage.
|
|
12
|
+
*/
|
|
13
|
+
export declare const NostrLoginProvider: FC<NostrLoginProviderProps>;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=NostrLoginProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NostrLoginProvider.d.ts","sourceRoot":"","sources":["../../login/NostrLoginProvider.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAE3C,sCAAsC;AACtC,UAAU,uBAAuB;IAC/B,iEAAiE;IACjE,QAAQ,EAAE,SAAS,CAAC;IACpB,qEAAqE;IACrE,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;GAGG;AACH,eAAO,MAAM,kBAAkB,EAAE,EAAE,CAAC,uBAAuB,CAc1D,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { NostrLoginContext } from "./NostrLoginContext.js";
|
|
3
|
+
import { useNostrLoginReducer } from "./useNostrLoginReducer.js";
|
|
4
|
+
const NostrLoginProvider = ({ children, storageKey }) => {
|
|
5
|
+
const [logins, dispatch] = useNostrLoginReducer(storageKey);
|
|
6
|
+
const value = {
|
|
7
|
+
logins,
|
|
8
|
+
addLogin: (login) => dispatch({ type: "login.add", login }),
|
|
9
|
+
removeLogin: (id) => dispatch({ type: "login.remove", id }),
|
|
10
|
+
setLogin: (id) => dispatch({ type: "login.set", id }),
|
|
11
|
+
clearLogins: () => dispatch({ type: "login.clear" })
|
|
12
|
+
};
|
|
13
|
+
return jsx(NostrLoginContext.Provider, { value, children });
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
NostrLoginProvider
|
|
17
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
|
|
3
|
+
import { NostrLoginContext, NostrLoginContextType } from './NostrLoginContext.ts';
|
|
4
|
+
import { useNostrLoginReducer } from './useNostrLoginReducer.ts';
|
|
5
|
+
|
|
6
|
+
import type { FC, ReactNode } from 'react';
|
|
7
|
+
|
|
8
|
+
/** Props for `NostrLoginProvider`. */
|
|
9
|
+
interface NostrLoginProviderProps {
|
|
10
|
+
/** The child components that will have access to the context. */
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
/** The key used to store (and revive) the logins in localStorage. */
|
|
13
|
+
storageKey: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* NostrLoginProvider is a React component that provides a context for managing Nostr logins.
|
|
18
|
+
* It uses a reducer to handle the state of logins and stores them in localStorage.
|
|
19
|
+
*/
|
|
20
|
+
export const NostrLoginProvider: FC<NostrLoginProviderProps> = (
|
|
21
|
+
{ children, storageKey }: NostrLoginProviderProps,
|
|
22
|
+
) => {
|
|
23
|
+
const [logins, dispatch] = useNostrLoginReducer(storageKey);
|
|
24
|
+
|
|
25
|
+
const value: NostrLoginContextType = {
|
|
26
|
+
logins,
|
|
27
|
+
addLogin: (login) => dispatch({ type: 'login.add', login }),
|
|
28
|
+
removeLogin: (id) => dispatch({ type: 'login.remove', id }),
|
|
29
|
+
setLogin: (id) => dispatch({ type: 'login.set', id }),
|
|
30
|
+
clearLogins: () => dispatch({ type: 'login.clear' }),
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return jsx(NostrLoginContext.Provider, { value, children });
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../login/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NLogin } from "./NLogin.js";
|
|
2
|
+
import { NostrLoginProvider } from "./NostrLoginProvider.js";
|
|
3
|
+
import { NUser } from "./NUser.js";
|
|
4
|
+
import { useNostrLogin } from "./useNostrLogin.js";
|
|
5
|
+
export {
|
|
6
|
+
NLogin,
|
|
7
|
+
NUser,
|
|
8
|
+
NostrLoginProvider,
|
|
9
|
+
useNostrLogin
|
|
10
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NLoginType } from './NLogin.ts';
|
|
2
|
+
export type NLoginAction = {
|
|
3
|
+
type: 'login.add';
|
|
4
|
+
login: NLoginType;
|
|
5
|
+
set?: boolean;
|
|
6
|
+
} | {
|
|
7
|
+
type: 'login.remove';
|
|
8
|
+
id: string;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'login.set';
|
|
11
|
+
id: string;
|
|
12
|
+
} | {
|
|
13
|
+
type: 'login.clear';
|
|
14
|
+
};
|
|
15
|
+
export declare function nostrLoginReducer(state: NLoginType[], action: NLoginAction): NLoginType[];
|
|
16
|
+
//# sourceMappingURL=nostrLoginReducer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nostrLoginReducer.d.ts","sourceRoot":"","sources":["../../login/nostrLoginReducer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,MAAM,MAAM,YAAY,GACpB;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,UAAU,CAAC;IAAC,GAAG,CAAC,EAAE,OAAO,CAAA;CAAE,GACvD;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACpC;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,aAAa,CAAA;CAAE,CAAC;AAE5B,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,UAAU,EAAE,EACnB,MAAM,EAAE,YAAY,GACnB,UAAU,EAAE,CA8Bd"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
function nostrLoginReducer(state, action) {
|
|
2
|
+
switch (action.type) {
|
|
3
|
+
case "login.add": {
|
|
4
|
+
const filtered = state.filter((login) => login.id !== action.login.id);
|
|
5
|
+
return action.set ? [action.login, ...filtered] : [...filtered, action.login];
|
|
6
|
+
}
|
|
7
|
+
case "login.remove": {
|
|
8
|
+
return state.filter((login) => login.id !== action.id);
|
|
9
|
+
}
|
|
10
|
+
case "login.set": {
|
|
11
|
+
const login = state.find((login2) => login2.id === action.id);
|
|
12
|
+
if (!login) {
|
|
13
|
+
return state;
|
|
14
|
+
}
|
|
15
|
+
const filtered = state.filter((login2) => login2.id !== action.id);
|
|
16
|
+
return [login, ...filtered];
|
|
17
|
+
}
|
|
18
|
+
case "login.clear": {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
default: {
|
|
22
|
+
return state;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
nostrLoginReducer
|
|
28
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { NLoginType } from './NLogin.ts';
|
|
2
|
+
|
|
3
|
+
export type NLoginAction =
|
|
4
|
+
| { type: 'login.add'; login: NLoginType; set?: boolean }
|
|
5
|
+
| { type: 'login.remove'; id: string }
|
|
6
|
+
| { type: 'login.set'; id: string }
|
|
7
|
+
| { type: 'login.clear' };
|
|
8
|
+
|
|
9
|
+
export function nostrLoginReducer(
|
|
10
|
+
state: NLoginType[],
|
|
11
|
+
action: NLoginAction,
|
|
12
|
+
): NLoginType[] {
|
|
13
|
+
switch (action.type) {
|
|
14
|
+
case 'login.add': {
|
|
15
|
+
const filtered = state.filter((login) => login.id !== action.login.id);
|
|
16
|
+
return action.set ? [action.login, ...filtered] : [...filtered, action.login];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
case 'login.remove': {
|
|
20
|
+
return state.filter((login) => login.id !== action.id);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
case 'login.set': {
|
|
24
|
+
const login = state.find((login) => login.id === action.id);
|
|
25
|
+
|
|
26
|
+
if (!login) {
|
|
27
|
+
return state;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const filtered = state.filter((login) => login.id !== action.id);
|
|
31
|
+
return [login, ...filtered];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
case 'login.clear': {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
default: {
|
|
39
|
+
return state;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useNostrLogin.d.ts","sourceRoot":"","sources":["../../login/useNostrLogin.ts"],"names":[],"mappings":"AAEA,OAAO,EAAqB,KAAK,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAEvF,wBAAgB,aAAa,IAAI,qBAAqB,CAQrD"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { NostrLoginContext } from "./NostrLoginContext.js";
|
|
3
|
+
function useNostrLogin() {
|
|
4
|
+
const context = useContext(NostrLoginContext);
|
|
5
|
+
if (!context) {
|
|
6
|
+
throw new Error("useNostrLogin must be used within a NostrLoginProvider");
|
|
7
|
+
}
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
useNostrLogin
|
|
12
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import { NostrLoginContext, type NostrLoginContextType } from './NostrLoginContext.ts';
|
|
4
|
+
|
|
5
|
+
export function useNostrLogin(): NostrLoginContextType {
|
|
6
|
+
const context = useContext(NostrLoginContext);
|
|
7
|
+
|
|
8
|
+
if (!context) {
|
|
9
|
+
throw new Error('useNostrLogin must be used within a NostrLoginProvider');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return context;
|
|
13
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type NLoginAction } from './nostrLoginReducer.ts';
|
|
2
|
+
import type { NLoginType } from './NLogin.ts';
|
|
3
|
+
export declare function useNostrLoginReducer(storageKey: string): [state: NLoginType[], dispatch: (action: NLoginAction) => void];
|
|
4
|
+
//# sourceMappingURL=useNostrLoginReducer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useNostrLoginReducer.d.ts","sourceRoot":"","sources":["../../login/useNostrLoginReducer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,YAAY,EAAqB,MAAM,wBAAwB,CAAC;AAE9E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,wBAAgB,oBAAoB,CAClC,UAAU,EAAE,MAAM,GACjB,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,QAAQ,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC,CAWjE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useEffect, useReducer } from "react";
|
|
2
|
+
import { nostrLoginReducer } from "./nostrLoginReducer.js";
|
|
3
|
+
function useNostrLoginReducer(storageKey) {
|
|
4
|
+
const [state, dispatch] = useReducer(nostrLoginReducer, [], () => {
|
|
5
|
+
const stored = localStorage.getItem(storageKey);
|
|
6
|
+
return stored ? JSON.parse(stored) : [];
|
|
7
|
+
});
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
localStorage.setItem(storageKey, JSON.stringify(state));
|
|
10
|
+
}, [state]);
|
|
11
|
+
return [state, dispatch];
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
useNostrLoginReducer
|
|
15
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useEffect, useReducer } from 'react';
|
|
2
|
+
|
|
3
|
+
import { type NLoginAction, nostrLoginReducer } from './nostrLoginReducer.ts';
|
|
4
|
+
|
|
5
|
+
import type { NLoginType } from './NLogin.ts';
|
|
6
|
+
|
|
7
|
+
export function useNostrLoginReducer(
|
|
8
|
+
storageKey: string,
|
|
9
|
+
): [state: NLoginType[], dispatch: (action: NLoginAction) => void] {
|
|
10
|
+
const [state, dispatch] = useReducer(nostrLoginReducer, [], () => {
|
|
11
|
+
const stored = localStorage.getItem(storageKey);
|
|
12
|
+
return stored ? JSON.parse(stored) : [];
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
localStorage.setItem(storageKey, JSON.stringify(state));
|
|
17
|
+
}, [state]);
|
|
18
|
+
|
|
19
|
+
return [state, dispatch];
|
|
20
|
+
}
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,cAAc,gBAAgB,CAAC"}
|
package/dist/mod.js
ADDED
package/dist/mod.ts
ADDED