Skip to content

Commit

Permalink
Merge pull request #9 from ecency/bugfix/review-issues-2
Browse files Browse the repository at this point in the history
Bugfix/review issues 2
  • Loading branch information
feruzm authored Sep 10, 2024
2 parents 829654a + f28a868 commit b2fb7d1
Show file tree
Hide file tree
Showing 47 changed files with 568 additions and 368 deletions.
277 changes: 139 additions & 138 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/sw.js

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions src/api/mutations/create-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { useGlobalStore } from "@/core/global-store";

export function useCreateReply(entry?: Entry | null, parent?: Entry, onSuccess?: () => void) {
const activeUser = useGlobalStore((state) => state.activeUser);

const { addReply } = EcencyEntriesCacheManagement.useAddReply(entry ?? undefined);
const { updateRepliesCount } = EcencyEntriesCacheManagement.useUpdateRepliesCount(
entry ?? undefined
);
const { updateEntryQueryData } = EcencyEntriesCacheManagement.useUpdateEntry();

const queryClient = useQueryClient();

return useMutation({
Expand Down Expand Up @@ -58,15 +65,15 @@ export function useCreateReply(entry?: Entry | null, parent?: Entry, onSuccess?:
return;
}

EcencyEntriesCacheManagement.addReply(entry, data);
EcencyEntriesCacheManagement.updateEntryQueryData([data]);
addReply(data);
updateEntryQueryData([data]);

// remove reply draft
ss.remove(`reply_draft_${entry.author}_${entry.permlink}`);

if (entry.children === 0) {
// Update parent comment.
EcencyEntriesCacheManagement.updateRepliesCount(entry, 1);
updateRepliesCount(1);
}
const previousReplies =
queryClient.getQueryData<Entry[]>([
Expand Down
8 changes: 4 additions & 4 deletions src/api/mutations/entry-reblog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { EcencyEntriesCacheManagement } from "@/core/caches";

export function useEntryReblog(entry: Entry) {
const activeUser = useGlobalStore((s) => s.activeUser);

const { mutateAsync: recordUserActivity } = useRecordUserActivity();

const { update: updateReblogsCount } = EcencyEntriesCacheManagement.useUpdateReblogsCount(entry);
const queryClient = useQueryClient();

return useMutation({
Expand All @@ -36,10 +39,7 @@ export function useEntryReblog(entry: Entry) {
isDelete
? info(i18next.t("entry-reblog.delete-success"))
: success(i18next.t("entry-reblog.success"));
EcencyEntriesCacheManagement.updateReblogsCount(
entry,
(entry.reblogs ?? 0) + (isDelete ? -1 : 1)
);
updateReblogsCount((entry.reblogs ?? 0) + (isDelete ? -1 : 1));
queryClient.setQueryData<BlogEntry[]>(
[QueryIdentifiers.REBLOGS, activeUser?.username, 200],
(data) => {
Expand Down
16 changes: 10 additions & 6 deletions src/api/mutations/entry-vote.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Entry } from "@/entities";
import { useGlobalStore } from "@/core/global-store";
import { formatError, vote } from "@/api/operations";
import { error } from "@/features/shared";
import { getQueryClient, QueryIdentifiers } from "@/core/react-query";
import { QueryIdentifiers } from "@/core/react-query";
import { EcencyEntriesCacheManagement } from "@/core/caches";

export function useEntryVote(entry?: Entry) {
const activeUser = useGlobalStore((s) => s.activeUser);
const updateActiveUser = useGlobalStore((s) => s.updateActiveUser);

const { invalidate } = EcencyEntriesCacheManagement.useInvalidation(entry);
const { update: updateVotes } = EcencyEntriesCacheManagement.useUpdateVotes(entry);
const qc = useQueryClient();

return useMutation({
mutationKey: ["entryVote", entry?.author, entry?.permlink],
mutationFn: async ({ weight, estimated }: { weight: number; estimated: number }) => {
Expand All @@ -22,7 +26,7 @@ export function useEntryVote(entry?: Entry) {
}

await vote(activeUser?.username, entry.author, entry.permlink, weight);
await updateActiveUser(); // refresh voting power
updateActiveUser(); // refresh voting power

return [
estimated,
Expand All @@ -41,12 +45,12 @@ export function useEntryVote(entry?: Entry) {

const newPayout = entry.payout + estimated;
if (entry.active_votes) {
EcencyEntriesCacheManagement.updateVotes(entry, [...votes], newPayout);
updateVotes([...votes], newPayout);
} else {
EcencyEntriesCacheManagement.invalidate(entry);
invalidate();
}

getQueryClient().invalidateQueries({
qc.invalidateQueries({
queryKey: [QueryIdentifiers.ENTRY_ACTIVE_VOTES, entry!.author, entry!.permlink]
});
},
Expand Down
32 changes: 25 additions & 7 deletions src/api/mutations/sign-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { hpToVests } from "@/features/shared/transfer/hp-to-vests";
import { error, TransferAsset, TransferMode } from "@/features/shared";
import { PrivateKey, TransactionConfirmation } from "@hiveio/dhive";
import { getDynamicPropsQuery } from "@/api/queries";
import { DEFAULT_DYNAMIC_PROPS, getDynamicPropsQuery } from "@/api/queries";
import { TxResponse } from "@/types";

export function useSignTransferByKey(mode: TransferMode, asset: TransferAsset) {
Expand Down Expand Up @@ -87,12 +87,18 @@ export function useSignTransferByKey(mode: TransferMode, asset: TransferAsset) {
break;
}
case "power-down": {
const vests = hpToVests(Number(amount), dynamicProps!.hivePerMVests);
const vests = hpToVests(
Number(amount),
(dynamicProps ?? DEFAULT_DYNAMIC_PROPS).hivePerMVests
);
promise = withdrawVesting(username, key, vests);
break;
}
case "delegate": {
const vests = hpToVests(Number(amount), dynamicProps!.hivePerMVests);
const vests = hpToVests(
Number(amount),
(dynamicProps ?? DEFAULT_DYNAMIC_PROPS).hivePerMVests
);
promise = delegateVestingShares(username, key, to, vests);
break;
}
Expand Down Expand Up @@ -155,12 +161,18 @@ export function useSignTransferByKeychain(mode: TransferMode, asset: TransferAss
break;
}
case "power-down": {
const vests = hpToVests(Number(amount), dynamicProps!.hivePerMVests);
const vests = hpToVests(
Number(amount),
(dynamicProps ?? DEFAULT_DYNAMIC_PROPS).hivePerMVests
);
promise = withdrawVestingKc(username, vests);
break;
}
case "delegate": {
const vests = hpToVests(Number(amount), dynamicProps!.hivePerMVests);
const vests = hpToVests(
Number(amount),
(dynamicProps ?? DEFAULT_DYNAMIC_PROPS).hivePerMVests
);
promise = delegateVestingSharesKc(username, to, vests);
break;
}
Expand Down Expand Up @@ -221,12 +233,18 @@ export function useSignTransferByHiveSigner(mode: TransferMode, asset: TransferA
break;
}
case "power-down": {
const vests = hpToVests(Number(amount), dynamicProps!.hivePerMVests);
const vests = hpToVests(
Number(amount),
(dynamicProps ?? DEFAULT_DYNAMIC_PROPS).hivePerMVests
);
withdrawVestingHot(username, vests);
break;
}
case "delegate": {
const vests = hpToVests(Number(amount), dynamicProps!.hivePerMVests);
const vests = hpToVests(
Number(amount),
(dynamicProps ?? DEFAULT_DYNAMIC_PROPS).hivePerMVests
);
delegateVestingSharesHot(username, to, vests);
break;
}
Expand Down
5 changes: 4 additions & 1 deletion src/api/mutations/update-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { CommentOptions, Entry, MetaData } from "@/entities";
import { comment, formatError } from "@/api/operations";
import { error } from "@/features/shared";
import { EcencyEntriesCacheManagement } from "@/core/caches";
import useUpdateEntry = EcencyEntriesCacheManagement.useUpdateEntry;

export function useUpdateReply(entry?: Entry | null, onSuccess?: () => void) {
const activeUser = useGlobalStore((state) => state.activeUser);

const { updateEntryQueryData } = useUpdateEntry();

return useMutation({
mutationKey: ["reply-update", activeUser?.username, entry?.author, entry?.permlink],
mutationFn: async ({
Expand Down Expand Up @@ -48,7 +51,7 @@ export function useUpdateReply(entry?: Entry | null, onSuccess?: () => void) {
return;
}

EcencyEntriesCacheManagement.updateEntryQueryData([data]);
updateEntryQueryData([data]);

// remove reply draft
ss.remove(`reply_draft_${entry.author}_${entry.permlink}`);
Expand Down
16 changes: 16 additions & 0 deletions src/api/queries/dynamic-props-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import {
import { DynamicProps } from "@/entities";
import { parseAsset } from "@/utils";

export const DEFAULT_DYNAMIC_PROPS = {
hivePerMVests: 1,
base: 1,
quote: 1,
fundRecentClaims: 1,
fundRewardBalance: 1,
hbdPrintRate: 1,
hbdInterestRate: 1,
headBlock: 1,
totalVestingFund: 1,
totalVestingShares: 1,
virtualSupply: 1,
vestingRewardPercent: 1,
accountCreationFee: "3.000 HIVE"
};

export const getDynamicPropsQuery = () =>
EcencyQueriesManager.generateClientServerQuery({
queryKey: [QueryIdentifiers.DYNAMIC_PROPS],
Expand Down
3 changes: 2 additions & 1 deletion src/api/queries/get-boost-plus-prices-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export const getBoostPlusPricesQuery = (activeUser: ActiveUser | null) =>
);
return response.data;
},
initialData: []
staleTime: Infinity,
refetchOnMount: true
});
7 changes: 1 addition & 6 deletions src/api/queries/get-discussions-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { bridgeApiCall } from "@/api/bridge";
import { parseAsset } from "@/utils";
import { SortOrder } from "@/enums";
import { IdentifiableEntry } from "@/app/decks/_components/columns/deck-threads-manager";
import { EcencyEntriesCacheManagement } from "@/core/caches";

export function sortDiscussions(entry: Entry, discussion: Entry[], order: SortOrder) {
const allPayout = (c: Entry) =>
Expand Down Expand Up @@ -89,9 +88,7 @@ export const getDiscussionsQuery = (entry: Entry, order: SortOrder, enabled: boo
permlink: entry.permlink
});
if (response) {
const entries = Array.from(Object.values(response));
EcencyEntriesCacheManagement.updateEntryQueryData([...entries]);
return entries;
return Array.from(Object.values(response));
}
return [];
},
Expand All @@ -112,8 +109,6 @@ export const getDiscussionsMapQuery = (entry: Entry | undefined, enabled: boolea
}
);
if (response) {
const entries = Array.from(Object.values(response));
EcencyEntriesCacheManagement.updateEntryQueryData([...entries]);
return response;
}
return {};
Expand Down
31 changes: 31 additions & 0 deletions src/api/queries/get-search-by-username-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { QueryIdentifiers } from "@/core/react-query";
import { lookupAccounts } from "@/api/hive";
import { error } from "@/features/shared";
import { formatError } from "@/api/operations";
import { useQuery } from "@tanstack/react-query";
import { useGlobalStore } from "@/core/global-store";

export function useSearchByUsernameQuery(query: string, excludeActiveUser = false) {
const activeUser = useGlobalStore((s) => s.activeUser);

return useQuery({
queryKey: [QueryIdentifiers.SEARCH_BY_USERNAME, query],
staleTime: Infinity,
refetchOnMount: true,
queryFn: async () => {
if (!query) {
return [];
}

try {
const resp = await lookupAccounts(query, 5);
if (resp) {
resp.filter((item) => (excludeActiveUser ? item !== activeUser?.username : true));
}
} catch (e) {
error(...formatError(e));
} finally {
}
}
});
}
1 change: 1 addition & 0 deletions src/api/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ export * from "./get-followers-query";
export * from "./get-deleted-entry-query";
export * from "./get-hive-hbd-stats-query";
export * from "./get-order-book-query";
export * from "./get-search-by-username-query";
6 changes: 3 additions & 3 deletions src/api/queries/points-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const getPointsQuery = (username?: string, filter = 0) => {
transactions
} as const;
},
initialData: DEFAULT,
enabled: !!username,
retryDelay: 30000
staleTime: 30000,
refetchOnMount: true,
enabled: !!username
}
);
};
13 changes: 10 additions & 3 deletions src/app/[...slugs]/_profile-components/delegated-vesting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import i18next from "i18next";
import { Pagination, Tooltip } from "@/features/ui";
import { formattedNumber, parseAsset, vestsToHp } from "@/utils";
import { KeyOrHotDialog, LinearProgress, ProfileLink, UserAvatar } from "@/features/shared";
import { getDynamicPropsQuery, getVestingDelegationsQuery } from "@/api/queries";
import {
DEFAULT_DYNAMIC_PROPS,
getDynamicPropsQuery,
getVestingDelegationsQuery
} from "@/api/queries";
import { useGlobalStore } from "@/core/global-store";
import { useDelegateVestingSharesByKey, useDelegateVestingSharesByKeychain } from "@/api/mutations";
import { delegateVestingSharesHot } from "@/api/operations";
Expand Down Expand Up @@ -55,7 +59,7 @@ export function DelegatedVesting({ onHide, account, totalDelegated }: Props) {
useEffect(() => {
const totalDelegatedValue = data.reduce((n, item) => {
let parsedValue: any = parseAsset(item.vesting_shares).amount;
parsedValue = vestsToHp(parsedValue, dynamicProps!.hivePerMVests);
parsedValue = vestsToHp(parsedValue, (dynamicProps ?? DEFAULT_DYNAMIC_PROPS).hivePerMVests);
parsedValue = formattedNumber(parsedValue);
parsedValue = parsedValue.replace(/,/g, "");
parsedValue = parseFloat(parsedValue);
Expand Down Expand Up @@ -119,7 +123,10 @@ export function DelegatedVesting({ onHide, account, totalDelegated }: Props) {
<Tooltip content={x.vesting_shares}>
<span>
{formattedNumber(
vestsToHp(vestingShares, dynamicProps!.hivePerMVests),
vestsToHp(
vestingShares,
(dynamicProps ?? DEFAULT_DYNAMIC_PROPS).hivePerMVests
),
{ suffix: "HP" }
)}
</span>
Expand Down
8 changes: 5 additions & 3 deletions src/app/[...slugs]/_profile-components/profile-info/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import moment from "moment";
import { RCAccount } from "@hiveio/dhive/lib/chain/rc";
import "./_index.scss";
import { Account, FullAccount } from "@/entities";
import { getDynamicPropsQuery, getRcAccountsQuery } from "@/api/queries";
import { DEFAULT_DYNAMIC_PROPS, getDynamicPropsQuery, getRcAccountsQuery } from "@/api/queries";
import { downVotingPower, powerRechargeTime, rcPower, votingPower, votingValue } from "@/api/hive";
import { formattedNumber } from "@/utils";
import i18next from "i18next";
Expand All @@ -27,8 +27,10 @@ function ProfileInfoContent({ account, rcAccount }: ContentProps) {
const vPowerRechargeDate = moment().add(vPowerRecharge, "seconds");

// Voting value
const vValue = votingValue(account, dynamicProps!, vPower * 100).toFixed(3);
const vValueFull = votingValue(account, dynamicProps!, 10000).toFixed(3);
const vValue = votingValue(account, dynamicProps ?? DEFAULT_DYNAMIC_PROPS, vPower * 100).toFixed(
3
);
const vValueFull = votingValue(account, dynamicProps ?? DEFAULT_DYNAMIC_PROPS, 10000).toFixed(3);

// Join date
const created = moment.utc(account.created).format("LL");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useEffect, useState } from "react";
import { getMetrics } from "@/api/hive-engine";
import i18next from "i18next";
import { getDynamicPropsQuery } from "@/api/queries";
import { DEFAULT_DYNAMIC_PROPS, getDynamicPropsQuery } from "@/api/queries";

export const EngineTokensEstimated = (props: any) => {
const { tokens: userTokens } = props;
Expand All @@ -12,7 +12,8 @@ export const EngineTokensEstimated = (props: any) => {
const getEstimatedUsdValue = useCallback(async () => {
const AllMarketTokens = await getMetrics();

const pricePerHive = dynamicProps!.base / dynamicProps!.quote;
const pricePerHive =
(dynamicProps ?? DEFAULT_DYNAMIC_PROPS).base / (dynamicProps ?? DEFAULT_DYNAMIC_PROPS).quote;

let mappedBalanceMetrics = userTokens.map((item: any) => {
let eachMetric = AllMarketTokens.find((m: any) => m.symbol === item.symbol);
Expand Down
Loading

0 comments on commit b2fb7d1

Please sign in to comment.