|
1 | 1 | import * as React from 'react' |
2 | 2 | import * as Kb from '@/common-adapters' |
3 | 3 | import * as Styles from '@/styles' |
| 4 | +import * as C from '@/constants' |
| 5 | +import * as Constants from '@/constants/users' |
4 | 6 |
|
5 | 7 | // Type for extra RouteProp passed to block modal sometimes when launching the |
6 | 8 | // modal from specific places from the app. |
@@ -35,6 +37,18 @@ export type Props = { |
35 | 37 | teamname?: string |
36 | 38 | } |
37 | 39 |
|
| 40 | +type OwnProps = { |
| 41 | + blockUserByDefault?: boolean |
| 42 | + filterUserByDefault?: boolean |
| 43 | + flagUserByDefault?: boolean |
| 44 | + reportsUserByDefault?: boolean |
| 45 | + context?: BlockModalContext |
| 46 | + conversationIDKey?: string |
| 47 | + others?: Array<string> |
| 48 | + team?: string |
| 49 | + username?: string |
| 50 | +} |
| 51 | + |
38 | 52 | type CheckboxRowProps = { |
39 | 53 | checked: boolean |
40 | 54 | disabled?: boolean |
@@ -407,7 +421,118 @@ const BlockModal = React.memo(function BlockModal(p: Props) { |
407 | 421 | ) |
408 | 422 | }) |
409 | 423 |
|
410 | | -export default BlockModal |
| 424 | +const Container = (ownProps: OwnProps) => { |
| 425 | + const {context, conversationIDKey, blockUserByDefault = false, filterUserByDefault = false} = ownProps |
| 426 | + const {flagUserByDefault = false, reportsUserByDefault = false, team: teamname} = ownProps |
| 427 | + let {username: adderUsername, others} = ownProps |
| 428 | + const waitingForLeave = C.Waiting.useAnyWaiting( |
| 429 | + teamname ? C.Teams.leaveTeamWaitingKey(teamname) : undefined |
| 430 | + ) |
| 431 | + const waitingForBlocking = C.Waiting.useAnyWaiting(Constants.setUserBlocksWaitingKey) |
| 432 | + const waitingForReport = C.Waiting.useAnyWaiting(Constants.reportUserWaitingKey) |
| 433 | + if (others?.length === 1 && !adderUsername) { |
| 434 | + adderUsername = others[0] |
| 435 | + others = undefined |
| 436 | + } |
| 437 | + |
| 438 | + const _allKnownBlocks = C.useUsersState(s => s.blockMap) |
| 439 | + const loadingWaiting = C.Waiting.useAnyWaiting(Constants.getUserBlocksWaitingKey) |
| 440 | + |
| 441 | + const onClose = C.useRouterState(s => s.dispatch.navigateUp) |
| 442 | + const leaveTeam = C.useTeamsState(s => s.dispatch.leaveTeam) |
| 443 | + const leaveTeamAndBlock = React.useCallback( |
| 444 | + (teamname: string) => { |
| 445 | + leaveTeam(teamname, true, 'chat') |
| 446 | + }, |
| 447 | + [leaveTeam] |
| 448 | + ) |
| 449 | + const getBlockState = C.useUsersState(s => s.dispatch.getBlockState) |
| 450 | + const _reportUser = C.useUsersState(s => s.dispatch.reportUser) |
| 451 | + const refreshBlocksFor = getBlockState |
| 452 | + const reportUser = React.useCallback( |
| 453 | + (username: string, conversationIDKey: string | undefined, report: ReportSettings) => { |
| 454 | + _reportUser({ |
| 455 | + comment: report.extraNotes, |
| 456 | + conversationIDKey, |
| 457 | + includeTranscript: report.includeTranscript && !!conversationIDKey, |
| 458 | + reason: report.reason, |
| 459 | + username, |
| 460 | + }) |
| 461 | + }, |
| 462 | + [_reportUser] |
| 463 | + ) |
| 464 | + const setConversationStatus = C.useChatContext(s => s.dispatch.blockConversation) |
| 465 | + const _setUserBlocks = C.useUsersState(s => s.dispatch.setUserBlocks) |
| 466 | + const setUserBlocks = React.useCallback( |
| 467 | + (newBlocks: NewBlocksMap) => { |
| 468 | + // Convert our state block array to action payload. |
| 469 | + const blocks = [...newBlocks.entries()] |
| 470 | + .filter( |
| 471 | + ([_, userBlocks]) => userBlocks.chatBlocked !== undefined || userBlocks.followBlocked !== undefined |
| 472 | + ) |
| 473 | + .map(([username, userBlocks]) => ({ |
| 474 | + setChatBlock: userBlocks.chatBlocked, |
| 475 | + setFollowBlock: userBlocks.followBlocked, |
| 476 | + username, |
| 477 | + })) |
| 478 | + if (blocks.length) { |
| 479 | + _setUserBlocks(blocks) |
| 480 | + } |
| 481 | + }, |
| 482 | + [_setUserBlocks] |
| 483 | + ) |
| 484 | + |
| 485 | + const otherUsernames = others && others.length > 0 ? others : undefined |
| 486 | + const props = { |
| 487 | + adderUsername, |
| 488 | + blockUserByDefault, |
| 489 | + context, |
| 490 | + conversationIDKey, |
| 491 | + filterUserByDefault, |
| 492 | + finishWaiting: waitingForLeave || waitingForBlocking || waitingForReport, |
| 493 | + flagUserByDefault, |
| 494 | + isBlocked: (username: string, which: BlockType) => { |
| 495 | + const blockObj = _allKnownBlocks.get(username) |
| 496 | + return blockObj ? blockObj[which] : false |
| 497 | + }, |
| 498 | + loadingWaiting, |
| 499 | + onClose, |
| 500 | + onFinish: (newBlocks: NewBlocksMap, blockTeam: boolean) => { |
| 501 | + let takingAction = false |
| 502 | + if (blockTeam) { |
| 503 | + if (teamname) { |
| 504 | + takingAction = true |
| 505 | + leaveTeamAndBlock(teamname) |
| 506 | + } else if (conversationIDKey) { |
| 507 | + takingAction = true |
| 508 | + const anyReported = [...newBlocks.values()].some(v => v.report !== undefined) |
| 509 | + setConversationStatus(anyReported) |
| 510 | + } |
| 511 | + } |
| 512 | + if (newBlocks.size) { |
| 513 | + takingAction = true |
| 514 | + setUserBlocks(newBlocks) |
| 515 | + } |
| 516 | + newBlocks.forEach(({report}, username) => report && reportUser(username, conversationIDKey, report)) |
| 517 | + if (!takingAction) { |
| 518 | + onClose() |
| 519 | + } |
| 520 | + }, |
| 521 | + otherUsernames, |
| 522 | + refreshBlocks: () => { |
| 523 | + const usernames = [...(adderUsername ? [adderUsername] : []), ...(otherUsernames || [])] |
| 524 | + if (usernames.length) { |
| 525 | + refreshBlocksFor(usernames) |
| 526 | + } |
| 527 | + }, |
| 528 | + reportsUserByDefault, |
| 529 | + teamname, |
| 530 | + } |
| 531 | + |
| 532 | + return <BlockModal {...props} /> |
| 533 | +} |
| 534 | + |
| 535 | +export default Container |
411 | 536 |
|
412 | 537 | const getListHeightStyle = (numOthers: number, expanded: boolean) => ({ |
413 | 538 | height: |
|
0 commit comments