aboutsummaryrefslogtreecommitdiffstats
path: root/apps/mobile/lib/utils.ts
blob: ce7298261962209835968ec138862583a0b9e52b (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { ClassValue } from "clsx";
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

/**
 * Merge props conditionally.
 *
 * @example
 * ```
 * <View {...condProps(
 *     { condition: true, props: { className: "foo" } },
 *     { condition: true, props: { style: { margin: "10px" } } },
 * )} />
 * ```
 * results in:
 * ```
 * <View className="foo" style={ margin: "10px" } />
 * ```
 * @example
 * ```
 * <View style={condProps(
 *     { condition: true, color: "red" },
 *     { condition: true, fontWeight: "bold" }
 * )} />
 * ```
 * results in:
 * ```
 * <View style={ color: "red", fontWeight: "bold" } />
 * ```
 */
export function condProps(
  ...condProps: {
    condition: boolean;
    props: Record<string, unknown>;
  }[]
): Record<string, unknown> {
  return condProps.reduce((acc, { condition, props }) => {
    return condition ? { ...acc, ...props } : acc;
  }, {});
}

/**
 * Build HTTP headers for API requests, merging Authorization and custom headers.
 * This ensures all direct HTTP calls (uploads, downloads, health checks) respect
 * the user's custom header configuration.
 */
export function buildApiHeaders(
  apiKey: string | undefined,
  customHeaders: Record<string, string> = {},
): Record<string, string> {
  return {
    ...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
    ...customHeaders,
  };
}