A super basic boilerplate wrapping Next.js route abort events
useUnsavedChangesWarning(form.isDirty === true && status !== 'pending')| import type { HTMLAttributes } from "react"; | |
| import { cn } from "@/lib/cn"; | |
| // Example: <LayerCard><LayerCardSecondary>Title</LayerCardSecondary><LayerCardContent>Content</LayerCardContent></LayerCard> | |
| export function LayerCard({ className, ...props }: HTMLAttributes<HTMLDivElement>) { | |
| return ( | |
| <div | |
| className={cn( | |
| "rounded-lg border border-neutral-200 bg-neutral-100", | |
| className, |
Latches a boolean flag for a short duration, then resets to false
Useful for when an action succeeds, then you want to reset back to normal after a short while
User completes swapping X to Y: temporarily show "Success" then allow user to interact with the form again
const { mutate, status } = useMutation(...)An ergonomic wrapper for react-query's useMutationState
export function useCreatePostMutation(...) {
return useMutation({ mutationKey: ['create-post'], ... })
}
export function useCreatePostMutationState(...) {| import { delay } from '../time' | |
| type PollValueCallback<T> = () => Promise<T> | |
| type PollConditionCallback<T> = (t: T) => boolean | |
| interface PollOptions { | |
| ms?: number | |
| max?: number | |
| signal?: AbortSignal |
| import { delay } from '../time' | |
| import { throttle } from './throttle' | |
| // @TODO: In the future, let's write some tests. It's proving to be challenging | |
| // because vitest has no means of flushing promises, and it causes some tests | |
| // to fail (ergo, we expect callback to resolve faster, but it doesn't). | |
| // | |
| // Manual test for throttle function. To run: | |
| // $ npm i -g ts-node && ts-node throttle.manual.ts | |
| const tests = { |