-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
41 lines (37 loc) · 1.31 KB
/
utils.ts
File metadata and controls
41 lines (37 loc) · 1.31 KB
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
import { Task, TaskReward } from "make-traffic-integration-core";
/**
* Returns the reward value for the given type, or 0 if not found.
*
* @example
* ```ts
* const points = getRewardValue(task.rewards, 'points');
* ```
*/
export const getRewardValue = (
rewards: TaskReward[],
type: TaskReward["type"]
): number => rewards.find((r) => r.type === type)?.value ?? 0;
/**
* Builds the icon URL for a task using `customMetadata.icon`.
* - If `icon` is an absolute URL it is returned as-is.
* - Otherwise `assetsPath` is prepended: `"<assetsPath>/<icon>.svg"`.
* - Falls back to `"<assetsPath>/www.svg"` when no icon is set.
*
* @param task The task object.
* @param assetsPath Base URL/path for assets (e.g. `"https://cdn.example.com/assets"`).
*
* @example
* ```ts
* const iconUrl = getTaskIconUrl(task, window.config.assets_path);
* ```
*/
export const getTaskIconUrl = (task: Task, assetsPath: string = ""): string => {
const meta = task.customMetadata as { icon?: string } | null;
const icon = meta?.icon;
if (icon) {
// Already a full URL — use directly
if (icon.startsWith("http://") || icon.startsWith("https://")) return icon;
return assetsPath ? `${assetsPath}/${icon}.svg` : `${icon}.svg`;
}
return assetsPath ? `${assetsPath}/www.svg` : "www.svg";
};