All files / src/apps/gbs-music-web/components MusicWebApp.tsx

0% Statements 0/80
0% Branches 0/25
0% Functions 0/26
0% Lines 0/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import React, { Suspense, lazy, useCallback, useEffect, useState } from "react";
import trackerActions from "store/features/tracker/trackerActions";
import styled from "styled-components";
import { saveSongFile } from "store/features/trackerDocument/trackerDocumentState";
import { webMusicEnvironment } from "gbs-music-web/lib/adapters";
import { MusicWebStatusBanner } from "gbs-music-web/components/MusicWebStatusBanner";
import { useAppDispatch, useAppSelector } from "store/hooks";
import { musicSelectors } from "store/features/entities/entitiesState";
import WebAPI from "gbs-music-web/lib/api";
import { ConfirmUnsavedChangesDialog } from "gbs-music-web/components/dialog/ConfirmUnsavedChangesDialog";
import { MusicWebSplash } from "gbs-music-web/components/MusicWebSplash";
import { useMusicWebUpdate } from "gbs-music-web/components/hooks/useMusicWebUpdate";
import { useUnsavedChangesGuard } from "gbs-music-web/components/hooks/useUnsavedChangesGuard";
import { useMusicWorkspace } from "gbs-music-web/components/hooks/useMusicWorkspace";
import { reloadMusicWebWithAlert } from "gbs-music-web/lib/reloadOnError";
import l10n from "shared/lib/lang/l10n";
 
const AppShell = styled.div`
  display: flex;
  flex-direction: column;
  flex-grow: 1;
`;
 
const AppContent = styled.div`
  flex: 1 1 0;
  min-height: 0;
  display: flex;
`;
 
const LoadingContent = styled.div`
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
`;
 
const recoverChunkLoad = (error: unknown) =>
  reloadMusicWebWithAlert(
    "GBS Music needs to reload to finish updating.",
    error,
  );
 
const StandaloneMusicPage = lazy(() =>
  import("gbs-music-web/components/StandaloneMusicPage").catch(
    recoverChunkLoad,
  ),
);
const MusicWebToolbar = lazy(async () =>
  import("gbs-music-web/components/MusicWebToolbar").catch(recoverChunkLoad),
);
 
