Skip to content

Commit

Permalink
Fix daily-co#7: Add idType param to useParticipantIds
Browse files Browse the repository at this point in the history
See daily-co#7 for discussion.
  • Loading branch information
rileyjshaw committed Jan 4, 2023
1 parent 010bbb9 commit 53aeb8e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/hooks/useParticipantIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type SortParticipants =
| 'user_id'
| 'user_name'
| SortParticipantsFunction;
type IdType = 'user' | 'session';

const defaultFilter: FilterParticipants = Boolean;
const defaultSort: SortParticipants = () => 0;
Expand All @@ -45,10 +46,11 @@ interface UseParticipantIdsArgs {
onParticipantLeft?(ev: DailyEventObjectParticipantLeft): void;
onParticipantUpdated?(ev: DailyEventObjectParticipant): void;
sort?: SortParticipants;
idType?: IdType;
}

/**
* Returns a list of participant ids (= session_id).
* Returns a list of participant ids (default = session_id).
* The list can optionally be filtered and sorted, using the filter and sort options.
*/
export const useParticipantIds = (
Expand All @@ -59,6 +61,7 @@ export const useParticipantIds = (
onParticipantLeft,
onParticipantUpdated,
sort = defaultSort,
idType = 'session',
}: UseParticipantIdsArgs = {
filter: defaultFilter,
sort: defaultSort,
Expand Down Expand Up @@ -121,13 +124,14 @@ export const useParticipantIds = (
return sortFn;
}, [sort]);

const idKey = idType === 'user' ? 'user_id' : 'session_id';
const sortedIds = useMemo(() => {
return allParticipants
.filter(filterFn)
.sort(sortFn)
.map((p) => p.session_id)
.map((p) => p[idKey])
.filter(Boolean);
}, [allParticipants, filterFn, sortFn]);
}, [allParticipants, filterFn, sortFn, idKey]);

useThrottledDailyEvent(
[
Expand Down

0 comments on commit 53aeb8e

Please sign in to comment.