feat: referral (#3761)

Co-authored-by: DIYgod <i@diygod.me>
Co-authored-by: Innei <tukon479@gmail.com>
This commit is contained in:
Stephen Zhou
2025-07-10 09:47:11 +08:00
committed by GitHub
co-authored by DIYgod Innei
parent 79c9b05246
commit 30bb37c7b9
62 changed files with 27487 additions and 20935 deletions
@@ -38,6 +38,7 @@
"@radix-ui/react-label": "2.1.7",
"@radix-ui/react-navigation-menu": "1.2.13",
"@radix-ui/react-popover": "1.1.14",
"@radix-ui/react-progress": "1.1.7",
"@radix-ui/react-radio-group": "1.3.7",
"@radix-ui/react-scroll-area": "1.2.9",
"@radix-ui/react-select": "2.2.5",
@@ -28,6 +28,7 @@ const motionBaseMap = {
export const MotionButtonBase = ({
ref,
children,
disabled,
...rest
}: HTMLMotionProps<"button"> & { ref?: React.Ref<HTMLButtonElement | null> }) => {
const isMobile = useMobile()
@@ -35,7 +36,8 @@ export const MotionButtonBase = ({
<m.button
layout="size"
initial
{...motionBaseMap[isMobile ? "mobile" : "pc"]}
disabled={disabled}
{...(disabled ? {} : motionBaseMap[isMobile ? "mobile" : "pc"])}
{...rest}
ref={ref}
>
@@ -0,0 +1,28 @@
"use client"
import { cn } from "@follow/utils/utils"
import * as ProgressPrimitive from "@radix-ui/react-progress"
import * as React from "react"
const Progress = ({
ref,
className,
value,
...props
}: React.ComponentPropsWithoutRef<typeof ProgressPrimitive.Root> & {
ref?: React.RefObject<React.ElementRef<typeof ProgressPrimitive.Root> | null>
}) => (
<ProgressPrimitive.Root
ref={ref}
className={cn("bg-accent/20 relative h-2 w-full overflow-hidden rounded-full", className)}
{...props}
>
<ProgressPrimitive.Indicator
className="bg-accent size-full flex-1 transition-all"
style={{ transform: `translateX(-${100 - (value || 0)}%)` }}
/>
</ProgressPrimitive.Root>
)
Progress.displayName = ProgressPrimitive.Root.displayName
export { Progress }
+23 -1
View File
@@ -13,6 +13,28 @@ export enum Routes {
}
export enum UserRole {
Admin = "admin",
PreProTrial = "pre_pro_trial",
PrePro = "pre_pro",
Free = "free",
/**
* @deprecated
* @see UserRole.Free
*/
// TODO: remove this
Trial = "trial",
User = "user",
Pro = "pro",
}
export const UserRoleName: Record<UserRole, string> = {
[UserRole.Admin]: "Admin",
[UserRole.PreProTrial]: "Pro Preview Trial",
[UserRole.PrePro]: "Pro Preview",
[UserRole.Free]: "Free",
/**
* @deprecated
* @see UserRole.Free
*/
[UserRole.Trial]: "Free",
[UserRole.Pro]: "Pro",
} as const
+1
View File
@@ -16,6 +16,7 @@ export type UserModel = OptionalKey<
| "bio"
| "website"
| "socialLinks"
| "stripeCustomerId"
>
export type ExtractBizResponse<T extends (...args: any[]) => any> = Exclude<
+2
View File
@@ -31,6 +31,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@better-auth/stripe": "1.2.9",
"@electron-toolkit/preload": "3.0.2",
"@electron-toolkit/tsconfig": "1.0.1",
"@hono/node-server": "1.15.0",
@@ -40,6 +41,7 @@
"drizzle-orm": "0.44.2",
"hono": "4.8.1",
"sonner": "2.0.6",
"stripe": "18.2.1",
"zod": "3.25.75"
}
}
+19 -1
View File
@@ -1,3 +1,4 @@
import { stripeClient } from "@better-auth/stripe/client"
import { IN_ELECTRON } from "@follow/shared"
import type { authPlugins } from "@follow/shared/hono"
import type { BetterAuthClientPlugin, BetterFetchOption } from "better-auth/client"
@@ -31,6 +32,7 @@ export const baseAuthPlugins = [
},
}),
twoFactorClient(),
stripeClient({ subscription: true }),
] satisfies BetterAuthClientPlugin[]
export type AuthClient<ExtraPlugins extends BetterAuthClientPlugin[] = []> = ReturnType<
@@ -54,7 +56,19 @@ export class Auth {
this.authClient = createAuthClient({
baseURL: `${this.options.apiURL}/better-auth`,
plugins: baseAuthPlugins,
fetchOptions: this.options.fetchOptions,
fetchOptions: {
...this.options.fetchOptions,
onRequest: (context) => {
const referralCode = localStorage.getItem(getStorageNS("referral-code"))
if (referralCode) {
context.headers.set("folo-referral-code", referralCode)
}
this.options.fetchOptions?.onRequest?.(context)
return context
},
},
})
}
@@ -86,3 +100,7 @@ export class Auth {
}
}
}
// copy from packages/internal/utils/src/ns.ts
const ns = "follow"
const getStorageNS = (key: string) => `${ns}:${key}`
File diff suppressed because it is too large Load Diff
@@ -42,6 +42,10 @@ export const useUserRole = () => {
return useUserStore((state) => state.role)
}
export const useRoleEndAt = () => {
return useUserStore((state) => state.roleEndAt)
}
export const useUserById = (userId: string | undefined) => {
return useUserStore((state) => (userId ? state.users[userId] : undefined))
}
+8 -2
View File
@@ -20,12 +20,14 @@ type UserStore = {
users: Record<string, UserModel>
whoami: MeModel | null
role: UserRole | null
roleEndAt: Date | null
}
const defaultState: UserStore = {
users: {},
whoami: null,
role: null,
roleEndAt: null,
}
export const useUserStore = createZustandStore<UserStore>("user")(() => defaultState)
@@ -71,7 +73,10 @@ class UserSyncService {
const user = honoMorph.toUser(res.user, true)
immerSet((state) => {
state.whoami = { ...user, emailVerified: res.user.emailVerified }
state.role = res.role as UserRole
state.role = res.role
if (res.roleEndAt) {
state.roleEndAt = new Date(res.roleEndAt)
}
})
userActions.upsertMany([user])
@@ -176,7 +181,7 @@ class UserSyncService {
const res = await apiClient().invitations.use.$post({ json: { code } })
if (res.code === 0) {
immerSet((state) => {
state.role = UserRole.User
state.role = UserRole.PrePro
})
}
@@ -249,6 +254,7 @@ class UserActions implements Hydratable, Resetable {
immerSet((state) => {
state.whoami = null
state.role = null
state.roleEndAt = null
})
})
tx.persist(() => UserService.removeCurrentUser())