feat: display power ranking

This commit is contained in:
DIYgod
2024-10-16 02:43:59 +08:00
parent 8b6c3f9f50
commit 1150289d64
6 changed files with 308 additions and 6 deletions
@@ -3,7 +3,6 @@ import { Trans, useTranslation } from "react-i18next"
import { Button } from "~/components/ui/button"
import { CopyButton } from "~/components/ui/code-highlighter"
import { Divider } from "~/components/ui/divider"
import { LoadingWithIcon } from "~/components/ui/loading"
import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from "~/components/ui/tooltip"
import { DAILY_CLAIM_AMOUNT } from "~/constants"
@@ -127,8 +126,6 @@ export const MyWalletSection = () => {
</Tooltip>
</div>
</div>
<Divider className="my-8" />
</>
)
}
@@ -0,0 +1,126 @@
import { Fragment } from "react"
import { useTranslation } from "react-i18next"
import { useWhoami } from "~/atoms/user"
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar"
import { MotionButtonBase } from "~/components/ui/button"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "~/components/ui/table"
import { EllipsisHorizontalTextWithTooltip } from "~/components/ui/typography"
import { replaceImgUrlIfNeed } from "~/lib/img-proxy"
import { cn } from "~/lib/utils"
import { usePresentUserProfileModal } from "~/modules/profile/hooks"
import { SettingSectionTitle } from "~/modules/settings/section"
import { Balance } from "~/modules/wallet/balance"
import type { useWalletTransactions } from "~/queries/wallet"
import { useWalletRanking } from "~/queries/wallet"
const medals = ["", "🥇", "🥈", "🥉"]
const rankNumber = (index: number) => {
if (index < medals.length) {
return <span className="-ml-[3px]">{medals[index]}</span>
}
return <span>{index}</span>
}
export const PowerRanking: Component = ({ className }) => {
const { t } = useTranslation("settings")
const ranking = useWalletRanking()
return (
<div className="relative flex min-w-0 grow flex-col">
<SettingSectionTitle title={t("wallet.ranking.title")} />
<div className={cn("w-fit min-w-0 grow overflow-x-auto", className)}>
<Table className="w-full table-fixed text-sm">
<TableHeader className="sticky top-0 bg-theme-background">
<TableRow className="[&_*]:!font-semibold">
<TableHead className="w-20">#</TableHead>
<TableHead>{t("wallet.ranking.name")}</TableHead>
<TableHead>{t("wallet.ranking.power")}</TableHead>
<TableHead>{t("wallet.ranking.level")}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{ranking.data?.map((row, index) => {
const lastRow = ranking.data?.[index - 1]
const hasGap = lastRow?.rank && row.rank && row.rank - lastRow.rank !== 1
return (
<Fragment key={row.userId}>
{!!hasGap && (
<>
<TableRow>
<TableCell className="py-2">...</TableCell>
</TableRow>
<TableRow>
<div className="h-px w-full" />
</TableRow>
</>
)}
<TableRow>
<TableCell className="py-2">{!!row.rank && rankNumber(row.rank)}</TableCell>
<TableCell className="py-2">
<UserRenderer user={row.user} />
</TableCell>
<TableCell className="py-2">
<Balance>{row.powerToken}</Balance>
</TableCell>
<TableCell className="py-2">{row.level}</TableCell>
</TableRow>
</Fragment>
)
})}
<TableRow>
<TableCell className="py-2">...</TableCell>
</TableRow>
</TableBody>
</Table>
</div>
</div>
)
}
const UserRenderer = ({
user,
}: {
user?: NonNullable<ReturnType<typeof useWalletTransactions>["data"]>[number][
| "fromUser"
| "toUser"]
}) => {
const { t } = useTranslation("settings")
const me = useWhoami()
const isMe = user?.id === me?.id
const name = isMe ? t("wallet.transactions.you") : user?.name || APP_NAME
const presentUserModal = usePresentUserProfileModal("drawer")
return (
<MotionButtonBase
onClick={() => {
if (user?.id) presentUserModal(user.id)
}}
className="flex w-full min-w-0 cursor-button items-center gap-2"
>
<Avatar className="aspect-square size-6 duration-200 animate-in fade-in-0">
<AvatarImage src={replaceImgUrlIfNeed(user?.image || undefined)} />
<AvatarFallback>{name?.slice(0, 2)}</AvatarFallback>
</Avatar>
<div className="ml-1 w-0 grow truncate">
<EllipsisHorizontalTextWithTooltip className="text-left">
{isMe ? (
<span className="font-bold">{t("wallet.transactions.you")}</span>
) : (
<span>{name}</span>
)}
</EllipsisHorizontalTextWithTooltip>
</div>
</MotionButtonBase>
)
}
@@ -1,6 +1,8 @@
import { useTranslation } from "react-i18next"
import { Divider } from "~/components/ui/divider"
import { MyWalletSection } from "~/modules/power/my-wallet-section"
import { PowerRanking } from "~/modules/power/ranking"
import { TransactionsSection } from "~/modules/power/transaction-section"
import { useSubViewTitle } from "../hooks"
@@ -23,6 +25,10 @@ export function Component() {
</div>
<MyWalletSection />
<Divider className="my-8" />
<PowerRanking />
<Divider className="my-8" />
<TransactionsSection />
</div>
)
+16
View File
@@ -42,6 +42,20 @@ export const wallet = {
},
),
},
ranking: {
get: () =>
defineQuery(
["wallet", "ranking"],
async () => {
const res = await apiClient.wallets.ranking.$get()
return res.data
},
{
rootKey: ["wallet", "ranking"],
},
),
},
}
export const useWallet = () => useAuthQuery(wallet.get())
@@ -49,6 +63,8 @@ export const useWallet = () => useAuthQuery(wallet.get())
export const useWalletTransactions = (query: Parameters<typeof wallet.transactions.get>[0] = {}) =>
useAuthQuery(wallet.transactions.get(query))
export const useWalletRanking = () => useAuthQuery(wallet.ranking.get())
export const useCreateWalletMutation = () =>
useMutation({
mutationKey: ["createWallet"],
+5
View File
@@ -229,6 +229,11 @@
"wallet.create.description": "Create a free wallet to receive <PowerIcon /> <strong>Power</strong>, which can be used to reward creators and also get rewarded for your content contributions.",
"wallet.power.dailyClaim": "You can claim {{amount}} free Power daily, which can be used to tip RSS entries on Follow.",
"wallet.power.description": "Power is an <Link>ERC-20 token</Link> on the {{blockchainName}} blockchain.",
"wallet.ranking.level": "Level",
"wallet.ranking.name": "Name",
"wallet.ranking.power": "Power",
"wallet.ranking.rank": "Rank",
"wallet.ranking.title": "Power Ranking",
"wallet.sidebar_title": "Power",
"wallet.transactions.amount": "Amount",
"wallet.transactions.date": "Date",
+155 -3
View File
@@ -3923,6 +3923,22 @@ declare const wallets: drizzle_orm_pg_core.PgTableWithColumns<{
baseColumn: never;
generated: undefined;
}, {}, {}>;
powerToken: drizzle_orm_pg_core.PgColumn<{
name: "power_token";
tableName: "wallets";
dataType: "string";
columnType: "PgNumeric";
data: string;
driverParam: string;
notNull: true;
hasDefault: true;
isPrimaryKey: false;
isAutoincrement: false;
hasRuntimeDefault: false;
enumValues: undefined;
baseColumn: never;
generated: undefined;
}, {}, {}>;
dailyPowerToken: drizzle_orm_pg_core.PgColumn<{
name: "daily_power_token";
tableName: "wallets";
@@ -3955,6 +3971,86 @@ declare const wallets: drizzle_orm_pg_core.PgTableWithColumns<{
baseColumn: never;
generated: undefined;
}, {}, {}>;
rank: drizzle_orm_pg_core.PgColumn<{
name: "rank";
tableName: "wallets";
dataType: "number";
columnType: "PgInteger";
data: number;
driverParam: string | number;
notNull: false;
hasDefault: false;
isPrimaryKey: false;
isAutoincrement: false;
hasRuntimeDefault: false;
enumValues: undefined;
baseColumn: never;
generated: undefined;
}, {}, {}>;
level: drizzle_orm_pg_core.PgColumn<{
name: "level";
tableName: "wallets";
dataType: "number";
columnType: "PgInteger";
data: number;
driverParam: string | number;
notNull: false;
hasDefault: false;
isPrimaryKey: false;
isAutoincrement: false;
hasRuntimeDefault: false;
enumValues: undefined;
baseColumn: never;
generated: undefined;
}, {}, {}>;
preActivePoints: drizzle_orm_pg_core.PgColumn<{
name: "pre_active_points";
tableName: "wallets";
dataType: "number";
columnType: "PgInteger";
data: number;
driverParam: string | number;
notNull: false;
hasDefault: false;
isPrimaryKey: false;
isAutoincrement: false;
hasRuntimeDefault: false;
enumValues: undefined;
baseColumn: never;
generated: undefined;
}, {}, {}>;
activePoints: drizzle_orm_pg_core.PgColumn<{
name: "active_points";
tableName: "wallets";
dataType: "number";
columnType: "PgInteger";
data: number;
driverParam: string | number;
notNull: false;
hasDefault: false;
isPrimaryKey: false;
isAutoincrement: false;
hasRuntimeDefault: false;
enumValues: undefined;
baseColumn: never;
generated: undefined;
}, {}, {}>;
statusUpdatedAt: drizzle_orm_pg_core.PgColumn<{
name: "status_updated_at";
tableName: "wallets";
dataType: "date";
columnType: "PgTimestamp";
data: Date;
driverParam: string;
notNull: false;
hasDefault: false;
isPrimaryKey: false;
isAutoincrement: false;
hasRuntimeDefault: false;
enumValues: undefined;
baseColumn: never;
generated: undefined;
}, {}, {}>;
};
dialect: "pg";
}>;
@@ -3963,22 +4059,40 @@ declare const walletsOpenAPISchema: zod.ZodObject<{
address: zod.ZodNullable<zod.ZodString>;
userId: zod.ZodString;
createdAt: zod.ZodString;
powerToken: zod.ZodString;
dailyPowerToken: zod.ZodString;
cashablePowerToken: zod.ZodString;
rank: zod.ZodNullable<zod.ZodNumber>;
level: zod.ZodNullable<zod.ZodNumber>;
preActivePoints: zod.ZodNullable<zod.ZodNumber>;
activePoints: zod.ZodNullable<zod.ZodNumber>;
statusUpdatedAt: zod.ZodNullable<zod.ZodString>;
}, zod.UnknownKeysParam, zod.ZodTypeAny, {
createdAt: string;
userId: string;
addressIndex: number;
address: string | null;
powerToken: string;
dailyPowerToken: string;
cashablePowerToken: string;
rank: number | null;
level: number | null;
preActivePoints: number | null;
activePoints: number | null;
statusUpdatedAt: string | null;
}, {
createdAt: string;
userId: string;
addressIndex: number;
address: string | null;
powerToken: string;
dailyPowerToken: string;
cashablePowerToken: string;
rank: number | null;
level: number | null;
preActivePoints: number | null;
activePoints: number | null;
statusUpdatedAt: string | null;
}>;
declare const walletsRelations: drizzle_orm.Relations<"wallets", {
user: drizzle_orm.One<"user", true>;
@@ -4170,10 +4284,10 @@ declare const transactionsOpenAPISchema: zod.ZodObject<{
fromUserId: string | null;
toUserId: string | null;
hash: string;
powerToken: string;
toFeedId: string | null;
toListId: string | null;
toEntryId: string | null;
powerToken: string;
comment: string | null;
}, {
type: "tip" | "mint" | "burn" | "withdraw" | "purchase";
@@ -4181,10 +4295,10 @@ declare const transactionsOpenAPISchema: zod.ZodObject<{
fromUserId: string | null;
toUserId: string | null;
hash: string;
powerToken: string;
toFeedId: string | null;
toListId: string | null;
toEntryId: string | null;
powerToken: string;
comment: string | null;
}>;
declare const transactionsRelations: drizzle_orm.Relations<"transactions", {
@@ -4783,10 +4897,10 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
fromUserId: string | null;
toUserId: string | null;
hash: string;
powerToken: string;
toFeedId: string | null;
toListId: string | null;
toEntryId: string | null;
powerToken: string;
comment: string | null;
fromUser?: {
name: string | null;
@@ -4903,8 +5017,14 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
userId: string;
addressIndex: number;
address: string | null;
powerToken: string;
dailyPowerToken: string;
cashablePowerToken: string;
rank: number | null;
level: number | null;
preActivePoints: number | null;
activePoints: number | null;
statusUpdatedAt: string | null;
}[];
};
outputFormat: "json" | "text";
@@ -4930,6 +5050,38 @@ declare const _routes: hono_hono_base.HonoBase<Env, {
status: 200;
};
};
"/wallets/ranking": {
$get: {
input: {};
output: {
code: 0;
data: {
user: {
name: string | null;
id: string;
emailVerified: string | null;
image: string | null;
handle: string | null;
createdAt: string;
};
createdAt: string;
userId: string;
addressIndex: number;
address: string | null;
powerToken: string;
dailyPowerToken: string;
cashablePowerToken: string;
rank: number | null;
level: number | null;
preActivePoints: number | null;
activePoints: number | null;
statusUpdatedAt: string | null;
}[];
};
outputFormat: "json" | "text";
status: 200;
};
};
} & {
"/subscriptions": {
$get: {