Skip to content

Commit 29090b8

Browse files
committed
[RealtimeKit] Allow disabling sub-platforms
1 parent 5ced6cd commit 29090b8

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

src/components/realtimekit/RTKSDKSelector/RTKSDKSelector.astro

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
import { z } from "astro:schema";
33
import SDKSelector from "./RTKSDKSelector";
44
5-
const platform = z.enum(["web", "mobile"]);
5+
const platform = z.enum([
6+
"web",
7+
"mobile",
8+
"web-react",
9+
"web-components",
10+
"web-angular",
11+
"mobile-android",
12+
"mobile-ios",
13+
"mobile-flutter",
14+
"mobile-react-native",
15+
]);
616
717
const props = z.object({
818
disabledPlatforms: z

src/components/realtimekit/RTKSDKSelector/RTKSDKSelector.tsx

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,85 @@ export default function SDKSelector({ disabledPlatforms }: SDKSelectorProps) {
2525
label: "Mobile",
2626
id: "mobile",
2727
},
28+
{
29+
label: "React",
30+
id: "web-react",
31+
},
32+
{
33+
label: "Web Components",
34+
id: "web-components",
35+
},
36+
{
37+
label: "Angular",
38+
id: "web-angular",
39+
},
40+
{
41+
label: "Android",
42+
id: "mobile-android",
43+
},
44+
{
45+
label: "iOS",
46+
id: "mobile-ios",
47+
},
48+
{
49+
label: "Flutter",
50+
id: "mobile-flutter",
51+
},
52+
{
53+
label: "React Native",
54+
id: "mobile-react-native",
55+
},
2856
];
2957

58+
const mainPlatforms = platforms.filter(
59+
(p) => p.id === "web" || p.id === "mobile",
60+
);
61+
62+
const frameworkToPlatform: Record<string, Platform> = {
63+
react: "web-react",
64+
"web-components": "web-components",
65+
angular: "web-angular",
66+
android: "mobile-android",
67+
ios: "mobile-ios",
68+
flutter: "mobile-flutter",
69+
"react-native": "mobile-react-native",
70+
};
71+
3072
const frameworks = useMemo(
3173
() => (platform === "web" ? webFrameworks : mobileFrameworks),
3274
[platform],
3375
);
3476

3577
const isPlatformDisabled = (p: Platform) =>
3678
Boolean(disabledPlatforms?.includes(p));
37-
const activePlatformDisabled = isPlatformDisabled(platform);
3879

39-
const disabledPlatformsString = disabledPlatforms?.join(", ");
80+
const isFrameworkDisabled = (fw: { id: string; label: string }) => {
81+
const subPlatform = frameworkToPlatform[fw.id];
82+
return subPlatform ? isPlatformDisabled(subPlatform) : false;
83+
};
84+
85+
const isWebPlatformDisabled = () => {
86+
if (isPlatformDisabled("web")) return true;
87+
88+
const allWebDisabled = webFrameworks.every((fw) => isFrameworkDisabled(fw));
89+
return allWebDisabled;
90+
};
91+
92+
const isMobilePlatformDisabled = () => {
93+
if (isPlatformDisabled("mobile")) return true;
94+
95+
const allMobileDisabled = mobileFrameworks.every((fw) =>
96+
isFrameworkDisabled(fw),
97+
);
98+
return allMobileDisabled;
99+
};
100+
101+
const activePlatformDisabled =
102+
platform === "web" ? isWebPlatformDisabled() : isMobilePlatformDisabled();
103+
104+
const disabledPlatformsString = disabledPlatforms
105+
?.map((p) => platforms.find((platform) => platform.id === p)?.label || p)
106+
.join(", ");
40107

41108
return (
42109
<>
@@ -48,8 +115,13 @@ export default function SDKSelector({ disabledPlatforms }: SDKSelectorProps) {
48115
)}
49116
<div className="my-5 flex flex-col gap-0 rounded-md bg-blue-100 p-2 dark:bg-neutral-800">
50117
<div className="flex w-full flex-row items-start justify-start gap-2">
51-
{platforms.map((p) => {
52-
const disabled = isPlatformDisabled(p.id);
118+
{mainPlatforms.map((p) => {
119+
const disabled =
120+
p.id === "web"
121+
? isWebPlatformDisabled()
122+
: p.id === "mobile"
123+
? isMobilePlatformDisabled()
124+
: isPlatformDisabled(p.id);
53125

54126
return (
55127
<button
@@ -86,15 +158,18 @@ export default function SDKSelector({ disabledPlatforms }: SDKSelectorProps) {
86158
{!activePlatformDisabled && (
87159
<div className="m-0 flex w-full flex-row items-center gap-2 rounded-r-md rounded-b-md bg-neutral-50 p-2 text-gray-500 dark:bg-neutral-700 dark:text-gray-400">
88160
{frameworks.map((fw) => {
161+
const disabled = isFrameworkDisabled(fw);
89162
const handleClick = () => {
163+
if (disabled) return;
90164
setSelection(platform, fw);
91165
};
92166

93167
return (
94168
<button
95169
key={fw.id}
96170
type="button"
97-
className={`m-0 flex ${framework?.id === fw.id ? "text-blue-500 italic" : ""} text-md cursor-pointer items-center rounded-md bg-neutral-50 px-3 py-1 font-medium dark:bg-neutral-700`}
171+
disabled={disabled}
172+
className={`m-0 flex ${framework?.id === fw.id ? "text-blue-500 italic" : ""} ${disabled ? "cursor-not-allowed opacity-50" : "cursor-pointer"} text-md items-center rounded-md bg-neutral-50 px-3 py-1 font-medium dark:bg-neutral-700`}
98173
onClick={handleClick}
99174
>
100175
{fw.label}

src/components/realtimekit/hooks/useFramework.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { useEffect, useState } from "react";
22
const STORAGE_KEY = "realtimekit-sdk-selector";
33

4-
export type Platform = "web" | "mobile";
4+
export type Platform =
5+
| "web"
6+
| "mobile"
7+
| "web-react"
8+
| "web-components"
9+
| "web-angular"
10+
| "mobile-android"
11+
| "mobile-ios"
12+
| "mobile-flutter"
13+
| "mobile-react-native";
514
export type Framework = {
615
id: string;
716
label: string;

0 commit comments

Comments
 (0)