Skip to content

Commit

Permalink
Using Realm in renderer process with existing UI
Browse files Browse the repository at this point in the history
  • Loading branch information
stel committed Aug 9, 2017
1 parent 387e132 commit 4f3b39d
Show file tree
Hide file tree
Showing 12 changed files with 617 additions and 70 deletions.
49 changes: 46 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-draggable": "^2.2.6",
"react-virtualized": "^9.9.0",
"realm": "^1.10.0"
},
"devDependencies": {
"@types/node": "^8.0.8",
"@types/react": "^15.0.34",
"@types/react-dom": "^15.5.1",
"@types/react-virtualized": "^9.7.3",
"electron": "^1.6.11",
"electron-builder": "^19.6.3",
"node-sass": "^4.5.3",
Expand Down
3 changes: 2 additions & 1 deletion src/main/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export default class Application {
var window = this.windowManager.createWindow(path);

window.once('ready-to-show', () => {
window.show()
window.show();
window.webContents.send('open-file', { path: path });
});
}

Expand Down
58 changes: 52 additions & 6 deletions src/renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
import * as electron from 'electron'
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import Browser from './ui/browser/browser'

// FIXME: see https://github.com/realm/realm-js/issues/818
var userDataPath = electron.remote.app.getPath("userData");
process.chdir(userDataPath);
import * as Realm from 'realm'

// We might want to keep a strong referencce to realm when using sync
var realmRef: Realm

ReactDOM.render(
<div>
Hi there
</div>,
document.getElementById("app")
);
electron.ipcRenderer.on("open-file", (event: Event, args: { path: string }) => {
const configuration: Realm.Configuration = {
path: args.path
};

openWithConfiguration(configuration);
});

electron.ipcRenderer.on("open-url", (event: Event, args: { url: string, username: string, password: string }) => {
const authURL = args.url;

Realm.Sync.User.login(authURL, args.username, args.password, (error: any, user: Realm.Sync.User) => {
if (user) {
const configuration: Realm.Configuration = {
sync: {
user: user,
url: args.url,
validate_ssl: false
}
};

openWithConfiguration(configuration);
} else {
// TODO: display errors properly
alert(error);
}
});
});

function openWithConfiguration(configuration: Realm.Configuration) {
// TODOL this shouldn't happen so we may consider to throw an exception instead
if (realmRef) {
realmRef.close();
}

Realm.openAsync(configuration, (error: any, realm: Realm) => {
realmRef = realm;

if (realm) {
ReactDOM.render(
<Browser realm={realm} />,
document.getElementById("app")
);
} else {
// TODO: display errors properly
alert(error);
}
});
}
51 changes: 51 additions & 0 deletions src/ui/browser/browser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as React from 'react';
import * as Realm from 'realm';
import Sidebar from './sidebar/sidebar';
import Content from './content/content';

interface IBrowserProps {
realm: Realm
}

interface IBrowserState {
selectedIndex: number;
}

export default class Browser extends React.Component<IBrowserProps, IBrowserState> {
private types: Realm.ObjectSchema[];

constructor(props: IBrowserProps) {
super(props);

this.state = {
selectedIndex: 0
};
}

sidebarSelectionChanged(index: number) {
this.setState({
selectedIndex: index
});
}

render () {
const selectedIndex = this.state.selectedIndex;
const selectedType = this.props.realm.schema[selectedIndex];

return (
<div className="RealmBrowser">
<div className="RealmBrowser__container">
<Sidebar
realm={this.props.realm}
selectedIndex={selectedIndex}
onSelectionChange={this.sidebarSelectionChanged.bind(this)}
/>
<Content
realm={this.props.realm}
type={selectedType}
/>
</div>
</div>
);
}
}
Loading

0 comments on commit 4f3b39d

Please sign in to comment.