facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.79k stars 26.87k forks source link

HMR not working #13613

Open nyck33 opened 5 months ago

nyck33 commented 5 months ago

it's supposed to work out of the box right? Why isn't it working?

I changed the name to App.jsx

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import './App.css';

function App() {
  const [file, setFile] = useState(null);
  const [fileName, setFileName] = useState("");
  const [outputDir, setOutputDir] = useState("");
  const [promptFileName, setPromptFileName] = useState("");
  const [outputFile, setOutputFile] = useState("");
  const [tokenLimit, setTokenLimit] = useState(800);
  const [openAiPrompt, setOpenAiPrompt] = useState("");
  const [message, setMessage] = useState("");

  useEffect(() => {
    if (fileName) {
      const fileBaseName = fileName.substring(0, fileName.lastIndexOf('.'));
      setOutputFile(`translated_${fileBaseName}.srt`);
    }
  }, [fileName]);

  const handleFileChange = (e) => {
    const uploadedFile = e.target.files[0];
    if (uploadedFile) {
      setFile(uploadedFile);
      setFileName(uploadedFile.name);

      // Extract directory path and set output directory
      const directory = uploadedFile.webkitRelativePath.split('/').slice(0, -1).join('/');
      const outputDirectory = directory ? `${directory}/chunks` : 'chunks';
      setOutputDir(outputDirectory);
    }
  };

  const handlePromptFileChange = (e) => {
    const promptFile = e.target.files[0];
    if (promptFile) {
      setPromptFileName(promptFile.name);
      // Optionally, read the file content and set it to openAiPrompt
      const reader = new FileReader();
      reader.onload = (event) => {
        setOpenAiPrompt(event.target.result);
      };
      reader.readAsText(promptFile);
    }
  };

  const handleOutputDirChange = (e) => {
    const userInput = e.target.value;
    if (userInput) {
      setOutputDir((prev) => prev.replace(/\/output$/, `/${userInput}`));
    }
  };

  const handleSubmit = async (e) => {
    e.preventDefault();
    if (!file) {
      setMessage("Please upload a file.");
      return;
    }

    const formData = new FormData();
    formData.append("file", file);

    try {
      const uploadResponse = await axios.post("http://localhost:8000/uploadfile/", formData);
      const fileLocation = uploadResponse.data.file_location;

      const request = {
        input_file: fileLocation,
        output_dir: outputDir,
        prompt_file: promptFileName,
        output_file: outputFile,
        token_limit: tokenLimit,
        openai_prompt: openAiPrompt
      };

      const translationResponse = await axios.post("http://localhost:8000/translate/", request);
      setMessage(translationResponse.data.message);
    } catch (error) {
      setMessage(`An error occurred: ${error.response.data.message}`);
    }
  };

  return (
    <div className="App">
      <header className="App-header">
        <h1>OpenAI Translator App</h1>
        <form onSubmit={handleSubmit}>
          <div className="form-group">
            <label>Upload SRT File:</label>
            <input type="file" onChange={handleFileChange} />
            <input type="text" value={fileName} readOnly />
          </div>

          <div className="form-group">
            <label>Output Directory:</label>
            <input type="text" value={outputDir} onChange={handleOutputDirChange} />
          </div>

          <div className="form-group">
            <label>Prompt File Path:</label>
            <input type="file" onChange={handlePromptFileChange} />
            <input type="text" value={promptFileName} readOnly />
          </div>

          <div className="form-group">
            <label>Token Limit:</label>
            <input type="number" value={tokenLimit} onChange={(e) => setTokenLimit(e.target.value)} />
          </div>

          <div className="form-group">
            <label>OpenAI Prompt:</label>
            <textarea value={openAiPrompt} onChange={(e) => setOpenAiPrompt(e.target.value)} />
          </div>

          <div className="form-group">
            <label>Output File Path:</label>
            <input type="text" value={outputFile} readOnly />
          </div>

          <button type="submit">Translate</button>
        </form>

        {message && <p>{message}</p>}
      </header>
    </div>
  );
}

export default App;
bharat407 commented 3 months ago

Steps to Troubleshoot HMR Issues 1- Check Your Environment:

Could you make sure you're running the app in development mode? HMR typically doesn't work in production builds.

2 Ensure Dependencies are Up-to-date:

Update your dependencies using npm update or yarn upgrade. Sometimes, HMR issues are related to outdated dependencies.

3 Check Webpack Configuration:

If you've ejected your Create React App or customized the Webpack configuration, ensure HMR is properly configured. HMR requires specific setup in Webpack to work correctly. Server Configuration:

Ensure your development server supports HMR. The default development server provided by Create React App should support it out of the box.

4 Console Logs and Errors:

Check the browser console and terminal for any errors or warnings related to HMR. These logs often provide clues to what might be going wrong.

5 Network Tab:

Use the Network tab in your browser's developer tools to see if HMR-related requests are being made and if they are returning expected responses.

6 Restart Development Server:

Sometimes, simply restarting the development server can resolve HMR issues.

7 Clean Cache and Rebuild:

Clear your browser cache and try restarting your development server. You can also delete the node_modules directory and reinstall dependencies.