import dayjs from "dayjs";
import "dayjs/locale/ru";
import { ViewPage } from "./pages/ViewPublicationPage";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import "./index.css";

import {
  clearAuthToken,
  getMessageFromFetchError,
  useUserFetcher,
} from "@frontend/kitui";
import { clearUserData, setUser, useUserStore } from "@root/user";
import { enqueueSnackbar } from "notistack";

dayjs.locale("ru");

export default function App() {
  const userId = useUserStore((state) => state.userId);

  useUserFetcher({
    url: `https://hub.pena.digital/user/${userId}`,
    userId,
    onNewUser: setUser,
    onError: (error) => {
      const errorMessage = getMessageFromFetchError(error);
      if (errorMessage) {
        enqueueSnackbar(errorMessage);
        clearUserData();
        clearAuthToken();
      }
    },
  });

  return (
    <>
      <BrowserRouter>
        <Routes>
          <Route path="/view" element={<ViewPage />} />
        </Routes>
      </BrowserRouter>
    </>
  );
}