export const MusicWebApp = () => {
  const dispatch = useAppDispatch();
  const { updateAvailable, reloadApp } = useMusicWebUpdate();
  const [themeId, setThemeId] = useState(WebAPI.getThemeId());
  const [localeId, setLocaleId] = useState(WebAPI.getLocaleId());
  const modified = useAppSelector((state) => state.tracker.modified);
  const currentSongName = useAppSelector((state) => {
    const currentId = state.tracker.selectedSongId;
    return musicSelectors.selectById(state, currentId)?.name ?? "";
  });
  const selectedSongId = useAppSelector(
    (state) => state.tracker.selectedSongId,
  );
 
  const saveCurrentSong = useCallback(async () => {
    const result = await dispatch(saveSongFile());
    return saveSongFile.fulfilled.match(result);
  }, [dispatch]);
 
  const {
    hasPendingAction,
    runWithUnsavedCheck,
    closeConfirm,
    saveAndContinue,
    discardAndContinue,
  } = useUnsavedChangesGuard({
    modified,
    save: saveCurrentSong,
  });
  useEffect(() => {
    webMusicEnvironment.setWindowTitle(
      currentSongName ? `GBS Music - ${currentSongName}` : "GBS Music",
    );
  }, [currentSongName]);
 
  const {
    singleDocumentMode,
    hasBackup,
    createSong,
    importSong,
    openDirectoryWorkspace,
    restoreBackupSong,
    openExample,
    closeWorkspace,
  } = useMusicWorkspace();
 
  const onSelectSong = useCallback(
    (nextSongId: string) => {
      Iif (nextSongId === selectedSongId) {
        return;
      }
      void runWithUnsavedCheck(async () => {
        dispatch(trackerActions.setSelectedSongId(nextSongId));
      });
    },
    [dispatch, runWithUnsavedCheck, selectedSongId],
  );
 
  const onThemeChange = useCallback((nextThemeId: string) => {
    WebAPI.setThemeId(nextThemeId);
    setThemeId(nextThemeId);
  }, []);
 
  const onLocaleChange = useCallback(async (nextLocaleId: string) => {
    await WebAPI.setLocaleId(nextLocaleId);
    setLocaleId(nextLocaleId);
  }, []);
 
  const allSongs = useAppSelector(musicSelectors.selectAll);
  const hasSongs = allSongs.length > 0;
 
  const onCreateSong = useCallback(
    (name: string, songArtist: string) => {
      void runWithUnsavedCheck(() => createSong({ name, artist: songArtist }));
    },
    [createSong, runWithUnsavedCheck],
  );
 
  const onAddSong = useCallback(() => {
    void runWithUnsavedCheck(() => createSong());
  }, [createSong, runWithUnsavedCheck]);
 
  const onImportSong = useCallback(() => {
    void runWithUnsavedCheck(importSong);
  }, [importSong, runWithUnsavedCheck]);
 
  const onOpenDirectoryWorkspace = useCallback(() => {
    void runWithUnsavedCheck(openDirectoryWorkspace);
  }, [openDirectoryWorkspace, runWithUnsavedCheck]);
 
  const onRestoreBackup = useCallback(() => {
    void runWithUnsavedCheck(restoreBackupSong);
  }, [restoreBackupSong, runWithUnsavedCheck]);
 
  const onOpenExample = useCallback(
    (name: string, filename: string, url: string) => {
      void runWithUnsavedCheck(() => openExample(name, filename, url));
    },
    [openExample, runWithUnsavedCheck],
  );
 
  const onCloseWorkspace = useCallback(() => {
    void runWithUnsavedCheck(closeWorkspace);
  }, [closeWorkspace, runWithUnsavedCheck]);
 
  return (
    <AppShell
      data-id="app-shell"
      onContextMenu={(e) => {
        e.preventDefault();
        e.stopPropagation();
      }}
    >
      {updateAvailable ? (
        <MusicWebStatusBanner
          title={l10n("DIALOG_UPDATE_AVAILABLE")}
          actionLabel={l10n("DIALOG_OK")}
          onAction={reloadApp}
        />
      ) : null}
      {hasSongs ? (
        <Suspense fallback={null}>
          <MusicWebToolbar
            themeId={themeId}
            localeId={localeId}
            onThemeChange={onThemeChange}
            onLocaleChange={onLocaleChange}
            onCreateSong={onAddSong}
            onImportSong={onImportSong}
            onOpenDirectoryWorkspace={
              singleDocumentMode ? undefined : onOpenDirectoryWorkspace
            }
            onCloseWorkspace={onCloseWorkspace}
          />
        </Suspense>
      ) : null}
      <AppContent data-id="app-content">
        {hasSongs ? (
          <Suspense
            fallback={<LoadingContent>{l10n("FIELD_LOADING")}</LoadingContent>}
          >
            <StandaloneMusicPage
              localeId={localeId}
              onCreateSong={onAddSong}
              onImportSong={onImportSong}
              onOpenDirectoryWorkspace={onOpenDirectoryWorkspace}
              onSelectSong={onSelectSong}
            />
          </Suspense>
        ) : (
          <MusicWebSplash
            themeId={themeId}
            localeId={localeId}
            onThemeChange={onThemeChange}
            onLocaleChange={onLocaleChange}
            onCreateSong={onCreateSong}
            onImportSong={onImportSong}
            onOpenDirectoryWorkspace={
              singleDocumentMode ? undefined : onOpenDirectoryWorkspace
            }
            onRestoreBackup={hasBackup ? onRestoreBackup : undefined}
            onOpenExample={onOpenExample}
          />
        )}
      </AppContent>
      {hasPendingAction ? (
        <ConfirmUnsavedChangesDialog
          filename={currentSongName}
          onCancel={closeConfirm}
          onSave={saveAndContinue}
          onDiscard={discardAndContinue}
        />
      ) : null}
    </AppShell>
  );
};