Skip to content

Commit d87b1fb

Browse files
committed
feat: add recent spaces, home displays favorite and recent spaces
1 parent 5e46b30 commit d87b1fb

39 files changed

+705
-239
lines changed

public/app/config/channels.js

+1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ module.exports = {
4949
GET_USER_MODE_CHANNEL: 'user:mode:get',
5050
SET_USER_MODE_CHANNEL: 'user:mode:set',
5151
SET_SPACE_AS_FAVORITE_CHANNEL: 'user:set-space-favorite:set',
52+
SET_SPACE_AS_RECENT_CHANNEL: 'user:set-space-recent:set',
5253
};

public/app/config/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ const DEFAULT_USER = {
6363
userMode: DEFAULT_USER_MODE,
6464
},
6565
favoriteSpaces: [],
66+
recentSpaces: [],
6667
};
6768

6869
const ANONYMOUS_USERNAME = 'Anonymous';
6970

71+
const MAX_RECENT_SPACES_SPACES = 5;
72+
7073
module.exports = {
7174
DEFAULT_LOGGING_LEVEL,
7275
DEFAULT_PROTOCOL,
@@ -89,4 +92,5 @@ module.exports = {
8992
VISUAL_SYNC_MODE,
9093
DEFAULT_SYNC_MODE,
9194
DEFAULT_USER_MODE,
95+
MAX_RECENT_SPACES_SPACES,
9296
};

public/app/config/messages.js

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const ERROR_GETTING_USER_MODE = 'There was an error getting the user mode';
6161
const ERROR_SETTING_USER_MODE = 'There was an error setting the user mode';
6262
const ERROR_SETTING_SPACE_AS_FAVORITE =
6363
'There was an error setting space as favorite';
64+
const ERROR_SETTING_SPACE_AS_RECENT =
65+
'There was an error setting space as recent space';
6466

6567
module.exports = {
6668
ERROR_GETTING_DEVELOPER_MODE,
@@ -108,4 +110,5 @@ module.exports = {
108110
ERROR_SETTING_USER_MODE,
109111
ERROR_GETTING_USER_MODE,
110112
ERROR_SETTING_SPACE_AS_FAVORITE,
113+
ERROR_SETTING_SPACE_AS_RECENT,
111114
};

public/app/listeners/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const postAppInstanceResource = require('./postAppInstanceResource');
3030
const patchAppInstanceResource = require('./patchAppInstanceResource');
3131
const getAppInstance = require('./getAppInstance');
3232
const setSpaceAsFavorite = require('./setSpaceAsFavorite');
33+
const setSpaceAsRecent = require('./setSpaceAsRecent');
3334

3435
module.exports = {
3536
loadSpace,
@@ -64,4 +65,5 @@ module.exports = {
6465
getUserMode,
6566
setUserMode,
6667
setSpaceAsFavorite,
68+
setSpaceAsRecent,
6769
};

public/app/listeners/loadSpace.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const loadSpace = (mainWindow, db) => async (event, { fileLocation }) => {
9797
.push(...actions)
9898
.write();
9999

100-
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL);
100+
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, { spaceId: id });
101101
} catch (err) {
102102
logger.error(err);
103103
mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);

public/app/listeners/setSpaceAsFavorite.js

+9-21
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,19 @@ const setSpaceAsFavorite = (mainWindow, db) => async (
66
event,
77
{ spaceId, favorite }
88
) => {
9-
// add spaceId if does not exist
109
try {
11-
if (favorite) {
12-
const foundId = db
13-
.get('user.favoriteSpaces')
14-
.find(id => id === spaceId)
15-
.value();
16-
if (!foundId) {
17-
db.get('user.favoriteSpaces')
18-
.push(spaceId)
19-
.write();
20-
}
10+
const favoriteSpaces = db.get('user.favoriteSpaces');
11+
const foundId = favoriteSpaces.find(id => id === spaceId).value();
12+
13+
// add spaceId if does not exist
14+
if (favorite && !foundId) {
15+
favoriteSpaces.push(spaceId).write();
2116
}
2217
// remove spaceId if exists
23-
else {
24-
const foundId = db
25-
.get('user.favoriteSpaces')
26-
.find(id => id === spaceId)
27-
.value();
28-
if (foundId) {
29-
db.get('user.favoriteSpaces')
30-
.remove(id => id === spaceId)
31-
.write();
32-
}
18+
else if (!favorite && foundId) {
19+
favoriteSpaces.remove(id => id === spaceId).write();
3320
}
21+
3422
mainWindow.webContents.send(SET_SPACE_AS_FAVORITE_CHANNEL, {
3523
favorite,
3624
spaceId,
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { SET_SPACE_AS_RECENT_CHANNEL } = require('../config/channels');
2+
const logger = require('../logger');
3+
const { ERROR_GENERAL } = require('../config/errors');
4+
const { MAX_RECENT_SPACES_SPACES } = require('../config/config');
5+
6+
const setSpaceAsRecent = (mainWindow, db) => async (
7+
event,
8+
{ spaceId, isRecent }
9+
) => {
10+
try {
11+
const recentSpaces = db.get('user.recentSpaces');
12+
const foundId = recentSpaces.find(id => id === spaceId).value();
13+
14+
// add spaceId
15+
if (isRecent) {
16+
// if exist, remove, then push
17+
if (foundId) {
18+
recentSpaces.remove(id => id === spaceId).write();
19+
}
20+
recentSpaces.push(spaceId).write();
21+
22+
// remove first element if contains more than fixed elements
23+
if (recentSpaces.size().value() > MAX_RECENT_SPACES_SPACES) {
24+
const firstEl = recentSpaces.take(1).value();
25+
recentSpaces.remove(firstEl).write();
26+
}
27+
}
28+
// remove spaceId if exists
29+
else if (!isRecent && foundId) {
30+
recentSpaces.remove(id => id === spaceId).write();
31+
}
32+
33+
mainWindow.webContents.send(SET_SPACE_AS_RECENT_CHANNEL, {
34+
isRecent,
35+
spaceId,
36+
});
37+
} catch (e) {
38+
logger.error(e);
39+
mainWindow.webContents.send(SET_SPACE_AS_RECENT_CHANNEL, ERROR_GENERAL);
40+
}
41+
};
42+
43+
module.exports = setSpaceAsRecent;

public/electron.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const {
5757
GET_USER_MODE_CHANNEL,
5858
SET_USER_MODE_CHANNEL,
5959
SET_SPACE_AS_FAVORITE_CHANNEL,
60+
SET_SPACE_AS_RECENT_CHANNEL,
6061
} = require('./app/config/channels');
6162
const env = require('./env.json');
6263
const {
@@ -92,6 +93,7 @@ const {
9293
setUserMode,
9394
getUserMode,
9495
setSpaceAsFavorite,
96+
setSpaceAsRecent,
9597
} = require('./app/listeners');
9698
const isMac = require('./app/utils/isMac');
9799

@@ -406,9 +408,12 @@ app.on('ready', async () => {
406408
// called when getting sync mode
407409
ipcMain.on(GET_SYNC_MODE_CHANNEL, getSyncMode(mainWindow, db));
408410

409-
// called when setting developer mode
411+
// called when setting space as favorite
410412
ipcMain.on(SET_SPACE_AS_FAVORITE_CHANNEL, setSpaceAsFavorite(mainWindow, db));
411413

414+
// called when setting space as recent
415+
ipcMain.on(SET_SPACE_AS_RECENT_CHANNEL, setSpaceAsRecent(mainWindow, db));
416+
412417
// called when getting geolocation enabled
413418
ipcMain.on(
414419
GET_GEOLOCATION_ENABLED_CHANNEL,

src/App.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import SpacesNearby from './components/SpacesNearby';
1515
import Settings from './components/Settings';
1616
import LoadSpace from './components/LoadSpace';
1717
import SpaceScreen from './components/space/SpaceScreen';
18-
import FavoriteSpaces from './components/FavoriteSpaces';
1918
import SyncScreen from './components/space/SyncScreen';
2019
import DeveloperScreen from './components/developer/DeveloperScreen';
2120
import { OnlineTheme, OfflineTheme } from './themes';
@@ -33,7 +32,7 @@ import {
3332
DEVELOPER_PATH,
3433
DASHBOARD_PATH,
3534
SIGN_IN_PATH,
36-
FAVORITE_PATH,
35+
SAVED_SPACES_PATH,
3736
} from './config/paths';
3837
import {
3938
getGeolocation,
@@ -50,6 +49,7 @@ import {
5049
CONNECTION_ONLINE_MESSAGE,
5150
} from './config/messages';
5251
import './App.css';
52+
import SavedSpaces from './components/SavedSpaces';
5353

5454
const styles = () => ({
5555
toastrIcon: { marginBottom: '-20px', fontSize: '45px' },
@@ -165,8 +165,8 @@ export class App extends Component {
165165
/>
166166
<Route
167167
exact
168-
path={FAVORITE_PATH}
169-
component={Authorization()(FavoriteSpaces)}
168+
path={SAVED_SPACES_PATH}
169+
component={Authorization()(SavedSpaces)}
170170
/>
171171
<Route
172172
exact

0 commit comments

Comments
 (0)