stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
81 lines • 2.72 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useIsSelectionEmpty = exports.useIsSelected = exports.SelectionStore = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _react = require("react");
var _streamChat = require("stream-chat");
var _useStateStore = require("../hooks/useStateStore");
var createInitialState = () => ({
selectedIds: new Set()
});
var SelectionStore = exports.SelectionStore = function () {
function SelectionStore() {
(0, _classCallCheck2.default)(this, SelectionStore);
this.state = new _streamChat.StateStore(createInitialState());
}
return (0, _createClass2.default)(SelectionStore, [{
key: "select",
value: function 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
});
}
}, {
key: "deselect",
value: function 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
});
}
}, {
key: "toggle",
value: function 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);
}
}
}]);
}();
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