stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
46 lines • 2.29 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useChannelPreviewPollLabel = void 0;
var _react = require("react");
var _ChatContext = require("../../../contexts/chatContext/ChatContext");
var _TranslationContext = require("../../../contexts/translationContext/TranslationContext");
var _useStateStore2 = require("../../../hooks/useStateStore");
var selector = nextValue => ({
latest_votes_by_option: nextValue.latest_votes_by_option,
options: nextValue.options
});
var useChannelPreviewPollLabel = ({
pollId
}) => {
var _useStateStore, _client$user2;
var _useChatContext = (0, _ChatContext.useChatContext)(),
client = _useChatContext.client;
var _useTranslationContex = (0, _TranslationContext.useTranslationContext)(),
t = _useTranslationContex.t;
var poll = client.polls.fromState(pollId);
var pollState = (_useStateStore = (0, _useStateStore2.useStateStore)(poll == null ? void 0 : poll.state, selector)) != null ? _useStateStore : {};
var latestVotesByOption = pollState.latest_votes_by_option,
pollOptions = pollState.options;
var latestVotes = (0, _react.useMemo)(() => latestVotesByOption ? Object.values(latestVotesByOption).map(votes => votes == null ? void 0 : votes[0]).sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()) : [], [latestVotesByOption]);
return (0, _react.useMemo)(() => {
if (!(latestVotes && latestVotes.length && latestVotes[0].user)) return;
{
var _client$user;
var option = pollOptions == null ? void 0 : pollOptions.find(option => option.id === latestVotes[0].option_id);
var voteUser = latestVotes[0].user;
if (voteUser.id === ((_client$user = client.user) == null ? void 0 : _client$user.id)) {
return t('You voted: {{ option }}', {
option: option == null ? void 0 : option.text
});
} else {
return t('{{ user }} voted: {{ option }}', {
user: voteUser.name || voteUser.id,
option: option == null ? void 0 : option.text
});
}
}
}, [latestVotes, pollOptions, (_client$user2 = client.user) == null ? void 0 : _client$user2.id, t]);
};
exports.useChannelPreviewPollLabel = useChannelPreviewPollLabel;
//# sourceMappingURL=useChannelPreviewPollLabel.js.map