Skip to content

An open-source, faster Devin alternative

TypeScript

refactor(web): extract useTransientFlag for "set flag, clear after Ns"

Igor Zalutski2f2fae31ae70
diff --git a/web/src/components/copy-row.tsx b/web/src/components/copy-row.tsx
index 28a6e8e4..69436154 100644
--- a/web/src/components/copy-row.tsx
+++ b/web/src/components/copy-row.tsx
@@ -1,6 +1,7 @@
import { useState } from 'react'
import { Check, Copy, Eye, EyeOff } from 'lucide-react'
import { Button } from '@/components/ui/button'
+import { useTransientFlag } from '@/lib/use-transient-flag'
import { cn } from '@/lib/utils'
/**
@@ -19,7 +20,7 @@ export function CopyRow({
maskable?: boolean
className?: string
}) {
- const [copied, setCopied] = useState(false)
+ const [copied, markCopied] = useTransientFlag(1500)
const [revealed, setRevealed] = useState(!maskable)
const shown =
@@ -29,8 +30,7 @@ export function CopyRow({
const copy = () => {
void navigator.clipboard.writeText(copyText)
- setCopied(true)
- setTimeout(() => setCopied(false), 1500)
+ markCopied()
}
return (
diff --git a/web/src/lib/use-transient-flag.ts b/web/src/lib/use-transient-flag.ts
new file mode 100644
index 00000000..924111d8
--- /dev/null
+++ b/web/src/lib/use-transient-flag.ts
@@ -0,0 +1,27 @@
+import { useCallback, useEffect, useRef, useState } from 'react'
+
+/**
+ * A boolean that flips `true` on `trigger()` and auto-resets to `false` after
+ * `ms`. Centralizes the repeated "set a flag, clear it on a timer" pattern
+ * (copied / saved confirmations) with proper cleanup — the timer is cleared on
+ * unmount, so it never calls setState after the component is gone.
+ */
+export function useTransientFlag(ms = 2000): [boolean, () => void] {
+ const [on, setOn] = useState(false)
+ const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
+
+ useEffect(
+ () => () => {
+ if (timer.current) clearTimeout(timer.current)
+ },
+ [],
+ )
+
+ const trigger = useCallback(() => {
+ setOn(true)
+ if (timer.current) clearTimeout(timer.current)
+ timer.current = setTimeout(() => setOn(false), ms)
+ }, [ms])
+
+ return [on, trigger]
+}
diff --git a/web/src/pages/Billing.tsx b/web/src/pages/Billing.tsx
index 9ee73c45..fd9b4ae1 100644
--- a/web/src/pages/Billing.tsx
+++ b/web/src/pages/Billing.tsx
@@ -3,6 +3,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { CircleAlert, CircleCheck } from 'lucide-react'
import { notifyError } from '@/lib/errors'
+import { useTransientFlag } from '@/lib/use-transient-flag'
import {
autumnSubscribeConcurrency,
autumnTopup,
@@ -451,7 +452,7 @@ function AutoTopupCard({
const enabled = draft.enabled ?? current?.enabled ?? false
const threshold = draft.threshold ?? current?.threshold ?? 5
const quantity = draft.quantity ?? current?.quantity ?? 25
- const [saved, setSaved] = useState(false)
+ const [saved, markSaved] = useTransientFlag(3000)
const [confirm, setConfirm] = useState(false)
const mutation = useMutation({
@@ -464,8 +465,7 @@ function AutoTopupCard({
}
queryClient.invalidateQueries({ queryKey: ['autumn-billing'] })
setDraft({}) // server config is canonical again
- setSaved(true)
- setTimeout(() => setSaved(false), 3000)
+ markSaved()
},
onError: (e) => notifyError("Couldn't save auto-top-up settings.", e),
})
diff --git a/web/src/pages/Settings.tsx b/web/src/pages/Settings.tsx
index 31019c01..da297949 100644
--- a/web/src/pages/Settings.tsx
+++ b/web/src/pages/Settings.tsx
@@ -1,6 +1,7 @@
import { useState, type ReactNode } from 'react'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { notifyError } from '@/lib/errors'
+import { useTransientFlag } from '@/lib/use-transient-flag'
import {
deleteCustomDomain,
getInvitations,
@@ -70,7 +71,7 @@ export default function Settings() {
// Local edits override the fetched name; null = "not edited" (avoids an
// effect to sync the field with the query).
const [draftName, setDraftName] = useState<string | null>(null)
- const [saved, setSaved] = useState(false)
+ const [saved, markSaved] = useTransientFlag(2000)
const [domainInput, setDomainInput] = useState('')
const [confirmRemoveDomain, setConfirmRemoveDomain] = useState(false)
const name = draftName ?? org?.name ?? ''
@@ -80,8 +81,7 @@ export default function Settings() {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['org'] })
setDraftName(null)
- setSaved(true)
- setTimeout(() => setSaved(false), 2000)
+ markSaved()
},
onError: (e) => notifyError("Couldn't save organization settings.", e),
})