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