Overview
Validation and toast utilities provide functions for validating URLs and displaying toast notifications to users.
isValidUrl
Validates whether a string is a properly formatted URL.
Function Signature
const isValidUrl = ( url : string ) : boolean
Parameters
The URL string to validate
Return Value
Returns true if the URL is valid, false otherwise
Usage Example
import isValidUrl from "../utils/isValidUrl"
import { showErrorToast } from "../utils/showToast"
const validateImageUrl = ( imageUrl : string ) => {
if ( imageUrl && ! isValidUrl ( imageUrl )) {
return showErrorToast ( "Please insert a valid image URL!" )
}
// Proceed with valid URL
}
Implementation Details
The function uses JavaScript’s native URL constructor to validate URLs. If the constructor throws an error, the URL is considered invalid.
Source Location
~/workspace/source/src/utils/isValidUrl.ts:1
Toast Utilities
Toast utilities provide standardized functions for displaying different types of notifications using the Sonner library.
showSuccessToast
Displays a success toast notification.
Function Signature
const showSuccessToast = ( message : string , options ?: ToastOptions )
Parameters
The success message to display
Optional configuration for the toast Show ToastOptions properties
Unique identifier for the toast (useful for updating existing toasts)
Whether to show a close button (default: true for success toasts)
How long the toast should be visible in milliseconds
Action button configuration with label and onClick properties
Additional description text below the main message
Usage Example
import { showSuccessToast } from "../utils/showToast"
showSuccessToast ( "Bookmark added successfully!" )
// With options
showSuccessToast ( "Bookmark refreshed successfully!" , { id: toastId })
showErrorToast
Displays an error toast notification.
Function Signature
const showErrorToast = ( message : string , options ?: ToastOptions )
Parameters
The error message to display
Optional configuration for the toast (see ToastOptions above)
Usage Example
import { showErrorToast } from "../utils/showToast"
if ( ! response . success ) {
return showErrorToast ( response . data )
}
// With custom ID for updating
showErrorToast ( "Failed to retrieve new metadata." , { id: toastId })
showInfoToast
Displays an informational toast notification.
Function Signature
const showInfoToast = ( message : string , options ?: ToastOptions )
Parameters
The informational message to display
Optional configuration for the toast (see ToastOptions above)
Usage Example
import { showInfoToast } from "../utils/showToast"
if ( response . data [ 0 ]. pinned ) {
showInfoToast ( "Bookmark pinned to the top!" )
} else {
showInfoToast ( "Bookmark unpinned!" )
}
showLoadingToast
Displays a loading toast notification without a close button.
Function Signature
const showLoadingToast = ( message : string , options ?: ToastOptions )
Parameters
The loading message to display
Optional configuration for the toast (see ToastOptions above)
Return Value
Returns the toast ID, which can be used to update or dismiss the toast later
Usage Example
import { showLoadingToast , showSuccessToast } from "../utils/showToast"
const toastId = showLoadingToast ( "Searching for new metadata..." )
// Later, update the toast
showSuccessToast ( "Metadata found!" , { id: toastId })
dismissToast
Dismisses a specific toast or all toasts.
Function Signature
const dismissToast = ( toastId ?: string | number )
Parameters
The ID of the toast to dismiss. If omitted, dismisses all toasts.
Usage Example
import { dismissToast } from "../utils/showToast"
// Dismiss a specific toast
dismissToast ( toastId )
// Dismiss all toasts
dismissToast ()
Source Location
~/workspace/source/src/utils/showToast.ts