Skip to content

Commit

Permalink
App slice
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Dec 20, 2024
1 parent 373fa93 commit e424b59
Show file tree
Hide file tree
Showing 33 changed files with 216 additions and 171 deletions.
8 changes: 4 additions & 4 deletions packages/desktop-client/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ expect.extend({
: locator.locator('[data-theme]');

// Check lightmode
await locator.evaluate(() => window.Actual.setTheme('auto'));
await locator.evaluate(() => global.Actual.setTheme('auto'));
await expect(dataThemeLocator).toHaveAttribute('data-theme', 'auto');
const lightmode = await expect(locator).toHaveScreenshot(config);

Expand All @@ -35,7 +35,7 @@ expect.extend({
}

// Switch to darkmode and check
await locator.evaluate(() => window.Actual.setTheme('dark'));
await locator.evaluate(() => global.Actual.setTheme('dark'));
await expect(dataThemeLocator).toHaveAttribute('data-theme', 'dark');
const darkmode = await expect(locator).toHaveScreenshot(config);

Expand All @@ -45,7 +45,7 @@ expect.extend({
}

// Switch to midnight theme and check
await locator.evaluate(() => window.Actual.setTheme('midnight'));
await locator.evaluate(() => global.Actual.setTheme('midnight'));
await expect(dataThemeLocator).toHaveAttribute('data-theme', 'midnight');
const midnightMode = await expect(locator).toHaveScreenshot(config);

Expand All @@ -55,7 +55,7 @@ expect.extend({
}

// Switch back to lightmode
await locator.evaluate(() => window.Actual.setTheme('auto'));
await locator.evaluate(() => global.Actual.setTheme('auto'));
return {
message: () => 'pass',
pass: true,
Expand Down
9 changes: 5 additions & 4 deletions packages/desktop-client/src/browser-preload.browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { initBackend as initSQLBackend } from 'absurd-sql/dist/indexeddb-main-thread';
import { registerSW } from 'virtual:pwa-register';

import { send } from 'loot-core/platform/client/fetch';
import * as Platform from 'loot-core/src/client/platform';

import packageJson from '../package.json';
Expand Down Expand Up @@ -118,10 +119,10 @@ global.Actual = {
reader.readAsArrayBuffer(file);
reader.onload = async function (ev) {
const filepath = `/uploads/${filename}`;

window.__actionsForMenu
.uploadFile(filename, ev.target.result)
.then(() => resolve([filepath]));
send('upload-file-web', {
filename,
contents: ev.target.result,
}).then(() => resolve([filepath]));
};
reader.onerror = function () {
alert('Error reading file');
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop-client/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
closeBudget,
loadBudget,
loadGlobalPrefs,
setAppState,
sync,
} from 'loot-core/client/actions';
import { setAppState } from 'loot-core/client/app/appSlice';
import { SpreadsheetProvider } from 'loot-core/client/SpreadsheetProvider';
import * as Platform from 'loot-core/src/client/platform';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop-client/src/components/FatalError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export function FatalError({ error }: FatalErrorProps) {
)}

<Paragraph>
<Button onPress={() => window.Actual?.relaunch()}>
<Button onPress={() => global.Actual.relaunch()}>
<Trans>Restart app</Trans>
</Button>
</Paragraph>
Expand Down
27 changes: 25 additions & 2 deletions packages/desktop-client/src/components/HelpMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useLocation } from 'react-router-dom';

import { useToggle } from 'usehooks-ts';

import { openDocsForCurrentPage } from 'loot-core/client/actions';
import { pushModal } from 'loot-core/client/actions/modals';

import { useFeatureFlag } from '../hooks/useFeatureFlag';
Expand All @@ -17,6 +16,30 @@ import { Menu } from './common/Menu';
import { Popover } from './common/Popover';
import { SpaceBetween } from './common/SpaceBetween';

const getPageDocs = (page: string) => {
switch (page) {
case '/budget':
return 'https://actualbudget.org/docs/getting-started/envelope-budgeting';
case '/reports':
return 'https://actualbudget.org/docs/reports/';
case '/schedules':
return 'https://actualbudget.org/docs/schedules';
case '/payees':
return 'https://actualbudget.org/docs/transactions/payees';
case '/rules':
return 'https://actualbudget.org/docs/budgeting/rules';
case '/settings':
return 'https://actualbudget.org/docs/settings';
default:
// All pages under /accounts, plus any missing pages
return 'https://actualbudget.org/docs';
}
};

function openDocsForCurrentPage() {
global.Actual.openURLInBrowser(getPageDocs(window.location.pathname));
}

type HelpMenuItem = 'docs' | 'keyboard-shortcuts' | 'goal-templates';

type HelpButtonProps = {
Expand Down Expand Up @@ -58,7 +81,7 @@ export const HelpMenu = () => {
const handleItemSelect = (item: HelpMenuItem) => {
switch (item) {
case 'docs':
dispatch(openDocsForCurrentPage());
openDocsForCurrentPage();
break;
case 'keyboard-shortcuts':
dispatch(pushModal('keyboard-shortcuts'));
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop-client/src/components/UpdateNotification.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { useTranslation } from 'react-i18next';

import { setAppState, updateApp } from 'loot-core/client/actions';
import { setAppState, updateApp } from 'loot-core/client/app/appSlice';

import { SvgClose } from '../icons/v1';
import { useAppSelector, useAppDispatch } from '../redux';
Expand Down Expand Up @@ -69,7 +69,7 @@ export function UpdateNotification() {
textDecoration: 'underline',
}}
onClick={() =>
window.Actual?.openURLInBrowser(
global.Actual.openURLInBrowser(
'https://actualbudget.org/docs/releases',
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop-client/src/components/accounts/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ class AccountInternal extends PureComponent<
const account = this.props.accounts.find(acct => acct.id === accountId);

if (account) {
const res = await window.Actual?.openFileDialog({
const res = await global.Actual.openFileDialog({
filters: [
{
name: t('Financial Files'),
Expand Down Expand Up @@ -667,7 +667,7 @@ class AccountInternal extends PureComponent<
accountName && accountName.replace(/[()]/g, '').replace(/\s+/g, '-');
const filename = `${normalizedName || 'transactions'}.csv`;

window.Actual?.saveFile(
global.Actual.saveFile(
exportedTransactions,
filename,
t('Export Transactions'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function ConfigServer() {
const [error, setError] = useState<string | null>(null);

const restartElectronServer = useCallback(() => {
globalThis.window.Actual.restartElectronServer();
global.Actual.restartElectronServer();
setError(null);
}, []);

Expand Down Expand Up @@ -88,7 +88,7 @@ export function ConfigServer() {
}

async function onSelectSelfSignedCertificate() {
const selfSignedCertificateLocation = await window.Actual?.openFileDialog({
const selfSignedCertificateLocation = await global.Actual.openFileDialog({
properties: ['openFile'],
filters: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect } from 'react';
import { Navigate, Route, Routes } from 'react-router-dom';

import { loggedIn, setAppState } from 'loot-core/client/actions';
import { loggedIn } from 'loot-core/client/actions';
import { setAppState } from 'loot-core/client/app/appSlice';

import { useMetaThemeColor } from '../../hooks/useMetaThemeColor';
import { useAppSelector, useAppDispatch } from '../../redux';
Expand Down Expand Up @@ -44,7 +45,7 @@ function Version() {
},
}}
>
{`App: v${window.Actual?.ACTUAL_VERSION} | Server: ${version}`}
{`App: v${global.Actual.ACTUAL_VERSION} | Server: ${version}`}
</Text>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export function ImportTransactionsModal({ options }) {
}

async function onNewFile() {
const res = await window.Actual?.openFileDialog({
const res = await global.Actual.openFileDialog({
filters: [
{
name: 'Financial Files',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function ConfirmChangeDocumentDirModal({
const dispatch = useAppDispatch();

const restartElectronServer = useCallback(() => {
globalThis.window.Actual?.restartElectronServer();
global.Actual.restartElectronServer();
}, []);

const [_documentDir, setDocumentDirPref] = useGlobalPref(
Expand All @@ -64,7 +64,7 @@ export function ConfirmChangeDocumentDirModal({
setLoading(true);
try {
if (moveFiles) {
await globalThis.window.Actual?.moveBudgetDirectory(
await global.Actual.moveBudgetDirectory(
currentBudgetDirectory,
newDirectory,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function FileLocationSettings() {
const dispatch = useAppDispatch();

async function onChooseDocumentDir() {
const chosenDirectory = await window.Actual?.openFileDialog({
const chosenDirectory = await global.Actual.openFileDialog({
properties: ['openDirectory'],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function ImportActualModal() {
const [importing, setImporting] = useState(false);

async function onImport() {
const res = await window.Actual?.openFileDialog({
const res = await global.Actual.openFileDialog({
properties: ['openFile'],
filters: [{ name: 'actual', extensions: ['zip', 'blob'] }],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function ImportYNAB4Modal() {
const [importing, setImporting] = useState(false);

async function onImport() {
const res = await window.Actual?.openFileDialog({
const res = await global.Actual.openFileDialog({
properties: ['openFile'],
filters: [{ name: 'ynab', extensions: ['zip'] }],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function ImportYNAB5Modal() {
const [importing, setImporting] = useState(false);

async function onImport() {
const res = await window.Actual?.openFileDialog({
const res = await global.Actual.openFileDialog({
properties: ['openFile'],
filters: [{ name: 'ynab', extensions: ['json'] }],
});
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop-client/src/components/reports/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ export function Overview() {
}),
} satisfies ExportImportDashboard;

window.Actual?.saveFile(
global.Actual.saveFile(
JSON.stringify(data, null, 2),
'dashboard.json',
'Export Dashboard',
);
};
const onImport = async () => {
const openFileDialog = window.Actual?.openFileDialog;
const openFileDialog = global.Actual.openFileDialog;

if (!openFileDialog) {
dispatch(
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop-client/src/components/settings/Export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function ExportBudget() {
return;
}

window.Actual?.saveFile(
global.Actual.saveFile(
response.data,
`${format(new Date(), 'yyyy-MM-dd')}-${budgetName}.zip`,
t('Export budget'),
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop-client/src/components/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function About() {
})}
data-vrt-mask
>
<Text>Client version: v{window.Actual?.ACTUAL_VERSION}</Text>
<Text>Client version: v{global.Actual.ACTUAL_VERSION}</Text>
<Text>Server version: {version}</Text>
{isOutdated ? (
<Link
Expand Down
5 changes: 2 additions & 3 deletions packages/desktop-client/src/global-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import {
closeModal,
loadPrefs,
pushModal,
reloadApp,
replaceModal,
setAppState,
} from 'loot-core/client/actions';
import { setAppState } from 'loot-core/client/app/appSlice';
import {
getAccounts,
getCategories,
Expand Down Expand Up @@ -162,6 +161,6 @@ export function handleGlobalEvents(store: AppStore) {
});

listen('api-fetch-redirected', () => {
store.dispatch(reloadApp());
global.Actual.reload();
});
}
2 changes: 1 addition & 1 deletion packages/desktop-client/src/gocardless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function _authorize(

if ('error' in resp) return resp;
const { link, requisitionId } = resp;
window.Actual?.openURLInBrowser(link);
global.Actual.openURLInBrowser(link);

return send('gocardless-poll-web-token', {
upgradingAccountId,
Expand Down
41 changes: 27 additions & 14 deletions packages/desktop-client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { createRoot } from 'react-dom/client';

import * as accountsSlice from 'loot-core/src/client/accounts/accountsSlice';
import * as actions from 'loot-core/src/client/actions';
import * as appSlice from 'loot-core/src/client/app/appSlice';
import * as queriesSlice from 'loot-core/src/client/queries/queriesSlice';
import { runQuery } from 'loot-core/src/client/query-helpers';
import { store } from 'loot-core/src/client/store';
Expand All @@ -34,6 +35,7 @@ const boundActions = bindActionCreators(
{
...actions,
...accountsSlice.actions,
...appSlice.actions,
...queriesSlice.actions,
},
store.dispatch,
Expand All @@ -42,19 +44,8 @@ const boundActions = bindActionCreators(
// Listen for global events from the server or main process
handleGlobalEvents(store);

declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
__actionsForMenu: typeof boundActions & {
undo: typeof undo;
redo: typeof redo;
inputFocused: typeof inputFocused;
};

$send: typeof send;
$query: typeof runQuery;
$q: typeof q;
}
async function appFocused() {
await send('app-focused');
}

function inputFocused() {
Expand All @@ -66,7 +57,13 @@ function inputFocused() {
}

// Expose this to the main process to menu items can access it
window.__actionsForMenu = { ...boundActions, undo, redo, inputFocused };
window.__actionsForMenu = {
...boundActions,
undo,
redo,
appFocused,
inputFocused,
};

// Expose send for fun!
window.$send = send;
Expand All @@ -82,3 +79,19 @@ root.render(
</ServerProvider>
</Provider>,
);

declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
__actionsForMenu: typeof boundActions & {
undo: typeof undo;
redo: typeof redo;
appFocused: typeof appFocused;
inputFocused: typeof inputFocused;
};

$send: typeof send;
$query: typeof runQuery;
$q: typeof q;
}
}
Loading

0 comments on commit e424b59

Please sign in to comment.