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: store to buy subscription limit #2631

Closed
wants to merge 2 commits into from
Closed
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
82 changes: 82 additions & 0 deletions apps/renderer/src/modules/power/store-section/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Button } from "@follow/components/ui/button/index.js"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@follow/components/ui/table/index.jsx"
import { useTranslation } from "react-i18next"

import { useTOTPModalWrapper } from "~/modules/profile/hooks"
import { SettingSectionTitle } from "~/modules/settings/section"
import type { Product } from "~/queries/store"
import { useProductList, usePurchaseProduct } from "~/queries/store"
import { useWallet } from "~/queries/wallet"

export const StoreSection: Component = () => {
const { t } = useTranslation("settings")

const wallet = useWallet()
const myWallet = wallet.data?.[0]

if (!myWallet) return null

return (
<div className="relative flex min-w-0 grow flex-col">
<SettingSectionTitle title={t("wallet.store.title")} />
<ProductList />
</div>
)
}

const ProductList: Component = () => {
const { t } = useTranslation("settings")
const { data: products } = useProductList()
if (!products) return null

return (
<Table>
<TableHeader>
<TableRow>
<TableHead>{t("wallet.store.product.name")}</TableHead>
<TableHead>{t("wallet.store.product.category")}</TableHead>
<TableHead>{t("wallet.store.product.price")}</TableHead>
<TableHead>{t("wallet.store.product.quantity_per_user")}</TableHead>
<TableHead>{t("wallet.store.product.quantity_purchased")}</TableHead>
<TableHead>{t("wallet.store.product.action")}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{products.map((product) => (
<TableRow key={product.id}>
<TableCell>{product.name}</TableCell>
<TableCell>{product.category}</TableCell>
<TableCell>{product.price}</TableCell>
<TableCell>{product.quantityPerUser}</TableCell>
<TableCell>{product.quantityPurchased}</TableCell>
<TableCell className="w-40">
<PurchaseButton product={product} />
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)
}

const PurchaseButton: Component<{ product: Product }> = ({ product }) => {
const mutation = usePurchaseProduct()
const { t } = useTranslation("settings")
const present = useTOTPModalWrapper(mutation.mutateAsync)
return (
<Button
disabled={product.quantityPurchased >= product.quantityPerUser}
onClick={() => present({ productId: product.id })}
isLoading={mutation.isPending}
>
{t("wallet.store.product.buy")}
</Button>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useTranslation } from "react-i18next"
import { useSubViewTitle } from "~/modules/app-layout/subview/hooks"
import { MyWalletSection } from "~/modules/power/my-wallet-section"
import { PowerRanking } from "~/modules/power/ranking"
import { StoreSection } from "~/modules/power/store-section"
import { TransactionsSection } from "~/modules/power/transaction-section"

export function Component() {
Expand All @@ -28,6 +29,9 @@ export function Component() {
<Divider className="my-8" />
<PowerRanking />

<Divider className="my-8" />
<StoreSection />

<Divider className="my-8" />
<AutoResizeHeight>
<TransactionsSection />
Expand Down
34 changes: 34 additions & 0 deletions apps/renderer/src/queries/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useMutation } from "@tanstack/react-query"
import { useTranslation } from "react-i18next"
import { toast } from "sonner"

import { useAuthQuery } from "~/hooks/common"
import { apiClient } from "~/lib/api-fetch"
import { defineQuery } from "~/lib/defineQuery"

export const store = {
products: {
get: () =>
defineQuery(["store", "products"], async () => {
const { data } = await apiClient.store.products.$get()
return data.products
}),
},
}
export type Product = Awaited<
ReturnType<typeof apiClient.store.products.$get>
>["data"]["products"][number]
export const useProductList = () => useAuthQuery(store.products.get())

export const usePurchaseProduct = () => {
const { t } = useTranslation("settings")
return useMutation({
mutationKey: ["purchaseProduct"],
mutationFn: ({ productId, TOTPCode }: { productId: string } & { TOTPCode?: string }) =>
apiClient.store.products.$post({ json: { productId, TOTPCode } }),
onSuccess() {
toast.success(t("wallet.store.product.purchase_success"))
store.products.get().invalidate()
},
})
}
4 changes: 3 additions & 1 deletion locales/errors/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@
"13003": "RSSHub not found",
"13004": "RSSHub user limit exceeded",
"13005": "RSSHub purchase not found",
"13006": "RSSHub config invalid"
"13006": "RSSHub config invalid",
"14000": "Product not found",
"14001": "Product sold out"
}
9 changes: 9 additions & 0 deletions locales/settings/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@
"wallet.rewardDescription.title": "Reward Description",
"wallet.rewardDescription.total": "Total Reward Per Day",
"wallet.sidebar_title": "Power",
"wallet.store.product.action": "Action",
"wallet.store.product.buy": "Buy",
"wallet.store.product.category": "Category",
"wallet.store.product.name": "Name",
"wallet.store.product.price": "Price",
"wallet.store.product.purchase_success": "Product purchased successfully",
"wallet.store.product.quantity_per_user": "Quantity",
"wallet.store.product.quantity_purchased": "Purchased",
"wallet.store.title": "Store",
"wallet.transactions.amount": "Amount",
"wallet.transactions.date": "Date",
"wallet.transactions.description": "Certain transactions incur a {{percentage}}% platform fee to support Follow go further. For details, please refer to the blockchain transaction.",
Expand Down
65 changes: 63 additions & 2 deletions packages/shared/src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6455,6 +6455,23 @@ declare const transactions: drizzle_orm_pg_core.PgTableWithColumns<{
identity: undefined;
generated: undefined;
}, {}, {}>;
toProductId: drizzle_orm_pg_core.PgColumn<{
name: "to_product_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;
identity: undefined;
generated: undefined;
}, {}, {}>;
powerToken: drizzle_orm_pg_core.PgColumn<{
name: "power_token";
tableName: "transactions";
Expand Down Expand Up @@ -6535,6 +6552,7 @@ declare const transactionsOpenAPISchema: zod.ZodObject<{
toListId: zod.ZodNullable<zod.ZodString>;
toEntryId: zod.ZodNullable<zod.ZodString>;
toRSSHubId: zod.ZodNullable<zod.ZodString>;
toProductId: zod.ZodNullable<zod.ZodString>;
powerToken: zod.ZodString;
tax: zod.ZodString;
createdAt: zod.ZodString;
Expand All @@ -6550,6 +6568,7 @@ declare const transactionsOpenAPISchema: zod.ZodObject<{
toListId: string | null;
toEntryId: string | null;
toRSSHubId: string | null;
toProductId: string | null;
tax: string;
comment: string | null;
}, {
Expand All @@ -6563,6 +6582,7 @@ declare const transactionsOpenAPISchema: zod.ZodObject<{
toListId: string | null;
toEntryId: string | null;
toRSSHubId: string | null;
toProductId: string | null;
tax: string;
comment: string | null;
}>;
Expand Down Expand Up @@ -15605,6 +15625,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, ({
toListId: string | null;
toEntryId: string | null;
toRSSHubId: string | null;
toProductId: string | null;
tax: string;
comment: string | null;
fromUser?: {
Expand Down Expand Up @@ -16559,7 +16580,7 @@ declare const _routes: hono_hono_base.HonoBase<Env, ({
status: 200;
};
};
}, "boosts"> | hono_types.MergeSchemaPath<{
}, "/boosts"> | hono_types.MergeSchemaPath<{
"/postgresql": {
$get: {
input: {};
Expand Down Expand Up @@ -16729,7 +16750,47 @@ declare const _routes: hono_hono_base.HonoBase<Env, ({
status: 200;
};
};
}, "/rsshub">, "/">;
}, "/rsshub"> | hono_types.MergeSchemaPath<hono_types.MergeSchemaPath<{
"/": {
$get: {
input: {};
output: {
code: 0;
data: {
products: {
name: string;
id: string;
category: string;
price: number;
quantityPerUser: number;
quantityPurchased: number;
}[];
};
};
outputFormat: "json";
status: 200;
};
};
} & {
"/": {
$post: {
input: {
json: {
productId: string;
TOTPCode?: string | undefined;
};
};
output: {
code: 0;
data: {
transactionHash: string;
};
};
outputFormat: "json";
status: 200;
};
};
}, "/products">, "/store">, "/">;
type AppType = typeof _routes;

export { type ActionsModel, type AirdropActivity, type AppType, type AttachmentsModel, type AuthSession, type AuthUser, CommonEntryFields, type ConditionItem, type DetailModel, type EntriesModel, type EntryReadHistoriesModel, type ExtraModel, type FeedModel, type MediaModel, type MessagingData, MessagingType, type SettingsModel, type UrlReadsModel, account, achievements, achievementsOpenAPISchema, actions, actionsItemOpenAPISchema, actionsOpenAPISchema, actionsRelations, activityEnum, airdrops, airdropsOpenAPISchema, attachmentsZodSchema, authPlugins, boosts, collections, collectionsOpenAPISchema, collectionsRelations, detailModelSchema, entries, entriesOpenAPISchema, entriesRelations, entryReadHistories, entryReadHistoriesOpenAPISchema, entryReadHistoriesRelations, extraZodSchema, feedPowerTokens, feedPowerTokensOpenAPISchema, feedPowerTokensRelations, feeds, feedsOpenAPISchema, feedsRelations, inboxHandleSchema, inboxes, inboxesEntries, inboxesEntriesInsertOpenAPISchema, type inboxesEntriesModel, inboxesEntriesOpenAPISchema, inboxesEntriesRelations, inboxesOpenAPISchema, inboxesRelations, invitations, invitationsOpenAPISchema, invitationsRelations, languageSchema, levels, levelsOpenAPISchema, levelsRelations, lists, listsOpenAPISchema, listsRelations, listsSubscriptions, listsSubscriptionsOpenAPISchema, listsSubscriptionsRelations, listsTimeline, listsTimelineOpenAPISchema, listsTimelineRelations, lower, mediaZodSchema, messaging, messagingOpenAPISchema, messagingRelations, rsshub, rsshubOpenAPISchema, rsshubPurchase, rsshubUsage, rsshubUsageOpenAPISchema, rsshubUsageRelations, session, settings, subscriptions, subscriptionsOpenAPISchema, subscriptionsRelations, timeline, timelineOpenAPISchema, timelineRelations, transactionType, transactions, transactionsOpenAPISchema, transactionsRelations, twoFactor, urlReads, urlReadsOpenAPISchema, user, users, usersOpenApiSchema, usersRelations, verification, wallets, walletsOpenAPISchema, walletsRelations };
Loading