Skip to content

Commit

Permalink
feat: add workspace security setting definition
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyjoygh committed Aug 17, 2024
1 parent faa6fcf commit a5bc443
Show file tree
Hide file tree
Showing 21 changed files with 488 additions and 650 deletions.
2 changes: 1 addition & 1 deletion frontend/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function App() {
}, [workspaceStore.setting.customStyle]);

useEffect(() => {
const hasCustomBranding = workspaceStore.profile.plan === PlanType.PRO;
const hasCustomBranding = workspaceStore.profile.subscription?.plan === PlanType.PRO;
if (!hasCustomBranding || !workspaceStore.setting.branding) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/web/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ const Header: React.FC = () => {
<Logo className="mr-2" />
Slash
</Link>
{[PlanType.PRO, PlanType.ENTERPRISE].includes(profile.plan) && (
{profile.subscription?.plan && [PlanType.PRO, PlanType.ENTERPRISE].includes(profile.subscription.plan) && (
<span className="ml-1 text-xs px-1.5 leading-5 border rounded-full bg-blue-600 border-blue-700 text-white shadow dark:opacity-70">
{/* PRO or ENT */}
{profile.plan.substring(0, 3)}
{profile.subscription.plan.substring(0, 3)}
</span>
)}
{shouldShowRouterSwitch && (
Expand Down
2 changes: 1 addition & 1 deletion frontend/web/src/components/Logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Props {

const Logo = ({ className }: Props) => {
const workspaceStore = useWorkspaceStore();
const hasCustomBranding = workspaceStore.profile.plan === PlanType.PRO;
const hasCustomBranding = workspaceStore.profile.subscription?.plan === PlanType.PRO;
const branding = hasCustomBranding && workspaceStore.setting.branding ? new TextDecoder().decode(workspaceStore.setting.branding) : "";
return (
<div className={classNames("w-8 h-auto dark:text-gray-500 rounded-lg overflow-hidden", className)}>
Expand Down
12 changes: 5 additions & 7 deletions frontend/web/src/components/setting/WorkspaceSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const WorkspaceSection = () => {
const [workspaceSetting, setWorkspaceSetting] = useState<WorkspaceSetting>(workspaceStore.setting);
const originalWorkspaceSetting = useRef<WorkspaceSetting>(workspaceStore.setting);
const allowSave = !isEqual(originalWorkspaceSetting.current, workspaceSetting);
const hasCustomBranding = workspaceStore.profile.plan === PlanType.PRO;
const hasCustomBranding = workspaceStore.profile.subscription?.plan === PlanType.PRO;
const branding = hasCustomBranding && workspaceSetting.branding ? new TextDecoder().decode(workspaceSetting.branding) : "";

const onBrandingChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -80,12 +80,10 @@ const WorkspaceSection = () => {
}

try {
const setting = (
await workspaceServiceClient.updateWorkspaceSetting({
setting: workspaceSetting,
updateMask: updateMask,
})
).setting as WorkspaceSetting;
const setting = await workspaceServiceClient.updateWorkspaceSetting({
setting: workspaceSetting,
updateMask: updateMask,
});
setWorkspaceSetting(setting);
await workspaceStore.fetchWorkspaceSetting();
originalWorkspaceSetting.current = setting;
Expand Down
6 changes: 3 additions & 3 deletions frontend/web/src/pages/SubscriptionSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const SubscriptionSetting: React.FC = () => {
<p className="text-2xl shrink-0 font-semibold text-gray-900 dark:text-gray-500">Subscription</p>
<div className="mt-2">
<span className="text-gray-500 mr-2">Current plan:</span>
<span className="text-2xl mr-4 dark:text-gray-400">{stringifyPlanType(profile.plan)}</span>
<span className="text-2xl mr-4 dark:text-gray-400">{stringifyPlanType(profile.subscription?.plan)}</span>
</div>
<Textarea
className="w-full mt-2"
Expand All @@ -70,15 +70,15 @@ const SubscriptionSetting: React.FC = () => {
/>
<div className="w-full flex justify-between items-center mt-4">
<div>
{profile.plan === PlanType.FREE && (
{profile.subscription?.plan === PlanType.FREE && (
<Link href="https://yourselfhosted.lemonsqueezy.com/checkout/buy/947e9a56-c93a-4294-8d71-2ea4b0f3ec51" target="_blank">
Buy a license key
<Icon.ExternalLink className="w-4 h-auto ml-1" />
</Link>
)}
</div>
<div className="flex justify-end items-center gap-2">
{profile.plan !== PlanType.FREE && (
{profile.subscription?.plan !== PlanType.FREE && (
<Button color="neutral" variant="plain" onClick={handleDeleteLicenseKey}>
Reset
</Button>
Expand Down
2 changes: 1 addition & 1 deletion frontend/web/src/pages/WorkspaceSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const WorkspaceSetting = () => {
<p className="text-2xl shrink-0 font-semibold text-gray-900 dark:text-gray-500">Subscription</p>
<div className="mt-2">
<span className="text-gray-500 mr-2">Current plan:</span>
<span className="text-2xl mr-4 dark:text-gray-400">{stringifyPlanType(profile.plan)}</span>
<span className="text-2xl mr-4 dark:text-gray-400">{stringifyPlanType(profile.subscription?.plan)}</span>
<Link to="/setting/subscription" unstable_viewTransition>
<Button size="sm" variant="outlined" startDecorator={<Icon.Settings className="w-4 h-auto" />}>
Manage
Expand Down
2 changes: 1 addition & 1 deletion frontend/web/src/stores/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PlanType } from "@/types/proto/api/v1/subscription_service";

export const stringifyPlanType = (planType: PlanType) => {
export const stringifyPlanType = (planType: PlanType = PlanType.FREE) => {
if (planType === PlanType.FREE) {
return "Free";
} else if (planType === PlanType.PRO) {
Expand Down
6 changes: 3 additions & 3 deletions frontend/web/src/stores/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ const useWorkspaceStore = create<WorkspaceState>()((set, get) => ({
profile: WorkspaceProfile.fromPartial({}),
setting: WorkspaceSetting.fromPartial({}),
fetchWorkspaceProfile: async () => {
const workspaceProfile = (await workspaceServiceClient.getWorkspaceProfile({})).profile as WorkspaceProfile;
const workspaceProfile = await workspaceServiceClient.getWorkspaceProfile({});
set({ ...get(), profile: workspaceProfile });
return workspaceProfile;
},
fetchWorkspaceSetting: async () => {
const workspaceSetting = (await workspaceServiceClient.getWorkspaceSetting({})).setting as WorkspaceSetting;
const workspaceSetting = await workspaceServiceClient.getWorkspaceSetting({});
set({ ...get(), setting: workspaceSetting });
return workspaceSetting;
},
checkFeatureAvailable: (feature: FeatureType): boolean => {
return get().profile.features.includes(feature);
return get().profile.subscription?.features.includes(feature) || false;
},
}));

Expand Down
27 changes: 5 additions & 22 deletions proto/api/v1/workspace_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import "google/protobuf/field_mask.proto";
option go_package = "gen/api/v1";

service WorkspaceService {
rpc GetWorkspaceProfile(GetWorkspaceProfileRequest) returns (GetWorkspaceProfileResponse) {
rpc GetWorkspaceProfile(GetWorkspaceProfileRequest) returns (WorkspaceProfile) {
option (google.api.http) = {get: "/api/v1/workspace/profile"};
}
rpc GetWorkspaceSetting(GetWorkspaceSettingRequest) returns (GetWorkspaceSettingResponse) {
rpc GetWorkspaceSetting(GetWorkspaceSettingRequest) returns (WorkspaceSetting) {
option (google.api.http) = {get: "/api/v1/workspace/setting"};
}
rpc UpdateWorkspaceSetting(UpdateWorkspaceSettingRequest) returns (UpdateWorkspaceSettingResponse) {
rpc UpdateWorkspaceSetting(UpdateWorkspaceSettingRequest) returns (WorkspaceSetting) {
option (google.api.http) = {
patch: "/api/v1/workspace/setting"
body: "setting"
Expand All @@ -31,8 +31,8 @@ message WorkspaceProfile {
string mode = 1;
// Current workspace version.
string version = 2;
// The workspace plan.
PlanType plan = 3;
// The workspace subscription.
Subscription subscription = 3;
// Whether to enable other users to sign up.
bool enable_signup = 4;
// The custom style.
Expand All @@ -42,8 +42,6 @@ message WorkspaceProfile {
string owner = 6;
// The workspace branding.
bytes branding = 7;
// The workspace available features.
repeated string features = 8;
}

message WorkspaceSetting {
Expand Down Expand Up @@ -94,26 +92,11 @@ message IdentityProviderConfig {

message GetWorkspaceProfileRequest {}

message GetWorkspaceProfileResponse {
// The workspace profile.
WorkspaceProfile profile = 1;
}

message GetWorkspaceSettingRequest {}

message GetWorkspaceSettingResponse {
// The user setting.
WorkspaceSetting setting = 1;
}

message UpdateWorkspaceSettingRequest {
// The user setting.
WorkspaceSetting setting = 1;
// The update mask.
google.protobuf.FieldMask update_mask = 2;
}

message UpdateWorkspaceSettingResponse {
// The user setting.
WorkspaceSetting setting = 1;
}
57 changes: 4 additions & 53 deletions proto/gen/api/v1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,12 @@

- [api/v1/workspace_service.proto](#api_v1_workspace_service-proto)
- [GetWorkspaceProfileRequest](#slash-api-v1-GetWorkspaceProfileRequest)
- [GetWorkspaceProfileResponse](#slash-api-v1-GetWorkspaceProfileResponse)
- [GetWorkspaceSettingRequest](#slash-api-v1-GetWorkspaceSettingRequest)
- [GetWorkspaceSettingResponse](#slash-api-v1-GetWorkspaceSettingResponse)
- [IdentityProvider](#slash-api-v1-IdentityProvider)
- [IdentityProviderConfig](#slash-api-v1-IdentityProviderConfig)
- [IdentityProviderConfig.FieldMapping](#slash-api-v1-IdentityProviderConfig-FieldMapping)
- [IdentityProviderConfig.OAuth2Config](#slash-api-v1-IdentityProviderConfig-OAuth2Config)
- [UpdateWorkspaceSettingRequest](#slash-api-v1-UpdateWorkspaceSettingRequest)
- [UpdateWorkspaceSettingResponse](#slash-api-v1-UpdateWorkspaceSettingResponse)
- [WorkspaceProfile](#slash-api-v1-WorkspaceProfile)
- [WorkspaceSetting](#slash-api-v1-WorkspaceSetting)

Expand Down Expand Up @@ -1368,21 +1365,6 @@



<a name="slash-api-v1-GetWorkspaceProfileResponse"></a>

### GetWorkspaceProfileResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| profile | [WorkspaceProfile](#slash-api-v1-WorkspaceProfile) | | The workspace profile. |






<a name="slash-api-v1-GetWorkspaceSettingRequest"></a>

### GetWorkspaceSettingRequest
Expand All @@ -1393,21 +1375,6 @@



<a name="slash-api-v1-GetWorkspaceSettingResponse"></a>

### GetWorkspaceSettingResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| setting | [WorkspaceSetting](#slash-api-v1-WorkspaceSetting) | | The user setting. |






<a name="slash-api-v1-IdentityProvider"></a>

### IdentityProvider
Expand Down Expand Up @@ -1494,21 +1461,6 @@



<a name="slash-api-v1-UpdateWorkspaceSettingResponse"></a>

### UpdateWorkspaceSettingResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| setting | [WorkspaceSetting](#slash-api-v1-WorkspaceSetting) | | The user setting. |






<a name="slash-api-v1-WorkspaceProfile"></a>

### WorkspaceProfile
Expand All @@ -1519,12 +1471,11 @@
| ----- | ---- | ----- | ----------- |
| mode | [string](#string) | | Current workspace mode: dev, prod. |
| version | [string](#string) | | Current workspace version. |
| plan | [PlanType](#slash-api-v1-PlanType) | | The workspace plan. |
| subscription | [Subscription](#slash-api-v1-Subscription) | | The workspace subscription. |
| enable_signup | [bool](#bool) | | Whether to enable other users to sign up. |
| custom_style | [string](#string) | | The custom style. |
| owner | [string](#string) | | The owner name. Format: &#34;users/{id}&#34; |
| branding | [bytes](#bytes) | | The workspace branding. |
| features | [string](#string) | repeated | The workspace available features. |



Expand Down Expand Up @@ -1574,9 +1525,9 @@

| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
| GetWorkspaceProfile | [GetWorkspaceProfileRequest](#slash-api-v1-GetWorkspaceProfileRequest) | [GetWorkspaceProfileResponse](#slash-api-v1-GetWorkspaceProfileResponse) | |
| GetWorkspaceSetting | [GetWorkspaceSettingRequest](#slash-api-v1-GetWorkspaceSettingRequest) | [GetWorkspaceSettingResponse](#slash-api-v1-GetWorkspaceSettingResponse) | |
| UpdateWorkspaceSetting | [UpdateWorkspaceSettingRequest](#slash-api-v1-UpdateWorkspaceSettingRequest) | [UpdateWorkspaceSettingResponse](#slash-api-v1-UpdateWorkspaceSettingResponse) | |
| GetWorkspaceProfile | [GetWorkspaceProfileRequest](#slash-api-v1-GetWorkspaceProfileRequest) | [WorkspaceProfile](#slash-api-v1-WorkspaceProfile) | |
| GetWorkspaceSetting | [GetWorkspaceSettingRequest](#slash-api-v1-GetWorkspaceSettingRequest) | [WorkspaceSetting](#slash-api-v1-WorkspaceSetting) | |
| UpdateWorkspaceSetting | [UpdateWorkspaceSettingRequest](#slash-api-v1-UpdateWorkspaceSettingRequest) | [WorkspaceSetting](#slash-api-v1-WorkspaceSetting) | |



Expand Down
Loading

0 comments on commit a5bc443

Please sign in to comment.