Skip to content

Commit

Permalink
feat: tip to entries
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Sep 5, 2024
1 parent b062ea2 commit de22411
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
25 changes: 22 additions & 3 deletions src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,22 @@ declare const transactions: drizzle_orm_pg_core.PgTableWithColumns<{
baseColumn: never;
generated: undefined;
}, {}, {}>;
toEntryId: drizzle_orm_pg_core.PgColumn<{
name: "to_entry_id";
tableName: "transactions";
dataType: "string";
columnType: "PgText";
data: string;
driverParam: string;
notNull: false;
hasDefault: false;
isPrimaryKey: false;
isAutoincrement: false;
hasRuntimeDefault: false;
enumValues: [string, ...string[]];
baseColumn: never;
generated: undefined;
}, {}, {}>;
powerToken: drizzle_orm_pg_core.PgColumn<{
name: "power_token";
tableName: "transactions";
Expand Down Expand Up @@ -2354,6 +2370,7 @@ declare const transactionsOpenAPISchema: zod.ZodObject<{
fromUserId: zod.ZodNullable<zod.ZodString>;
toUserId: zod.ZodNullable<zod.ZodString>;
toFeedId: zod.ZodNullable<zod.ZodString>;
toEntryId: zod.ZodNullable<zod.ZodString>;
powerToken: zod.ZodString;
createdAt: zod.ZodString;
comment: zod.ZodNullable<zod.ZodString>;
Expand All @@ -2364,6 +2381,7 @@ declare const transactionsOpenAPISchema: zod.ZodObject<{
fromUserId: string | null;
toUserId: string | null;
toFeedId: string | null;
toEntryId: string | null;
powerToken: string;
comment: string | null;
}, {
Expand All @@ -2373,6 +2391,7 @@ declare const transactionsOpenAPISchema: zod.ZodObject<{
fromUserId: string | null;
toUserId: string | null;
toFeedId: string | null;
toEntryId: string | null;
powerToken: string;
comment: string | null;
}>;
Expand Down Expand Up @@ -2535,9 +2554,8 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
$post: {
input: {
json: {
amount: string;
userId?: string | undefined;
feedId?: string | undefined;
entryId: string;
amount: "1000000000000000000" | "2000000000000000000";
};
};
output: {
Expand Down Expand Up @@ -2572,6 +2590,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
fromUserId: string | null;
toUserId: string | null;
toFeedId: string | null;
toEntryId: string | null;
powerToken: string;
comment: string | null;
fromUser: {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/hooks/biz/useEntryActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const useEntryActions = ({
const openTipModal = useTipModal({
userId: populatedEntry?.feeds.ownerUserId ?? undefined,
feedId: populatedEntry?.feeds.id ?? undefined,
entryId: populatedEntry?.entries.id ?? undefined,
})

const collect = useCollect(populatedEntry)
Expand Down
10 changes: 6 additions & 4 deletions src/renderer/src/modules/wallet/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ import { TipModalContent } from "./tip-modal"
export const useTipModal = ({
userId,
feedId,
entryId,
}: {
userId?: string
feedId?: string
entryId?: string
}) => {
const { present } = useModalStack()

return useCallback(() => {
if (!userId && !feedId) {
if (!feedId || !entryId) {
// this should not happen unless there is a bug in the code
toast.error("Invalid user id or feed id")
toast.error("Invalid feed id or entry id")
return
}
window.posthog?.capture("tip_modal_opened", { feedId })
present({
title: "Tip Power",
content: () => createElement(TipModalContent, { userId, feedId }),
content: () => createElement(TipModalContent, { userId, feedId, entryId }),
})
}, [present, userId, feedId])
}, [present, userId, feedId, entryId])
}
35 changes: 11 additions & 24 deletions src/renderer/src/modules/wallet/tip-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import { nextFrame } from "@renderer/lib/dom"
import {
useWallet,
useWalletTipMutation,
useWalletTransactions,
} from "@renderer/queries/wallet"
import { from, toNumber } from "dnum"
import { from } from "dnum"
import type { FC } from "react"
import { useState } from "react"

Expand All @@ -36,7 +35,8 @@ const Loading = () => (

export const TipModalContent: FC<{
userId?: string
feedId?: string
feedId: string
entryId: string
}> = (props) => {
const myWallet = useMyWallet()

Expand All @@ -47,31 +47,19 @@ export const TipModalContent: FC<{
}
const TipModalContent_: FC<{
userId?: string
feedId?: string
}> = ({ userId, feedId }) => {
feedId: string
entryId: string
}> = ({ userId, feedId, entryId }) => {
const myWallet = useMyWallet()
const myWalletData = myWallet.data?.[0]

const dPowerBigInt = BigInt(myWalletData?.dailyPowerToken ?? 0)
const cPowerBigInt = BigInt(myWalletData?.cashablePowerToken ?? 0)
const balanceBigInt = cPowerBigInt + dPowerBigInt
const balanceNumber = toNumber(from(balanceBigInt, 18), {
digits: 2,
trailingZeros: true,
})

const transactionsQuery = useWalletTransactions({
toUserId: userId,
toFeedId: feedId,
})

const tipMutation = useWalletTipMutation()

const [amount, setAmount] = useState(
DEFAULT_RECOMMENDED_TIP > balanceNumber ?
balanceNumber :
DEFAULT_RECOMMENDED_TIP,
)
const [amount, setAmount] = useState<1 | 2>(DEFAULT_RECOMMENDED_TIP)

const amountBigInt = from(amount, 18)[0]

Expand All @@ -86,7 +74,7 @@ const TipModalContent_: FC<{
feedId,
})

if (transactionsQuery.isPending || myWallet.isPending) {
if (myWallet.isPending) {
return <Loading />
}

Expand Down Expand Up @@ -176,7 +164,7 @@ const TipModalContent_: FC<{

<RadioGroup
value={amount.toString()}
onValueChange={(value) => setAmount(Number(value))}
onValueChange={(value) => setAmount(Number(value) as 1 | 2)}
>
<div className="grid grid-cols-2 gap-2">
<RadioCard
Expand Down Expand Up @@ -213,9 +201,8 @@ const TipModalContent_: FC<{
onClick={() => {
if (tipMutation.isPending) return
tipMutation.mutate({
userId,
feedId,
amount: amountBigInt.toString(),
entryId,
amount: amountBigInt.toString() as "1000000000000000000" | "2000000000000000000",
})
}}
variant={tipMutation.isSuccess ? "outline" : "primary"}
Expand Down

0 comments on commit de22411

Please sign in to comment.