Skip to main content

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

url
string
required
The URL string to validate

Return Value

valid
boolean
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

message
string
required
The success message to display
options
ToastOptions
Optional configuration for the toast

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

message
string
required
The error message to display
options
ToastOptions
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

message
string
required
The informational message to display
options
ToastOptions
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

message
string
required
The loading message to display
options
ToastOptions
Optional configuration for the toast (see ToastOptions above)

Return Value

toastId
string | number
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

toastId
string | number
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

Build docs developers (and LLMs) love