From 70f13c3e16cb21d0a2da6f95ff2285e7752ef2da Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 29 Jun 2024 23:34:59 +0800 Subject: [PATCH] feat: mandatory invitation code and invitation page --- src/renderer/src/hono.ts | 50 ++++++++---- src/renderer/src/lib/api-fetch.ts | 8 ++ .../src/pages/(external)/invitation.tsx | 77 +++++++++++++++++++ src/renderer/src/queries/invitations.ts | 11 +++ 4 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 src/renderer/src/pages/(external)/invitation.tsx create mode 100644 src/renderer/src/queries/invitations.ts diff --git a/src/renderer/src/hono.ts b/src/renderer/src/hono.ts index 2d0b00326..5f36bb0ae 100644 --- a/src/renderer/src/hono.ts +++ b/src/renderer/src/hono.ts @@ -15,6 +15,20 @@ declare const routes: hono_hono_base.HonoBase; + data: { + [x: string]: number; + }; }; outputFormat: "json"; status: 200; @@ -559,21 +575,27 @@ declare const routes: hono_hono_base.HonoBase; - maintainers: string[]; - location: string; - }>; - }>; + url: string; + routes: { + [x: string]: { + path: string; + example: string; + description: string; + name: string; + categories: string[]; + parameters: { + [x: string]: string; + }; + maintainers: string[]; + location: string; + }; + }; + }; + }; }; outputFormat: "json"; status: 200; diff --git a/src/renderer/src/lib/api-fetch.ts b/src/renderer/src/lib/api-fetch.ts index e96e992d2..4ccc0abfb 100644 --- a/src/renderer/src/lib/api-fetch.ts +++ b/src/renderer/src/lib/api-fetch.ts @@ -30,6 +30,14 @@ export const apiFetch = ofetch.create({ // Or we can present LoginModal here. router.navigate("/login") } + try { + const json = JSON.parse(context.response._data) + if (context.response.status === 400 && json.code === 1003) { + router.navigate("/invitation") + } + } catch { + // ignore + } }, }) diff --git a/src/renderer/src/pages/(external)/invitation.tsx b/src/renderer/src/pages/(external)/invitation.tsx new file mode 100644 index 000000000..90c672e68 --- /dev/null +++ b/src/renderer/src/pages/(external)/invitation.tsx @@ -0,0 +1,77 @@ +import { zodResolver } from "@hookform/resolvers/zod" +import { Logo } from "@renderer/components/icons/logo" +import { StyledButton } from "@renderer/components/ui/button" +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@renderer/components/ui/form" +import { Input } from "@renderer/components/ui/input" +import { useInvitationMutation } from "@renderer/queries/invitations" +import { useEffect } from "react" +import { useForm } from "react-hook-form" +import { useNavigate } from "react-router-dom" +import { z } from "zod" + +const formSchema = z.object({ + code: z.string().min(1), +}) + +export function Component() { + const navigate = useNavigate() + const form = useForm>({ + resolver: zodResolver(formSchema), + }) + const invitationMutation = useInvitationMutation() + + function onSubmit(values: z.infer) { + invitationMutation.mutate(values.code) + } + + useEffect(() => { + if (invitationMutation.isSuccess) { + navigate("/") + } + }) + + return ( +
+ +
+ + ( + + Invitation Code + + + + + + )} + /> +
+ + Active + +
+ + +
+ 😰 Sorry, Follow is currently in + {" "} + early access + {" "} + and requires an invitation to use. +
+
+ ) +} diff --git a/src/renderer/src/queries/invitations.ts b/src/renderer/src/queries/invitations.ts new file mode 100644 index 000000000..a73b5c005 --- /dev/null +++ b/src/renderer/src/queries/invitations.ts @@ -0,0 +1,11 @@ +import { apiClient, getFetchErrorMessage } from "@renderer/lib/api-fetch" +import { useMutation } from "@tanstack/react-query" +import { toast } from "sonner" + +export const useInvitationMutation = () => useMutation({ + mutationFn: (code: string) => apiClient.invitations.use.$post({ json: { code } }), + onSuccess() {}, + async onError(err) { + toast.error(await getFetchErrorMessage(err)) + }, +})