Skip to content

Commit 2ce05f7

Browse files
committed
Add useChildren hook and Store.getChildren method
1 parent 8f0df76 commit 2ce05f7

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

lib/src/resource.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,21 @@ export class Resource {
176176
return this.commitBuilder;
177177
}
178178

179+
/** Returns the subject of the list of Children */
180+
public getChildrenCollection(): string | undefined {
181+
// We create a collection that contains all children of the current Subject
182+
const generatedCollectionURL = new URL(this.subject);
183+
generatedCollectionURL.pathname = '/collections';
184+
generatedCollectionURL.searchParams.set('property', properties.parent);
185+
generatedCollectionURL.searchParams.set('value', this.subject);
186+
187+
const childrenCollection = generatedCollectionURL.toString();
188+
189+
console.log('Children collection', childrenCollection);
190+
191+
return childrenCollection;
192+
}
193+
179194
/** Returns the subject URL of the Resource */
180195
public getSubject(): string {
181196
return this.subject;

lib/src/websockets.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export async function authenticate(client: WebSocket, store: Store) {
7575
!client.url.startsWith('ws://localhost:') &&
7676
agent?.subject?.startsWith('http://localhost')
7777
) {
78-
console.warn("Can't authenticate localhost Agent over websocket");
78+
console.warn(
79+
"Can't authenticate localhost Agent over websocket with remote server, because the server will nog be able to retrieve your Agent and verify your public key.",
80+
);
7981

8082
return;
8183
}

react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
export * from './hooks.js';
2626
export * from './useServerURL.js';
2727
export * from './useCurrentAgent.js';
28+
export * from './useChildren.js';
2829
export * from './useDebounce.js';
2930
export * from './useImporter.js';
3031
export * from './useLocalStorage.js';

react/src/useChildren.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Sorry for the name of this
2+
import { properties, Resource } from '@tomic/lib';
3+
import { useArray, useResource } from './index.js';
4+
5+
/** Creates a Collection and returns all children */
6+
export const useChildren = (resource: Resource) => {
7+
const childrenUrl = resource.getChildrenCollection();
8+
const childrenCollection = useResource(childrenUrl);
9+
const [children] = useArray(
10+
childrenCollection,
11+
properties.collection.members,
12+
);
13+
14+
return children;
15+
};

0 commit comments

Comments
 (0)