Menu

Our whole hooks directory is 68 lines: a confirm dialog that takes a callback instead of returning a promise
📰

Our whole hooks directory is 68 lines: a confirm dialog that takes a callback instead of returning a promise

DEV Community: typescript·Daniel Pertu·17 days ago
#jFl84T1D
#devtotypescript#devto#dev#dialog#action
Reading 0:00
15s threshold

Nakodo is a Next.js app over 25 Postgres tables with 76 public pages in front of the login. Its src/hooks directory holds this:

$ wc -l src/hooks/ * 18 src/hooks/use-action.ts 50 src/hooks/use-confirm.tsx 68 total

Enter fullscreen mode

Exit fullscreen mode

There are five other functions in the codebase whose names begin with use , and all five sit in the file of the feature that needs them: usePlanChange and useUpgrade next to the plan switcher, useShares next to the campaign shares editor, useSaveFirst next to the context it reads, useIlloId next to the illustrations. The two in src/hooks are the two that are used by unrelated screens, which is the whole rule for what goes there.

The confirm hook

export type Ask = { title : string ; text : string ; action : string ; // the confirm button cancel ?: string ; destructive ?: boolean ; run : () => void ; };

// One confirmation dialog per component, kept filled while it animates out.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.