MCPcopy Create free account
hub / github.com/atomicdata-dev/atomic-data-browser / useCanWrite

Function useCanWrite

react/src/hooks.ts:461–496  ·  view source on GitHub ↗
(
  resource: Resource,
  agent?: string,
)

Source from the content-addressed store, hash-verified

459 * don't explicitly pass an Agent URL, it will select the current Agent set by the store.
460 */
461export function useCanWrite(
462 resource: Resource,
463 agent?: string,
464): [canWrite: boolean | null, message: string] {
465 const store = useStore();
466 const [canWrite, setCanWrite] = useState<boolean | null>(null);
467 const [msg, setMsg] = useState<string | null>(null);
468 const agentStore = store.getAgent();
469
470 // If the subject changes, make sure to change the resource!
471 useEffect(() => {
472 if (agent == undefined) {
473 agent = agentStore?.subject;
474 }
475 if (agent == undefined) {
476 setMsg('No Agent set');
477 setCanWrite(false);
478 return;
479 }
480 setMsg('Checking write rights...');
481 async function tryCanWrite() {
482 const [canWriteAsync, msg] = await resource.canWrite(store, agent);
483 setCanWrite(canWriteAsync);
484 if (canWriteAsync) {
485 setMsg(null);
486 } else {
487 setMsg(
488 "You don't have write rights in this resource or its parents: " + msg,
489 );
490 }
491 }
492 tryCanWrite();
493 }, [resource, agent, agentStore?.subject]);
494
495 return [canWrite, msg];
496}
497
498/**
499 * The context must be provided by wrapping a high level React element in

Callers 4

ResourceContextMenuFunction · 0.90
ShareRouteFunction · 0.90
DocumentPageFunction · 0.90
ElementEditFunction · 0.90

Calls 3

useStoreFunction · 0.85
tryCanWriteFunction · 0.85
getAgentMethod · 0.80

Tested by

no test coverage detected