Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Add badge in app dock in macos

Hakan Shehude22f2fedc39
diff --git a/apps/desktop/src/main/bootstrapper.ts b/apps/desktop/src/main/bootstrapper.ts
index c6dd9b7f..aeee2486 100644
--- a/apps/desktop/src/main/bootstrapper.ts
+++ b/apps/desktop/src/main/bootstrapper.ts
@@ -5,6 +5,7 @@ import { socketService } from '@/main/services/socket-service';
import { serverService } from '@/main/services/server-service';
import { accountService } from '@/main/services/account-service';
import { syncService } from '@/main/services/sync-service';
+import { notificationService } from '@/main/services/notification-service';
const EVENT_LOOP_INTERVAL = 1000 * 60;
@@ -30,6 +31,7 @@ class Bootstrapper {
await serverService.syncServers();
await radarService.init();
await socketService.checkConnections();
+ notificationService.init();
if (!this.eventLoop) {
this.eventLoop = setTimeout(this.executeEventLoop, EVENT_LOOP_INTERVAL);
@@ -43,6 +45,7 @@ class Bootstrapper {
await socketService.checkConnections();
await accountService.syncDeletedTokens();
await syncService.syncAllWorkspaces();
+ notificationService.checkBadge();
} catch (error) {
console.log('error', error);
}
diff --git a/apps/desktop/src/main/queries/radar-data-get.ts b/apps/desktop/src/main/queries/radar-data-get.ts
index af7c9cc7..3912a361 100644
--- a/apps/desktop/src/main/queries/radar-data-get.ts
+++ b/apps/desktop/src/main/queries/radar-data-get.ts
@@ -10,7 +10,7 @@ export class RadarDataGetQueryHandler
public async handleQuery(
_: RadarDataGetQueryInput
): Promise<Record<string, WorkspaceRadarData>> {
- const data = radarService.getWorkspaceStates();
+ const data = radarService.getData();
return data;
}
@@ -20,7 +20,7 @@ export class RadarDataGetQueryHandler
___: Record<string, WorkspaceRadarData>
): Promise<ChangeCheckResult<RadarDataGetQueryInput>> {
if (event.type === 'radar_data_updated') {
- const data = radarService.getWorkspaceStates();
+ const data = radarService.getData();
return {
hasChanges: true,
result: data,
diff --git a/apps/desktop/src/main/services/notification-service.ts b/apps/desktop/src/main/services/notification-service.ts
new file mode 100644
index 00000000..f3ae2206
--- /dev/null
+++ b/apps/desktop/src/main/services/notification-service.ts
@@ -0,0 +1,46 @@
+import { app } from 'electron';
+import { eventBus } from '@/shared/lib/event-bus';
+import { radarService } from '@/main/services/radar-service';
+
+class NotificationService {
+ constructor() {
+ if (process.platform !== 'darwin') {
+ return;
+ }
+
+ eventBus.subscribe((event) => {
+ if (event.type === 'radar_data_updated') {
+ this.checkBadge();
+ }
+ });
+ }
+
+ public init() {
+ this.checkBadge();
+ }
+
+ public checkBadge() {
+ if (process.platform !== 'darwin') {
+ return;
+ }
+
+ const radarData = radarService.getData();
+ const importantCount = Object.values(radarData).reduce(
+ (acc, curr) => acc + curr.importantCount,
+ 0
+ );
+ const hasUnseenChanges = Object.values(radarData).some(
+ (data) => data.hasUnseenChanges
+ );
+
+ if (importantCount > 0) {
+ app.dock.setBadge(importantCount.toString());
+ } else if (hasUnseenChanges) {
+ app.dock.setBadge('·');
+ } else {
+ app.dock.setBadge('');
+ }
+ }
+}
+
+export const notificationService = new NotificationService();
diff --git a/apps/desktop/src/main/services/radar-service.ts b/apps/desktop/src/main/services/radar-service.ts
index e0af1970..e0b83693 100644
--- a/apps/desktop/src/main/services/radar-service.ts
+++ b/apps/desktop/src/main/services/radar-service.ts
@@ -5,7 +5,7 @@ import { eventBus } from '@/shared/lib/event-bus';
import { Event } from '@/shared/types/events';
class RadarService {
- private readonly workspaceStates: Record<string, WorkspaceRadarData> = {};
+ private readonly workspaceStates: Map<string, WorkspaceRadarData> = new Map();
constructor() {
eventBus.subscribe(this.handleEvent.bind(this));
@@ -18,25 +18,25 @@ class RadarService {
.execute();
for (const workspace of workspaces) {
- this.workspaceStates[workspace.user_id] = {
+ this.workspaceStates.set(workspace.user_id, {
userId: workspace.user_id,
workspaceId: workspace.workspace_id,
accountId: workspace.account_id,
nodeStates: {},
importantCount: 0,
hasUnseenChanges: false,
- };
+ });
await this.initWorkspace(workspace.user_id);
}
}
- public getWorkspaceStates(): Record<string, WorkspaceRadarData> {
- return this.workspaceStates;
+ public getData(): Record<string, WorkspaceRadarData> {
+ return Object.fromEntries(this.workspaceStates);
}
private async initWorkspace(userId: string): Promise<void> {
- let existingData = this.workspaceStates[userId];
+ let existingData = this.workspaceStates.get(userId);
if (!existingData) {
const workspace = await databaseService.appDatabase
.selectFrom('workspaces')
@@ -121,12 +121,12 @@ class RadarService {
}
}
- this.workspaceStates[userId] = data;
+ this.workspaceStates.set(userId, data);
}
private async handleEvent(event: Event) {
if (event.type === 'workspace_deleted') {
- delete this.workspaceStates[event.workspace.userId];
+ this.workspaceStates.delete(event.workspace.userId);
eventBus.publish({
type: 'radar_data_updated',
});