refactor: power modal

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2024-10-10 16:58:08 +08:00
parent d4a2cf6816
commit f490d7d62f
25 changed files with 224 additions and 161 deletions
-2
View File
@@ -10,8 +10,6 @@ export const initPosthog = () => {
}
export const posthog = initPosthog()
// posthog?.debug(true)
export const trackEvent = (event: string, properties?: Record<string, any>) => {
const userId = getUser()?.id
if (!userId) return
+1
View File
@@ -99,6 +99,7 @@
"sonner": "^1.5.0",
"swiper": "11.1.14",
"tailwind-merge": "2.5.2",
"tailwindcss-motion": "0.3.0-beta",
"tldts": "6.1.48",
"unified": "11.0.5",
"use-context-selector": "2.0.0",
+1 -1
View File
@@ -9,9 +9,9 @@ import { whoami } from "~/atoms/user"
import { cn } from "~/lib/utils"
import { usePresentUserProfileModal } from "~/modules/profile/hooks"
import { UserAvatar } from "../modules/user/UserAvatar"
import { FollowIcon } from "./icons/follow"
import { Logo } from "./icons/logo"
import { UserAvatar } from "./user-button"
const useMotionValueToState = (value: MotionValue<number>) => {
const [state, setState] = useState(value.get())
+9 -6
View File
@@ -13,10 +13,18 @@ declare global {
}
}
}
export const initPostHog = async () => {
export const initPostHog = () => {
if (env.VITE_POSTHOG_KEY === undefined) return
posthog.init(env.VITE_POSTHOG_KEY, {
person_profiles: "identified_only",
enable_heatmaps: false,
autocapture: false,
loaded: () => {
const user = whoami()
if (user) {
posthog.identify(user.id, { name: user.name, handle: user.handle })
}
},
})
const { capture, reset } = posthog
@@ -40,9 +48,4 @@ export const initPostHog = async () => {
] as const)
},
}
const user = whoami()
if (user) {
posthog.identify(user.id, { name: user.name, handle: user.handle })
}
}
@@ -2,7 +2,7 @@ import { useTranslation } from "react-i18next"
import { Button } from "~/components/ui/button"
import { Divider } from "~/components/ui/divider"
import { UserAvatar } from "~/components/user-button"
import { UserAvatar } from "~/modules/user/UserAvatar"
import { useEntry } from "~/store/entry"
import { useFeedById } from "~/store/feed"
@@ -12,11 +12,11 @@ import { setFeedColumnShow, useFeedColumnShow, useSidebarActiveView } from "~/at
import { Logo } from "~/components/icons/logo"
import { ActionButton } from "~/components/ui/button"
import { Popover, PopoverContent, PopoverTrigger } from "~/components/ui/popover"
import { ProfileButton } from "~/components/user-button"
import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
import { useI18n } from "~/hooks/common"
import { stopPropagation } from "~/lib/dom"
import { cn } from "~/lib/utils"
import { ProfileButton } from "~/modules/user/ProfileButton"
const useBackHome = (active: number) => {
const navigate = useNavigateEntry()
@@ -0,0 +1,29 @@
import { Divider } from "~/components/ui/divider"
import { MyWalletSection } from "./my-wallet-section"
import { TransactionsSection } from "./transaction-section"
export const PowerModalContent = () => {
// const [tab, setTab] = useState<"power" | "transaction">("power")
return (
<div className="flex h-0 w-full grow flex-col">
<div className="motion-preset-shake center -mt-3 mb-6 h-24 text-accent motion-delay-500">
<i className="i-mgc-power size-20" />
</div>
<MyWalletSection />
<Divider className="my-8" />
<TransactionsSection className="h-0 grow overflow-auto pb-5" />
{/* {tab === "power" && <MyWalletSection />}
{tab === "transaction" && <TransactionsSection />} */}
{/* <div className="absolute bottom-0 inset-x-0 flex center">
<Button variant="ghost" onClick={() => setTab(tab === "power" ? "transaction" : "power")}>
{tab === "power" ? "Transactions" : "Power"}
</Button>
</div> */}
</div>
)
}
+19
View File
@@ -0,0 +1,19 @@
import { t } from "i18next"
import { useCallback } from "react"
import { useModalStack } from "~/components/ui/modal"
import { SlideUpModal } from "~/components/ui/modal/stacked/custom-modal"
import { PowerModalContent } from "./PowerModalContent"
export const usePowerModal = () => {
const { present } = useModalStack()
return useCallback(() => {
return present({
title: t("settings:titles.power"),
content: () => <PowerModalContent />,
CustomModalComponent: SlideUpModal.class(tw`aspect-[7/8]`),
})
}, [present])
}
@@ -4,7 +4,6 @@ import { Trans, useTranslation } from "react-i18next"
import { useWhoami } from "~/atoms/user"
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"
@@ -125,7 +124,6 @@ export const MyWalletSection = () => {
</Tooltip>
</div>
</div>
<Divider className="my-8" />
</>
)
}
@@ -23,7 +23,7 @@ import { SettingSectionTitle } from "~/modules/settings/section"
import { Balance } from "~/modules/wallet/balance"
import { useWallet, useWalletTransactions } from "~/queries/wallet"
export const TransactionsSection = () => {
export const TransactionsSection: Component = ({ className }) => {
const { t } = useTranslation("settings")
const user = useWhoami()
const wallet = useWallet({ userId: user?.id })
@@ -35,7 +35,7 @@ export const TransactionsSection = () => {
if (transactions.isLoading) {
return (
<div className="center mt-12">
<div className={cn("center mt-12", className)}>
<LoadingCircle size="large" />
</div>
)
@@ -45,9 +45,9 @@ export const TransactionsSection = () => {
<div className="relative flex min-w-0 grow flex-col">
<SettingSectionTitle title={t("wallet.transactions.title")} />
<div className="w-fit min-w-0 grow overflow-x-auto">
<div className={cn("w-fit min-w-0 grow overflow-x-auto", className)}>
<Table className="w-full table-fixed">
<TableHeader>
<TableHeader className="sticky top-0 z-10 bg-theme-background">
<TableRow className="[&_*]:!font-semibold">
<TableHead className="whitespace-nowrap text-center" size="sm">
{t("wallet.transactions.type")}
@@ -133,7 +133,7 @@ export const SettingGeneral = () => {
}),
{
label: t("general.rebuild_database.label"),
action: async () => {
action: () => {
present({
title: t("general.rebuild_database.title"),
clickOutsideToDismiss: true,
@@ -143,7 +143,7 @@ export const SettingGeneral = () => {
<p>{t("general.rebuild_database.warning.line2")}</p>
<div className="mt-4 flex justify-end">
<Button
className="bg-red-600 px-3 text-white dark:bg-red-500"
className="bg-red-500 px-3 text-white"
onClick={async () => {
await clearLocalPersistStoreData()
window.location.reload()
@@ -1,13 +0,0 @@
import { SettingsTitle } from "../../title"
import { MyWalletSection } from "./my-wallet-section"
import { TransactionsSection } from "./transaction-section"
export const SettingWallet = () => (
<div className="mt-4 flex grow flex-col">
<SettingsTitle />
<MyWalletSection />
<TransactionsSection />
</div>
)
@@ -0,0 +1,41 @@
import type { FC } from "react"
import { useTranslation } from "react-i18next"
import { Link } from "react-router-dom"
import { LoginModalContent } from "~/modules/auth/LoginModalContent"
import { UserArrowLeftIcon } from "../../components/icons/user"
import { ActionButton } from "../../components/ui/button"
import { PlainModal } from "../../components/ui/modal/stacked/custom-modal"
import { useModalStack } from "../../components/ui/modal/stacked/hooks"
export interface LoginProps {
method?: "redirect" | "modal"
}
export const LoginButton: FC<LoginProps> = (props) => {
const { method } = props
const modalStack = useModalStack()
const { t } = useTranslation()
const Content = (
<ActionButton
className="relative z-[1]"
onClick={
method === "modal"
? () => {
modalStack.present({
CustomModalComponent: PlainModal,
title: "Login",
id: "login",
content: () => <LoginModalContent runtime={window.electron ? "app" : "browser"} />,
clickOutsideToDismiss: true,
})
}
: undefined
}
tooltip={t("words.login")}
>
<UserArrowLeftIcon className="size-4" />
</ActionButton>
)
return method === "modal" ? Content : <Link to="/login">{Content}</Link>
}
@@ -1,28 +1,22 @@
import { repository } from "@pkg"
import type { FC } from "react"
import { forwardRef, memo, useState } from "react"
import { memo, useState } from "react"
import { useTranslation } from "react-i18next"
import { Link } from "react-router-dom"
import { useWhoami } from "~/atoms/user"
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar"
import { useSignOut } from "~/hooks/biz/useSignOut"
import { useAuthQuery, useMeasure } from "~/hooks/common"
import { apiClient } from "~/lib/api-fetch"
import { defineQuery } from "~/lib/defineQuery"
import { useMeasure } from "~/hooks/common"
import { nextFrame } from "~/lib/dom"
import { replaceImgUrlIfNeed } from "~/lib/img-proxy"
import { cn } from "~/lib/utils"
import { useAchievementModal } from "~/modules/achievement/hooks"
import { LoginModalContent } from "~/modules/auth/LoginModalContent"
import { usePowerModal } from "~/modules/power/hooks"
import { usePresentUserProfileModal } from "~/modules/profile/hooks"
import { useSettingModal } from "~/modules/settings/modal/hooks"
import { Balance } from "~/modules/wallet/balance"
import { useSession } from "~/queries/auth"
import { useWallet } from "~/queries/wallet"
import { UserArrowLeftIcon } from "./icons/user"
import { ActionButton } from "./ui/button"
import { ActionButton } from "../../components/ui/button"
import {
DropdownMenu,
DropdownMenuContent,
@@ -30,41 +24,12 @@ import {
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "./ui/dropdown-menu/dropdown-menu"
import { PlainModal } from "./ui/modal/stacked/custom-modal"
import { useModalStack } from "./ui/modal/stacked/hooks"
import { RootPortal } from "./ui/portal"
} from "../../components/ui/dropdown-menu/dropdown-menu"
import { RootPortal } from "../../components/ui/portal"
import type { LoginProps } from "./LoginButton"
import { LoginButton } from "./LoginButton"
import { UserAvatar } from "./UserAvatar"
interface LoginProps {
method?: "redirect" | "modal"
}
export const LoginButton: FC<LoginProps> = (props) => {
const { method } = props
const modalStack = useModalStack()
const { t } = useTranslation()
const Content = (
<ActionButton
className="relative z-[1]"
onClick={
method === "modal"
? () => {
modalStack.present({
CustomModalComponent: PlainModal,
title: "Login",
id: "login",
content: () => <LoginModalContent runtime={window.electron ? "app" : "browser"} />,
clickOutsideToDismiss: true,
})
}
: undefined
}
tooltip={t("words.login")}
>
<UserArrowLeftIcon className="size-4" />
</ActionButton>
)
return method === "modal" ? Content : <Link to="/login">{Content}</Link>
}
export const ProfileButton: FC<LoginProps> = memo((props) => {
const { status, session } = useSession()
const { user } = session || {}
@@ -75,6 +40,9 @@ export const ProfileButton: FC<LoginProps> = memo((props) => {
const { t } = useTranslation()
const [ref, { x, y }] = useMeasure()
const [dropdown, setDropdown] = useState(false)
const presentPowerModal = usePowerModal()
if (status !== "authenticated") {
return <LoginButton {...props} />
}
@@ -116,7 +84,7 @@ export const ProfileButton: FC<LoginProps> = memo((props) => {
<DropdownMenuItem
onClick={() => {
nextFrame(() => settingModalPresent("wallet"))
nextFrame(presentPowerModal)
}}
>
<div className="flex w-full items-center justify-between gap-6 px-1.5 font-semibold">
@@ -185,78 +153,22 @@ export const ProfileButton: FC<LoginProps> = memo((props) => {
})
ProfileButton.displayName = "ProfileButton"
export const UserAvatar = forwardRef<
HTMLDivElement,
{
className?: string
avatarClassName?: string
hideName?: boolean
userId?: string
enableModal?: boolean
style?: React.CSSProperties
} & LoginProps
>(({ className, avatarClassName, hideName, userId, enableModal, style, ...props }, ref) => {
const { session, status } = useSession({
enabled: !userId,
})
const presentUserProfile = usePresentUserProfileModal("drawer")
const profile = useAuthQuery(
defineQuery(["profiles", userId], async () => {
const res = await apiClient.profiles.$get({
query: { id: userId! },
})
return res.data
}),
{
enabled: !!userId,
},
)
if (!userId && status !== "authenticated") {
return <LoginButton {...props} />
}
const renderUserData = userId ? profile.data : session?.user
return (
<div
style={style}
ref={ref}
className={cn(
"flex h-20 items-center justify-center gap-2 px-5 py-2 font-medium text-zinc-600 dark:text-zinc-300",
className,
)}
onClick={enableModal ? () => presentUserProfile(userId) : undefined}
>
<Avatar
className={cn(
"aspect-square h-full w-auto overflow-hidden rounded-full border bg-stone-300",
avatarClassName,
)}
>
<AvatarImage
className="duration-200 animate-in fade-in-0"
src={replaceImgUrlIfNeed(renderUserData?.image || undefined)}
/>
<AvatarFallback>{renderUserData?.name?.slice(0, 2)}</AvatarFallback>
</Avatar>
{!hideName && <div>{renderUserData?.name}</div>}
</div>
)
})
UserAvatar.displayName = "UserAvatar"
function PowerButton() {
const user = useWhoami()
const wallet = useWallet({ userId: user?.id })
const { isLoading } = wallet
const myWallet = wallet.data?.[0]
return (
<div className="flex items-center gap-1">
<i className="i-mgc-power text-accent" />
<Balance>
{BigInt(myWallet?.dailyPowerToken || 0n) + BigInt(myWallet?.cashablePowerToken || 0n)}
</Balance>
{isLoading ? (
<span className="h-3 w-8 animate-pulse rounded-xl bg-theme-inactive" />
) : (
<Balance>
{BigInt(myWallet?.dailyPowerToken || 0n) + BigInt(myWallet?.cashablePowerToken || 0n)}
</Balance>
)}
</div>
)
}
@@ -266,7 +178,7 @@ function LevelButton() {
<div className="flex items-center gap-1">
<i className="i-mgc-vip-2-cute-fi text-accent" />
<span>Lv.1</span>
<sub className="text-[0.6rem] font-normal">x1</sub>
<sub className="-translate-y-px text-[0.6rem] font-normal">x1</sub>
</div>
)
}
@@ -0,0 +1,75 @@
import { forwardRef } from "react"
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar"
import { useAuthQuery } from "~/hooks/common"
import { apiClient } from "~/lib/api-fetch"
import { defineQuery } from "~/lib/defineQuery"
import { replaceImgUrlIfNeed } from "~/lib/img-proxy"
import { cn } from "~/lib/utils"
import { usePresentUserProfileModal } from "~/modules/profile/hooks"
import { useSession } from "~/queries/auth"
import type { LoginProps } from "./LoginButton"
import { LoginButton } from "./LoginButton"
export const UserAvatar = forwardRef<
HTMLDivElement,
{
className?: string
avatarClassName?: string
hideName?: boolean
userId?: string
enableModal?: boolean
style?: React.CSSProperties
} & LoginProps
>(({ className, avatarClassName, hideName, userId, enableModal, style, ...props }, ref) => {
const { session, status } = useSession({
enabled: !userId,
})
const presentUserProfile = usePresentUserProfileModal("drawer")
const profile = useAuthQuery(
defineQuery(["profiles", userId], async () => {
const res = await apiClient.profiles.$get({
query: { id: userId! },
})
return res.data
}),
{
enabled: !!userId,
},
)
if (!userId && status !== "authenticated") {
return <LoginButton {...props} />
}
const renderUserData = userId ? profile.data : session?.user
return (
<div
style={style}
ref={ref}
className={cn(
"flex h-20 items-center justify-center gap-2 px-5 py-2 font-medium text-zinc-600 dark:text-zinc-300",
className,
)}
onClick={enableModal ? () => presentUserProfile(userId) : undefined}
>
<Avatar
className={cn(
"aspect-square h-full w-auto overflow-hidden rounded-full border bg-stone-300",
avatarClassName,
)}
>
<AvatarImage
className="duration-200 animate-in fade-in-0"
src={replaceImgUrlIfNeed(renderUserData?.image || undefined)}
/>
<AvatarFallback>{renderUserData?.name?.slice(0, 2)}</AvatarFallback>
</Avatar>
{!hideName && <div>{renderUserData?.name}</div>}
</div>
)
})
UserAvatar.displayName = "UserAvatar"
@@ -9,9 +9,9 @@ import { LoadingWithIcon } from "~/components/ui/loading"
import { useCurrentModal } from "~/components/ui/modal"
import { RadioGroup } from "~/components/ui/radio-group"
import { RadioCard } from "~/components/ui/radio-group/RadioCard"
import { UserAvatar } from "~/components/user-button"
import { useI18n } from "~/hooks/common"
import { nextFrame } from "~/lib/dom"
import { UserAvatar } from "~/modules/user/UserAvatar"
import { useWallet, useWalletTipMutation } from "~/queries/wallet"
import { useFeedClaimModal } from "../claim"
+1 -1
View File
@@ -5,9 +5,9 @@ import { useLocation, useNavigate } from "react-router-dom"
import { Logo } from "~/components/icons/logo"
import { Button } from "~/components/ui/button"
import { UserAvatar } from "~/components/user-button"
import { useSignOut } from "~/hooks/biz/useSignOut"
import { LOGIN_CALLBACK_URL, loginHandler } from "~/lib/auth"
import { UserAvatar } from "~/modules/user/UserAvatar"
export function Component() {
return (
@@ -5,8 +5,8 @@ import { useNavigate } from "react-router-dom"
import { PoweredByFooter } from "~/components/common/PoweredByFooter"
import { Button } from "~/components/ui/button"
import { UserAvatar } from "~/components/user-button"
import { apiClient } from "~/lib/api-fetch"
import { UserAvatar } from "~/modules/user/UserAvatar"
export function Component() {
const navigate = useNavigate()
@@ -1,16 +0,0 @@
import { SettingWallet } from "~/modules/settings/tabs/wallet"
import { defineSettingPageData } from "~/modules/settings/utils"
const iconName = `i-mgc-power-outline`
const priority = 1040
export const loader = defineSettingPageData({
iconName,
name: "titles.power",
priority,
headerIcon: `i-mgc-power text-accent`,
})
export function Component() {
return <SettingWallet />
}
+1
View File
@@ -52,6 +52,7 @@ export default defineConfig(
"unicorn/prefer-math-trunc": "off",
"@eslint-react/no-clone-element": 0,
"@eslint-react/hooks-extra/no-direct-set-state-in-use-effect": 0,
"@typescript-eslint/no-misused-promises": 0,
// NOTE: Disable this temporarily
"react-compiler/react-compiler": 0,
"no-restricted-syntax": 0,
+12
View File
@@ -588,6 +588,9 @@ importers:
tailwind-merge:
specifier: 2.5.2
version: 2.5.2
tailwindcss-motion:
specifier: 0.3.0-beta
version: 0.3.0-beta(tailwindcss@3.4.13)
tldts:
specifier: 6.1.48
version: 6.1.48
@@ -8261,6 +8264,11 @@ packages:
peerDependencies:
tailwindcss: '>=3.0.0 || insiders'
tailwindcss-motion@0.3.0-beta:
resolution: {integrity: sha512-dBmakhcseaB7zK82H9zMmzNhAZFCA9DxWeuw0rkttIarur/7tnjWg1Nv9LlZZR9c+YctRyZns3D+1Fjbksq7Fg==}
peerDependencies:
tailwindcss: '>=3.0.0 || insiders'
tailwindcss@3.4.13:
resolution: {integrity: sha512-KqjHOJKogOUt5Bs752ykCeiwvi0fKVkr5oqsFNt/8px/tA8scFPIlkygsf6jXrfCqGHz7VflA6+yytWuM+XhFw==}
engines: {node: '>=14.0.0'}
@@ -18088,6 +18096,10 @@ snapshots:
dependencies:
tailwindcss: 3.4.13
tailwindcss-motion@0.3.0-beta(tailwindcss@3.4.13):
dependencies:
tailwindcss: 3.4.13
tailwindcss@3.4.13:
dependencies:
'@alloc/quick-lru': 5.2.0
+3
View File
@@ -151,7 +151,10 @@ export default resolveConfig({
require("tailwindcss-animate"),
require("@tailwindcss/container-queries"),
require("@tailwindcss/typography"),
require("tailwindcss-motion"),
require("./apps/renderer/src/styles/tailwind-extend.css"),
plugin(({ addVariant }) => {
addVariant("f-motion-reduce", '[data-motion-reduce="true"] &')
addVariant("group-motion-reduce", ':merge(.group)[data-motion-reduce="true"] &')