import { QueryTopicForHolder } from '@/services/data/tuples/query-topic-types' ; import { Space } from '@/services/data/tuples/space-types' ; import { listTopicsForHolder } from '@/services/data/tuples/topic' ; import { TopicId } from '@/services/data/tuples/topic-types' ; import { TupleItemPicker } from '@/widgets/tuple-workbench/tuple-item-picker' ; import React from 'react' ; import { useSpaceEventBus } from './space-event-bus' ; import { SpaceEventTypes } from './space-event-bus-types' ; const hasTopic = ( space ) => ! ! space . topicIds && space . topicIds . length > 0 ; const getTopicIds = ( space ) : Array < TopicId > => space . topicIds ; const findNameFromTopics = ( topicId , topics ) : string => { return topics . find ( topic => topic . topicId == topicId ) ! . name ; } ; const getIdOfTopic = ( topic ) => topic . topicId ; const getNameOfTopic = ( topic ) => topic . name ; const isTopicPicked = ( space ) => ( topic ) => space . topicIds . some ( topicId => topicId == topic . topicId ) ; export const TopicPicker = ( props ) => { const { label , space , codes } = props ; const { fire } = useSpaceEventBus ( ) ; const addTopic = ( space ) => ( topic ) => { const { topicId } = topic ; const index = space . topicIds . findIndex ( id => id == topicId ) ; if ( index === - 1 ) { space . topicIds . push ( topicId ) ; fire ( SpaceEventTypes . TOPIC_ADDED , topicId ) ; } } ; const removeTopic = ( space ) => ( topicOrId ) => { let topicId ; if ( typeof topicOrId === 'string' ) { topicId = topicOrId ; } else { topicId = topicOrId . topicId ; } const index = space . topicIds . findIndex ( id => id == topicId ) ; if ( index !== - 1 ) { space . topicIds . splice ( index , 1 ) ; fire ( SpaceEventTypes . TOPIC_REMOVED , topicId ) ; } } ; return < TupleItemPicker actionLabel = { label } holder = { space } codes = { codes } isHolding = { hasTopic } getHoldIds = { getTopicIds } getNameOfHold = { findNameFromTopics } listCandidates = { listTopicsForHolder } getIdOfCandidate = { getIdOfTopic } getNameOfCandidate = { getNameOfTopic } isCandidateHold = { isTopicPicked ( space ) } removeHold = { removeTopic ( space ) } addHold = { addTopic ( space ) } / > ; } ;