@bifold/react-hooks 0.6.1

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.
Files changed (35) hide show
  1. package/README.md +24 -0
  2. package/build/AgentProvider.d.ts +13 -0
  3. package/build/AgentProvider.js +77 -0
  4. package/build/AgentProvider.js.map +1 -0
  5. package/build/BasicMessageProvider.d.ts +12 -0
  6. package/build/BasicMessageProvider.js +83 -0
  7. package/build/BasicMessageProvider.js.map +1 -0
  8. package/build/ConnectionProvider.d.ts +24 -0
  9. package/build/ConnectionProvider.js +106 -0
  10. package/build/ConnectionProvider.js.map +1 -0
  11. package/build/CredentialFormatDataProvider.d.ts +19 -0
  12. package/build/CredentialFormatDataProvider.js +121 -0
  13. package/build/CredentialFormatDataProvider.js.map +1 -0
  14. package/build/CredentialProvider.d.ts +15 -0
  15. package/build/CredentialProvider.js +101 -0
  16. package/build/CredentialProvider.js.map +1 -0
  17. package/build/ExchangesProvider.d.ts +8 -0
  18. package/build/ExchangesProvider.js +76 -0
  19. package/build/ExchangesProvider.js.map +1 -0
  20. package/build/ProofFormatDataProvider.d.ts +19 -0
  21. package/build/ProofFormatDataProvider.js +119 -0
  22. package/build/ProofFormatDataProvider.js.map +1 -0
  23. package/build/ProofProvider.d.ts +15 -0
  24. package/build/ProofProvider.js +107 -0
  25. package/build/ProofProvider.js.map +1 -0
  26. package/build/QuestionAnswerProvider.d.ts +14 -0
  27. package/build/QuestionAnswerProvider.js +98 -0
  28. package/build/QuestionAnswerProvider.js.map +1 -0
  29. package/build/index.d.ts +14 -0
  30. package/build/index.js +71 -0
  31. package/build/index.js.map +1 -0
  32. package/build/recordUtils.d.ts +22 -0
  33. package/build/recordUtils.js +81 -0
  34. package/build/recordUtils.js.map +1 -0
  35. package/package.json +44 -0
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ <p align="center">
2
+ <br />
3
+ <img
4
+ alt="Credo logo"
5
+ src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png"
6
+ height="250px"
7
+ />
8
+ </p>
9
+ <h1 align="center"><b>React Hooks for Credo</b></h1>
10
+ <p align="center">
11
+ <a
12
+ href="https://raw.githubusercontent.com/openwallet-foundation/credo-ts-ext/main/LICENSE"
13
+ ><img
14
+ alt="License"
15
+ src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"
16
+ /></a>
17
+ <a href="https://www.typescriptlang.org/"
18
+ ><img
19
+ alt="typescript"
20
+ src="https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg"
21
+ /></a>
22
+ </p>
23
+
24
+ For documentation on installation and usage of the React Hooks package, refer to the [Docs](https://credo.js.org/guides/extensions/react-hooks).
@@ -0,0 +1,13 @@
1
+ import type { Agent } from '@credo-ts/core';
2
+ import type { PropsWithChildren } from 'react';
3
+ import * as React from 'react';
4
+ interface AgentContextInterface<AppAgent extends Agent = Agent> {
5
+ loading: boolean;
6
+ agent: AppAgent;
7
+ }
8
+ export declare const useAgent: <AppAgent extends Agent>() => AgentContextInterface<AppAgent>;
9
+ interface Props {
10
+ agent: Agent;
11
+ }
12
+ declare const AgentProvider: React.FC<PropsWithChildren<Props>>;
13
+ export default AgentProvider;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.useAgent = void 0;
40
+ const question_answer_1 = require("@credo-ts/question-answer");
41
+ const React = __importStar(require("react"));
42
+ const react_1 = require("react");
43
+ const BasicMessageProvider_1 = __importDefault(require("./BasicMessageProvider"));
44
+ const ConnectionProvider_1 = __importDefault(require("./ConnectionProvider"));
45
+ const CredentialFormatDataProvider_1 = __importDefault(require("./CredentialFormatDataProvider"));
46
+ const CredentialProvider_1 = __importDefault(require("./CredentialProvider"));
47
+ const ProofFormatDataProvider_1 = __importDefault(require("./ProofFormatDataProvider"));
48
+ const ProofProvider_1 = __importDefault(require("./ProofProvider"));
49
+ const QuestionAnswerProvider_1 = __importDefault(require("./QuestionAnswerProvider"));
50
+ const recordUtils_1 = require("./recordUtils");
51
+ const AgentContext = (0, react_1.createContext)(undefined);
52
+ const useAgent = () => {
53
+ const agentContext = (0, react_1.useContext)(AgentContext);
54
+ if (!agentContext) {
55
+ throw new Error('useAgent must be used within a AgentContextProvider');
56
+ }
57
+ return agentContext;
58
+ };
59
+ exports.useAgent = useAgent;
60
+ const AgentProvider = ({ agent, children }) => {
61
+ const isQaRegistered = (0, recordUtils_1.useIsModuleRegistered)(agent, question_answer_1.QuestionAnswerModule);
62
+ const [agentState] = (0, react_1.useState)({
63
+ loading: false,
64
+ agent,
65
+ });
66
+ return (React.createElement(AgentContext.Provider, { value: agentState },
67
+ React.createElement(ConnectionProvider_1.default, { agent: agent },
68
+ React.createElement(CredentialProvider_1.default, { agent: agent },
69
+ React.createElement(ProofProvider_1.default, { agent: agent },
70
+ React.createElement(CredentialFormatDataProvider_1.default, { agent: agent },
71
+ React.createElement(ProofFormatDataProvider_1.default, { agent: agent },
72
+ React.createElement(BasicMessageProvider_1.default, { agent: agent }, isQaRegistered ? (React.createElement(QuestionAnswerProvider_1.default, { agent: agent },
73
+ children,
74
+ " ")) : (children)))))))));
75
+ };
76
+ exports.default = AgentProvider;
77
+ //# sourceMappingURL=AgentProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AgentProvider.js","sourceRoot":"","sources":["../src/AgentProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,+DAAgE;AAChE,6CAA8B;AAC9B,iCAA2D;AAE3D,kFAAyD;AACzD,8EAAqD;AACrD,kGAAyE;AACzE,8EAAqD;AACrD,wFAA+D;AAC/D,oEAA2C;AAC3C,sFAA6D;AAC7D,+CAAqD;AAOrD,MAAM,YAAY,GAAG,IAAA,qBAAa,EAAoC,SAAS,CAAC,CAAA;AAEzE,MAAM,QAAQ,GAAG,GAA2B,EAAE;IACnD,MAAM,YAAY,GAAG,IAAA,kBAAU,EAAC,YAAY,CAAC,CAAA;IAC7C,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAA;IACxE,CAAC;IACD,OAAO,YAA+C,CAAA;AACxD,CAAC,CAAA;AANY,QAAA,QAAQ,YAMpB;AAMD,MAAM,aAAa,GAAuC,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE;IAChF,MAAM,cAAc,GAAG,IAAA,mCAAqB,EAAC,KAAK,EAAE,sCAAoB,CAAC,CAAA;IACzE,MAAM,CAAC,UAAU,CAAC,GAAG,IAAA,gBAAQ,EAAwB;QACnD,OAAO,EAAE,KAAK;QACd,KAAK;KACN,CAAC,CAAA;IAEF,OAAO,CACL,oBAAC,YAAY,CAAC,QAAQ,IAAC,KAAK,EAAE,UAAU;QACtC,oBAAC,4BAAkB,IAAC,KAAK,EAAE,KAAK;YAC9B,oBAAC,4BAAkB,IAAC,KAAK,EAAE,KAAK;gBAC9B,oBAAC,uBAAa,IAAC,KAAK,EAAE,KAAK;oBACzB,oBAAC,sCAA4B,IAAC,KAAK,EAAE,KAAK;wBACxC,oBAAC,iCAAuB,IAAC,KAAK,EAAE,KAAK;4BACnC,oBAAC,8BAAoB,IAAC,KAAK,EAAE,KAAK,IAC/B,cAAc,CAAC,CAAC,CAAC,CAChB,oBAAC,gCAAsB,IAAC,KAAK,EAAE,KAAK;gCAAG,QAAQ;oCAA2B,CAC3E,CAAC,CAAC,CAAC,CACF,QAAQ,CACT,CACoB,CACC,CACG,CACjB,CACG,CACF,CACC,CACzB,CAAA;AACH,CAAC,CAAA;AAED,kBAAe,aAAa,CAAA"}
@@ -0,0 +1,12 @@
1
+ import type { RecordsState } from './recordUtils';
2
+ import type { Agent } from '@credo-ts/core';
3
+ import type { PropsWithChildren } from 'react';
4
+ import { BasicMessageRecord } from '@credo-ts/core';
5
+ import * as React from 'react';
6
+ export declare const useBasicMessages: () => RecordsState<BasicMessageRecord>;
7
+ export declare const useBasicMessagesByConnectionId: (connectionId: string) => BasicMessageRecord[];
8
+ interface Props {
9
+ agent: Agent;
10
+ }
11
+ declare const BasicMessageProvider: React.FC<PropsWithChildren<Props>>;
12
+ export default BasicMessageProvider;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.useBasicMessagesByConnectionId = exports.useBasicMessages = void 0;
37
+ const core_1 = require("@credo-ts/core");
38
+ const react_1 = require("react");
39
+ const React = __importStar(require("react"));
40
+ const recordUtils_1 = require("./recordUtils");
41
+ const BasicMessageContext = (0, react_1.createContext)(undefined);
42
+ const useBasicMessages = () => {
43
+ const basicMessageContext = (0, react_1.useContext)(BasicMessageContext);
44
+ if (!basicMessageContext) {
45
+ throw new Error('useBasicMessages must be used within a BasicMessageContextProvider');
46
+ }
47
+ return basicMessageContext;
48
+ };
49
+ exports.useBasicMessages = useBasicMessages;
50
+ const useBasicMessagesByConnectionId = (connectionId) => {
51
+ const { records: basicMessages } = (0, exports.useBasicMessages)();
52
+ const messages = (0, react_1.useMemo)(() => basicMessages.filter((m) => m.connectionId === connectionId), [basicMessages, connectionId]);
53
+ return messages;
54
+ };
55
+ exports.useBasicMessagesByConnectionId = useBasicMessagesByConnectionId;
56
+ const BasicMessageProvider = ({ agent, children }) => {
57
+ const [state, setState] = (0, react_1.useState)({
58
+ records: [],
59
+ loading: true,
60
+ });
61
+ const setInitialState = (0, react_1.useCallback)(async () => {
62
+ const records = await agent.basicMessages.findAllByQuery({});
63
+ setState({ records, loading: false });
64
+ }, [agent]);
65
+ (0, react_1.useEffect)(() => {
66
+ setInitialState();
67
+ }, [setInitialState]);
68
+ (0, react_1.useEffect)(() => {
69
+ if (state.loading)
70
+ return;
71
+ const basicMessageAdded$ = (0, recordUtils_1.recordsAddedByType)(agent, core_1.BasicMessageRecord).subscribe((record) => setState((0, recordUtils_1.addRecord)(record, state)));
72
+ const basicMessageUpdated$ = (0, recordUtils_1.recordsUpdatedByType)(agent, core_1.BasicMessageRecord).subscribe((record) => setState((0, recordUtils_1.updateRecord)(record, state)));
73
+ const basicMessageRemoved$ = (0, recordUtils_1.recordsRemovedByType)(agent, core_1.BasicMessageRecord).subscribe((record) => setState((0, recordUtils_1.removeRecord)(record, state)));
74
+ return () => {
75
+ basicMessageAdded$ === null || basicMessageAdded$ === void 0 ? void 0 : basicMessageAdded$.unsubscribe();
76
+ basicMessageUpdated$ === null || basicMessageUpdated$ === void 0 ? void 0 : basicMessageUpdated$.unsubscribe();
77
+ basicMessageRemoved$ === null || basicMessageRemoved$ === void 0 ? void 0 : basicMessageRemoved$.unsubscribe();
78
+ };
79
+ }, [state, agent]);
80
+ return React.createElement(BasicMessageContext.Provider, { value: state }, children);
81
+ };
82
+ exports.default = BasicMessageProvider;
83
+ //# sourceMappingURL=BasicMessageProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BasicMessageProvider.js","sourceRoot":"","sources":["../src/BasicMessageProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,yCAAmD;AACnD,iCAA4F;AAC5F,6CAA8B;AAE9B,+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 +1,24 @@
1
+ import type { Agent, DidExchangeState, ConnectionType } from '@credo-ts/core';
2
+ import type { PropsWithChildren } from 'react';
3
+ import { ConnectionRecord } from '@credo-ts/core';
4
+ import * as React from 'react';
5
+ export type UseConnectionsOptions = {
6
+ excludedTypes?: Array<ConnectionType | string>;
7
+ connectionState?: DidExchangeState;
8
+ };
9
+ /**
10
+ * This method retrieves the connection context for the current agent.
11
+ * From this you can access all connection records for the agent.
12
+ * @param options options for useConnections hook, lets us filter out specific types and limit states
13
+ * @returns a connection context containing information about the agents connections
14
+ */
15
+ export declare const useConnections: (options?: UseConnectionsOptions) => {
16
+ records: ConnectionRecord[];
17
+ loading?: boolean | undefined;
18
+ };
19
+ export declare const useConnectionById: (id: string) => ConnectionRecord | undefined;
20
+ interface Props {
21
+ agent: Agent;
22
+ }
23
+ declare const ConnectionProvider: React.FC<PropsWithChildren<Props>>;
24
+ export default ConnectionProvider;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.useConnectionById = exports.useConnections = void 0;
37
+ const core_1 = require("@credo-ts/core");
38
+ const react_1 = require("react");
39
+ const React = __importStar(require("react"));
40
+ const recordUtils_1 = require("./recordUtils");
41
+ const ConnectionContext = (0, react_1.createContext)(undefined);
42
+ /**
43
+ * This method retrieves the connection context for the current agent.
44
+ * From this you can access all connection records for the agent.
45
+ * @param options options for useConnections hook, lets us filter out specific types and limit states
46
+ * @returns a connection context containing information about the agents connections
47
+ */
48
+ const useConnections = (options = {}) => {
49
+ const connectionContext = (0, react_1.useContext)(ConnectionContext);
50
+ let connections = connectionContext === null || connectionContext === void 0 ? void 0 : connectionContext.records;
51
+ connections = (0, react_1.useMemo)(() => {
52
+ if (!connections) {
53
+ throw new Error('useConnections must be used within a ConnectionContextProvider');
54
+ }
55
+ // do not filter if not filter options are provided to save on a loop
56
+ if (!options.connectionState && !options.excludedTypes)
57
+ return connections;
58
+ return connections.filter((record) => {
59
+ // By default we include this connection
60
+ // Filter by state (if connectionState is defined)
61
+ if (options.connectionState && options.connectionState !== record.state)
62
+ return false;
63
+ // Exclude records with certain connection types (if defined)
64
+ const recordTypes = record.connectionTypes;
65
+ if (options.excludedTypes && recordTypes && recordTypes.length !== 0) {
66
+ return recordTypes.some((connectionType) => { var _a; return !((_a = options.excludedTypes) === null || _a === void 0 ? void 0 : _a.includes(connectionType)); });
67
+ }
68
+ return true;
69
+ });
70
+ }, [connections, options.connectionState, options.excludedTypes]);
71
+ return Object.assign(Object.assign({}, connectionContext), { records: connections });
72
+ };
73
+ exports.useConnections = useConnections;
74
+ const useConnectionById = (id) => {
75
+ const { records: connections } = (0, exports.useConnections)();
76
+ return connections.find((c) => c.id === id);
77
+ };
78
+ exports.useConnectionById = useConnectionById;
79
+ const ConnectionProvider = ({ agent, children }) => {
80
+ const [state, setState] = (0, react_1.useState)({
81
+ records: [],
82
+ loading: true,
83
+ });
84
+ const setInitialState = (0, react_1.useCallback)(async () => {
85
+ const records = await agent.connections.getAll();
86
+ setState({ records, loading: false });
87
+ }, [agent]);
88
+ (0, react_1.useEffect)(() => {
89
+ setInitialState();
90
+ }, [setInitialState]);
91
+ (0, react_1.useEffect)(() => {
92
+ if (state.loading)
93
+ return;
94
+ const connectionAdded$ = (0, recordUtils_1.recordsAddedByType)(agent, core_1.ConnectionRecord).subscribe((record) => setState((0, recordUtils_1.addRecord)(record, state)));
95
+ const connectionUpdated$ = (0, recordUtils_1.recordsUpdatedByType)(agent, core_1.ConnectionRecord).subscribe((record) => setState((0, recordUtils_1.updateRecord)(record, state)));
96
+ const connectionRemoved$ = (0, recordUtils_1.recordsRemovedByType)(agent, core_1.ConnectionRecord).subscribe((record) => setState((0, recordUtils_1.removeRecord)(record, state)));
97
+ return () => {
98
+ connectionAdded$.unsubscribe();
99
+ connectionUpdated$.unsubscribe();
100
+ connectionRemoved$.unsubscribe();
101
+ };
102
+ }, [state, agent]);
103
+ return React.createElement(ConnectionContext.Provider, { value: state }, children);
104
+ };
105
+ exports.default = ConnectionProvider;
106
+ //# sourceMappingURL=ConnectionProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConnectionProvider.js","sourceRoot":"","sources":["../src/ConnectionProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,yCAAiD;AACjD,iCAA4F;AAC5F,6CAA8B;AAE9B,+CAOsB;AAOtB,MAAM,iBAAiB,GAAG,IAAA,qBAAa,EAA6C,SAAS,CAAC,CAAA;AAE9F;;;;;GAKG;AACI,MAAM,cAAc,GAAG,CAAC,UAAiC,EAAE,EAAE,EAAE;IACpE,MAAM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EAAE,KAAK,CAAC,CAAC,CACtC,CAAA;QAED,MAAM,kBAAkB,GAAG,IAAA,kCAAoB,EAAC,KAAK,EAAE,uBAAgB,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,EAAE,EAAE,CAC5F,QAAQ,CAAC,IAAA,0BAAY,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CACtC,CAAA;QAED,OAAO,GAAG,EAAE;YACV,gBAAgB,CAAC,WAAW,EAAE,CAAA;YAC9B,kBAAkB,CAAC,WAAW,EAAE,CAAA;YAChC,kBAAkB,CAAC,WAAW,EAAE,CAAA;QAClC,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;IAElB,OAAO,oBAAC,iBAAiB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAA8B,CAAA;AAC1F,CAAC,CAAA;AAED,kBAAe,kBAAkB,CAAA"}
@@ -0,0 +1,19 @@
1
+ import type { Agent } from '@credo-ts/core';
2
+ import type { Awaited } from '@credo-ts/core/build/types';
3
+ import type { PropsWithChildren } from 'react';
4
+ import React from 'react';
5
+ type FormatReturn = Awaited<ReturnType<Agent['credentials']['getFormatData']>>;
6
+ export type CredentialFormatData = FormatReturn & {
7
+ id: string;
8
+ };
9
+ type FormattedDataState = {
10
+ formattedData: Array<CredentialFormatData>;
11
+ loading: boolean;
12
+ };
13
+ export declare const useCredentialsFormatData: () => FormattedDataState;
14
+ export declare const useCredentialFormatDataById: (id: string) => CredentialFormatData | undefined;
15
+ interface Props {
16
+ agent: Agent;
17
+ }
18
+ declare const CredentialFormatDataProvider: React.FC<PropsWithChildren<Props>>;
19
+ export default CredentialFormatDataProvider;
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.useCredentialFormatDataById = exports.useCredentialsFormatData = void 0;
37
+ const core_1 = require("@credo-ts/core");
38
+ const react_1 = __importStar(require("react"));
39
+ const recordUtils_1 = require("./recordUtils");
40
+ const addRecord = (record, state) => {
41
+ const newRecordsState = [...state.formattedData];
42
+ newRecordsState.unshift(record);
43
+ return {
44
+ loading: state.loading,
45
+ formattedData: newRecordsState,
46
+ };
47
+ };
48
+ const updateRecord = (record, state) => {
49
+ const newRecordsState = [...state.formattedData];
50
+ const index = newRecordsState.findIndex((r) => r.id === record.id);
51
+ if (index > -1) {
52
+ newRecordsState[index] = record;
53
+ }
54
+ return {
55
+ loading: state.loading,
56
+ formattedData: newRecordsState,
57
+ };
58
+ };
59
+ const removeRecord = (recordId, state) => {
60
+ const newRecordsState = state.formattedData.filter((r) => r.id !== recordId);
61
+ return {
62
+ loading: state.loading,
63
+ formattedData: newRecordsState,
64
+ };
65
+ };
66
+ const CredentialFormatDataContext = (0, react_1.createContext)(undefined);
67
+ const useCredentialsFormatData = () => {
68
+ const credentialFormatDataContext = (0, react_1.useContext)(CredentialFormatDataContext);
69
+ if (!credentialFormatDataContext) {
70
+ throw new Error('useCredentialFormatData must be used within a CredentialFormatDataContextProvider');
71
+ }
72
+ return credentialFormatDataContext;
73
+ };
74
+ exports.useCredentialsFormatData = useCredentialsFormatData;
75
+ const useCredentialFormatDataById = (id) => {
76
+ const { formattedData } = (0, exports.useCredentialsFormatData)();
77
+ return formattedData.find((c) => c.id === id);
78
+ };
79
+ exports.useCredentialFormatDataById = useCredentialFormatDataById;
80
+ const CredentialFormatDataProvider = ({ agent, children }) => {
81
+ const [state, setState] = (0, react_1.useState)({
82
+ formattedData: [],
83
+ loading: true,
84
+ });
85
+ const fetchCredentialInformation = async (agent, record) => {
86
+ const formatData = await agent.credentials.getFormatData(record.id);
87
+ return Object.assign(Object.assign({}, formatData), { id: record.id });
88
+ };
89
+ const setInitialState = (0, react_1.useCallback)(async () => {
90
+ const records = await agent.credentials.getAll();
91
+ const formattedData = [];
92
+ for (const record of records) {
93
+ formattedData.push(await fetchCredentialInformation(agent, record));
94
+ }
95
+ setState({ formattedData, loading: false });
96
+ }, [agent]);
97
+ (0, react_1.useEffect)(() => {
98
+ void setInitialState();
99
+ }, [setInitialState]);
100
+ (0, react_1.useEffect)(() => {
101
+ if (state.loading)
102
+ return;
103
+ const credentialAdded$ = (0, recordUtils_1.recordsAddedByType)(agent, core_1.CredentialExchangeRecord).subscribe(async (record) => {
104
+ const formatData = await fetchCredentialInformation(agent, record);
105
+ setState(addRecord(formatData, state));
106
+ });
107
+ const credentialUpdate$ = (0, recordUtils_1.recordsUpdatedByType)(agent, core_1.CredentialExchangeRecord).subscribe(async (record) => {
108
+ const formatData = await fetchCredentialInformation(agent, record);
109
+ setState(updateRecord(formatData, state));
110
+ });
111
+ const credentialRemove$ = (0, recordUtils_1.recordsRemovedByType)(agent, core_1.CredentialExchangeRecord).subscribe((record) => setState(removeRecord(record.id, state)));
112
+ return () => {
113
+ credentialAdded$.unsubscribe();
114
+ credentialUpdate$.unsubscribe();
115
+ credentialRemove$.unsubscribe();
116
+ };
117
+ }, [state, agent]);
118
+ return react_1.default.createElement(CredentialFormatDataContext.Provider, { value: state }, children);
119
+ };
120
+ exports.default = CredentialFormatDataProvider;
121
+ //# sourceMappingURL=CredentialFormatDataProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CredentialFormatDataProvider.js","sourceRoot":"","sources":["../src/CredentialFormatDataProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,yCAAyD;AACzD,+CAA0F;AAE1F,+CAA8F;AAa9F,MAAM,SAAS,GAAG,CAAC,MAA4B,EAAE,KAAyB,EAAsB,EAAE;IAChG,MAAM,eAAe,GAAG,CAAC,GAAG,KAAK,CAAC,aAAa,CAAC,CAAA;IAChD,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAE/B,OAAO;QACL,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,aAAa,EAAE,eAAe;KAC/B,CAAA;AACH,CAAC,CAAA;AAED,MAAM,YAAY,GAAG,CAAC,MAA4B,EAAE,KAAyB,EAAsB,EAAE;IACnG,MAAM,eAAe,GAAG,CAAC,GAAG,KAAK,CAAC,aAAa,CAAC,CAAA;IAChD,MAAM,KAAK,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,MAAM,CAAC,EAAE,CAAC,CAAA;IAElE,IAAI,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;QACf,eAAe,CAAC,KAAK,CAAC,GAAG,MAAM,CAAA;IACjC,CAAC;IAED,OAAO;QACL,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,aAAa,EAAE,eAA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AU,GAAG,MAAM,0BAA0B,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;YAClE,QAAQ,CAAC,YAAY,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAA;QAC3C,CAAC,CACF,CAAA;QAED,MAAM,iBAAiB,GAAG,IAAA,kCAAoB,EAAC,KAAK,EAAE,+BAAwB,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,EAAE,EAAE,CACnG,QAAQ,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CACzC,CAAA;QAED,OAAO,GAAG,EAAE;YACV,gBAAgB,CAAC,WAAW,EAAE,CAAA;YAC9B,iBAAiB,CAAC,WAAW,EAAE,CAAA;YAC/B,iBAAiB,CAAC,WAAW,EAAE,CAAA;QACjC,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;IAElB,OAAO,8BAAC,2BAA2B,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAAwC,CAAA;AAC9G,CAAC,CAAA;AAED,kBAAe,4BAA4B,CAAA"}
@@ -0,0 +1,15 @@
1
+ import type { RecordsState } from './recordUtils';
2
+ import type { Agent, CredentialState } from '@credo-ts/core';
3
+ import type { PropsWithChildren } from 'react';
4
+ import { CredentialExchangeRecord } from '@credo-ts/core';
5
+ import * as React from 'react';
6
+ export declare const useCredentials: () => RecordsState<CredentialExchangeRecord>;
7
+ export declare const useCredentialsByConnectionId: (connectionId: string) => CredentialExchangeRecord[];
8
+ export declare const useCredentialById: (id: string) => CredentialExchangeRecord | undefined;
9
+ export declare const useCredentialByState: (state: CredentialState | CredentialState[]) => CredentialExchangeRecord[];
10
+ export declare const useCredentialNotInState: (state: CredentialState | CredentialState[]) => CredentialExchangeRecord[];
11
+ interface Props {
12
+ agent: Agent;
13
+ }
14
+ declare const CredentialProvider: React.FC<PropsWithChildren<Props>>;
15
+ export default CredentialProvider;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.useCredentialNotInState = exports.useCredentialByState = exports.useCredentialById = exports.useCredentialsByConnectionId = exports.useCredentials = void 0;
37
+ const core_1 = require("@credo-ts/core");
38
+ const react_1 = require("react");
39
+ const React = __importStar(require("react"));
40
+ const recordUtils_1 = require("./recordUtils");
41
+ const CredentialContext = (0, react_1.createContext)(undefined);
42
+ const useCredentials = () => {
43
+ const credentialContext = (0, react_1.useContext)(CredentialContext);
44
+ if (!credentialContext) {
45
+ throw new Error('useCredentials must be used within a CredentialContextProvider');
46
+ }
47
+ return credentialContext;
48
+ };
49
+ exports.useCredentials = useCredentials;
50
+ const useCredentialsByConnectionId = (connectionId) => {
51
+ const { records: credentials } = (0, exports.useCredentials)();
52
+ return (0, react_1.useMemo)(() => credentials.filter((credential) => credential.connectionId === connectionId), [credentials, connectionId]);
53
+ };
54
+ exports.useCredentialsByConnectionId = useCredentialsByConnectionId;
55
+ const useCredentialById = (id) => {
56
+ const { records: credentials } = (0, exports.useCredentials)();
57
+ return credentials.find((c) => c.id === id);
58
+ };
59
+ exports.useCredentialById = useCredentialById;
60
+ const useCredentialByState = (state) => {
61
+ const states = (0, react_1.useMemo)(() => (typeof state === 'string' ? [state] : state), [state]);
62
+ const { records: credentials } = (0, exports.useCredentials)();
63
+ const filteredCredentials = (0, react_1.useMemo)(() => credentials.filter((r) => states.includes(r.state)), [credentials, states]);
64
+ return filteredCredentials;
65
+ };
66
+ exports.useCredentialByState = useCredentialByState;
67
+ const useCredentialNotInState = (state) => {
68
+ const states = (0, react_1.useMemo)(() => (typeof state === 'string' ? [state] : state), [state]);
69
+ const { records: credentials } = (0, exports.useCredentials)();
70
+ const filteredCredentials = (0, react_1.useMemo)(() => credentials.filter((r) => !states.includes(r.state)), [credentials, states]);
71
+ return filteredCredentials;
72
+ };
73
+ exports.useCredentialNotInState = useCredentialNotInState;
74
+ const CredentialProvider = ({ agent, children }) => {
75
+ const [state, setState] = (0, react_1.useState)({
76
+ records: [],
77
+ loading: true,
78
+ });
79
+ const setInitialState = (0, react_1.useCallback)(async () => {
80
+ const records = await agent.credentials.getAll();
81
+ setState({ records, loading: false });
82
+ }, [agent]);
83
+ (0, react_1.useEffect)(() => {
84
+ setInitialState();
85
+ }, [setInitialState]);
86
+ (0, react_1.useEffect)(() => {
87
+ if (state.loading)
88
+ return;
89
+ const credentialAdded$ = (0, recordUtils_1.recordsAddedByType)(agent, core_1.CredentialExchangeRecord).subscribe((record) => setState((0, recordUtils_1.addRecord)(record, state)));
90
+ const credentialUpdated$ = (0, recordUtils_1.recordsUpdatedByType)(agent, core_1.CredentialExchangeRecord).subscribe((record) => setState((0, recordUtils_1.updateRecord)(record, state)));
91
+ const credentialRemoved$ = (0, recordUtils_1.recordsRemovedByType)(agent, core_1.CredentialExchangeRecord).subscribe((record) => setState((0, recordUtils_1.removeRecord)(record, state)));
92
+ return () => {
93
+ credentialAdded$ === null || credentialAdded$ === void 0 ? void 0 : credentialAdded$.unsubscribe();
94
+ credentialUpdated$ === null || credentialUpdated$ === void 0 ? void 0 : credentialUpdated$.unsubscribe();
95
+ credentialRemoved$ === null || credentialRemoved$ === void 0 ? void 0 : credentialRemoved$.unsubscribe();
96
+ };
97
+ }, [state, agent]);
98
+ return React.createElement(CredentialContext.Provider, { value: state }, children);
99
+ };
100
+ exports.default = CredentialProvider;
101
+ //# sourceMappingURL=CredentialProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CredentialProvider.js","sourceRoot":"","sources":["../src/CredentialProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,yCAAyD;AACzD,iCAA4F;AAC5F,6CAA8B;AAE9B,+CAOsB;AAEtB,MAAM,iBAAiB,GAAG,IAAA,qBAAa,EAAqD,SAAS,CAAC,CAAA;AAE/F,MAAM,cAAc,GAAG,GAAG,EAAE;IACjC,MAAM,iBAAiB,GAAG,IAAA,kBAAU,EAAC,iBAAiB,CAAC,CAAA;IACvD,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAA;IACnF,CAAC;IACD,OAAO,iBAAiB,CAAA;AAC1B,CAAC,CAAA;AANY,QAAA,cAAc,kBAM1B;AAEM,MAAM,4BAA4B,GAAG,CAAC,YAAoB,EAA8B,EAAE;IAC/F,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,IAAA,sBAAc,GAAE,CAAA;IACjD,OAAO,IAAA,eAAO,EACZ,GAAG,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,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