types APIs
- SimpleOptions
- RootState
- User
- ChainType
- FeedbackCategoryAPI
- FeedbackStatusAPI
- FeedbackUserProfileAPI
- NavItems
- SingleNavItem
- NonNullableChildrenDeep
- SingleArticle
- ColumnType
- DataType
- Hub
- PluginConfig
- RawEventMessage
- DualStakingInfo
- StakingInfo
- SyrupInfo
- CommonStakingInfo
- LairInfo
- StakingBasic
- DualStakingBasic
- SyrupBasic
- Questionnaire
- TaskStatuses
- LoginPayload
- SignupPayload
- MutationType
- ProteinSize
- Config
- Themes
- defaults
- CoordinateSpaceInitial
- Background
- Metric
- Metadata
- TManageValue
- Domain
- Organization
- OrganizationTag
- Scan
- ScanSchema
- Query
- SavedSearch
- Role
- ScanTask
- Vulnerability
- GenericObject
- Annotation
- AnnotationList
- VideoAnnotation
- IconName
- IllustrationName
- INotification
- BottomTabScreens
- KYCScreens
- CCVPScreens
- LoginScreens
- PreAuthScreens
- SignUpScreens
- TFAScreens
- PNVScreens
- ProfileScreens
- SecurityScreens
- SMTYFScreens
- WalletScreens
- WithdrawalScreens
- StackNavigationProps
- Coin
- Country
- TransactionStatus
- SetApiActionPayloadInterface
- LoadingStateInterface
- MessageStateInterface
- UiOptionType
- ApiDiffInterface
- ApiStateInterface
- Account
- NFT
- NFTDetails
- AlertType
- DefaultTemplateOptions
- ProviderProps
- AlertTimer
- AlertInstance
- AlertOptions
- TemplateAlertOptions
- AlertContainerFactory
- ChainBase
- ChainNetwork
- WalletId
- ProposalType
- ChainEventNotification
- WebsocketMessageNames
- WebsocketNamespaces
- NotificationCategories
- InviteCodeAttributes
- IPostNotificationData
- ChainCategoryType
- Screens
- NavigationProperty
- IP
- Maybe
- Tuple
- ChainProperties
- ApiPromise
- ApiAction
- BlueprintOptions
- ContractQuery
- ContractOptions
- ContractTx
- KeyringPair
- ContractDryRunParams
- InstantiateData
- SubmittableExtrinsic
- DbState
- OnInstantiateSuccess$Code
- OnInstantiateSuccess$Hash
- InstantiateState
- ApiState
- OrFalsy
- Bytes
- AbiParam
- Registry
- Weight
- SubmittableResult
- Hash
- CodeBundleDocument
- MyCodeBundles
- ContractDocument
- MyContracts
- Database
- DbStatistics
- UserDocument
- QueuedTxOptions
- TransactionsState
- DropdownOption
- DropdownProps
- ValidFormField
- SetState
- BN
- CallResult
- ContractPromise
- RegistryError
- Abi
- TypeDef
- Validation
- FileState
- UseWeight
- ArgComponentProps
- TypeDefInfo
- UseMetadata
- AbiMessage
- AbiConstructor
- InstantiateProps
- CodeSubmittableResult
- BlueprintSubmittableResult
- BlueprintPromise
- Step2FormData
- TxOptions
- TransactionsQueue
- Keyring
- DbQuery
- UseBalance
- CodeBundle
- ValidateFn
- MetadataState
- UseStepper
- UseToggle
- Party
- Restaurant
- BoardMember
- Id
- Label
- Priority
- Avatar
- PriorityValue
- AuthSetup
- IColumn
- Board
- ITask
- TaskComment
- NewTaskComment
- WithTheme
- UserDetail
- TasksByColumn
- NewTask
- ValueOf
- APIResponse
- Nullable
- Currency
- Route
- Author
- Post
- Category
- Watch
- ComputeCallback
Other Related APIs
- react#useState
- react#useEffect
- react#useCallback
- react#ReactNode
- react#createContext
- react#useMemo
- react#useRef
- react#ComponentType
- react-dom#createPortal
- react-transition-group#TransitionGroup
- types#Account
- types#AlertType
- types#DefaultTemplateOptions
- types#ProviderProps
- types#AlertTimer
- types#AlertOptions
- types#TemplateAlertOptions
- types#AlertContainerFactory
- components#Transition
types#AlertInstance TypeScript Examples
The following examples show how to use
types#AlertInstance.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Alert.tsx From gear-js with GNU General Public License v3.0 | 4 votes |
AlertProvider = ({ children, template: Template, containerClassName }: Props) => {
const root = useRef<HTMLDivElement | null>(null);
const timers = useRef<AlertTimer>(new Map());
const [alerts, setAlerts] = useState<AlertInstance[]>([]);
const removeTimer = useCallback((alertId: string) => {
const timerId = timers.current.get(alertId);
if (timerId) {
clearTimeout(timerId);
timers.current.delete(alertId);
}
}, []);
const remove = useCallback(
(alertId: string) => {
removeTimer(alertId);
setAlerts((prevState) => prevState.filter((alert) => alert.id !== alertId));
},
[removeTimer],
);
const createTimer = useCallback(
(alertId: string, timeout = 0) => {
if (timeout > 0) {
const timerId = setTimeout(() => remove(alertId), timeout);
timers.current.set(alertId, timerId);
}
},
[remove],
);
const show = useCallback(
(content: ReactNode, options: AlertOptions): string => {
const id = nanoid(6);
createTimer(id, options.timeout);
setAlerts((prevState) => [
...prevState,
{
id,
content,
options,
},
]);
return id;
},
[createTimer],
);
const update = useCallback(
(alertId: string, content: ReactNode, options?: AlertOptions) => {
removeTimer(alertId);
setAlerts((prevState) =>
prevState.map((alert) => {
if (alert.id !== alertId) return alert;
const updatedAlert = {
id: alert.id,
content,
options: {
...alert.options,
...options,
},
};
createTimer(updatedAlert.id, updatedAlert.options.timeout);
return updatedAlert;
}),
);
},
[removeTimer, createTimer],
);
const getAlertTemplate = useCallback(
(templateOptions: AlertOptions) => (content: ReactNode, options?: TemplateAlertOptions) =>
show(content, {
...templateOptions,
...options,
}),
[show],
);
const alertContext = useMemo(
() => ({
update,
remove,
info: getAlertTemplate(DEFAULT_INFO_OPTIONS),
error: getAlertTemplate(DEFAULT_ERROR_OPTIONS),
success: getAlertTemplate(DEFAULT_SUCCESS_OPTIONS),
loading: getAlertTemplate(DEFAULT_LOADING_OPTIONS),
}),
[update, remove, getAlertTemplate],
);
useEffect(() => {
root.current = document.createElement('div');
root.current.id = 'alert-root';
containerClassName && root.current.classList.add(containerClassName);
document.body.appendChild(root.current);
}, []);
return (
<>
<AlertContext.Provider value={alertContext}>{children}</AlertContext.Provider>
{root.current &&
createPortal(
<TransitionGroup appear>
{alerts.map((alert) => (
<Transition key={alert.id}>
<Template alert={alert} close={() => remove(alert.id)} />
</Transition>
))}
</TransitionGroup>,
root.current,
)}
</>
);
}