File size: 845 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { useMemo } from 'react';

type Activity = {
	activityIsRewindable: boolean;
	rewindId?: string;
	streams?: Activity[];
};

type Hook = ( activity: Activity ) => string | null | undefined;

// NOTE: Use the hook version if at all possible! This method exists mainly to
//       allow for finding an actionable rewind ID in places that don't
//       support hooks; e.g., class components.
//
// If the activity isn't rewindable but has streams that are,
// use one of the streams' rewind ID as the download or restore point
export const getActionableRewindId: Hook = ( activity ) =>
	activity.activityIsRewindable
		? activity.rewindId
		: activity.streams?.find?.( ( a ) => a.activityIsRewindable )?.rewindId;

export const useActionableRewindId: Hook = ( activity ) =>
	useMemo( () => getActionableRewindId( activity ), [ activity ] );