( resource: Resource, agent?: string, )
| 459 | * don't explicitly pass an Agent URL, it will select the current Agent set by the store. |
| 460 | */ |
| 461 | export 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 |
no test coverage detected