message.ts


Overview

The message.ts file provides a centralized utility for displaying various types of toast notifications—success, error, warning, and informational messages—using the sonner toast library. The main purpose of this module is to offer a simple, consistent API for showing user feedback messages throughout the application with pre-configured toast appearance and behavior.

By abstracting the toast calls behind a single message object, this file promotes reusable, standardized notification styling and timing, improving maintainability and user experience consistency.


Detailed Explanation

Constants

duration

const duration = { duration: 1500 };

Object: message

The message object exposes four methods corresponding to different notification types. Each method triggers a toast with a specified message and consistent display options.

const message = {
  success: (msg: string) => { ... },
  error: (msg: string) => { ... },
  warning: (msg: string) => { ... },
  info: (msg: string) => { ... },
};

Methods

Each method accepts a single parameter:


message.success(msg: string): void

Displays a success toast notification with the supplied message.

Example Usage:

message.success('Data saved successfully!');

message.error(msg: string): void

Displays an error toast notification with the supplied message.

Example Usage:

message.error('Failed to load user data.');

message.warning(msg: string): void

Displays a warning toast notification with the supplied message.

Example Usage:

message.warning('Your subscription will expire soon.');

message.info(msg: string): void

Displays an informational toast notification with the supplied message.

Example Usage:

message.info('New updates are available.');

Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class message {
        +success(msg: string): void
        +error(msg: string): void
        +warning(msg: string): void
        +info(msg: string): void
    }
    class toast {
        +success(msg: string, options: object): void
        +error(msg: string, options: object): void
        +warning(msg: string, options: object): void
        +info(msg: string, options: object): void
    }
    message ..> toast : uses

Summary

The message.ts file is a lightweight, focused utility module that provides a standardized interface for showing toast notifications using the sonner library. It simplifies the process of displaying user feedback messages by exposing four clear methods for success, error, warning, and info types—all sharing consistent styling and duration parameters. This promotes a unified user experience and easier maintenance across the application.