File size: 995 Bytes
5cb63c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * Shared channel action helpers.
 *
 * Filters token-backed accounts and composes account-level action gates.
 */
type OptionalDefaultGate<TKey extends string> = (key: TKey, defaultValue?: boolean) => boolean;

type TokenSourcedAccount = {
  tokenSource?: string | null;
};

/**
 * Filters out accounts explicitly marked as tokenless.
 */
export function listTokenSourcedAccounts<TAccount extends TokenSourcedAccount>(
  accounts: readonly TAccount[],
): TAccount[] {
  return accounts.filter((account) => account.tokenSource !== "none");
}

/**
 * Creates an action gate that is enabled when any account-level gate enables the action.
 */
export function createUnionActionGate<TAccount, TKey extends string>(
  accounts: readonly TAccount[],
  createGate: (account: TAccount) => OptionalDefaultGate<TKey>,
): OptionalDefaultGate<TKey> {
  const gates = accounts.map((account) => createGate(account));
  return (key, defaultValue = true) => gates.some((gate) => gate(key, defaultValue));
}