Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | 66x 66x 1385x 30x 8x 22x 13x 9x 1385x 1385x 1385x 66x 1385x 29x 29x 29x 1385x 30x 30x 3x 1385x 66x 66x 30x 8x 22x 22x 22x 13x 9x 8x 8x 8x 6x 6x 6x 7x 3x 3x 4x 3x 3x 6x 6x 9x 9x | import { fetchCanUpdateDeleteObjects } from "@utils/FetchUtils"; import React, { useEffect, useMemo } from "react"; export type DomainPermitState = { canUpdate: boolean, canUpdateErrorMsg: string | undefined, canDelete: boolean, canDeleteErrorMsg: string | undefined }; const defaultPermission: DomainPermitState = { canUpdateErrorMsg: undefined, canUpdate: false, canDeleteErrorMsg: undefined, canDelete: false, }; export const useDomainPermit = (domainName?: string, id?: number | string): DomainPermitState => { const idNumber = useMemo(() => { if (!id) { return undefined; } if (typeof id === 'number') { return id; } return parseInt(id); }, [id]); const ids = useMemo(() => idNumber ? [idNumber] : [], [idNumber]); const record = useDomainPermits(domainName, ids); return idNumber ? record[idNumber] ?? defaultPermission : defaultPermission; }; export const useDomainPermits = (domainName: string | undefined, ids: number[]): Record<number, DomainPermitState> => { const [stateRecord, setStateRecord] = React.useState<Record<number, DomainPermitState>>(() => { const newState: Record<number, DomainPermitState> = {}; ids.forEach(id => newState[id] = defaultPermission); return newState; }); useEffect(() => { Iif (!domainName) { return; } fetchDomainPermitStates(domainName, ids) .then(setStateRecord) .catch(e => console.error(`Error fetching permissions for [${domainName}], ids [${ids}]: `, JSON.stringify(e))); }, [domainName, ids]); return stateRecord; }; const cache: Map<string, Promise<Record<number, DomainPermitState>>> = new Map(); export const fetchDomainPermitStates = async (domainName: string, ids: number[]): Promise<Record<number, DomainPermitState>> => { if (ids.length === 0) { return {}; } const cacheKey = `${domainName}:${ids.join(',')}`; const cached = cache.get(cacheKey); if (cached) { return cached; } const promise = fetchCanUpdateDeleteObjects(domainName, ids).then(actionRecord => { const stateRecord: Record<number, DomainPermitState> = {}; for (const id of ids) { const actions = actionRecord[id]; const state: DomainPermitState = { ...defaultPermission }; stateRecord[id] = state; actions?.forEach(action => { if (action.name === 'update') { Iif (action.message != null && action.message !== '') { state.canUpdateErrorMsg = action.message; state.canUpdate = false; } else { state.canUpdate = action.enable; } } else if (action.name === 'delete') { Iif (action.message != null && action.message !== '') { state.canDeleteErrorMsg = action.message; state.canDelete = false; } else { state.canDelete = action.enable; } } }); } setTimeout(() => cache.delete(cacheKey), 1000); return stateRecord; }); cache.set(cacheKey, promise); return promise; }; |