stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
69 lines • 2.08 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useIsSelectionEmpty = exports.useIsSelected = exports.SelectionStore = void 0;
var _react = require("react");
var _streamChat = require("stream-chat");
var _useStateStore = require("../hooks/useStateStore");
var createInitialState = () => ({
selectedIds: new Set()
});
class SelectionStore {
state = new _streamChat.StateStore(createInitialState());
select(id) {
if (id === undefined || id === null) {
return;
}
var _this$state$getLatest = this.state.getLatestValue(),
selectedIds = _this$state$getLatest.selectedIds;
if (selectedIds.has(id)) {
return;
}
var next = new Set(selectedIds);
next.add(id);
this.state.partialNext({
selectedIds: next
});
}
deselect(id) {
if (id === undefined || id === null) {
return;
}
var _this$state$getLatest2 = this.state.getLatestValue(),
selectedIds = _this$state$getLatest2.selectedIds;
if (!selectedIds.has(id)) {
return;
}
var next = new Set(selectedIds);
next.delete(id);
this.state.partialNext({
selectedIds: next
});
}
toggle(id) {
if (id === undefined || id === null) {
return;
}
var _this$state$getLatest3 = this.state.getLatestValue(),
selectedIds = _this$state$getLatest3.selectedIds;
if (selectedIds.has(id)) {
this.deselect(id);
} else {
this.select(id);
}
}
}
exports.SelectionStore = SelectionStore;
var selectIsSelectionEmpty = state => ({
isSelectionEmpty: state.selectedIds.size === 0
});
var useIsSelectionEmpty = store => (0, _useStateStore.useStateStore)(store.state, selectIsSelectionEmpty).isSelectionEmpty;
exports.useIsSelectionEmpty = useIsSelectionEmpty;
var useIsSelected = (store, id) => {
var selector = (0, _react.useCallback)(state => ({
isSelected: state.selectedIds.has(id)
}), [id]);
return (0, _useStateStore.useStateStore)(store.state, selector).isSelected;
};
exports.useIsSelected = useIsSelected;
//# sourceMappingURL=selection-store.js.map