Skip to content

Commit

Permalink
Merge branch 'fix-react-gno-feedbacks' of github.com:TERITORI/teritor…
Browse files Browse the repository at this point in the history
…i-dapp into fix-react-gno-feedbacks
  • Loading branch information
WaDadidou committed Nov 19, 2024
2 parents d9b30c8 + 59103b8 commit d5bed42
Show file tree
Hide file tree
Showing 16 changed files with 607 additions and 861 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/buf-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
check-codegen:
lint-buf:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/desktop-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
prepare:
prepare-desktop-build:
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
Expand All @@ -23,7 +23,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.22.8"

- name: Install node modules
run: yarn
Expand All @@ -37,8 +37,8 @@ jobs:
name: electron-web-build
path: electron/web-build
if-no-files-found: error
build:
needs: prepare
build-desktop:
needs: prepare-desktop-build
strategy:
matrix:
include:
Expand All @@ -63,7 +63,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.22.8"

- name: Install node modules
run: yarn
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gno-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
go:
gno-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gno-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
go:
gno-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
go:
check-go:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
lint-and-build:
check-js:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/mobile-build-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
build:
build-android:
runs-on: ubuntu-latest
continue-on-error: true
steps:
Expand All @@ -27,7 +27,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.22.8"

- uses: actions/checkout@v4
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/mobile-build-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
build:
build-ios:
runs-on: macos-14
continue-on-error: true
steps:
Expand All @@ -17,7 +17,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.22.8"

- name: Select xcode
run: sudo xcode-select -s /Applications/Xcode_15.2.app
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
merge_group:

jobs:
rust:
check-rust:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 3 additions & 3 deletions packages/components/socialFeed/SocialActions/TipButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ export const TipButton: React.FC<{
amount={tipAmountLocal}
/>
) : (
<TeritoriTipAmount amount={amount} />
<TeritoriTipAmount amount={tipAmountLocal} />
)}
</BrandText>
</TouchableOpacity>

