Skip to content

Commit 596f337

Browse files
Pollepsjoepio
authored andcommitted
#283 Refactor feedback
1 parent 7dae5e8 commit 596f337

26 files changed

+104
-94
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ This changelog covers all three packages, as they are (for now) updated as a who
1616
- Add the ability to change the `fetch` function used to fetch resources over http.
1717
- `store.addResource` is depricated in favor of `store.addResources`.
1818
- Add `AgentChange` event on store that is fired whenever the stores agent changes.
19+
- `store.fetchResourceFromServer` now returns the requested resource.
20+
- Add `postCommit` method to `store` that respects the injected `fetch` function.
1921

2022
#### Breaking Changes:
2123

2224
- `uploadFiles()` has moved to `store.uploadFiles()`.
2325
- Remove `Agent.fromJSON()`
24-
- `tryValidURL` and `isValidURL` are now static methods on `Client`.
26+
- `tryValidURL` and `isValidURL` are now static methods on `Client` and have been renamed to `tryValidSubject` and `isValidSubject`.
2527
- Rename `store.fetchResource` to `store.fetchResourceFromServer`.
28+
- Rename `store.handleError` to `store.notifyError`.
29+
- Rename `agent.checkPublicKey` to `agent.verifyPublicKeyWithServer`.
30+
- Remove `store.errorHandler` and replace with new `StoreEvents.Error` event.
2631

2732
## v0.34.10
2833

data-browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@
7272
"typecheck": "tsc --noEmit"
7373
},
7474
"type": "module",
75-
"version": "0.35.0"
75+
"version": "0.35.0-beta.0"
7676
}

data-browser/src/App.tsx

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
import React from 'react';
22
import { BrowserRouter } from 'react-router-dom';
33
import { HelmetProvider } from 'react-helmet-async';
4-
import { StoreContext, Store, urls, StoreEvents } from '@tomic/react';
4+
import { StoreContext, Store, urls } from '@tomic/react';
55

66
import { GlobalStyle, ThemeWrapper } from './styling';
77
import { AppRoutes } from './routes/Routes';
88
import { NavWrapper } from './components/Navigation';
99
import { MetaSetter } from './components/MetaSetter';
1010
import { Toaster } from './components/Toaster';
1111
import { isDev } from './config';
12-
import { handleError, initBugsnag } from './helpers/handlers';
12+
import { initBugsnag } from './helpers/loggingHandlers';
1313
import HotKeysWrapper from './components/HotKeyWrapper';
1414
import { AppSettingsContextProvider } from './helpers/AppSettings';
1515
import CrashPage from './views/CrashPage';
16-
import toast from 'react-hot-toast';
1716
import { DialogContainer } from './components/Dialog/DialogContainer';
1817
import { registerHandlers } from './handlers';
1918
import { ErrorBoundary } from './views/ErrorPage';
2019
import { NetworkIndicator } from './components/NetworkIndicator';
21-
import {
22-
getAgentFromLocalStorage,
23-
saveAgentToLocalStorage,
24-
} from './helpers/agentStorage';
20+
import { getAgentFromLocalStorage } from './helpers/agentStorage';
2521

