# yatra/3.0.11/resources/js/components/trip-form/hooks/useAutoSave.ts

Yatra – Travel Booking &amp; Tour Operator Software, version 3.0.11. 40 lines.

- Page: https://pluginprobe.com/plugins/yatra/3.0.11/code/resources/js/components/trip-form/hooks/useAutoSave.ts
- Raw: https://pluginprobe.com/plugins/yatra/3.0.11/raw/resources/js/components/trip-form/hooks/useAutoSave.ts
- Modified: 2026-04-14T03:47:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/yatra/3.0.11/code/resources/js/components/trip-form/hooks/useAutoSave.ts#L10-L20`.

```typescript
/**
 * Custom hook for auto-save functionality
 */

import { useState, useCallback, useRef } from "react";

interface UseAutoSaveOptions {
  delay?: number;
  onSave?: () => void;
}

export const useAutoSave = (options: UseAutoSaveOptions = {}) => {
  const { delay = 1000, onSave } = options;
  const [isAutoSaving, setIsAutoSaving] = useState(false);
  const [lastSaved, setLastSaved] = useState<Date | null>(null);
  const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);

  const autoSave = useCallback(() => {
    if (timeoutRef.current) {
      clearTimeout(timeoutRef.current);
    }

    setIsAutoSaving(true);

    timeoutRef.current = setTimeout(() => {
      if (onSave) {
        onSave();
      }
      setLastSaved(new Date());
      setIsAutoSaving(false);
    }, delay);
  }, [delay, onSave]);

  return {
    isAutoSaving,
    lastSaved,
    autoSave,
  };
};

```
