mirror of
https://github.com/Qortal/qapp-core.git
synced 2025-06-14 17:41:20 +00:00
memorize hooks
This commit is contained in:
parent
a9822446da
commit
e910958b26
@ -3,28 +3,33 @@ import { useAuthStore } from "../state/auth";
|
|||||||
import { useAppStore } from "../state/app";
|
import { useAppStore } from "../state/app";
|
||||||
import { EnumCollisionStrength, hashWord } from "../utils/encryption";
|
import { EnumCollisionStrength, hashWord } from "../utils/encryption";
|
||||||
|
|
||||||
|
|
||||||
export const useAppInfo = (appName?: string, publicSalt?: string) => {
|
export const useAppInfo = (appName?: string, publicSalt?: string) => {
|
||||||
const setAppState = useAppStore((state)=> state.setAppState)
|
const setAppState = useAppStore((state) => state.setAppState);
|
||||||
const appNameHashed = useAppStore((state)=> state.appNameHashed)
|
const appNameHashed = useAppStore((state) => state.appNameHashed);
|
||||||
|
|
||||||
|
|
||||||
const handleAppInfoSetup = useCallback(async (name: string, salt: string)=> {
|
const handleAppInfoSetup = useCallback(async (name: string, salt: string) => {
|
||||||
const appNameHashed = await hashWord(name, EnumCollisionStrength.HIGH, salt)
|
const appNameHashed = await hashWord(
|
||||||
|
name,
|
||||||
|
EnumCollisionStrength.HIGH,
|
||||||
|
salt
|
||||||
|
);
|
||||||
setAppState({
|
setAppState({
|
||||||
appName: name,
|
appName: name,
|
||||||
publicSalt: salt,
|
publicSalt: salt,
|
||||||
appNameHashed
|
appNameHashed,
|
||||||
})
|
});
|
||||||
}, [])
|
}, []);
|
||||||
|
|
||||||
useEffect(()=> {
|
useEffect(() => {
|
||||||
if(appName && publicSalt){
|
if (appName && publicSalt) {
|
||||||
handleAppInfoSetup(appName, publicSalt)
|
handleAppInfoSetup(appName, publicSalt);
|
||||||
}
|
}
|
||||||
}, [appName, publicSalt, handleAppInfoSetup])
|
}, [appName, publicSalt, handleAppInfoSetup]);
|
||||||
return {
|
return useMemo(
|
||||||
appName,
|
() => ({
|
||||||
appNameHashed
|
appName,
|
||||||
};
|
appNameHashed,
|
||||||
|
}),
|
||||||
|
[appName, appNameHashed]
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,42 +1,72 @@
|
|||||||
import React, { useCallback } from "react";
|
import React, { useCallback, useMemo } from "react";
|
||||||
import { buildIdentifier, buildSearchPrefix, EnumCollisionStrength, hashWord } from "../utils/encryption";
|
import {
|
||||||
|
buildIdentifier,
|
||||||
|
buildSearchPrefix,
|
||||||
|
EnumCollisionStrength,
|
||||||
|
hashWord,
|
||||||
|
} from "../utils/encryption";
|
||||||
|
|
||||||
export const useIdentifiers = (publicSalt: string, appName: string) => {
|
export const useIdentifiers = (publicSalt: string, appName: string) => {
|
||||||
|
const buildIdentifierFunc = useCallback(
|
||||||
|
(entityType: string, parentId: string | null) => {
|
||||||
|
return buildIdentifier(appName, publicSalt, entityType, parentId);
|
||||||
|
},
|
||||||
|
[appName, publicSalt]
|
||||||
|
);
|
||||||
|
|
||||||
|
const buildSearchPrefixFunc = useCallback(
|
||||||
|
(entityType: string, parentId: string | null) => {
|
||||||
|
return buildSearchPrefix(appName, publicSalt, entityType, parentId);
|
||||||
|
},
|
||||||
|
[appName, publicSalt]
|
||||||
|
);
|
||||||
|
|
||||||
const buildIdentifierFunc = useCallback(( entityType: string,
|
const createSingleIdentifier = useCallback(
|
||||||
parentId: string | null)=> {
|
async (partialIdentifier: string) => {
|
||||||
return buildIdentifier(appName, publicSalt, entityType, parentId)
|
const appNameHashed = await hashWord(
|
||||||
}, [appName, publicSalt])
|
appName,
|
||||||
|
EnumCollisionStrength.HIGH,
|
||||||
|
publicSalt
|
||||||
|
);
|
||||||
|
return appNameHashed + "_" + partialIdentifier;
|
||||||
|
},
|
||||||
|
[appName, publicSalt]
|
||||||
|
);
|
||||||
|
|
||||||
const buildSearchPrefixFunc = useCallback(( entityType: string,
|
const hashQortalName = useCallback(
|
||||||
parentId: string | null)=> {
|
async (qortalName: string) => {
|
||||||
return buildSearchPrefix(appName, publicSalt, entityType, parentId)
|
const hashedQortalName = await hashWord(
|
||||||
}, [appName, publicSalt])
|
qortalName,
|
||||||
|
EnumCollisionStrength.HIGH,
|
||||||
const createSingleIdentifier = useCallback(async ( partialIdentifier: string)=> {
|
publicSalt
|
||||||
const appNameHashed = await hashWord(appName, EnumCollisionStrength.HIGH, publicSalt)
|
);
|
||||||
return appNameHashed + '_' + partialIdentifier
|
return hashedQortalName;
|
||||||
}, [appName, publicSalt])
|
},
|
||||||
|
[publicSalt]
|
||||||
|
);
|
||||||
|
|
||||||
const hashQortalName = useCallback(async ( qortalName: string)=> {
|
const hashString = useCallback(
|
||||||
const hashedQortalName = await hashWord(qortalName, EnumCollisionStrength.HIGH, publicSalt)
|
async (string: string, strength: EnumCollisionStrength) => {
|
||||||
return hashedQortalName
|
const hashedQortalName = await hashWord(string, strength, publicSalt);
|
||||||
}, [publicSalt])
|
return hashedQortalName;
|
||||||
|
},
|
||||||
|
[publicSalt]
|
||||||
|
);
|
||||||
|
|
||||||
const hashString = useCallback(async ( string: string, strength: EnumCollisionStrength)=> {
|
return useMemo(
|
||||||
const hashedQortalName = await hashWord(string, strength, publicSalt)
|
() => ({
|
||||||
return hashedQortalName
|
buildIdentifier: buildIdentifierFunc,
|
||||||
}, [publicSalt])
|
buildSearchPrefix: buildSearchPrefixFunc,
|
||||||
|
createSingleIdentifier,
|
||||||
|
hashQortalName,
|
||||||
|
hashString,
|
||||||
return {
|
}),
|
||||||
buildIdentifier: buildIdentifierFunc,
|
[
|
||||||
buildSearchPrefix: buildSearchPrefixFunc,
|
buildIdentifierFunc,
|
||||||
createSingleIdentifier,
|
buildSearchPrefixFunc,
|
||||||
hashQortalName,
|
createSingleIdentifier,
|
||||||
hashString
|
hashQortalName,
|
||||||
};
|
hashString,
|
||||||
|
]
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback } from "react";
|
import React, { useCallback, useMemo } from "react";
|
||||||
import { OpenIndex, useIndexStore } from "../state/indexes";
|
import { OpenIndex, useIndexStore } from "../state/indexes";
|
||||||
|
|
||||||
export const useIndexes = () => {
|
export const useIndexes = () => {
|
||||||
@ -16,7 +16,8 @@ export const useIndexes = () => {
|
|||||||
[setOpen]
|
[setOpen]
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
openPageIndexManager,
|
return useMemo(() => ({
|
||||||
};
|
openPageIndexManager
|
||||||
|
}), [openPageIndexManager]);
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { EnumCollisionStrength, hashWord } from '../utils/encryption';
|
import { EnumCollisionStrength, hashWord } from '../utils/encryption';
|
||||||
import { db } from '../utils/persistentDb';
|
import { db } from '../utils/persistentDb';
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ export const usePersistentStore = (
|
|||||||
return await db.dynamicData.toArray();
|
return await db.dynamicData.toArray();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return {
|
return useMemo(() => ({
|
||||||
setTimestamp,
|
setTimestamp,
|
||||||
getTimestamp,
|
getTimestamp,
|
||||||
isNewTimestamp,
|
isNewTimestamp,
|
||||||
@ -83,5 +83,5 @@ export const usePersistentStore = (
|
|||||||
getData,
|
getData,
|
||||||
deleteData,
|
deleteData,
|
||||||
listAllData,
|
listAllData,
|
||||||
};
|
}), [setTimestamp, getTimestamp, isNewTimestamp, saveData, getData, deleteData, listAllData]);
|
||||||
};
|
};
|
||||||
|
@ -3,14 +3,14 @@ import { usePublishStore } from "../state/publishes";
|
|||||||
import { QortalGetMetadata } from "../types/interfaces/resources";
|
import { QortalGetMetadata } from "../types/interfaces/resources";
|
||||||
|
|
||||||
interface PropsUseResourceStatus {
|
interface PropsUseResourceStatus {
|
||||||
resource: QortalGetMetadata;
|
resource: QortalGetMetadata | null;
|
||||||
retryAttempts?: number;
|
retryAttempts?: number;
|
||||||
}
|
}
|
||||||
export const useResourceStatus = ({
|
export const useResourceStatus = ({
|
||||||
resource,
|
resource,
|
||||||
retryAttempts = 15,
|
retryAttempts = 15,
|
||||||
}: PropsUseResourceStatus) => {
|
}: PropsUseResourceStatus) => {
|
||||||
const resourceId = `${resource.service}-${resource.name}-${resource.identifier}`;
|
const resourceId = !resource ? null : `${resource.service}-${resource.name}-${resource.identifier}`;
|
||||||
const status = usePublishStore((state)=> state.getResourceStatus(resourceId)) || null
|
const status = usePublishStore((state)=> state.getResourceStatus(resourceId)) || null
|
||||||
const intervalRef = useRef<null | number>(null)
|
const intervalRef = useRef<null | number>(null)
|
||||||
const timeoutRef = useRef<null | number>(null)
|
const timeoutRef = useRef<null | number>(null)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback } from "react";
|
import React, { useCallback, useMemo } from "react";
|
||||||
import {
|
import {
|
||||||
QortalMetadata,
|
QortalMetadata,
|
||||||
QortalSearchParams,
|
QortalSearchParams,
|
||||||
@ -341,14 +341,15 @@ export const useResources = (retryAttempts: number = 2) => {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
return {
|
return useMemo(() => ({
|
||||||
fetchResources,
|
fetchResources,
|
||||||
addNewResources,
|
addNewResources,
|
||||||
updateNewResources,
|
updateNewResources,
|
||||||
deleteResource,
|
deleteResource,
|
||||||
deleteList,
|
deleteList,
|
||||||
fetchResourcesResultsOnly
|
fetchResourcesResultsOnly
|
||||||
};
|
}), [fetchResources, addNewResources, updateNewResources, deleteResource, deleteList, fetchResourcesResultsOnly]);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const generateCacheKey = (params: QortalSearchParams): string => {
|
export const generateCacheKey = (params: QortalSearchParams): string => {
|
||||||
|
@ -34,7 +34,7 @@ interface PublishState {
|
|||||||
resourceStatus: Record<string, ResourceStatus | null>;
|
resourceStatus: Record<string, ResourceStatus | null>;
|
||||||
setResourceStatus: (qortalGetMetadata: QortalGetMetadata, data: ResourceStatus | null) => void;
|
setResourceStatus: (qortalGetMetadata: QortalGetMetadata, data: ResourceStatus | null) => void;
|
||||||
getPublish: (qortalGetMetadata: QortalGetMetadata | null, ignoreExpire?: boolean) => Resource | null;
|
getPublish: (qortalGetMetadata: QortalGetMetadata | null, ignoreExpire?: boolean) => Resource | null;
|
||||||
getResourceStatus: (resourceId: string) => ResourceStatus | null;
|
getResourceStatus: (resourceId: string | null) => ResourceStatus | null;
|
||||||
setPublish: (qortalGetMetadata: QortalGetMetadata, data: Resource | null, customExpiry?: number) => void;
|
setPublish: (qortalGetMetadata: QortalGetMetadata, data: Resource | null, customExpiry?: number) => void;
|
||||||
clearExpiredPublishes: () => void;
|
clearExpiredPublishes: () => void;
|
||||||
publishExpiryDuration: number; // Default expiry duration
|
publishExpiryDuration: number; // Default expiry duration
|
||||||
@ -91,6 +91,7 @@ export const usePublishStore = create<PublishState>((set, get) => ({
|
|||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
getResourceStatus: (resourceId) => {
|
getResourceStatus: (resourceId) => {
|
||||||
|
if(!resourceId) return null;
|
||||||
const status = get().resourceStatus[resourceId];
|
const status = get().resourceStatus[resourceId];
|
||||||
return status || null;
|
return status || null;
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user