Skip to content

Commit d414891

Browse files
committed
Implement team-update ipc message handler
1 parent 3650328 commit d414891

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

src/components/App.tsx

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ import { useGeneralStatus } from '../lib/generalStatus'
3535
import { getFolderItemId } from '../lib/nav'
3636
import AppModeModal from './organisms/AppModeModal'
3737
import { useBoostNoteProtocol } from '../lib/protocol'
38-
import { useBoostHub } from '../lib/boosthub'
38+
import { useBoostHub, getBoostHubTeamIconUrl } from '../lib/boosthub'
3939
import { useDialog, DialogIconTypes } from '../lib/dialog'
4040
import {
4141
listenBoostHubTeamCreateEvent,
4242
unlistenBoostHubTeamCreateEvent,
4343
BoostHubTeamCreateEvent,
44+
BoostHubTeamUpdateEvent,
45+
listenBoostHubTeamUpdateEvent,
46+
unlistenBoostHubTeamUpdateEvent,
4447
} from '../lib/events'
4548
import {
4649
useCheckedFeatures,
@@ -215,18 +218,55 @@ const App = () => {
215218
}, [togglePreferencesModal])
216219

217220
useEffect(() => {
218-
const boosthubTeamCreateEventHandler = (event: BoostHubTeamCreateEvent) => {
221+
const boostHubTeamCreateEventHandler = (event: BoostHubTeamCreateEvent) => {
219222
const team = event.detail.team
220223
setGeneralStatus((previousGeneralStatus) => {
221224
return {
222-
boostHubTeams: [...previousGeneralStatus.boostHubTeams!, team],
225+
boostHubTeams: [
226+
...previousGeneralStatus.boostHubTeams!,
227+
{
228+
id: team.id,
229+
name: team.name,
230+
domain: team.domain,
231+
iconUrl:
232+
team.icon != null
233+
? getBoostHubTeamIconUrl(team.icon.location)
234+
: undefined,
235+
},
236+
],
223237
}
224238
})
225239
push(`/app/boosthub/teams/${team.domain}`)
226240
}
227-
listenBoostHubTeamCreateEvent(boosthubTeamCreateEventHandler)
241+
242+
const boostHubTeamUpdateEventHandler = (event: BoostHubTeamUpdateEvent) => {
243+
const updatedTeam = event.detail.team
244+
setGeneralStatus((previousGeneralStatus) => {
245+
const teamMap =
246+
previousGeneralStatus.boostHubTeams!.reduce((map, team) => {
247+
map.set(team.id, team)
248+
return map
249+
}, new Map()) || new Map()
250+
teamMap.set(updatedTeam.id, {
251+
id: updatedTeam.id,
252+
name: updatedTeam.name,
253+
domain: updatedTeam.domain,
254+
iconUrl:
255+
updatedTeam.icon != null
256+
? getBoostHubTeamIconUrl(updatedTeam.icon.location)
257+
: undefined,
258+
})
259+
return {
260+
boostHubTeams: [...teamMap.values()],
261+
}
262+
})
263+
}
264+
265+
listenBoostHubTeamCreateEvent(boostHubTeamCreateEventHandler)
266+
listenBoostHubTeamUpdateEvent(boostHubTeamUpdateEventHandler)
228267
return () => {
229-
unlistenBoostHubTeamCreateEvent(boosthubTeamCreateEventHandler)
268+
unlistenBoostHubTeamCreateEvent(boostHubTeamCreateEventHandler)
269+
unlistenBoostHubTeamUpdateEvent(boostHubTeamUpdateEventHandler)
230270
}
231271
}, [push, setGeneralStatus])
232272

src/components/atoms/BoostHubWebview.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { FormSecondaryButton } from './form'
2727
import {
2828
dispatchBoostHubNavigateRequestEvent,
2929
dispatchBoostHubTeamCreateEvent,
30+
dispatchBoostHubTeamUpdateEvent,
3031
} from '../../lib/events'
3132

3233
export interface WebviewControl {
@@ -132,6 +133,9 @@ const BoostHubWebview = ({
132133
case 'team-create':
133134
dispatchBoostHubTeamCreateEvent({ team: event.args[0] })
134135
break
136+
case 'team-update':
137+
dispatchBoostHubTeamUpdateEvent({ team: event.args[0] })
138+
break
135139
default:
136140
console.log('Unhandled ipc message event', event.channel, event.args)
137141
break

src/lib/events.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ interface BoostHubTeamCreateEventDetail {
7575
id: string
7676
name: string
7777
domain: string
78+
icon?: {
79+
location: string
80+
}
7881
}
7982
}
8083
export type BoostHubTeamCreateEvent = CustomEvent<BoostHubTeamCreateEventDetail>
@@ -85,3 +88,23 @@ export const {
8588
} = createCustomEventHelper<BoostHubTeamCreateEventDetail>(
8689
BoostHubTeamCreateEventName
8790
)
91+
92+
const BoostHubTeamUpdateEventName = 'BoostHub:teamCreate'
93+
interface BoostHubTeamUpdateEventDetail {
94+
team: {
95+
id: string
96+
name: string
97+
domain: string
98+
icon?: {
99+
location: string
100+
}
101+
}
102+
}
103+
export type BoostHubTeamUpdateEvent = CustomEvent<BoostHubTeamCreateEventDetail>
104+
export const {
105+
dispatch: dispatchBoostHubTeamUpdateEvent,
106+
listen: listenBoostHubTeamUpdateEvent,
107+
unlisten: unlistenBoostHubTeamUpdateEvent,
108+
} = createCustomEventHelper<BoostHubTeamUpdateEventDetail>(
109+
BoostHubTeamUpdateEventName
110+
)

0 commit comments

Comments
 (0)