Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add calc progress number #166

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/network-clients/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ export * from './ipfs';
export function min(a: BigNumber, b: BigNumber): BigNumber {
return a.lte(b) ? a : b;
}

export const indexingProgress = ({
currentHeight,
targetHeight,
startHeight = 0,
}: {
currentHeight: number;
targetHeight: number;
startHeight: number;
}) => {
if (targetHeight === startHeight) return 0;
const rawProgress = (currentHeight - startHeight) / (targetHeight - startHeight);
return isNaN(rawProgress) ? 0 : Math.min(Math.max(rawProgress, 0), 1);
};
20 changes: 10 additions & 10 deletions packages/react-hooks/src/renderAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { AsyncData } from './types';
import { Handlers, HandlersArray, RenderResult } from './utils';

export function renderAsync<T>(data: AsyncData<T>, handlers: Handlers<T>): RenderResult {
if (data.data !== undefined) {
if (data.error) {
return handlers.error(data.error);
} else if (data.loading) {
return handlers.loading();
} else if (data.data !== undefined) {
try {
return handlers.data(data.data, data);
} catch (e) {
// TODO not sure this is desired behaviour
return handlers.error(e as Error);
}
} else if (data.error) {
return handlers.error(data.error);
} else if (data.loading) {
return handlers.loading();
}

return null;
Expand All @@ -25,6 +25,11 @@ export function renderAsyncArray<T extends any[]>(
data: AsyncData<T>,
handlers: HandlersArray<T>
): RenderResult {
if (data.error) {
return handlers.error(data.error);
} else if (data.loading) {
return handlers.loading();
}
if (data.data !== undefined) {
try {
if (data.data === null || (Array.isArray(data.data) && !data.data.length)) {
Expand All @@ -36,11 +41,6 @@ export function renderAsyncArray<T extends any[]>(
return handlers.error(e as Error);
}
}
if (data.error) {
return handlers.error(data.error);
} else if (data.loading) {
return handlers.loading();
}

return null;
}
Loading