Skip to content

Commit

Permalink
feat: rsshub list check purchased instance
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Dec 30, 2024
1 parent 5252f97 commit c772b91
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 33 deletions.
64 changes: 39 additions & 25 deletions apps/renderer/src/pages/(main)/(layer)/(subview)/rsshub/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,24 @@ function List({ data }: { data?: RSSHubModel[] }) {
<TableCell>{instance.userLimit || t("rsshub.table.unlimited")}</TableCell>
<TableCell>
<div className="flex items-center gap-2">
{status?.data?.usage?.rsshubId === instance.id && (
<Button disabled className="shrink-0">
{t("rsshub.table.inuse")}
</Button>
)}
{status?.data?.usage?.rsshubId !== instance.id && (
<Button
onClick={() => {
present({
title: t("rsshub.useModal.title"),
content: ({ dismiss }) => (
<SetModalContent dismiss={dismiss} instance={instance} />
),
})
}}
>
{t("rsshub.table.use")}
</Button>
)}
<Button
className="shrink-0"
variant={status?.data?.usage?.rsshubId === instance.id ? "outline" : "primary"}
onClick={() => {
present({
title: t("rsshub.useModal.title"),
content: ({ dismiss }) => (
<SetModalContent dismiss={dismiss} instance={instance} />
),
})
}}
>
{t(
status?.data?.usage?.rsshubId === instance.id
? "rsshub.table.inuse"
: "rsshub.table.use",
)}
</Button>
{me?.id === instance.ownerUserId && (
<Button variant="outline">{t("rsshub.table.edit")}</Button>
)}
Expand All @@ -175,18 +174,23 @@ function List({ data }: { data?: RSSHubModel[] }) {
)
}

const formSchema = z.object({
months: z.coerce.number().min(1).max(12),
})

const SetModalContent = ({ dismiss, instance }: { dismiss: () => void; instance: RSSHubModel }) => {
const { t } = useTranslation("settings")
const setRSSHubMutation = useSetRSSHubMutation()
const details = useAuthQuery(Queries.rsshub.get({ id: instance.id }))
const hasPurchase = !!details.data?.purchase

const formSchema = z.object({
months: z.coerce
.number()
.min(hasPurchase ? 0 : 1)
.max(12),
})

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
months: 1,
months: hasPurchase ? 0 : 1,
},
})

Expand Down Expand Up @@ -235,6 +239,16 @@ const SetModalContent = ({ dismiss, instance }: { dismiss: () => void; instance:
</div>
</CardContent>
</Card>
{details.data?.purchase && (
<div>
<div className="text-sm text-muted-foreground">
{t("rsshub.useModal.purchase_expires_at")}
</div>
<div className="line-clamp-2">
{new Date(details.data.purchase.expiresAt).toLocaleString()}
</div>
</div>
)}
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
{instance.price > 0 && (
Expand All @@ -247,7 +261,7 @@ const SetModalContent = ({ dismiss, instance }: { dismiss: () => void; instance:
<FormControl>
<div className="flex items-center gap-10">
<div className="space-x-2">
<Input className="w-24" type="number" max={12} min={1} {...field} />
<Input className="w-24" type="number" max={12} {...field} />
<span className="text-sm text-muted-foreground">
{t("rsshub.useModal.month")}
</span>
Expand Down
6 changes: 5 additions & 1 deletion apps/renderer/src/queries/rsshub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ export const useSetRSSHubMutation = ({ onError }: MutationBaseProps = {}) =>
mutationFn: (data: { id: string | null; durationInMonths?: number }) =>
apiClient.rsshub.use.$post({ json: data }),

onSuccess: () => {
onSuccess: (_, variables) => {
rsshub.list().invalidate()
rsshub.status().invalidate()

if (variables.id) {
rsshub.get({ id: variables.id }).invalidate()
}
},

onError: (error) => {
Expand Down
1 change: 1 addition & 0 deletions locales/errors/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
"11002": "RSSHub is in use",
"11003": "RSSHub not found",
"11004": "RSSHub user limit exceeded",
"11005": "RSSHub purchase not found",
"12000": "Action limit exceeded"
}
1 change: 1 addition & 0 deletions locales/settings/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@
"rsshub.useModal.about": "About this Instance",
"rsshub.useModal.month": "month",
"rsshub.useModal.months_label": "The number of months you want to purchase",
"rsshub.useModal.purchase_expires_at": "You have purchased this Instance, and your purchase expires at",
"rsshub.useModal.title": "RSSHub Instance",
"rsshub.useModal.useWith": "Use with {{amount}} Power",
"titles.about": "About",
Expand Down
20 changes: 13 additions & 7 deletions packages/shared/src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12726,13 +12726,19 @@ declare const _routes: hono_hono_base.HonoBase<Env, ({
output: {
code: 0;
data: {
description: string | null;
id: string;
ownerUserId: string;
baseUrl: string;
accessKey: string | null;
price: number;
userLimit: number | null;
purchase: {
hash: string | null;
expiresAt: string;
} | null;
instance: {
description: string | null;
id: string;
ownerUserId: string;
price: number;
userLimit: number | null;
baseUrl?: string | null | undefined;
accessKey?: string | null | undefined;
};
};
};
outputFormat: "json";
Expand Down

0 comments on commit c772b91

Please sign in to comment.