Skip to content

Commit

Permalink
feat: KTP-27 refresh on activate (#5)
Browse files Browse the repository at this point in the history
* feat: refresh state on activate and other cases

* complete refresh on active
  • Loading branch information
a7madgamal authored Mar 27, 2020
1 parent ebba8e3 commit 3a711ce
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 33 deletions.
27 changes: 27 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const IPC_SELECTOR = 'IPC_SELECTOR'
const IPC_REFRESH_TICKETS = 'IPC_REFRESH_TICKETS'
const IPC_REFRESH_PRS = 'IPC_REFRESH_PRS'
const IPC_REFRESH_GIT = 'IPC_REFRESH_GIT'
const IPC_CHECKOUT_LOCAL_BRANCH = 'IPC_CHECKOUT_LOCAL_BRANCH'
const IPC_CREATE_BRANCH = 'IPC_CREATE_BRANCH'
const IPC_REBASE_BRANCH = 'IPC_REBASE_BRANCH'
const IPC_DELETE_BRANCH = 'IPC_DELETE_BRANCH'
const IPC_PUSH_BRANCH = 'IPC_PUSH_BRANCH'
const IPC_CANCEL_SELECT = 'IPC_CANCEL_SELECT'
const IPC_HIDE_SELECT = 'IPC_HIDE_SELECT'
const IPC_REPO_SELECT = 'IPC_REPO_SELECT'

export {
IPC_SELECTOR,
IPC_REFRESH_TICKETS,
IPC_REFRESH_PRS,
IPC_REFRESH_GIT,
IPC_CHECKOUT_LOCAL_BRANCH,
IPC_CREATE_BRANCH,
IPC_REBASE_BRANCH,
IPC_DELETE_BRANCH,
IPC_PUSH_BRANCH,
IPC_CANCEL_SELECT,
IPC_REPO_SELECT,
IPC_HIDE_SELECT,
}
41 changes: 34 additions & 7 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ import { showNotification } from '../plugins/notifications'
// @ts-ignore
import electronTimber from 'electron-timber'
import { updateChecker } from '../plugins/updateChecker'
import {
IPC_CHECKOUT_LOCAL_BRANCH,
IPC_CREATE_BRANCH,
IPC_REBASE_BRANCH,
IPC_DELETE_BRANCH,
IPC_PUSH_BRANCH,
IPC_CANCEL_SELECT,
IPC_HIDE_SELECT,
IPC_REFRESH_TICKETS,
IPC_REFRESH_GIT,
IPC_REFRESH_PRS,
} from '../constants'

const logger = electronTimber.create({ name: 'index' })

Expand Down Expand Up @@ -66,6 +78,12 @@ app.on('ready', () => {
// getInfo()
})

app.on('browser-window-focus', () => {
mainWindow.webContents.send(IPC_REFRESH_TICKETS)
mainWindow.webContents.send(IPC_REFRESH_GIT)
mainWindow.webContents.send(IPC_REFRESH_PRS)
})

function registerShortcuts() {
okk(
globalShortcut.register('Control+z', () => {
Expand All @@ -74,55 +92,64 @@ function registerShortcuts() {
)
}

ipcMain.on('on-create-branch-click', async (e, key) => {
ipcMain.on(IPC_CREATE_BRANCH, async (e, key) => {
const result = await createBranchFromTicketId(key)

if (result) {
showNotification({
title: 'branch created',
body: key,
})
mainWindow.webContents.send(IPC_REFRESH_GIT)
}
})

ipcMain.on(
'on-delete-branch-click',
IPC_DELETE_BRANCH,
async (e, repoId: string, branchName: string, isRemote: boolean) => {
await deleteBranch(repoId, branchName, isRemote, false)
if (isRemote) {
mainWindow.webContents.send(IPC_REFRESH_PRS)
} else {
mainWindow.webContents.send(IPC_REFRESH_GIT)
}
},
)

ipcMain.on('on-rebase-local-branch-click', async (e, repoId, branchName) => {
ipcMain.on(IPC_REBASE_BRANCH, async (e, repoId, branchName) => {
await rebaseLocalBranch(repoId, branchName)
showNotification({
title: 'branch rebased',
body: `${repoId}:${branchName}`,
})
mainWindow.webContents.send(IPC_REFRESH_GIT)
})

ipcMain.on(
'on-push-local-branch-click',
IPC_PUSH_BRANCH,
async (
e,
{ repoId, skipChecks, branchName }: Parameters<typeof pushTask>[0],
) => {
await pushTask({ repoId, skipChecks, branchName })
mainWindow.webContents.send(IPC_REFRESH_GIT)
},
)

ipcMain.on('on-checkout-local-branch-click', async (e, repoId, branchName) => {
ipcMain.on(IPC_CHECKOUT_LOCAL_BRANCH, async (e, repoId, branchName) => {
await checkoutLocalBranch(repoId, branchName)
showNotification({
title: 'checked out branch',
body: `${repoId}:${branchName}`,
})
mainWindow.webContents.send(IPC_REFRESH_GIT)
})

ipcMain.on('hide-select-window', () => {
ipcMain.on(IPC_HIDE_SELECT, () => {
selectWindow.hide()
})

ipcMain.on('cancel-select-window', () => {
ipcMain.on(IPC_CANCEL_SELECT, () => {
selectWindow.hide()
})

Expand Down
7 changes: 4 additions & 3 deletions src/plugins/windows.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BrowserWindow, screen, ipcMain } from 'electron'
import { IPC_SELECTOR, IPC_CANCEL_SELECT, IPC_REPO_SELECT } from '../constants'

var mainWindow: BrowserWindow
var selectWindow: BrowserWindow
Expand Down Expand Up @@ -46,7 +47,7 @@ const createSelectWindow = () => {
selectWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY)
// selectWindow.webContents.openDevTools()
selectWindow.webContents.on('did-finish-load', () => {
selectWindow.webContents.send('navigate_to_selector')
selectWindow.webContents.send(IPC_SELECTOR)
})
return selectWindow
// mainWindow.on('closed', () => {
Expand All @@ -58,10 +59,10 @@ const showRepoSelector = () => {
selectWindow.show()

return new Promise<{ repoId: string } | false>((resolve, reject) => {
ipcMain.once('repo-selected', (e, repoId: string) => {
ipcMain.once(IPC_REPO_SELECT, (e, repoId: string) => {
resolve({ repoId })
})
ipcMain.once('cancel-select-window', e => {
ipcMain.once(IPC_CANCEL_SELECT, e => {
resolve(undefined)
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/store/branches/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
checkoutLocalBranch,
getGitRepoFromId,
} from '../../plugins/git'
import { TAppState, getRepoSettingsFromId } from '..'
import { TAppState } from '..'

import { LOCAL_BRANCHES_UPDATED, TBranches, LOAD_BRANCHES } from './types'
import { showNotification } from '../../plugins/notifications'
Expand All @@ -16,7 +16,7 @@ import { okk } from '../../helpers/helpers'
import electronTimber from 'electron-timber'
const logger = electronTimber.create({ name: 'branches/actions' })

export const fetchBranches = (): ThunkAction<
export const fetchGit = (): ThunkAction<
void,
TAppState,
null,
Expand Down
1 change: 1 addition & 0 deletions src/store/tickets/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const fetchTickets = (
}
} catch (error) {
logger.error('fetchTickets failed', error)

dispatch({ type: LOADING_JIRA_TICKETS_FAIL })

return
Expand Down
15 changes: 5 additions & 10 deletions src/windows/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, { useEffect } from 'react'
import { css, jsx } from '@emotion/core'

import { fetchTickets, fetchPRs } from '../store/tickets/actions'
import { fetchBranches } from '../store/branches/actions'
import { fetchGit } from '../store/branches/actions'

import { connect, ConnectedProps } from 'react-redux'
import { TAppState } from '../store'
Expand All @@ -25,7 +25,7 @@ const mapState = (state: TAppState) => ({
const mapDispatch = {
fetchTicketsAction: fetchTickets,
fetchPRsAction: fetchPRs,
fetchBranchesAction: fetchBranches,
fetchGitAction: fetchGit,
}

const connector = connect(mapState, mapDispatch)
Expand All @@ -38,14 +38,14 @@ const app: React.FC<TAppProps> = ({
settings,
fetchTicketsAction,
fetchPRsAction,
fetchBranchesAction,
fetchGitAction,
history,
}) => {
const fetchData = (isFirstTime: boolean) => {
if (validateSettings(settings)) {
fetchTicketsAction(isFirstTime)
fetchPRsAction(isFirstTime)
fetchBranchesAction()
fetchGitAction()
} else {
history.replace('/settings')
}
Expand Down Expand Up @@ -119,12 +119,7 @@ const app: React.FC<TAppProps> = ({
text-align: right;
`}
>
<div>
<span css={BadgeStyle}>Ctrl + z</span> to show this window
</div>
<div>
<span css={BadgeStyle}>Cmd + shift + up</span> to open push dialogue
</div>
<span css={BadgeStyle}>Ctrl + z</span> to show this window
</div>
</div>
)
Expand Down
17 changes: 12 additions & 5 deletions src/windows/components/TicketRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import { TBranches } from '../../store/branches/types'
import { ticketUrlFromKey } from '../../plugins/jira'
import { TExtendedPullRequest } from '../../types'
import { pushTask } from '../../tasks/push'
import {
IPC_CHECKOUT_LOCAL_BRANCH,
IPC_CREATE_BRANCH,
IPC_REBASE_BRANCH,
IPC_DELETE_BRANCH,
IPC_PUSH_BRANCH,
} from '../../constants'

interface ITicketRowProps {
relatedPRs: Array<TExtendedPullRequest>
Expand Down Expand Up @@ -100,7 +107,7 @@ const TicketRow: React.FC<ITicketRowProps> = ({
icon={faCheckSquare}
onClick={async () => {
ipcRenderer.send(
'on-checkout-local-branch-click',
IPC_CHECKOUT_LOCAL_BRANCH,
relatedBranch.repoId,
relatedBranch.name,
)
Expand Down Expand Up @@ -139,7 +146,7 @@ const TicketRow: React.FC<ITicketRowProps> = ({
icon={faExchangeAlt}
onClick={async () => {
ipcRenderer.send(
'on-rebase-local-branch-click',
IPC_REBASE_BRANCH,
relatedBranch.repoId,
relatedBranch.name,
)
Expand Down Expand Up @@ -168,7 +175,7 @@ const TicketRow: React.FC<ITicketRowProps> = ({
branchName: relatedBranch.name,
}

ipcRenderer.send('on-push-local-branch-click', options)
ipcRenderer.send(IPC_PUSH_BRANCH, options)
}}
css={css`
${ClickableBadgeStyle}
Expand All @@ -181,7 +188,7 @@ const TicketRow: React.FC<ITicketRowProps> = ({
icon={faTrashAlt}
onClick={async () => {
ipcRenderer.send(
'on-delete-branch-click',
IPC_DELETE_BRANCH,
relatedBranch.repoId,
relatedBranch.name,
relatedBranch.isRemote,
Expand All @@ -197,7 +204,7 @@ const TicketRow: React.FC<ITicketRowProps> = ({

<span
onClick={async () => {
ipcRenderer.send('on-create-branch-click', ticketData.key)
ipcRenderer.send(IPC_CREATE_BRANCH, ticketData.key)
}}
css={css`
${BadgeStyle}
Expand Down
25 changes: 24 additions & 1 deletion src/windows/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import sourceMapSupport from 'source-map-support'
sourceMapSupport.install()

import electronUnhandled from 'electron-unhandled'
electronUnhandled({ showDialog: true })

Expand All @@ -16,13 +19,33 @@ import { Select } from './modals/Select'
import { store } from '../store/index'
import { ErrorBoundary } from '../ErrorBoundary'
import { ipcRenderer } from 'electron'
import { fetchTickets, fetchPRs } from '../store/tickets/actions'
import { fetchGit } from '../store/branches/actions'
import {
IPC_SELECTOR,
IPC_REFRESH_TICKETS,
IPC_REFRESH_PRS,
IPC_REFRESH_GIT,
} from '../constants'

const customHistory = createHashHistory()

ipcRenderer.on('navigate_to_selector', event => {
ipcRenderer.on(IPC_SELECTOR, event => {
customHistory.replace('/select')
})

ipcRenderer.on(IPC_REFRESH_TICKETS, event => {
fetchTickets(false)(store.dispatch, store.getState, null)
})

ipcRenderer.on(IPC_REFRESH_PRS, event => {
fetchPRs(false)(store.dispatch, store.getState, null)
})

ipcRenderer.on(IPC_REFRESH_GIT, event => {
fetchGit()(store.dispatch, store.getState, null)
})

render(
<React.StrictMode>
<ErrorBoundary>
Expand Down
11 changes: 8 additions & 3 deletions src/windows/modals/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { ipcRenderer } from 'electron'
import { css, jsx } from '@emotion/core'
import { connect, ConnectedProps } from 'react-redux'
import { TAppState } from '../../store'
import {
IPC_CANCEL_SELECT,
IPC_REPO_SELECT,
IPC_HIDE_SELECT,
} from '../../constants'

const connector = connect((state: TAppState) => ({
settings: state.settings,
Expand Down Expand Up @@ -35,7 +40,7 @@ const select: React.FC<TProps> = ({ settings }) => {
}
`}
onClick={() => {
ipcRenderer.send('cancel-select-window')
ipcRenderer.send(IPC_CANCEL_SELECT)
}}
>
x
Expand Down Expand Up @@ -65,8 +70,8 @@ const select: React.FC<TProps> = ({ settings }) => {
`}
key={repoId}
onClick={() => {
ipcRenderer.send('repo-selected', repoId)
ipcRenderer.send('hide-select-window')
ipcRenderer.send(IPC_REPO_SELECT, repoId)
ipcRenderer.send(IPC_HIDE_SELECT)
}}
>
{repoId}
Expand Down
2 changes: 0 additions & 2 deletions src/windows/modals/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ const settings: React.FC<TProps> = ({ settings, saveSettingsAction }) => (

<Field name={`${fieldKey}.enableAutoRefresh`}>
{({ input }) => {
console.log(input.value, input.checked)

return (
<div>
<input
Expand Down

0 comments on commit 3a711ce

Please sign in to comment.