File size: 2,035 Bytes
fc69895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { env as publicEnv } from "$env/dynamic/public";
import { page } from "$app/state";
import { base } from "$app/paths";

import type { Transporter } from "@sveltejs/kit";
import { getContext } from "svelte";

type PublicConfigKey = keyof typeof publicEnv;

class PublicConfigManager {
	#configStore = $state<Record<PublicConfigKey, string>>({});

	constructor(initialConfig?: Record<PublicConfigKey, string>) {
		this.init = this.init.bind(this);
		this.getPublicConfig = this.getPublicConfig.bind(this);
		if (initialConfig) {
			this.init(initialConfig);
		}
	}

	init(publicConfig: Record<PublicConfigKey, string>) {
		this.#configStore = publicConfig;
	}

	get(key: PublicConfigKey) {
		return this.#configStore[key];
	}

	getPublicConfig() {
		return this.#configStore;
	}

	get isHuggingChat() {
		return this.#configStore.PUBLIC_APP_ASSETS === "huggingchat";
	}

	get assetPath() {
		return (
			(this.#configStore.PUBLIC_ORIGIN || page.url.origin) +
			base +
			"/" +
			this.#configStore.PUBLIC_APP_ASSETS
		);
	}
}
type ConfigProxy = PublicConfigManager & { [K in PublicConfigKey]: string };

export function getConfigManager(initialConfig?: Record<PublicConfigKey, string>) {
	const publicConfigManager = new PublicConfigManager(initialConfig);

	const publicConfig: ConfigProxy = new Proxy(publicConfigManager, {
		get(target, prop) {
			if (prop in target) {
				return Reflect.get(target, prop);
			}
			if (typeof prop === "string") {
				return target.get(prop as PublicConfigKey);
			}
			return undefined;
		},
		set(target, prop, value, receiver) {
			if (prop in target) {
				return Reflect.set(target, prop, value, receiver);
			}
			return false;
		},
	}) as ConfigProxy;
	return publicConfig;
}

export const publicConfigTransporter: Transporter = {
	encode: (value) =>
		value instanceof PublicConfigManager ? JSON.stringify(value.getPublicConfig()) : false,
	decode: (value) => getConfigManager(JSON.parse(value)),
};

export const usePublicConfig = () => getContext<ConfigProxy>("publicConfig");