@linkedrecords/react 0.1.0

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.
@@ -0,0 +1,29 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import LinkedRecords from '@linkedrecords/browser';
4
+ import * as react_jsx_runtime from 'react/jsx-runtime';
5
+
6
+ interface LinkedRecordsContextType {
7
+ lr: LinkedRecords;
8
+ }
9
+ declare const LinkedRecordsContext: react.Context<LinkedRecordsContextType | undefined>;
10
+
11
+ interface LinkedRecordsProviderProps {
12
+ children: ReactNode;
13
+ serverUrl: string;
14
+ }
15
+ declare function LinkedRecordsProvider({ children, serverUrl }: LinkedRecordsProviderProps): react_jsx_runtime.JSX.Element;
16
+
17
+ interface KVValue {
18
+ [key: string]: KVValue | string | boolean | number | undefined;
19
+ }
20
+ declare function useKeyValueAttributes(query: any[]): KVValue[];
21
+
22
+ declare function useLinkedRecords(): LinkedRecordsContextType;
23
+
24
+ type UserInfo = {
25
+ email: string;
26
+ };
27
+ declare function useUserInfo(): UserInfo | null;
28
+
29
+ export { LinkedRecordsContext, type LinkedRecordsContextType, LinkedRecordsProvider, useKeyValueAttributes, useLinkedRecords, useUserInfo };
@@ -0,0 +1,29 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import LinkedRecords from '@linkedrecords/browser';
4
+ import * as react_jsx_runtime from 'react/jsx-runtime';
5
+
6
+ interface LinkedRecordsContextType {
7
+ lr: LinkedRecords;
8
+ }
9
+ declare const LinkedRecordsContext: react.Context<LinkedRecordsContextType | undefined>;
10
+
11
+ interface LinkedRecordsProviderProps {
12
+ children: ReactNode;
13
+ serverUrl: string;
14
+ }
15
+ declare function LinkedRecordsProvider({ children, serverUrl }: LinkedRecordsProviderProps): react_jsx_runtime.JSX.Element;
16
+
17
+ interface KVValue {
18
+ [key: string]: KVValue | string | boolean | number | undefined;
19
+ }
20
+ declare function useKeyValueAttributes(query: any[]): KVValue[];
21
+
22
+ declare function useLinkedRecords(): LinkedRecordsContextType;
23
+
24
+ type UserInfo = {
25
+ email: string;
26
+ };
27
+ declare function useUserInfo(): UserInfo | null;
28
+
29
+ export { LinkedRecordsContext, type LinkedRecordsContextType, LinkedRecordsProvider, useKeyValueAttributes, useLinkedRecords, useUserInfo };
package/dist/index.js ADDED
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.tsx
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ LinkedRecordsContext: () => LinkedRecordsContext,
34
+ LinkedRecordsProvider: () => LinkedRecordsProvider,
35
+ useKeyValueAttributes: () => useKeyValueAttributes,
36
+ useLinkedRecords: () => useLinkedRecords,
37
+ useUserInfo: () => useUserInfo
38
+ });
39
+ module.exports = __toCommonJS(index_exports);
40
+
41
+ // src/linkedRecordsContext.ts
42
+ var import_react = require("react");
43
+ var LinkedRecordsContext = (0, import_react.createContext)(void 0);
44
+
45
+ // src/LinkedRecordsProvider.tsx
46
+ var import_react2 = require("react");
47
+ var import_browser = __toESM(require("@linkedrecords/browser"));
48
+ var import_jsx_runtime = require("react/jsx-runtime");
49
+ function LinkedRecordsProvider({ children, serverUrl }) {
50
+ const lr = import_browser.default.getPublicClient(serverUrl);
51
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LinkedRecordsContext.Provider, { value: { lr }, children });
52
+ }
53
+
54
+ // src/useAttributes.ts
55
+ var import_react4 = require("react");
56
+
57
+ // src/useLinkedRecords.ts
58
+ var import_react3 = require("react");
59
+ function useLinkedRecords() {
60
+ const context = (0, import_react3.useContext)(LinkedRecordsContext);
61
+ if (!context) {
62
+ throw new Error("useLinkedRecords must be used within a LinkedRecordsProvider");
63
+ }
64
+ return context;
65
+ }
66
+
67
+ // src/useAttributes.ts
68
+ var import_browser2 = require("@linkedrecords/browser");
69
+ function useKeyValueAttributes(query) {
70
+ const { lr } = useLinkedRecords();
71
+ const [attributes, setAttributes] = (0, import_react4.useState)([]);
72
+ (0, import_react4.useEffect)(() => {
73
+ const unsubscribeFnPromise = new Promise((resolve) => {
74
+ const checkActorId = () => {
75
+ if (lr.actorId !== void 0) {
76
+ resolve();
77
+ } else {
78
+ setTimeout(checkActorId, 50);
79
+ }
80
+ };
81
+ checkActorId();
82
+ }).then(() => {
83
+ const queryUnsubscribe = lr.Attribute.subscribeToQuery({
84
+ attributes: [
85
+ ["$it", "$hasDataType", import_browser2.KeyValueAttribute],
86
+ ...query
87
+ ]
88
+ }, async ({ attributes: attributes2 }) => {
89
+ const values = await Promise.all(attributes2.map(async (a) => ({
90
+ _id: a.id,
91
+ ...await a.getValue()
92
+ })));
93
+ setAttributes(values);
94
+ attributes2.forEach((a) => {
95
+ a.subscribe(async () => {
96
+ const newValue = await a.getValue();
97
+ setAttributes((prev) => prev.map(
98
+ (v) => v._id === a.id ? { _id: a.id, ...newValue } : v
99
+ ));
100
+ });
101
+ });
102
+ });
103
+ return () => {
104
+ queryUnsubscribe.then((unsubscribe) => unsubscribe());
105
+ };
106
+ });
107
+ return () => {
108
+ unsubscribeFnPromise.then((fn) => fn());
109
+ };
110
+ }, [lr.Attribute, setAttributes]);
111
+ return attributes;
112
+ }
113
+
114
+ // src/useUserInfo.ts
115
+ var import_react5 = require("react");
116
+ function useUserInfo() {
117
+ const { lr } = useLinkedRecords();
118
+ const [userInfo, setUserInfo] = (0, import_react5.useState)(null);
119
+ (0, import_react5.useEffect)(() => {
120
+ lr.getCurrentUserEmail().then((email) => setUserInfo({ email })).catch(() => setUserInfo(null));
121
+ }, [lr]);
122
+ return userInfo;
123
+ }
124
+ // Annotate the CommonJS export names for ESM import in node:
125
+ 0 && (module.exports = {
126
+ LinkedRecordsContext,
127
+ LinkedRecordsProvider,
128
+ useKeyValueAttributes,
129
+ useLinkedRecords,
130
+ useUserInfo
131
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,90 @@
1
+ // src/linkedRecordsContext.ts
2
+ import { createContext } from "react";
3
+ var LinkedRecordsContext = createContext(void 0);
4
+
5
+ // src/LinkedRecordsProvider.tsx
6
+ import "react";
7
+ import LinkedRecords from "@linkedrecords/browser";
8
+ import { jsx } from "react/jsx-runtime";
9
+ function LinkedRecordsProvider({ children, serverUrl }) {
10
+ const lr = LinkedRecords.getPublicClient(serverUrl);
11
+ return /* @__PURE__ */ jsx(LinkedRecordsContext.Provider, { value: { lr }, children });
12
+ }
13
+
14
+ // src/useAttributes.ts
15
+ import { useEffect, useState } from "react";
16
+
17
+ // src/useLinkedRecords.ts
18
+ import { useContext } from "react";
19
+ function useLinkedRecords() {
20
+ const context = useContext(LinkedRecordsContext);
21
+ if (!context) {
22
+ throw new Error("useLinkedRecords must be used within a LinkedRecordsProvider");
23
+ }
24
+ return context;
25
+ }
26
+
27
+ // src/useAttributes.ts
28
+ import { KeyValueAttribute } from "@linkedrecords/browser";
29
+ function useKeyValueAttributes(query) {
30
+ const { lr } = useLinkedRecords();
31
+ const [attributes, setAttributes] = useState([]);
32
+ useEffect(() => {
33
+ const unsubscribeFnPromise = new Promise((resolve) => {
34
+ const checkActorId = () => {
35
+ if (lr.actorId !== void 0) {
36
+ resolve();
37
+ } else {
38
+ setTimeout(checkActorId, 50);
39
+ }
40
+ };
41
+ checkActorId();
42
+ }).then(() => {
43
+ const queryUnsubscribe = lr.Attribute.subscribeToQuery({
44
+ attributes: [
45
+ ["$it", "$hasDataType", KeyValueAttribute],
46
+ ...query
47
+ ]
48
+ }, async ({ attributes: attributes2 }) => {
49
+ const values = await Promise.all(attributes2.map(async (a) => ({
50
+ _id: a.id,
51
+ ...await a.getValue()
52
+ })));
53
+ setAttributes(values);
54
+ attributes2.forEach((a) => {
55
+ a.subscribe(async () => {
56
+ const newValue = await a.getValue();
57
+ setAttributes((prev) => prev.map(
58
+ (v) => v._id === a.id ? { _id: a.id, ...newValue } : v
59
+ ));
60
+ });
61
+ });
62
+ });
63
+ return () => {
64
+ queryUnsubscribe.then((unsubscribe) => unsubscribe());
65
+ };
66
+ });
67
+ return () => {
68
+ unsubscribeFnPromise.then((fn) => fn());
69
+ };
70
+ }, [lr.Attribute, setAttributes]);
71
+ return attributes;
72
+ }
73
+
74
+ // src/useUserInfo.ts
75
+ import { useEffect as useEffect2, useState as useState2 } from "react";
76
+ function useUserInfo() {
77
+ const { lr } = useLinkedRecords();
78
+ const [userInfo, setUserInfo] = useState2(null);
79
+ useEffect2(() => {
80
+ lr.getCurrentUserEmail().then((email) => setUserInfo({ email })).catch(() => setUserInfo(null));
81
+ }, [lr]);
82
+ return userInfo;
83
+ }
84
+ export {
85
+ LinkedRecordsContext,
86
+ LinkedRecordsProvider,
87
+ useKeyValueAttributes,
88
+ useLinkedRecords,
89
+ useUserInfo
90
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@linkedrecords/react",
3
+ "version": "0.1.0",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.js"
11
+ }
12
+ },
13
+ "scripts": {
14
+ "build": "tsup src/index.tsx --format cjs,esm --dts",
15
+ "prepare": "tsup src/index.tsx --format cjs,esm --dts"
16
+ },
17
+ "keywords": [],
18
+ "author": "Oliver Wolf",
19
+ "license": "AGPL-3.0",
20
+ "description": "",
21
+ "dependencies": {
22
+ "@linkedrecords/browser": "^0.1.0",
23
+ "react": "^19.1.1",
24
+ "react-dom": "^19.1.1"
25
+ },
26
+ "devDependencies": {
27
+ "@types/react": "^19.1.12",
28
+ "@types/react-dom": "^19.1.9",
29
+ "tsup": "^8.5.0",
30
+ "typescript": "^5.9.2"
31
+ }
32
+ }
@@ -0,0 +1,18 @@
1
+ import { type ReactNode } from 'react';
2
+ import LinkedRecords from '@linkedrecords/browser';
3
+ import { LinkedRecordsContext } from './linkedRecordsContext';
4
+
5
+ interface LinkedRecordsProviderProps {
6
+ children: ReactNode;
7
+ serverUrl: string;
8
+ }
9
+
10
+ export function LinkedRecordsProvider({ children, serverUrl }: LinkedRecordsProviderProps) {
11
+ const lr = LinkedRecords.getPublicClient(serverUrl);
12
+
13
+ return (
14
+ <LinkedRecordsContext.Provider value={{ lr }}>
15
+ {children}
16
+ </LinkedRecordsContext.Provider>
17
+ );
18
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,5 @@
1
+ export * from './linkedRecordsContext';
2
+ export * from './LinkedRecordsProvider';
3
+ export * from './useAttributes';
4
+ export * from './useLinkedRecords';
5
+ export * from './useUserInfo';
@@ -0,0 +1,8 @@
1
+ import { createContext } from 'react';
2
+ import type LinkedRecords from '@linkedrecords/browser';
3
+
4
+ export interface LinkedRecordsContextType {
5
+ lr: LinkedRecords;
6
+ }
7
+
8
+ export const LinkedRecordsContext = createContext<LinkedRecordsContextType | undefined>(undefined);
@@ -0,0 +1,63 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { useEffect, useState } from "react";
3
+ import { useLinkedRecords } from "./useLinkedRecords";
4
+ import { KeyValueAttribute } from '@linkedrecords/browser';
5
+
6
+ interface KVValue {
7
+ [key: string]: KVValue | string | boolean | number | undefined;
8
+ }
9
+
10
+ export function useKeyValueAttributes(query: any[]): KVValue[] {
11
+ const { lr } = useLinkedRecords();
12
+ const [ attributes, setAttributes ] = useState<KVValue[]>([]);
13
+
14
+ useEffect(() => {
15
+ const unsubscribeFnPromise = new Promise<void>((resolve) => {
16
+ const checkActorId = () => {
17
+ if (lr.actorId !== undefined) {
18
+ resolve();
19
+ } else {
20
+ setTimeout(checkActorId, 50);
21
+ }
22
+ };
23
+ checkActorId();
24
+ }).then(() => {
25
+ const queryUnsubscribe = lr.Attribute.subscribeToQuery({
26
+ attributes: [
27
+ ['$it', '$hasDataType', KeyValueAttribute],
28
+ ...query
29
+ ],
30
+ }, async ({ attributes }) => {
31
+ const values = await Promise.all(attributes.map(async (a) => ({
32
+ _id: a.id,
33
+ ...(await a.getValue()),
34
+ })));
35
+
36
+ setAttributes(values);
37
+
38
+ attributes.forEach((a) => {
39
+ a.subscribe(async () => {
40
+ const newValue = await a.getValue();
41
+
42
+ // Use functional update to avoid stale closure
43
+ setAttributes(prev => prev.map(v =>
44
+ v._id === a.id
45
+ ? { _id: a.id, ...newValue }
46
+ : v
47
+ ));
48
+ });
49
+ });
50
+ });
51
+
52
+ return () => {
53
+ queryUnsubscribe.then(unsubscribe => unsubscribe());
54
+ };
55
+ });
56
+
57
+ return () => {
58
+ unsubscribeFnPromise.then(fn => fn());
59
+ }
60
+ }, [ lr.Attribute, setAttributes ]);
61
+
62
+ return attributes;
63
+ }
@@ -0,0 +1,10 @@
1
+ import { useContext } from 'react';
2
+ import { LinkedRecordsContext } from './linkedRecordsContext';
3
+
4
+ export function useLinkedRecords() {
5
+ const context = useContext(LinkedRecordsContext);
6
+ if (!context) {
7
+ throw new Error('useLinkedRecords must be used within a LinkedRecordsProvider');
8
+ }
9
+ return context;
10
+ }
@@ -0,0 +1,19 @@
1
+ import { useEffect, useState } from "react";
2
+ import { useLinkedRecords } from "./useLinkedRecords";
3
+
4
+ type UserInfo = {
5
+ email: string,
6
+ }
7
+
8
+ export function useUserInfo() {
9
+ const { lr } = useLinkedRecords();
10
+ const [userInfo, setUserInfo] = useState<UserInfo | null>(null);
11
+
12
+ useEffect(() => {
13
+ lr.getCurrentUserEmail()
14
+ .then((email) => setUserInfo({ email }))
15
+ .catch(() => setUserInfo(null));
16
+ }, [lr]);
17
+
18
+ return userInfo;
19
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
4
+ "target": "ES2022",
5
+ "useDefineForClassFields": true,
6
+ "lib": ["ES2022", "DOM", "DOM.Iterable"],
7
+ "module": "ESNext",
8
+ "skipLibCheck": true,
9
+
10
+ /* Bundler mode */
11
+ "moduleResolution": "bundler",
12
+ "allowImportingTsExtensions": true,
13
+ "verbatimModuleSyntax": true,
14
+ "moduleDetection": "force",
15
+ "noEmit": true,
16
+ "jsx": "react-jsx",
17
+
18
+ /* Linting */
19
+ "strict": true,
20
+ "noUnusedLocals": true,
21
+ "noUnusedParameters": true,
22
+ "erasableSyntaxOnly": true,
23
+ "noFallthroughCasesInSwitch": true,
24
+ "noUncheckedSideEffectImports": true
25
+ },
26
+ "include": ["src"]
27
+ }