From 8396bbc3ac9e5e310ddd53a4fee4a3716056ac95 Mon Sep 17 00:00:00 2001 From: tiddlygit-test Date: Fri, 24 Jul 2020 14:09:24 +0800 Subject: [PATCH] refactor: remove unused code, add flow type annotation --- .eslintrc | 1 + package.json | 1 - public/libs/get-website-icon-url-async.js | 111 ------------------ public/listeners/index.js | 14 --- .../description-and-mode-switch.js | 3 +- .../existed-wiki-path-form.js | 3 +- .../dialog-add-workspace/github-login.js | 3 +- .../new-wiki-done-button.js | 2 +- .../new-wiki-path-form.js | 56 +++++---- .../dialog-add-workspace/search-repo.js | 2 +- src/constants/actions.js | 9 -- src/senders/index.js | 46 +++++--- src/state/dialog-add-workspace/actions.js | 93 +-------------- src/state/dialog-add-workspace/reducers.js | 108 +---------------- 14 files changed, 77 insertions(+), 375 deletions(-) delete mode 100644 public/libs/get-website-icon-url-async.js diff --git a/.eslintrc b/.eslintrc index e40a168a..4edbaedb 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,6 +14,7 @@ "unicorn/no-reduce": [0], "sonarjs/cognitive-complexity": ["error", 30], "react/jsx-props-no-spreading": [0], + "react/require-default-props": [1, { "ignoreFunctionalComponents": true }], "import/no-extraneous-dependencies": [2, { "devDependencies": true }], "react/static-property-placement": [0], "react/jsx-filename-extension": [1, { "extensions": [".js"] }], diff --git a/package.json b/package.json index 4dbf905d..229337df 100755 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "@material-ui/pickers": "^4.0.0-alpha.10", "ace-builds": "1.4.12", "blueimp-md5": "2.16.0", - "cheerio": "1.0.0-rc.3", "chokidar": "^3.4.1", "classnames": "2.2.6", "darkreader": "4.9.15", diff --git a/public/libs/get-website-icon-url-async.js b/public/libs/get-website-icon-url-async.js deleted file mode 100644 index 1ab68b16..00000000 --- a/public/libs/get-website-icon-url-async.js +++ /dev/null @@ -1,111 +0,0 @@ -const cheerio = require('cheerio'); -const url = require('url'); - -const customizedFetch = require('./customized-fetch'); - -const getWebsiteIconUrlAsync = (websiteURL) => customizedFetch(websiteURL) - .then((res) => res.text().then((html) => ({ html, redirectedUrl: res.url }))) - .then(({ html, redirectedUrl }) => { - const $ = cheerio.load(html); - // rel=fluid-icon - // https://webmasters.stackexchange.com/questions/23696/whats-the-fluid-icon-meta-tag-for - const $fluidIcon = $('head > link[rel=fluid-icon]'); - if ($fluidIcon.length > 0) { - return url.resolve(redirectedUrl, $fluidIcon.attr('href')); - } - - const getMaxSizeIcon = (rootElm) => { - let icon; - let maxSize = 0; - // find the icon with largest size - rootElm.each((i, _elm) => { - const elm = $(_elm); - // check if type is PNG - // also check to make sure links doesn't end with .ico - // as some websites specify icon type wrong - // see https://github.com/atomery/webcatalog/issues/630 for more details - if ((elm.attr('type') === 'image/png' && !elm.attr('href').endsWith('.ico')) - || elm.attr('href').endsWith('.png')) { // if type is not specified but link ends with .png then assumes that the icon is PNG - const size = elm.attr('sizes') ? parseInt(elm.attr('sizes').split('x'), 10) : 0; - if (size >= maxSize) { - maxSize = size; - icon = url.resolve(redirectedUrl, elm.attr('href')); - } - } - }); - return icon; - }; - - // rel=apple-touch-icon - // more preferred because it's not transparent - const $appleTouchIcon = $('head > link[rel=apple-touch-icon]'); - if ($appleTouchIcon.length > 0) { - const icon = getMaxSizeIcon($appleTouchIcon); - if (icon) return icon; - } - // rel=apple-touch-icon-precomposed - // more preferred because it's not transparent - const $appleTouchIconPrecomposed = $('head > link[rel=apple-touch-icon-precomposed]'); - if ($appleTouchIconPrecomposed.length > 0) { - const icon = getMaxSizeIcon($appleTouchIconPrecomposed); - if (icon) return icon; - } - - // for code sharing - // I know this is lazy, but it works so whatever - const lessPriorityCheck = () => { - // rel=icon - // less preferred because it's not always in high resolution - const $icon = $('head > link[rel=icon]'); - if ($icon.length > 0) { - const icon = getMaxSizeIcon($icon); - if (icon) return icon; - } - // rel=icon - // less preferred because it's not always in high resolution - const $shortcutIcon = $('head > link[rel=\'shortcut icon\']'); - if ($shortcutIcon.length > 0) { - const icon = getMaxSizeIcon($shortcutIcon); - if (icon) return icon; - } - return undefined; - }; - - // manifest.json icon - // https://developers.google.com/web/fundamentals/web-app-manifest - const $manifest = $('head > link[rel=manifest]'); - if ($('head > link[rel=manifest]').length > 0) { - const manifestUrl = url.resolve(redirectedUrl, $manifest.attr('href')); - return customizedFetch(manifestUrl) - .then((res) => res.text().then((manifestJson) => ({ - manifestJson, - manifestRedirectedUrl: res.url, - }))) - .then(({ manifestJson, manifestRedirectedUrl }) => { - // return icon with largest size - const { icons } = manifestJson; - icons.sort((x, y) => parseInt(x.sizes.split('x'), 10) - parseInt(y.sizes.split('x'), 10)); - return url.resolve(manifestRedirectedUrl, icons[icons.length - 1].src); - }) - // youtube.com/manifest.json doesn't specify icons - // error needs to be catched and the other checks need to be run - .catch(() => lessPriorityCheck()); - } - - return lessPriorityCheck(); - }) - .then((icon) => { - if (icon) return icon; - - // try to get /apple-touch-icon.png - // https://apple.stackexchange.com/questions/172204/how-apple-com-set-apple-touch-icon - const appleTouchIconUrl = url.resolve(websiteURL, '/apple-touch-icon.png'); - return customizedFetch(appleTouchIconUrl) - .then((res) => { - if (res.status === 200 && res.headers.get('Content-Type') === 'image/png') return appleTouchIconUrl; - return undefined; - }) - .catch(() => undefined); - }); - -module.exports = getWebsiteIconUrlAsync; diff --git a/public/listeners/index.js b/public/listeners/index.js index b558ac67..fffd9df8 100755 --- a/public/listeners/index.js +++ b/public/listeners/index.js @@ -42,7 +42,6 @@ const { reloadViewsDarkReader, reloadViewsWebContentsIfDidFailLoad } = require(' const { updatePauseNotificationsInfo, getPauseNotificationsInfo } = require('../libs/notifications'); const sendToAllWindows = require('../libs/send-to-all-windows'); -const getWebsiteIconUrlAsync = require('../libs/get-website-icon-url-async'); const getViewBounds = require('../libs/get-view-bounds'); const createMenu = require('../libs/create-menu'); @@ -511,19 +510,6 @@ const loadListeners = () => { autoUpdater.checkForUpdates(); }); - // to be replaced with invoke (electron 7+) - // https://electronjs.org/docs/api/ipc-renderer#ipcrendererinvokechannel-args - ipcMain.on('request-get-website-icon-url', (e, id, url) => { - getWebsiteIconUrlAsync(url) - .then(iconUrl => { - sendToAllWindows(id, iconUrl); - }) - .catch(err => { - console.log(err); // eslint-disable-line no-console - sendToAllWindows(id, null); - }); - }); - // Native Theme ipcMain.on('get-should-use-dark-colors', e => { e.returnValue = nativeTheme.shouldUseDarkColors; diff --git a/src/components/dialog-add-workspace/description-and-mode-switch.js b/src/components/dialog-add-workspace/description-and-mode-switch.js index bc89290b..8a97e748 100644 --- a/src/components/dialog-add-workspace/description-and-mode-switch.js +++ b/src/components/dialog-add-workspace/description-and-mode-switch.js @@ -1,4 +1,5 @@ // @flow +import type { ComponentType } from 'react'; import React from 'react'; import styled from 'styled-components'; @@ -7,7 +8,7 @@ import FormControlLabel from '@material-ui/core/FormControlLabel'; import Switch from '@material-ui/core/Switch'; import Typography from '@material-ui/core/Typography'; -const Container = styled(Paper)` +const Container: ComponentType<{}> = styled(Paper)` padding: 10px; `; diff --git a/src/components/dialog-add-workspace/existed-wiki-path-form.js b/src/components/dialog-add-workspace/existed-wiki-path-form.js index 720441bd..b75d5a1a 100644 --- a/src/components/dialog-add-workspace/existed-wiki-path-form.js +++ b/src/components/dialog-add-workspace/existed-wiki-path-form.js @@ -1,4 +1,5 @@ // @flow +import type { ComponentType } from 'react'; import React, { useState, useEffect } from 'react'; import styled from 'styled-components'; import { connect } from 'react-redux'; @@ -18,7 +19,7 @@ import * as actions from '../../state/dialog-add-workspace/actions'; import { getWorkspaces } from '../../senders'; -const CreateContainer = styled(Paper)` +const CreateContainer: ComponentType<{}> = styled(Paper)` margin-top: 5px; `; const LocationPickerContainer = styled.div` diff --git a/src/components/dialog-add-workspace/github-login.js b/src/components/dialog-add-workspace/github-login.js index 95cbad90..b15acf79 100644 --- a/src/components/dialog-add-workspace/github-login.js +++ b/src/components/dialog-add-workspace/github-login.js @@ -1,5 +1,6 @@ // @flow /* eslint-disable promise/no-nesting */ +import type { ComponentType } from 'react'; import React, { Component } from 'react'; import styled from 'styled-components'; import Button from '@material-ui/core/Button'; @@ -8,7 +9,7 @@ import AuthingSSO from '@authing/sso'; import { APP_DOMAIN, APP_ID } from '../../constants/auth'; -const SyncToGithubButton = styled(Button)` +const SyncToGithubButton: ComponentType<{}> = styled(Button)` white-space: nowrap; width: 100%; `; diff --git a/src/components/dialog-add-workspace/new-wiki-done-button.js b/src/components/dialog-add-workspace/new-wiki-done-button.js index abbb3fd0..a7d2b7fd 100644 --- a/src/components/dialog-add-workspace/new-wiki-done-button.js +++ b/src/components/dialog-add-workspace/new-wiki-done-button.js @@ -1,5 +1,5 @@ // @flow -import React, { useState, useEffect } from 'react'; +import React from 'react'; import styled from 'styled-components'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; diff --git a/src/components/dialog-add-workspace/new-wiki-path-form.js b/src/components/dialog-add-workspace/new-wiki-path-form.js index 5fdc0b4c..37563fb3 100644 --- a/src/components/dialog-add-workspace/new-wiki-path-form.js +++ b/src/components/dialog-add-workspace/new-wiki-path-form.js @@ -1,4 +1,5 @@ // @flow +import type { ComponentType } from 'react'; import React, { useState, useEffect } from 'react'; import styled from 'styled-components'; import { connect } from 'react-redux'; @@ -18,7 +19,7 @@ import * as actions from '../../state/dialog-add-workspace/actions'; import { getWorkspaces } from '../../senders'; -const CreateContainer = styled(Paper)` +const CreateContainer: ComponentType<{}> = styled(Paper)` margin-top: 5px; `; const LocationPickerContainer = styled.div` @@ -37,24 +38,24 @@ const SoftLinkToMainWikiSelectInputLabel = styled(InputLabel)` margin-top: 5px; `; -interface Props { - wikiCreationMessage?: string; - parentFolderLocationSetter: string => void; - wikiFolderName: string; - wikiFolderNameSetter: string => void; - mainWikiToLink: Object; - mainWikiToLinkSetter: Object => void; - parentFolderLocation: string; - wikiPort: Number; - wikiPortSetter: number => void; - isCreateMainWorkspace: boolean; -} -interface ActionProps { - setWikiCreationMessage: string => void; -} -interface StateProps { - wikiCreationMessage: string; -} +type OwnProps = {| + parentFolderLocationSetter: string => void, + wikiFolderName: string, + wikiFolderNameSetter: string => void, + mainWikiToLink: Object, + mainWikiToLinkSetter: Object => void, + parentFolderLocation: string, + wikiPort: number, + wikiPortSetter: number => void, + isCreateMainWorkspace: boolean, +|}; +type DispatchProps = {| + setWikiCreationMessage: string => void, +|}; +type StateProps = {| + wikiCreationMessage?: string, +|}; +type Props = { ...OwnProps, ...DispatchProps, ...StateProps }; function NewWikiPathForm({ setWikiCreationMessage, @@ -68,7 +69,7 @@ function NewWikiPathForm({ wikiPort, wikiPortSetter, isCreateMainWorkspace, -}: Props & ActionProps & StateProps) { +}: Props) { const [workspaces, workspacesSetter] = useState({}); useEffect(() => { workspacesSetter(getWorkspaces()); @@ -175,8 +176,13 @@ function NewWikiPathForm({ ); } -const mapStateToProps = state => ({ - wikiCreationMessage: state.dialogAddWorkspace.wikiCreationMessage, -}); - -export default connect(mapStateToProps, dispatch => bindActionCreators(actions, dispatch))(NewWikiPathForm); +export default connect( + (state): { wikiCreationMessage?: string } => ({ + wikiCreationMessage: state.dialogAddWorkspace.wikiCreationMessage, + }), + dispatch => + bindActionCreators( + actions, + dispatch, + ), +)(NewWikiPathForm); diff --git a/src/components/dialog-add-workspace/search-repo.js b/src/components/dialog-add-workspace/search-repo.js index 38ad34b5..96220859 100644 --- a/src/components/dialog-add-workspace/search-repo.js +++ b/src/components/dialog-add-workspace/search-repo.js @@ -42,7 +42,7 @@ interface Props { accessToken: string | null; githubWikiUrl: string; githubWikiUrlSetter: string => void; - userInfo?: IUserInfo; + userInfo?: IUserInfo | null; } export default function SearchRepo({ accessToken, githubWikiUrl, githubWikiUrlSetter, userInfo }: Props) { const [githubRepoSearchString, githubRepoSearchStringSetter] = useState('wiki'); diff --git a/src/constants/actions.js b/src/constants/actions.js index 7967b3e3..6650d9b9 100644 --- a/src/constants/actions.js +++ b/src/constants/actions.js @@ -54,16 +54,7 @@ export const ABOUT_OPEN = 'ABOUT_OPEN'; // Add Workspace export const ADD_WORKSPACE_CREATE_WIKI_MESSAGE = 'ADD_WORKSPACE_CREATE_WIKI_MESSAGE'; -export const ADD_WORKSPACE_GET_FAILED = 'ADD_WORKSPACE_GET_FAILED'; -export const ADD_WORKSPACE_GET_REQUEST = 'ADD_WORKSPACE_GET_REQUEST'; -export const ADD_WORKSPACE_GET_SUCCESS = 'ADD_WORKSPACE_GET_SUCCESS'; -export const ADD_WORKSPACE_RESET = 'ADD_WORKSPACE_RESET'; -export const ADD_WORKSPACE_UPDATE_SCROLL_OFFSET = 'ADD_WORKSPACE_UPDATE_SCROLL_OFFSET'; -export const ADD_WORKSPACE_UPDATE_CURRENT_QUERY = 'ADD_WORKSPACE_UPDATE_CURRENT_QUERY'; -export const ADD_WORKSPACE_UPDATE_DOWNLOADING_ICON = 'ADD_WORKSPACE_UPDATE_DOWNLOADING_ICON'; export const ADD_WORKSPACE_UPDATE_FORM = 'ADD_WORKSPACE_UPDATE_FORM'; -export const ADD_WORKSPACE_UPDATE_MODE = 'ADD_WORKSPACE_UPDATE_MODE'; -export const ADD_WORKSPACE_UPDATE_QUERY = 'ADD_WORKSPACE_UPDATE_QUERY'; // Updater export const UPDATE_UPDATER = 'UPDATE_UPDATER'; diff --git a/src/senders/index.js b/src/senders/index.js index 6b3e7c13..ee1498eb 100644 --- a/src/senders/index.js +++ b/src/senders/index.js @@ -12,7 +12,8 @@ export const requestCreateSubWiki = ( export const ensureWikiExist = (wikiPath: string, shouldBeMainWiki: boolean) => ipcRenderer.invoke('ensure-wiki-exist', wikiPath, shouldBeMainWiki); export const requestOpen = (uri: string, isDirectory?: boolean) => ipcRenderer.send('request-open', uri, !!isDirectory); -export const requestShowMessageBox = (message: string, type: string) => ipcRenderer.send('request-show-message-box', message, type); +export const requestShowMessageBox = (message: string, type: string) => + ipcRenderer.send('request-show-message-box', message, type); export const requestLoadUrl = (url: string, id: string) => ipcRenderer.send('request-load-url', url, id); export const requestGoHome = () => ipcRenderer.send('request-go-home'); @@ -21,20 +22,24 @@ export const requestGoForward = () => ipcRenderer.send('request-go-forward'); export const requestReload = () => ipcRenderer.send('request-reload'); export const requestQuit = () => ipcRenderer.send('request-quit'); -export const requestCheckForUpdates = isSilent => ipcRenderer.send('request-check-for-updates', isSilent); +export const requestCheckForUpdates = (isSilent: boolean) => ipcRenderer.send('request-check-for-updates', isSilent); export const requestShowAboutWindow = () => ipcRenderer.send('request-show-about-window'); export const requestShowAddWorkspaceWindow = () => ipcRenderer.send('request-show-add-workspace-window'); -export const requestShowCodeInjectionWindow = type => ipcRenderer.send('request-show-code-injection-window', type); +export const requestShowCodeInjectionWindow = (type: string) => + ipcRenderer.send('request-show-code-injection-window', type); export const requestShowCustomUserAgentWindow = () => ipcRenderer.send('request-show-custom-user-agent-window'); -export const requestShowEditWorkspaceWindow = id => ipcRenderer.send('request-show-edit-workspace-window', id); +export const requestShowEditWorkspaceWindow = (id: string) => + ipcRenderer.send('request-show-edit-workspace-window', id); export const requestShowNotificationsWindow = () => ipcRenderer.send('request-show-notifications-window'); -export const requestShowPreferencesWindow = scrollTo => ipcRenderer.send('request-show-preferences-window', scrollTo); +export const requestShowPreferencesWindow = (scrollTo?: string) => + ipcRenderer.send('request-show-preferences-window', scrollTo); export const requestShowProxyWindow = () => ipcRenderer.send('request-show-proxy-window'); export const requestShowSpellcheckLanguagesWindow = () => ipcRenderer.send('request-show-spellcheck-languages-window'); // Notifications -export const requestShowNotification = opts => ipcRenderer.send('request-show-notification', opts); +export const requestShowNotification = (options: { title: string, body: string }) => + ipcRenderer.send('request-show-notification', options); export const requestUpdatePauseNotificationsInfo = () => ipcRenderer.send('request-update-pause-notifications-info'); export const getPauseNotificationsInfo = () => ipcRenderer.sendSync('get-pause-notifications-info'); @@ -89,25 +94,27 @@ export const requestCreateWorkspace = ( transparentBackground, ); -export const requestHibernateWorkspace = id => ipcRenderer.send('request-hibernate-workspace', id); -export const requestOpenUrlInWorkspace = (url, id) => ipcRenderer.send('request-open-url-in-workspace', url, id); +export const requestHibernateWorkspace = (id: string) => ipcRenderer.send('request-hibernate-workspace', id); +export const requestOpenUrlInWorkspace = (url: string, id: string) => + ipcRenderer.send('request-open-url-in-workspace', url, id); export const requestRealignActiveWorkspace = () => ipcRenderer.send('request-realign-active-workspace'); -export const requestRemoveWorkspace = id => ipcRenderer.send('request-remove-workspace', id); -export const requestRemoveWorkspacePicture = id => ipcRenderer.send('request-remove-workspace-picture', id); -export const requestSetActiveWorkspace = id => ipcRenderer.send('request-set-active-workspace', id); -export const requestSetWorkspace = (id, opts) => ipcRenderer.send('request-set-workspace', id, opts); +export const requestRemoveWorkspace = (id: string) => ipcRenderer.send('request-remove-workspace', id); +export const requestRemoveWorkspacePicture = (id: string) => ipcRenderer.send('request-remove-workspace-picture', id); +export const requestSetActiveWorkspace = (id: string) => ipcRenderer.send('request-set-active-workspace', id); +export const requestSetWorkspace = (id: string, options) => ipcRenderer.send('request-set-workspace', id, options); export const requestSetWorkspaces = workspaces => ipcRenderer.send('request-set-workspaces', workspaces); -export const requestSetWorkspacePicture = (id, picturePath) => +export const requestSetWorkspacePicture = (id: string, picturePath: string) => ipcRenderer.send('request-set-workspace-picture', id, picturePath); -export const requestWakeUpWorkspace = id => ipcRenderer.send('request-wake-up-workspace', id); +export const requestWakeUpWorkspace = (id: string) => ipcRenderer.send('request-wake-up-workspace', id); +// eslint-disable-next-line sonarjs/no-duplicate-string export const getIconPath = () => ipcRenderer.sendSync('get-constant', 'ICON_PATH'); export const getReactPath = () => ipcRenderer.sendSync('get-constant', 'REACT_PATH'); export const getDesktopPath = () => ipcRenderer.sendSync('get-constant', 'DESKTOP_PATH'); export const getLogFolderPath = () => ipcRenderer.sendSync('get-constant', 'LOG_FOLDER'); // Workspace Meta -export const getWorkspaceMeta = id => ipcRenderer.sendSync('get-workspace-meta', id); +export const getWorkspaceMeta = (id: string) => ipcRenderer.sendSync('get-workspace-meta', id); export const getWorkspaceMetas = () => ipcRenderer.sendSync('get-workspace-metas'); // Workspace Git @@ -115,15 +122,16 @@ export const initWikiGit = (wikiFolderPath: string, githubRepoUrl: string, userI ipcRenderer.invoke('request-init-wiki-git', wikiFolderPath, githubRepoUrl, userInfo, isMainWiki); // Find In Page -export const requestFindInPage = (text, forward) => ipcRenderer.send('request-find-in-page', text, forward); -export const requestStopFindInPage = close => ipcRenderer.send('request-stop-find-in-page', close); +export const requestFindInPage = (text: string, forward?: boolean) => + ipcRenderer.send('request-find-in-page', text, !!forward); +export const requestStopFindInPage = (close?: boolean) => ipcRenderer.send('request-stop-find-in-page', !!close); // Auth -export const requestValidateAuthIdentity = (windowId, username, password) => +export const requestValidateAuthIdentity = (windowId: string, username: string, password: string) => ipcRenderer.send('request-validate-auth-identity', windowId, username, password); // Native Theme export const getShouldUseDarkColors = () => ipcRenderer.sendSync('get-should-use-dark-colors'); // Online Status -export const signalOnlineStatusChanged = online => ipcRenderer.send('online-status-changed', online); +export const signalOnlineStatusChanged = (online: boolean) => ipcRenderer.send('online-status-changed', online); diff --git a/src/state/dialog-add-workspace/actions.js b/src/state/dialog-add-workspace/actions.js index eceed0c9..ade4654c 100755 --- a/src/state/dialog-add-workspace/actions.js +++ b/src/state/dialog-add-workspace/actions.js @@ -1,14 +1,9 @@ +// @flow /* eslint-disable consistent-return */ /* eslint-disable unicorn/no-null */ /* eslint-disable unicorn/consistent-function-scoping */ -import { - ADD_WORKSPACE_CREATE_WIKI_MESSAGE, - ADD_WORKSPACE_UPDATE_SCROLL_OFFSET, - ADD_WORKSPACE_UPDATE_DOWNLOADING_ICON, - ADD_WORKSPACE_UPDATE_FORM, - ADD_WORKSPACE_UPDATE_MODE, - ADD_WORKSPACE_UPDATE_QUERY, -} from '../../constants/actions'; +import type { Dispatch } from 'redux'; +import { ADD_WORKSPACE_CREATE_WIKI_MESSAGE, ADD_WORKSPACE_UPDATE_FORM } from '../../constants/actions'; import validate from '../../helpers/validate'; import isUrl from '../../helpers/is-url'; @@ -16,16 +11,11 @@ import hasErrors from '../../helpers/has-errors'; import { requestCreateWorkspace } from '../../senders'; -export const setWikiCreationMessage = message => ({ +export const setWikiCreationMessage = (message: string) => ({ type: ADD_WORKSPACE_CREATE_WIKI_MESSAGE, value: message, }); -export const updateQuery = query => ({ - type: ADD_WORKSPACE_UPDATE_QUERY, - query, -}); - const getValidationRules = () => ({ name: { fieldName: 'Name', @@ -38,66 +28,7 @@ const getValidationRules = () => ({ }, }); -// to be replaced with invoke (electron 7+) -// https://electronjs.org/docs/api/ipc-renderer#ipcrendererinvokechannel-args -export const getWebsiteIconUrlAsync = url => - new Promise((resolve, reject) => { - try { - const id = Date.now().toString(); - const { ipcRenderer } = window.require('electron'); - ipcRenderer.once(id, (event, newUrl) => { - resolve(newUrl); - }); - ipcRenderer.send('request-get-website-icon-url', id, url); - } catch (error) { - reject(error); - } - }); - -export const getIconFromInternet = forceOverwrite => (dispatch, getState) => { - const { - form: { picturePath, homeUrl, homeUrlError }, - } = getState().dialogAddWorkspace; - if ((!forceOverwrite && picturePath) || !homeUrl || homeUrlError) return; - - dispatch({ - type: ADD_WORKSPACE_UPDATE_DOWNLOADING_ICON, - downloadingIcon: true, - }); - - getWebsiteIconUrlAsync(homeUrl) - .then(iconUrl => { - const { form } = getState().dialogAddWorkspace; - if (form.homeUrl === homeUrl) { - const changes = { internetIcon: iconUrl || form.internetIcon }; - if (forceOverwrite) changes.picturePath = null; - dispatch({ - type: ADD_WORKSPACE_UPDATE_FORM, - changes, - }); - dispatch({ - type: ADD_WORKSPACE_UPDATE_DOWNLOADING_ICON, - downloadingIcon: false, - }); - } - - if (forceOverwrite && !iconUrl) { - const { remote } = window.require('electron'); - return remote.dialog.showMessageBox(remote.getCurrentWindow(), { - message: 'Unable to find a suitable icon from the Internet.', - buttons: ['OK'], - cancelId: 0, - defaultId: 0, - }); - } - - return null; - }) - .catch(console.log); // eslint-disable-line no-console -}; - -let timeout2; -export const updateForm = changes => (dispatch, getState) => { +export const updateForm = changes => (dispatch: Dispatch, getState) => { const oldHomeUrl = getState().dialogAddWorkspace.form.homeUrl; dispatch({ @@ -105,12 +36,8 @@ export const updateForm = changes => (dispatch, getState) => { changes: validate(changes, getValidationRules()), }); - clearTimeout(timeout2); if (getState().dialogAddWorkspace.form.homeUrl === oldHomeUrl) return; // url didn't change if (changes.internetIcon === null) return; // user explictly want to get rid of icon - timeout2 = setTimeout(() => { - dispatch(getIconFromInternet()); - }, 300); }; export const save = () => async (dispatch, getState) => { @@ -139,13 +66,3 @@ export const save = () => async (dispatch, getState) => { dispatch(setWikiCreationMessage('工作区更新完毕,正在启动Wiki')); } }; - -export const updateMode = mode => ({ - type: ADD_WORKSPACE_UPDATE_MODE, - mode, -}); - -export const updateScrollOffset = scrollOffset => ({ - type: ADD_WORKSPACE_UPDATE_SCROLL_OFFSET, - scrollOffset, -}); diff --git a/src/state/dialog-add-workspace/reducers.js b/src/state/dialog-add-workspace/reducers.js index 10d40f96..c94675ad 100755 --- a/src/state/dialog-add-workspace/reducers.js +++ b/src/state/dialog-add-workspace/reducers.js @@ -1,18 +1,6 @@ import { combineReducers } from 'redux'; -import { - ADD_WORKSPACE_CREATE_WIKI_MESSAGE, - ADD_WORKSPACE_GET_FAILED, - ADD_WORKSPACE_GET_REQUEST, - ADD_WORKSPACE_GET_SUCCESS, - ADD_WORKSPACE_RESET, - ADD_WORKSPACE_UPDATE_SCROLL_OFFSET, - ADD_WORKSPACE_UPDATE_CURRENT_QUERY, - ADD_WORKSPACE_UPDATE_DOWNLOADING_ICON, - ADD_WORKSPACE_UPDATE_FORM, - ADD_WORKSPACE_UPDATE_MODE, - ADD_WORKSPACE_UPDATE_QUERY, -} from '../../constants/actions'; +import { ADD_WORKSPACE_CREATE_WIKI_MESSAGE, ADD_WORKSPACE_UPDATE_FORM } from '../../constants/actions'; const wikiCreationMessage = (state = '', action) => { switch (action.type) { @@ -23,62 +11,6 @@ const wikiCreationMessage = (state = '', action) => { } }; -const hasFailed = (state = false, action) => { - switch (action.type) { - case ADD_WORKSPACE_GET_FAILED: return true; - case ADD_WORKSPACE_GET_REQUEST: return false; - case ADD_WORKSPACE_GET_SUCCESS: return false; - default: return state; - } -}; - -const hits = (state = [], action) => { - switch (action.type) { - case ADD_WORKSPACE_GET_SUCCESS: return state.concat(action.hits); - case ADD_WORKSPACE_RESET: return []; - default: return state; - } -}; - -const isGetting = (state = false, action) => { - switch (action.type) { - case ADD_WORKSPACE_GET_FAILED: return false; - case ADD_WORKSPACE_GET_REQUEST: return true; - case ADD_WORKSPACE_GET_SUCCESS: return false; - default: return state; - } -}; - -const page = (state = -1, action) => { - switch (action.type) { - case ADD_WORKSPACE_GET_SUCCESS: return action.page; - case ADD_WORKSPACE_RESET: return -1; - default: return state; - } -}; - -const currentQuery = (state = '', action) => { - switch (action.type) { - case ADD_WORKSPACE_UPDATE_CURRENT_QUERY: return action.currentQuery; - default: return state; - } -}; - -const query = (state = '', action) => { - switch (action.type) { - case ADD_WORKSPACE_UPDATE_QUERY: return action.query; - default: return state; - } -}; - -const totalPage = (state = 1, action) => { - switch (action.type) { - case ADD_WORKSPACE_GET_SUCCESS: return action.totalPage; - case ADD_WORKSPACE_RESET: return 1; - default: return state; - } -}; - const defaultForm = { name: '', homeUrl: '', @@ -86,44 +18,14 @@ const defaultForm = { }; const form = (state = defaultForm, action) => { switch (action.type) { - case ADD_WORKSPACE_UPDATE_FORM: return { ...state, ...action.changes }; - default: return state; - } -}; - -const defaultMode = 'catalog'; -const mode = (state = defaultMode, action) => { - switch (action.type) { - case ADD_WORKSPACE_UPDATE_MODE: return action.mode; - default: return state; - } -}; - -const downloadingIcon = (state = false, action) => { - switch (action.type) { - case ADD_WORKSPACE_UPDATE_DOWNLOADING_ICON: return action.downloadingIcon; - default: return state; - } -}; - -const scrollOffset = (state = 0, action) => { - switch (action.type) { - case ADD_WORKSPACE_UPDATE_SCROLL_OFFSET: return action.scrollOffset; - default: return state; + case ADD_WORKSPACE_UPDATE_FORM: + return { ...state, ...action.changes }; + default: + return state; } }; export default combineReducers({ wikiCreationMessage, - currentQuery, - downloadingIcon, form, - hasFailed, - hits, - isGetting, - mode, - page, - query, - scrollOffset, - totalPage, });