2622
function fixDevUrl(url: string) {
2723
if (isDev()) {
@@ -31,34 +27,19 @@ function fixDevUrl(url: string) {
3127
return url;
3228
}
3329

30+
/**
31+
* Defaulting to the current URL's origin will make sense in most non-dev environments.
32+
* In dev envs, we want to default to port 9883
33+
*/
34+
const serverUrl = fixDevUrl(window.location.origin);
3435
const initalAgent = getAgentFromLocalStorage();
3536

3637
// Initialize the store
3738
const store = new Store({
3839
agent: initalAgent,
40+
serverUrl,
3941
});
4042

41-
store.on(StoreEvents.AgentChanged, saveAgentToLocalStorage);
42-
43-
/**
44-
* Defaulting to the current URL's origin will make sense in most non-dev environments.
45-
* In dev envs, we want to default to port 9883
46-
*/
47-
const currentOrigin = window.location.origin;
48-
49-
store.setServerUrl(fixDevUrl(currentOrigin));
50-
51-
// Show an error when things go wrong
52-
store.errorHandler = e => {
53-
handleError(e);
54-
55-
if (e.message.length > 100) {
56-
e.message = e.message.substring(0, 100) + '...';
57-
}
58-
59-
toast.error(e.message);
60-
};
61-
6243
declare global {
6344
interface Window {
6445
bugsnagApiKey: string;

data-browser/src/components/HotKeyWrapper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function HotKeysWrapper({ children }: Props): JSX.Element {
7171
shortcuts.edit,
7272
e => {
7373
e.preventDefault();
74-
Client.isValidURL(subject) && navigate(editURL(subject!));
74+
Client.isValidSubject(subject) && navigate(editURL(subject!));
7575
},
7676
{},
7777
[subject],
@@ -80,7 +80,7 @@ function HotKeysWrapper({ children }: Props): JSX.Element {
8080
shortcuts.data,
8181
e => {
8282
e.preventDefault();
83-
Client.isValidURL(subject) && navigate(dataURL(subject!));
83+
Client.isValidSubject(subject) && navigate(dataURL(subject!));
8484
},
8585
{},
8686
[subject],

data-browser/src/components/NewInstanceButton/useCreateAndNavigate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function useCreateAndNavigate(klass: string, parent?: string) {
5050
toast.success(`${title} created`);
5151
store.notifyResourceManuallyCreated(resource);
5252
} catch (e) {
53-
store.handleError(e);
53+
store.notifyError(e);
5454
}
5555

5656
return resource;

data-browser/src/components/ResourceContextMenu/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function ResourceContextMenu({
5050
return null;
5151
}
5252

53-
if (!Client.isValidURL(subject)) {
53+
if (!Client.isValidSubject(subject)) {
5454
return null;
5555
}
5656

data-browser/src/components/Searchbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function Searchbar({
4141
setInput(e.target.value);
4242

4343
try {
44-
Client.tryValidURL(e.target.value);
44+
Client.tryValidSubject(e.target.value);
4545
// Replace instead of push to make the back-button behavior better.
4646
navigate(constructOpenURL(e.target.value), { replace: true });
4747
} catch (_err) {

data-browser/src/components/forms/ResourceForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export function ResourceForm({
137137
function handleAddProp() {
138138
setNewPropErr(undefined);
139139

140-
if (!Client.isValidURL(newProperty)) {
140+
if (!Client.isValidSubject(newProperty)) {
141141
setNewPropErr(new Error('Invalid URL'));
142142

143143
return;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import toast from 'react-hot-toast';
2+
import { handleError } from '../helpers/loggingHandlers';
3+
4+
export const errorHandler = (e: Error) => {
5+
handleError(e);
6+
7+
let message = e.message;
8+
9+
if (e.message.length > 100) {
10+
message = e.message.substring(0, 100) + '...';
11+
}
12+
13+
toast.error(message);
14+
};

data-browser/src/handlers/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Store, StoreEvents } from '@tomic/react';
2+
import { saveAgentToLocalStorage } from '../helpers/agentStorage';
3+
import { errorHandler } from './errorHandler';
24
import {
35
buildSideBarNewResourceHandler,
46
buildSideBarRemoveResourceHandler,
@@ -13,4 +15,6 @@ export function registerHandlers(store: Store) {
1315
StoreEvents.ResourceRemoved,
1416
buildSideBarRemoveResourceHandler(store),
1517
);
18+
store.on(StoreEvents.Error, errorHandler);
19+
store.on(StoreEvents.AgentChanged, saveAgentToLocalStorage);
1620
}

0 commit comments

Comments
 (0)