File size: 3,795 Bytes
a336e20
a3306be
 
 
 
a336e20
a3306be
 
a336e20
a3306be
 
 
 
a336e20
a3306be
a336e20
 
 
a3306be
a336e20
 
 
 
 
 
 
 
a3306be
 
 
 
 
 
 
a336e20
a3306be
 
 
a336e20
a3306be
 
 
 
 
 
a336e20
 
 
a3306be
 
 
 
 
 
 
 
 
a336e20
a3306be
a336e20
a3306be
 
 
 
 
a336e20
a3306be
 
 
 
 
a336e20
a3306be
 
 
 
 
 
 
 
 
 
a336e20
a3306be
 
a336e20
 
 
 
 
 
 
 
a3306be
a336e20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3306be
a336e20
a3306be
 
a336e20
 
a3306be
 
a336e20
a3306be
 
a336e20
a3306be
 
 
 
 
a336e20
 
 
 
 
a3306be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useState, useEffect } from 'react';
import classNames from 'classnames';
import { toast } from 'react-toastify';
import { FaDownload } from 'react-icons/fa';

import { Auth } from './../../../utils/types';
import LoadButton from '../load-button/load-button';

function getUrlParam(paramName: string): string | null {
  const urlParams = new URLSearchParams(window.location.search);
  return urlParams.get(paramName);
}

function DownloadButton({
  html,
  error = false,
  auth,
  setHtml
}: {
  html: string;
  error: boolean;
  auth?: Auth;
  setHtml: (html: string) => void;
}) {
  const [open, setOpen] = useState(false);
  const [loading, setLoading] = useState(false);
  const [destination, setDestination] = useState<string | null>(null);
  const [filename, setFilename] = useState('index.html');

  // Get destination from URL parameters
  useEffect(() => {
    const fileDestination = getUrlParam('destination');
    const customFilename = getUrlParam('filename');

    if (fileDestination) {
      setDestination(fileDestination);
    }

    if (customFilename) {
      setFilename(customFilename);
    }
  }, []);

  const downloadFile = async () => {
    setLoading(true);

    try {
      if (!destination) {
        throw new Error(
          'No destination specified. Please provide a destination path in the URL.'
        );
      }

      // Make a POST request to the server endpoint
      const response = await fetch('/api/download', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          html,
          filename,
          destination
        })
      });

      const data = await response.json();

      if (!response.ok) {
        throw new Error(data.message || 'Failed to save file');
      }

      toast.success(
        <div className="w-full flex items-center justify-center">
          File saved successfully!
          <span className="ml-1">({data.path})</span>
        </div>,
        {
          autoClose: 3000
        }
      );
    } catch (err: any) {
      console.error('Download error:', err);
      toast.error(err.message || 'Error saving file');
    } finally {
      setLoading(false);
      setOpen(false);
    }
  };

  return (
    <div className="flex items-center justify-end gap-5">
      <LoadButton auth={auth} setHtml={setHtml} setPath={() => {}} />
      <div className="relative flex items-center justify-end">
        {auth && (
          <>
            <p className="mr-3 text-xs lg:text-sm text-gray-300">
              <span className="max-lg:hidden">Connected as </span>
              <a
                href={`https://huggingface.co/${auth.preferred_username}`}
                target="_blank"
                className="underline hover:text-white"
              >
                {auth.preferred_username}
              </a>
            </p>
          </>
        )}
        <button
          className={classNames(
            'relative cursor-pointer flex-none flex items-center justify-center rounded-md text-xs lg:text-sm font-semibold leading-5 lg:leading-6 py-1.5 px-5 hover:bg-green-400 text-white shadow-sm dark:shadow-highlight/20',
            {
              'bg-green-600': open,
              'bg-green-500': !open
            }
          )}
          onClick={downloadFile}
          disabled={loading || error}
        >
          <FaDownload className="mr-2" />
          Download
        </button>
        {destination && (
          <div className="absolute top-full right-0 mt-2 text-xs text-gray-300 bg-gray-800 rounded p-1">
            Destination: {destination}
          </div>
        )}
      </div>
    </div>
  );
}

export default DownloadButton;