mattermost-mobile/app/screens/channel/header/index.ts
Daniel Espino García bb7ff622af
Add Playbooks read-only support for mobile devices (#8978)
* Add the channel options to get into playbooks (#8750)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Address design issues

* Add requested comment

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Playbooks database (#8802)

* Add lastPlaybookFetchAt to channel table (#8916)

* Add lastPlaybookFetchAt to channel table

* Add missing commit

* Use my_channel table instead

* Fix test

* Address feedback

* First implementation of playbooks API (#8897)

* First implementation of playbooks API

* Add version check

* Address feedback

* Fix test

* Add last fetch at usage and other improvements

* Simplify test

* Add sort_order, update_at and previousReminder columns (#8927)

* Add sort_order, update_at and previousReminder columns

* Remove order from the schema

* Fix tests

* Add tests

* Add websockets for playbooks (#8947)

* Add websocket events for playbooks

* Fix typo

* Add playbook run list (#8761)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Fix merge

* Add API related functionality

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Add playbooks run details (#8872)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Add playbooks run details

* Fix merge

* Add API related functionality

* Add API related changes

* Order fixes

* Several fixes

* Add error state on playbook run

* i18n-extract

* Fix tests

* Fix test

* Several fixes

* Fixes and add missing UI elements

* i18n-extract

* Fix tests

* Remove files from bad merge

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Add missing tests for playbooks (#8976)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Add playbooks run details

* Fix merge

* Add API related functionality

* Add API related changes

* Order fixes

* Several fixes

* Add error state on playbook run

* i18n-extract

* Fix tests

* Fix test

* Several fixes

* Fixes and add missing UI elements

* i18n-extract

* Fix tests

* Remove files from bad merge

* Add tests

* Fix typo

* Add missing strings

* Fix tests and skip some

* Fix test

* Fix typo

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Address feedback

* Address feedback and fix tests

* Address comments and fix tests

* Address feedback

* Address plugin changes and fix bugs

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2025-07-14 09:21:37 +02:00

143 lines
5 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {withDatabase, withObservables} from '@nozbe/watermelondb/react';
import React from 'react';
import {of as of$} from 'rxjs';
import {combineLatestWith, distinctUntilChanged, switchMap} from 'rxjs/operators';
import {General} from '@constants';
import {queryPlaybookRunsPerChannel} from '@playbooks/database/queries/run';
import {observeIsPlaybooksEnabled} from '@playbooks/database/queries/version';
import {observeChannel, observeChannelInfo} from '@queries/servers/channel';
import {observeCanAddBookmarks, queryBookmarks} from '@queries/servers/channel_bookmark';
import {observeConfigBooleanValue, observeCurrentTeamId, observeCurrentUserId} from '@queries/servers/system';
import {observeUser} from '@queries/servers/user';
import {
getUserCustomStatus,
getUserIdFromChannelName,
isCustomStatusExpired as checkCustomStatusIsExpired,
} from '@utils/user';
import ChannelHeader from './header';
import type {WithDatabaseArgs} from '@typings/database/database';
type OwnProps = {
channelId: string;
};
const enhanced = withObservables(['channelId'], ({channelId, database}: OwnProps & WithDatabaseArgs) => {
const currentUserId = observeCurrentUserId(database);
const teamId = observeCurrentTeamId(database);
const channel = observeChannel(database, channelId);
const channelType = channel.pipe(switchMap((c) => of$(c?.type)));
const channelInfo = observeChannelInfo(database, channelId);
const dmUser = currentUserId.pipe(
combineLatestWith(channel),
switchMap(([userId, c]) => {
if (c?.type === General.DM_CHANNEL) {
const teammateId = getUserIdFromChannelName(userId, c.name);
return observeUser(database, teammateId);
}
return of$(undefined);
}),
);
const isOwnDirectMessage = currentUserId.pipe(
combineLatestWith(dmUser),
switchMap(([userId, dm]) => of$(userId === dm?.id)),
);
const customStatus = dmUser.pipe(
switchMap((dm) => of$(getUserCustomStatus(dm))),
);
const isCustomStatusExpired = dmUser.pipe(
switchMap((dm) => of$(checkCustomStatusIsExpired(dm))),
);
const isCustomStatusEnabled = observeConfigBooleanValue(database, 'EnableCustomUserStatuses');
const isPlaybooksEnabled = observeIsPlaybooksEnabled(database);
// const searchTerm = channel.pipe(
// combineLatestWith(dmUser),
// switchMap(([c, dm]) => {
// if (c?.type === General.DM_CHANNEL) {
// return of$(dm ? `@${dm.username}` : '');
// } else if (c?.type === General.GM_CHANNEL) {
// return of$(`@${c.name}`);
// }
// return of$(c?.name);
// }),
// );
const displayName = channel.pipe(switchMap((c) => of$(c?.displayName)));
const memberCount = channelInfo.pipe(
combineLatestWith(dmUser),
switchMap(([ci, dm]) => of$(dm ? undefined : ci?.memberCount)));
const hasBookmarks = queryBookmarks(database, channelId).observeCount(false).pipe(
switchMap((count) => of$(count > 0)),
distinctUntilChanged(),
);
const isBookmarksEnabled = observeConfigBooleanValue(database, 'FeatureFlagChannelBookmarks');
const canAddBookmarks = observeCanAddBookmarks(database, channelId);
const activeRuns = isPlaybooksEnabled.pipe(
switchMap((enabled) => {
if (!enabled) {
return of$([]);
}
return queryPlaybookRunsPerChannel(database, channelId, false).observe();
}),
);
const activeRunId = activeRuns.pipe(
switchMap((runs) => {
if (runs.length !== 1) {
// if there is more than one active run, we directly go to the playbook list
// so we don't need the id (since it is more than one)
return of$(undefined);
}
return of$(runs[0].id);
}),
);
return {
canAddBookmarks,
channelType,
customStatus,
displayName,
hasBookmarks,
isBookmarksEnabled,
isCustomStatusEnabled,
isCustomStatusExpired,
isOwnDirectMessage,
memberCount,
teamId,
playbooksActiveRuns: activeRuns.pipe(switchMap((r) => of$(r.length))),
hasPlaybookRuns: isPlaybooksEnabled.pipe(
switchMap((enabled) => {
if (!enabled) {
return of$(false);
}
return queryPlaybookRunsPerChannel(database, channelId).observeCount(false).pipe(
// eslint-disable-next-line max-nested-callbacks
switchMap((v) => of$(v > 0)),
distinctUntilChanged(),
);
}),
),
isPlaybooksEnabled,
activeRunId,
// searchTerm,
};
});
export default withDatabase(enhanced(React.memo(ChannelHeader)));