You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
5.7 KiB
TypeScript
186 lines
5.7 KiB
TypeScript
2 years ago
|
import { useSelector } from 'react-redux';
|
||
|
import { ConversationTypeEnum, isOpenOrClosedGroup } from '../../models/conversationAttributes';
|
||
|
import { ReduxConversationType } from '../ducks/conversations';
|
||
|
import { StateType } from '../reducer';
|
||
|
import { getCanWrite, getSubscriberCount } from './sogsRoomInfo';
|
||
|
|
||
|
/**
|
||
|
* Returns the formatted text for notification setting.
|
||
|
*/
|
||
|
const getCurrentNotificationSettingText = (state: StateType): string | undefined => {
|
||
|
if (!state) {
|
||
|
return undefined;
|
||
|
}
|
||
|
const currentNotificationSetting = getSelectedConversation(state)?.currentNotificationSetting;
|
||
|
switch (currentNotificationSetting) {
|
||
|
case 'all':
|
||
|
return window.i18n('notificationForConvo_all');
|
||
|
case 'mentions_only':
|
||
|
return window.i18n('notificationForConvo_mentions_only');
|
||
|
case 'disabled':
|
||
|
return window.i18n('notificationForConvo_disabled');
|
||
|
default:
|
||
|
return window.i18n('notificationForConvo_all');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const getIsSelectedPrivate = (state: StateType): boolean => {
|
||
|
return Boolean(getSelectedConversation(state)?.isPrivate) || false;
|
||
|
};
|
||
|
|
||
|
const getIsSelectedBlocked = (state: StateType): boolean => {
|
||
|
return Boolean(getSelectedConversation(state)?.isBlocked) || false;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Returns true if the currently selected conversation is active (has an active_at field > 0)
|
||
|
*/
|
||
|
const getIsSelectedActive = (state: StateType): boolean => {
|
||
|
return Boolean(getSelectedConversation(state)?.activeAt) || false;
|
||
|
};
|
||
|
|
||
|
const getIsSelectedNoteToSelf = (state: StateType): boolean => {
|
||
|
return getSelectedConversation(state)?.isMe || false;
|
||
|
};
|
||
|
|
||
|
export const getSelectedConversationKey = (state: StateType): string | undefined => {
|
||
|
return state.conversations.selectedConversation;
|
||
|
};
|
||
|
|
||
|
export const getSelectedConversation = (state: StateType): ReduxConversationType | undefined => {
|
||
|
const selected = getSelectedConversationKey(state);
|
||
|
return selected ? state.conversations.conversationLookup[selected] : undefined;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Returns true if the current conversation selected is a public group and false otherwise.
|
||
|
*/
|
||
|
export const getSelectedConversationIsPublic = (state: StateType): boolean => {
|
||
|
return Boolean(getSelectedConversation(state)?.isPublic) || false;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Returns true if the current conversation selected can be typed into
|
||
|
*/
|
||
|
export function getSelectedCanWrite(state: StateType) {
|
||
|
const selectedConvoPubkey = getSelectedConversationKey(state);
|
||
|
if (!selectedConvoPubkey) {
|
||
|
return false;
|
||
|
}
|
||
|
const selectedConvo = getSelectedConversation(state);
|
||
|
if (!selectedConvo) {
|
||
|
return false;
|
||
|
}
|
||
|
const canWrite = getCanWrite(state, selectedConvoPubkey);
|
||
|
const { isBlocked, isKickedFromGroup, left, isPublic } = selectedConvo;
|
||
|
|
||
|
return !(isBlocked || isKickedFromGroup || left || (isPublic && !canWrite));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns true if the current conversation selected is a group conversation.
|
||
|
* Returns false if the current conversation selected is not a group conversation, or none are selected
|
||
|
*/
|
||
|
const getSelectedConversationIsGroup = (state: StateType): boolean => {
|
||
|
const selected = getSelectedConversation(state);
|
||
|
if (!selected || !selected.type) {
|
||
|
return false;
|
||
|
}
|
||
|
return selected.type ? isOpenOrClosedGroup(selected.type) : false;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Returns true if the current conversation selected is a closed group and false otherwise.
|
||
|
*/
|
||
|
export const isClosedGroupConversation = (state: StateType): boolean => {
|
||
|
const selected = getSelectedConversation(state);
|
||
|
if (!selected) {
|
||
|
return false;
|
||
|
}
|
||
|
return (
|
||
|
(selected.type === ConversationTypeEnum.GROUP && !selected.isPublic) ||
|
||
|
selected.type === ConversationTypeEnum.GROUPV3 ||
|
||
|
false
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const getGroupMembers = (state: StateType): Array<string> => {
|
||
|
const selected = getSelectedConversation(state);
|
||
|
if (!selected) {
|
||
|
return [];
|
||
|
}
|
||
|
return selected.members || [];
|
||
|
};
|
||
|
|
||
|
const getSelectedSubscriberCount = (state: StateType): number | undefined => {
|
||
|
const convo = getSelectedConversation(state);
|
||
|
if (!convo) {
|
||
|
return undefined;
|
||
|
}
|
||
|
return getSubscriberCount(state, convo.id);
|
||
|
};
|
||
|
|
||
|
// ============== SELECTORS RELEVANT TO SELECTED/OPENED CONVERSATION ==============
|
||
|
|
||
|
export function useSelectedConversationKey() {
|
||
|
return useSelector(getSelectedConversationKey);
|
||
|
}
|
||
|
|
||
|
export function useSelectedIsGroup() {
|
||
|
return useSelector(getSelectedConversationIsGroup);
|
||
|
}
|
||
|
|
||
|
export function useSelectedIsPublic() {
|
||
|
return useSelector(getSelectedConversationIsPublic);
|
||
|
}
|
||
|
|
||
|
export function useSelectedIsPrivate() {
|
||
|
return useSelector(getIsSelectedPrivate);
|
||
|
}
|
||
|
|
||
|
export function useSelectedIsBlocked() {
|
||
|
return useSelector(getIsSelectedBlocked);
|
||
|
}
|
||
|
|
||
|
export function useSelectedIsActive() {
|
||
|
return useSelector(getIsSelectedActive);
|
||
|
}
|
||
|
|
||
|
export function useSelectedisNoteToSelf() {
|
||
|
return useSelector(getIsSelectedNoteToSelf);
|
||
|
}
|
||
|
|
||
|
export function useSelectedMembers() {
|
||
|
return useSelector(getGroupMembers);
|
||
|
}
|
||
|
|
||
|
export function useSelectedSubscriberCount() {
|
||
|
return useSelector(getSelectedSubscriberCount);
|
||
|
}
|
||
|
|
||
|
export function useSelectedNotificationSetting() {
|
||
|
return useSelector(getCurrentNotificationSettingText);
|
||
|
}
|
||
|
|
||
|
export function useSelectedIsKickedFromGroup() {
|
||
|
return useSelector(
|
||
|
(state: StateType) => Boolean(getSelectedConversation(state)?.isKickedFromGroup) || false
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function useSelectedIsLeft() {
|
||
|
return useSelector((state: StateType) => Boolean(getSelectedConversation(state)?.left) || false);
|
||
|
}
|
||
|
|
||
|
export function useSelectedNickname() {
|
||
|
return useSelector((state: StateType) => getSelectedConversation(state)?.nickname);
|
||
|
}
|
||
|
|
||
|
export function useSelectedDisplayNameInProfile() {
|
||
|
return useSelector((state: StateType) => getSelectedConversation(state)?.displayNameInProfile);
|
||
|
}
|
||
|
|
||
|
export function useSelectedWeAreAdmin() {
|
||
|
return useSelector((state: StateType) => getSelectedConversation(state)?.weAreAdmin || false);
|
||
|
}
|