Skip to content

Commit

Permalink
App: rewrote using hooks (#364)
Browse files Browse the repository at this point in the history
Also:
- removed Log component: console.log is more than enough
- added new generic useEventListener
- removed lots of hacks in App component
- removed apState.showDownloads tab: there is a single showExplorerTab prop now
- fixed toolbar path to only be disabled when filestate is busy
- removed checks for contenteditable in shouldCatch util
  • Loading branch information
warpdesign authored Jan 26, 2023
1 parent e66703f commit d30736c
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 704 deletions.
507 changes: 207 additions & 300 deletions src/components/App.tsx

Large diffs are not rendered by default.

192 changes: 0 additions & 192 deletions src/components/Log.tsx

This file was deleted.

35 changes: 11 additions & 24 deletions src/components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,15 @@ const Nav = observer(() => {
const downloadClass = classNames(Classes.MINIMAL, 'download')
const isSplitViewActive = appState.winStates[0].splitView

const showDownloadsTab = (): void => {
appState.showDownloadsTab()
}

const showExplorerTab = (): void => {
appState.showExplorerTab()
}

const navClick = (): void => {
if (appState.isExplorer) {
showDownloadsTab()
appState.toggleExplorerTab(false)
} else {
showExplorerTab()
}
}

const onToggleSplitView = (): void => {
if (appState.isExplorer) {
appState.toggleSplitViewMode()
appState.toggleExplorerTab(true)
}
}

const onOpenPrefs = (): void => {
runInAction(() => (appState.isPrefsOpen = true))
}

const onOpenShortcuts = (): void => {
runInAction(() => (appState.isShortcutsOpen = true))
}
const onToggleSplitView = (): void => appState.isExplorer && appState.toggleSplitViewMode()

return (
<Navbar>
Expand Down Expand Up @@ -85,7 +65,14 @@ const Nav = observer(() => {
title={t('NAV.SPLITVIEW')}
/>
<Navbar.Divider />
<Popover2 content={<HamburgerMenu onOpenShortcuts={onOpenShortcuts} onOpenPrefs={onOpenPrefs} />}>
<Popover2
content={
<HamburgerMenu
onOpenShortcuts={(): void => appState.toggleShortcutsDialog(true)}
onOpenPrefs={(): void => appState.togglePrefsDialog(true)}
/>
}
>
<Button className={`data-cy-toggle-app-menu ${Classes.MINIMAL}`} icon="menu" />
</Popover2>
</Navbar.Group>
Expand Down
2 changes: 1 addition & 1 deletion src/components/SideView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const SideView = observer(({ hide, viewState }: SideViewProps) => {
<div ref={drop} id={divId} className={activeClass}>
{needLogin && <LoginDialog isOpen={needLogin} onValidation={onValidation} onClose={onClose} />}
<TabList></TabList>
<Toolbar active={active && !busy} />
<Toolbar active={!busy} />
<FileView hide={hide} />
<Statusbar />
<Overlay id={`files-loader-${viewState.viewId}`} shouldShow={busy} delay={true}>
Expand Down
7 changes: 3 additions & 4 deletions src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useTranslation } from 'react-i18next'
import { FileMenu } from '$src/components/FileMenu'
import { MakedirDialog } from '$src/components/dialogs/MakedirDialog'
import { AppAlert } from '$src/components/AppAlert'
import { Logger } from '$src/components/Log'
import { AppToaster } from '$src/components/AppToaster'
import { LocalizedError } from '$src/locale/error'
import Keys from '$src/constants/keys'
Expand Down Expand Up @@ -113,7 +112,7 @@ export const Toolbar = observer(({ active }: Props) => {
}

try {
Logger.log("Let's create a directory :)", dirName, navigate)
console.log("Let's create a directory :)", dirName, navigate)
const dir = await cache.makedir(path, dirName)

if (!navigate) {
Expand Down Expand Up @@ -148,7 +147,7 @@ export const Toolbar = observer(({ active }: Props) => {
const onFileAction = (action: string): void => {
switch (action) {
case 'makedir':
Logger.log('Opening new folder dialog')
console.log('Opening new folder dialog')
onMakedir()
break

Expand All @@ -161,7 +160,7 @@ export const Toolbar = observer(({ active }: Props) => {
break

default:
Logger.warn('action unknown', action)
console.warn('action unknown', action)
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/components/__tests__/Nav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ describe('Nav', () => {
options.providerProps.appState = appState
await options.providerProps.appState.loadSettingsAndPrepareViews()

jest.spyOn(appState, 'showDownloadsTab')
jest.spyOn(appState, 'showExplorerTab')
jest.spyOn(appState, 'toggleExplorerTab')
jest.spyOn(appState, 'toggleSplitViewMode')

jest.clearAllMocks()
Expand Down Expand Up @@ -61,7 +60,7 @@ describe('Nav', () => {

await user.click(screen.getByRole('button', { name: t('NAV.TRANSFERS') }))

expect(options.providerProps.appState.showDownloadsTab).toHaveBeenCalled()
expect(options.providerProps.appState.toggleExplorerTab).toHaveBeenCalledWith(false)
})

it('should show downloads when clicking on downloads button and show explorer when clicking on explorer button', async () => {
Expand All @@ -72,12 +71,12 @@ describe('Nav', () => {

await user.click(transfersButton)

expect(options.providerProps.appState.showDownloadsTab).toHaveBeenCalled()
expect(options.providerProps.appState.toggleExplorerTab).toHaveBeenCalledWith(false)
expect(isSelected(explorerButton)).toBe(false)
expect(isSelected(transfersButton)).toBe(true)

await user.click(explorerButton)
expect(options.providerProps.appState.showExplorerTab).toHaveBeenCalled()
expect(options.providerProps.appState.toggleExplorerTab).toHaveBeenCalledWith(true)
expect(isSelected(explorerButton)).toBe(true)
expect(isSelected(transfersButton)).toBe(false)
})
Expand Down
9 changes: 2 additions & 7 deletions src/components/shortcuts/KeyboardHotkeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { inject } from 'mobx-react'
import { AppState } from '$src/state/appState'
import { FileState } from '$src/state/fileState'
import { SettingsState } from '$src/state/settingsState'
import { Logger } from '$src/components/Log'
import { isMac } from '$src/utils/platform'

interface InjectedProps extends WithTranslation {
Expand Down Expand Up @@ -45,13 +44,9 @@ class KeyboardHotkeysClass extends React.Component<WithTranslation> {
/**
* Event Handlers
*/
onShowDownloadsTab = (): void => {
this.appState.showDownloadsTab()
}
onShowDownloadsTab = (): void => this.appState.toggleExplorerTab(false)

onShowExplorerTab = (): void => {
this.appState.showExplorerTab()
}
onShowExplorerTab = (): void => this.appState.toggleExplorerTab(true)

onNextView = (): void => {
const winState = this.appState.winStates[0]
Expand Down
Loading

0 comments on commit d30736c

Please sign in to comment.