gitscope
← marzneshin / v0.6.4 / dashboard/src/modules/hosts/dialogs/mutation/profiles/use-profile-strategy.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/modules/hosts/dialogs/mutation/profiles/use-profile-strategy.ts · TypeScript
Blame Raw
 1import { ProtocolType } from "@marzneshin/modules/inbounds";
 2import { FC } from "react";
 3import { GeneralSchema, GeneralProfileFields, generalProfileDefaultValue } from "./general";
 4import { WireguardSchema, WireguardProfileFields, wireguardProfileDefaultValue } from "./wireguard";
 5import { Hysteria2Schema, Hysteria2ProfileFields, hysteria2ProfileDefaultValue } from "./hysteria2";
 6import { ShadowTlsSchema, ShadowTlsProfileFields, shadowTlsProfileDefaultValue } from "./shadowtls";
 7import { TuicSchema, TuicProfileFields, tuicProfileDefaultValue } from "./tuic";
 8
 9type ProfileSchema = typeof GeneralSchema | typeof WireguardSchema | typeof Hysteria2Schema | typeof TuicSchema;
10type ProfileFields = FC;
11
12/**
13 * @param protocol - Inbound protocol of host
14 * @returns [schema, fields, default values] - An array of schema, profile fields, and their default
15 */
16export const useProfileStrategy = (
17    protocol?: ProtocolType
18): [ProfileSchema, ProfileFields, object] => {
19    if (!protocol)
20        return [GeneralSchema, GeneralProfileFields, generalProfileDefaultValue]
21
22    const profileMap: { [key in ProtocolType]: [ProfileSchema, ProfileFields, object] } = {
23        vless: [GeneralSchema, GeneralProfileFields, generalProfileDefaultValue],
24        vmess: [GeneralSchema, GeneralProfileFields, generalProfileDefaultValue],
25        trojan: [GeneralSchema, GeneralProfileFields, generalProfileDefaultValue],
26        shadowsocks: [GeneralSchema, GeneralProfileFields, generalProfileDefaultValue],
27        shadowsocks2022: [GeneralSchema, GeneralProfileFields, generalProfileDefaultValue],
28        wireguard: [WireguardSchema, WireguardProfileFields, wireguardProfileDefaultValue],
29        hysteria2: [Hysteria2Schema, Hysteria2ProfileFields, hysteria2ProfileDefaultValue],
30        shadowtls: [ShadowTlsSchema, ShadowTlsProfileFields, shadowTlsProfileDefaultValue],
31        tuic: [TuicSchema, TuicProfileFields, tuicProfileDefaultValue],
32    };
33
34    const [Schema, Fields, defaultValues] = profileMap[protocol] || [GeneralSchema, GeneralProfileFields, generalProfileDefaultValue];
35    return [Schema, Fields, defaultValues];
36};