<TipModal
authorId={authorId}
postId={postId}
onClose={(newTipAmount: number | undefined) => {
onClose={(addedTipAmount: number | undefined) => {
setTipModalVisible(false);
newTipAmount && setTipAmountLocal(amount + newTipAmount);
addedTipAmount && setTipAmountLocal(amount + addedTipAmount);
}}
isVisible={tipModalVisible}
/>
Expand Down
88 changes: 65 additions & 23 deletions packages/components/socialFeed/SocialActions/TipModal.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { coin } from "@cosmjs/amino";
import { Decimal } from "@cosmjs/math";
import React from "react";
import React, { useState } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { View } from "react-native";

import { signingSocialFeedClient } from "../../../client-creators/socialFeedClient";
import { useFeedbacks } from "../../../context/FeedbacksProvider";
import { useTeritoriSocialFeedTipPostMutation } from "../../../contracts-clients/teritori-social-feed/TeritoriSocialFeed.react-query";
import { useBalances } from "../../../hooks/useBalances";
import { useSelectedNetworkInfo } from "../../../hooks/useSelectedNetwork";
import {
useSelectedNetworkId,
useSelectedNetworkInfo,
} from "../../../hooks/useSelectedNetwork";
import useSelectedWallet from "../../../hooks/useSelectedWallet";
import {
getStakingCurrency,
keplrCurrencyFromNativeCurrencyInfo,
NetworkKind,
parseNetworkObjectId,
} from "../../../networks";
import { prettyPrice } from "../../../utils/coins";
import { defaultSocialFeedFee } from "../../../utils/fee";
Expand All @@ -32,6 +36,7 @@ import ModalBase from "../../modals/ModalBase";
import { SpacerColumn } from "../../spacer";

import { Username } from "@/components/user/Username";
import { sanitizeFloatText } from "@/utils/text";

type TipFormType = {
amount: string;
Expand All @@ -40,38 +45,67 @@ type TipFormType = {
export const TipModal: React.FC<{
authorId: string;
postId: string;
onClose: (newTipAmount?: number) => void;
onClose: (addedTipAmount?: number) => void;
isVisible: boolean;
}> = ({ authorId, postId, onClose, isVisible }) => {
const selectedNetworkId = useSelectedNetworkId();
const nativeCurrency = getStakingCurrency(selectedNetworkId);
const {
control,
handleSubmit: formHandleSubmit,
setValue,
watch,
} = useForm<TipFormType>();
} = useForm<TipFormType>({
defaultValues: {
amount: "",
},
});
const formValues = watch();
const amount = nativeCurrency
? Decimal.fromUserInput(
sanitizeFloatText(formValues.amount),
nativeCurrency.decimals,
).atomics
: "0";

const { mutate: postMutate, isLoading } =
useTeritoriSocialFeedTipPostMutation({
onMutate() {
setLocalLoading(true);
},
onSuccess() {
onClose();
setToastSuccess({ title: "Tip success", message: "" });
setToast({
mode: "normal",
type: "success",
title: "Tip success",
message: "",
});
},
onError(error) {
console.error(error);
setToastError({ title: "Tip failed", message: error.message });
setToast({
mode: "normal",
type: "error",
title: "Tip failed",
message: error.message,
});
},

onSettled() {
onClose(+amount);
setLocalLoading(false);
},
});
const [islocalLoading, setLocalLoading] = useState(isLoading);
const selectedWallet = useSelectedWallet();
const selectedNetworkInfo = useSelectedNetworkInfo();
const selectedNetworkId = selectedNetworkInfo?.id || "";
const nativeCurrency = getStakingCurrency(selectedNetworkId);
const { setToastError, setToastSuccess } = useFeedbacks();
const { setToast } = useFeedbacks();
const { balances } = useBalances(selectedNetworkId, selectedWallet?.address);
const currencyBalance = balances.find(
(bal) => bal.denom === nativeCurrency?.denom,
);
const formValues = watch();

const handleSubmit: SubmitHandler<TipFormType> = async (fieldValues) => {
const handleSubmit: SubmitHandler<TipFormType> = async () => {
if (
!selectedWallet?.connected ||
!selectedWallet.address ||
Expand All @@ -80,22 +114,18 @@ export const TipModal: React.FC<{
return;
}

const amount = Decimal.fromUserInput(
fieldValues.amount,
nativeCurrency.decimals,
).atomics;

if (selectedNetworkInfo?.kind === NetworkKind.Gno) {
// We use Tip function from Social_feed contract to keep track of tip amount
const vmCall = {
caller: selectedWallet.address,
send: `${amount}ugnot`,
pkg_path: selectedNetworkInfo.socialFeedsPkgPath,
func: "TipPost",
args: [TERITORI_FEED_ID, postId],
args: [TERITORI_FEED_ID, parseNetworkObjectId(postId)[1]],
};

try {
setLocalLoading(true);
await adenaDoContract(
selectedNetworkId || "",
[{ type: AdenaDoContractMessageType.CALL, value: vmCall }],
Expand All @@ -104,11 +134,23 @@ export const TipModal: React.FC<{
},
);

onClose(+amount);
setToastSuccess({ title: "Tip success", message: "" });
setToast({
mode: "normal",
type: "success",
title: "Tip success",
message: "",
});
} catch (err: any) {
console.error(err);
setToastError({ title: "Tip failed", message: err.message });
setToast({
mode: "normal",
type: "error",
title: "Tip failed",
message: err.message,
});
} finally {
onClose(+amount);
setLocalLoading(false);
}
} else {
const client = await signingSocialFeedClient({
Expand All @@ -119,7 +161,7 @@ export const TipModal: React.FC<{
postMutate({
client,
msg: {
identifier: postId,
identifier: parseNetworkObjectId(postId)[1],
},
args: {
fee: defaultSocialFeedFee,
Expand Down Expand Up @@ -186,7 +228,7 @@ export const TipModal: React.FC<{
text="Send"
fullWidth
loader
isLoading={isLoading}
isLoading={islocalLoading}
onPress={formHandleSubmit(handleSubmit)}
disabled={
max === "0" || !formValues.amount || formValues.amount === "0"
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/feed/usePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const usePost = (id: string | undefined) => {
isDeleted: res.deleted,
subPostLength: res.sub_post_length,
createdAt,
tipAmount: res.tip_amount,
tipAmount: parseFloat(res.tip_amount),
premiumLevel: commonMetadata?.premium || 0,
};
return post;
Expand Down
8 changes: 8 additions & 0 deletions packages/utils/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ export const replaceBetweenString = (
insertion: string,
) =>
`${origin.substring(0, startIndex)}${insertion}${origin.substring(endIndex)}`;

// Fix amount when missing the fractional part like "2." by removing the "."
export const sanitizeFloatText = (amount: string) => {
if (isFloatText(amount) && amount.endsWith(".")) {
return amount.slice(0, -1);
}
return amount;
};
Loading

0 comments on commit d5bed42

Please sign in to comment.