Skip to content
Merged
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
33 changes: 32 additions & 1 deletion src/components/ui/button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from "react"
import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"
import { LoaderCircle } from "lucide-react"

import { cn } from "@/lib/utils"

Expand All @@ -22,9 +23,13 @@ const buttonVariants = cva(
},
size: {
default: "h-9 px-4 py-2 has-[>svg]:px-3",
xs: "h-6 gap-1 rounded-md px-2 text-xs has-[>svg]:px-1.5 [&_svg:not([class*='size-'])]:size-3",
sm: "h-8 rounded-md gap-1.5 px-3 has-[>svg]:px-2.5",
lg: "h-10 rounded-md px-6 has-[>svg]:px-4",
icon: "size-9",
"icon-xs": "size-6 rounded-md [&_svg:not([class*='size-'])]:size-3",
"icon-sm": "size-8",
"icon-lg": "size-10",
},
},
defaultVariants: {
Expand All @@ -39,19 +44,45 @@ function Button({
variant,
size,
asChild = false,
loading = false,
disabled,
children,
...props
}: React.ComponentProps<"button"> &
VariantProps<typeof buttonVariants> & {
asChild?: boolean
loading?: boolean
}) {
const Comp = asChild ? Slot : "button"
const ref = React.useRef<HTMLButtonElement>(null)
const [savedWidth, setSavedWidth] = React.useState<number | undefined>(
undefined
)

React.useLayoutEffect(() => {
if (ref.current && !loading) {
setSavedWidth(ref.current.offsetWidth)
}
}, [loading])

return (
<Comp
ref={asChild ? undefined : ref}
data-slot="button"
className={cn(buttonVariants({ variant, size, className }))}
disabled={disabled || loading}
style={loading && savedWidth ? { minWidth: savedWidth } : undefined}
{...props}
/>
>
{asChild ? (
children
) : (
<>
{loading && <LoaderCircle className="animate-spin" />}
{children}
</>
)}
</Comp>
)
}

Expand Down
21 changes: 19 additions & 2 deletions src/lib/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface AuthContextValue {
isLoading: boolean
email: string | null
userId: string | null
name: string | null
login: (email: string) => void
logout: () => Promise<void>
checkAuth: () => Promise<void>
Expand All @@ -31,12 +32,14 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
localStorage.getItem(EMAIL_KEY)
)
const [userId, setUserId] = useState<string | null>(null)
const [name, setName] = useState<string | null>(null)

const clearState = useCallback(() => {
localStorage.removeItem(EMAIL_KEY)
setIsAuthenticated(false)
setEmail(null)
setUserId(null)
setName(null)
queryClient.clear()
}, [queryClient])

Expand All @@ -59,10 +62,11 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
try {
const user = await api
.get("auth/me")
.json<{ id: string; email: string }>()
.json<{ id: string; email: string; name: string | null }>()
setIsAuthenticated(true)
setEmail(user.email)
setUserId(user.id)
setName(user.name)
localStorage.setItem(EMAIL_KEY, user.email)
} catch {
clearState()
Expand All @@ -75,6 +79,18 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
checkAuth()
}, [checkAuth])

// Re-validate auth when the user returns to the tab after being away
useEffect(() => {
function handleVisibilityChange() {
if (document.visibilityState === "visible") {
checkAuth()
}
}
document.addEventListener("visibilitychange", handleVisibilityChange)
return () =>
document.removeEventListener("visibilitychange", handleVisibilityChange)
}, [checkAuth])

useEffect(() => {
setOnUnauthorized(() => {
clearState()
Expand All @@ -87,11 +103,12 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
isLoading,
email,
userId,
name,
login,
logout,
checkAuth,
}),
[isAuthenticated, isLoading, email, userId, login, logout, checkAuth]
[isAuthenticated, isLoading, email, userId, name, login, logout, checkAuth]
)

return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
Expand Down
Loading