session-desktop/ts/hooks/useParamSelector.ts

84 lines
2.6 KiB
TypeScript
Raw Normal View History

import { useSelector } from 'react-redux';
import { PubKey } from '../session/types';
import { UserUtils } from '../session/utils';
import { StateType } from '../state/reducer';
export function useAvatarPath(convoId: string | undefined) {
const convoProps = useConversationPropsById(convoId);
return convoProps?.avatarPath || null;
}
export function useOurAvatarPath() {
return useAvatarPath(UserUtils.getOurPubKeyStrFromCache());
}
/**
*
* @returns convo.profileName || convo.name || convo.id or undefined if the convo is not found
*/
export function useConversationUsername(convoId?: string) {
const convoProps = useConversationPropsById(convoId);
return convoProps?.profileName || convoProps?.name || convoId;
}
/**
* Returns either the nickname, profileName, or the shorten pubkey
*/
export function useConversationUsernameOrShorten(convoId?: string) {
const convoProps = useConversationPropsById(convoId);
return convoProps?.profileName || convoProps?.name || (convoId && PubKey.shorten(convoId));
}
/**
* Returns either the nickname, the profileName, in '"' or the full pubkeys given
*/
export function useConversationsUsernameWithQuoteOrFullPubkey(pubkeys: Array<string>) {
return useSelector((state: StateType) => {
return pubkeys.map(pubkey => {
if (pubkey === UserUtils.getOurPubKeyStrFromCache() || pubkey.toLowerCase() === 'you') {
return window.i18n('you');
}
const convo = state.conversations.conversationLookup[pubkey];
return `"${convo?.profileName}"` || `"${convo?.name}"` || pubkey;
});
});
}
export function useOurConversationUsername() {
return useConversationUsername(UserUtils.getOurPubKeyStrFromCache());
}
export function useIsMe(pubkey?: string) {
return pubkey && pubkey === UserUtils.getOurPubKeyStrFromCache();
}
export function useIsClosedGroup(convoId?: string) {
const convoProps = useConversationPropsById(convoId);
return (convoProps && convoProps.isGroup && !convoProps.isPublic) || false;
}
export function useIsPrivate(convoId?: string) {
const convoProps = useConversationPropsById(convoId);
return Boolean(convoProps && convoProps.isPrivate);
}
export function useIsPinned(convoId?: string) {
const convoProps = useConversationPropsById(convoId);
return Boolean(convoProps && convoProps.isPinned);
}
export function useConversationPropsById(convoId?: string) {
return useSelector((state: StateType) => {
if (!convoId) {
return null;
}
const convo = state.conversations.conversationLookup[convoId];
if (!convo) {
return null;
}
return convo;
});
}