Skip to content

Commit d2e1fac

Browse files
committed
Handle team delete event from web view
1 parent 533a864 commit d2e1fac

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

src/components/App.tsx

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ import {
4444
BoostHubTeamUpdateEvent,
4545
listenBoostHubTeamUpdateEvent,
4646
unlistenBoostHubTeamUpdateEvent,
47+
BoostHubTeamDeleteEvent,
48+
listenBoostHubTeamDeleteEvent,
49+
unlistenBoostHubTeamDeleteEvent,
4750
} from '../lib/events'
4851
import {
4952
useCheckedFeatures,
@@ -219,24 +222,27 @@ const App = () => {
219222

220223
useEffect(() => {
221224
const boostHubTeamCreateEventHandler = (event: BoostHubTeamCreateEvent) => {
222-
const team = event.detail.team
225+
const createdTeam = event.detail.team
223226
setGeneralStatus((previousGeneralStatus) => {
227+
const teamMap =
228+
previousGeneralStatus.boostHubTeams!.reduce((map, team) => {
229+
map.set(team.id, team)
230+
return map
231+
}, new Map()) || new Map()
232+
teamMap.set(createdTeam.id, {
233+
id: createdTeam.id,
234+
name: createdTeam.name,
235+
domain: createdTeam.domain,
236+
iconUrl:
237+
createdTeam.icon != null
238+
? getBoostHubTeamIconUrl(createdTeam.icon.location)
239+
: undefined,
240+
})
224241
return {
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-
],
242+
boostHubTeams: [...teamMap.values()],
237243
}
238244
})
239-
push(`/app/boosthub/teams/${team.domain}`)
245+
push(`/app/boosthub/teams/${createdTeam.domain}`)
240246
}
241247

242248
const boostHubTeamUpdateEventHandler = (event: BoostHubTeamUpdateEvent) => {
@@ -262,11 +268,29 @@ const App = () => {
262268
})
263269
}
264270

271+
const boostHubTeamDeleteEventHandler = (event: BoostHubTeamDeleteEvent) => {
272+
const deletedTeam = event.detail.team
273+
setGeneralStatus((previousGeneralStatus) => {
274+
const teamMap =
275+
previousGeneralStatus.boostHubTeams!.reduce((map, team) => {
276+
map.set(team.id, team)
277+
return map
278+
}, new Map()) || new Map()
279+
teamMap.delete(deletedTeam.id)
280+
return {
281+
boostHubTeams: [...teamMap.values()],
282+
}
283+
})
284+
push(`/app`)
285+
}
286+
265287
listenBoostHubTeamCreateEvent(boostHubTeamCreateEventHandler)
266288
listenBoostHubTeamUpdateEvent(boostHubTeamUpdateEventHandler)
289+
listenBoostHubTeamDeleteEvent(boostHubTeamDeleteEventHandler)
267290
return () => {
268291
unlistenBoostHubTeamCreateEvent(boostHubTeamCreateEventHandler)
269292
unlistenBoostHubTeamUpdateEvent(boostHubTeamUpdateEventHandler)
293+
unlistenBoostHubTeamDeleteEvent(boostHubTeamDeleteEventHandler)
270294
}
271295
}, [push, setGeneralStatus])
272296

src/components/atoms/BoostHubWebview.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
dispatchBoostHubNavigateRequestEvent,
2929
dispatchBoostHubTeamCreateEvent,
3030
dispatchBoostHubTeamUpdateEvent,
31+
dispatchBoostHubTeamDeleteEvent,
3132
} from '../../lib/events'
3233

3334
export interface WebviewControl {
@@ -136,6 +137,8 @@ const BoostHubWebview = ({
136137
case 'team-update':
137138
dispatchBoostHubTeamUpdateEvent({ team: event.args[0] })
138139
break
140+
case 'team-delete':
141+
dispatchBoostHubTeamDeleteEvent({ team: event.args[0] })
139142
default:
140143
console.log('Unhandled ipc message event', event.channel, event.args)
141144
break

src/lib/events.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const {
8989
BoostHubTeamCreateEventName
9090
)
9191

92-
const BoostHubTeamUpdateEventName = 'BoostHub:teamCreate'
92+
const BoostHubTeamUpdateEventName = 'BoostHub:teamUpdate'
9393
interface BoostHubTeamUpdateEventDetail {
9494
team: {
9595
id: string
@@ -100,11 +100,31 @@ interface BoostHubTeamUpdateEventDetail {
100100
}
101101
}
102102
}
103-
export type BoostHubTeamUpdateEvent = CustomEvent<BoostHubTeamCreateEventDetail>
103+
export type BoostHubTeamUpdateEvent = CustomEvent<BoostHubTeamUpdateEventDetail>
104104
export const {
105105
dispatch: dispatchBoostHubTeamUpdateEvent,
106106
listen: listenBoostHubTeamUpdateEvent,
107107
unlisten: unlistenBoostHubTeamUpdateEvent,
108108
} = createCustomEventHelper<BoostHubTeamUpdateEventDetail>(
109109
BoostHubTeamUpdateEventName
110110
)
111+
112+
const BoostHubTeamDeleteEventName = 'BoostHub:teamDelete'
113+
interface BoostHubTeamDeleteEventDetail {
114+
team: {
115+
id: string
116+
name: string
117+
domain: string
118+
icon?: {
119+
location: string
120+
}
121+
}
122+
}
123+
export type BoostHubTeamDeleteEvent = CustomEvent<BoostHubTeamDeleteEventDetail>
124+
export const {
125+
dispatch: dispatchBoostHubTeamDeleteEvent,
126+
listen: listenBoostHubTeamDeleteEvent,
127+
unlisten: unlistenBoostHubTeamDeleteEvent,
128+
} = createCustomEventHelper<BoostHubTeamDeleteEventDetail>(
129+
BoostHubTeamDeleteEventName
130+
)

0 commit comments

Comments
 (0)