Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace] Add privacy levels to the workspace #8907

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

Kapian1234
Copy link
Contributor

@Kapian1234 Kapian1234 commented Nov 21, 2024

Description

Issues Resolved

Screenshot

Workspace create
Screenshot 2024-11-25 at 11 58 44
Collaborators
Screenshot 2024-11-25 at 12 00 14
Screenshot 2024-11-25 at 12 00 55
Screenshot 2024-11-25 at 12 01 20
Screenshot 2024-11-25 at 12 02 03
Workspace details
Screenshot 2024-11-28 at 10 49 00
Screenshot 2024-11-28 at 10 49 24

Testing the changes

Changelog

  • feat: Add privacy levels to the workspace

Check List

  • All tests pass
    • yarn test:jest
    • yarn test:jest_integration
  • New functionality includes testing.
  • New functionality has been documented.
  • Update CHANGELOG.md
  • Commits are signed per the DCO using --signoff

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

Attention: Patch coverage is 80.72289% with 16 lines in your changes missing coverage. Please review.

Project coverage is 60.89%. Comparing base (539675e) to head (50f1fac).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
...m/workspace_collaborator_privacy_setting_panel.tsx 40.00% 11 Missing and 1 partial ⚠️
...dd_collaborators_modal/add_collaborators_modal.tsx 83.33% 0 Missing and 1 partial ⚠️
...workspace_detail/workspace_detail_form_content.tsx 50.00% 1 Missing ⚠️
...workspace_form/workspace_privacy_setting_panel.tsx 85.71% 1 Missing ⚠️
...orkspace_form/workspace_privacy_setting_select.tsx 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8907      +/-   ##
==========================================
+ Coverage   60.88%   60.89%   +0.01%     
==========================================
  Files        3802     3805       +3     
  Lines       91070    91143      +73     
  Branches    14374    14392      +18     
==========================================
+ Hits        55444    55501      +57     
- Misses      32085    32097      +12     
- Partials     3541     3545       +4     
Flag Coverage Δ
Linux_1 29.07% <80.72%> (+0.05%) ⬆️
Linux_2 56.38% <ø> (-0.01%) ⬇️
Linux_3 37.90% <ø> (+<0.01%) ⬆️
Linux_4 29.00% <ø> (-0.01%) ⬇️
Windows_1 29.08% <80.72%> (+0.05%) ⬆️
Windows_2 56.34% <ø> (-0.01%) ⬇️
Windows_3 37.90% <ø> (+<0.01%) ⬆️
Windows_4 29.00% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

/>
</EuiPanel>
<EuiFlexGroup direction="column">
<EuiFlexItem>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use flex item here? I think can be replaced with EuiSpacer

Copy link
Contributor Author

@Kapian1234 Kapian1234 Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

      <WorkspaceCollaboratorPrivacySettingPanel
        permissionSettings={permissionSettings}
        handleSubmitPermissionSettings={handleSubmitPermissionSettings}
      />
      <EuiSpacer />
      <WorkspaceCollaboratorTable
        permissionSettings={permissionSettings}
        displayedCollaboratorTypes={displayedCollaboratorTypes}
        handleSubmitPermissionSettings={handleSubmitPermissionSettings}
      />

it would be like this if not using flex
Screenshot 2024-11-26 at 10 32 56

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to wrap with a regular div.


      <div>
        <WorkspaceCollaboratorPrivacySettingPanel
          permissionSettings={permissionSettings}
          handleSubmitPermissionSettings={handleSubmitPermissionSettings}
        />
        <EuiSpacer />
        <EuiPanel>
          <WorkspaceCollaboratorTable
            permissionSettings={permissionSettings}
            displayedCollaboratorTypes={displayedCollaboratorTypes}
            handleSubmitPermissionSettings={handleSubmitPermissionSettings}
          />
        </EuiPanel>
      </div>

It won't using a horizontal layout.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, the Edit button seems not aligned with "Workspace privacy" title. Shall we fix it?
image

Copy link
Contributor Author

@Kapian1234 Kapian1234 Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignItems="baseline" would fix it. Thanks for reminder!

const modes = permissionSettings.find(
(item) => item.type === WorkspacePermissionItemType.User && item.userId === '*'
)?.modes;
if (modes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm prefer to separate an util function to convert permission settings to privacy type and , can be refactor like code below:

if(modes?.includes(WorkspacePermissionMode.LibraryWrite)){
    return WorkspacePrivacyItemType.AnyoneCanEdit;
}
if(modes?.includes(WorkspacePermissionMode.LibraryRead)){
    return WorkspacePrivacyItemType.AnyoneCanView;
}

It would be more accurate to me. The privacyType would be "PrivateToCollaborators" if modes doesn't contain any LibraryWrite or LibraryRead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thank you!

defaultMessage: 'Workspace Privacy',
})}
>
<EuiSuperSelect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about separate a WorkspacePrivacySettingSelect to share with collaborators and details page?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

}),
};

const handleChange = async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer separate an util function (generatePermissionSettingsWithPrivacyType) to do this convert. The whole function can be refactor with code below:

const generatePermissionSettingsWithPrivacyType = (permissionSettings, privacyType)=>{
  const newSettings = permissionSettings.filter(
    (item) =>
      !(item.type === WorkspacePermissionItemType.User && item.userId === "*")
  );

  if (
    privacyType === WorkspacePrivacyItemType.AnyoneCanView ||
    privacyType === WorkspacePrivacyItemType.AnyoneCanEdit
  ) {
    newSettings.push({
      id: generateNextPermissionSettingsId(permissionSettings),
      type: WorkspacePermissionItemType.User,
      userId: "*",
      modes:
        optionIdToWorkspacePermissionModesMap[
          privacyType === WorkspacePrivacyItemType.AnyoneCanView
            ? PermissionModeId.Read
            : PermissionModeId.ReadAndWrite
        ],
    });
  }
  return newSettings;
}

let result; 
try {
    result = await handleSubmitPermissionSettings(
        generatePermissionSettingsWithPrivacyType(permissionSettings, selectedPrivacyType) as WorkspacePermissionSetting[]
    );
}catch(error){
    notifications?.toasts?.addError();
    return;
}
if (result?.success) {
  notifications?.toasts?.addSuccess(successfulMessage);
}

(item) => !(item.type === WorkspacePermissionItemType.User && item.userId === '*')
);
newSettings.push({
id: newSettings.length,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using generateNextPermissionSettingsId to generate next id.

const [privacyType, setPrivacyType] = useState(WorkspacePrivacyItemType.PrivateToCollaborators);
const workspaceAdmin = permissionSettings[0];

const options = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be refactor with

[WorkspacePrivacyItemType.PrivateToCollaborators, WorkspacePrivacyItemType.AnyoneCanView, WorkspacePrivacyItemType.AnyoneCanEdit].map((id)=>(
{
      id,
      title: privacyType2CopyMap[value].title,
      description: privacyType2CopyMap[value].description,
    }))

goToCollaborators,
onGoToCollaboratorsChange,
}: WorkspacePrivacySettingProps) => {
const [privacyType, setPrivacyType] = useState(WorkspacePrivacyItemType.PrivateToCollaborators);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move privacyType to useWorkspaceForm, then we can share with create and detail page. Then we don't need to pass permissionSettings and onPermissionChange in this component. And the privacyType should calculate from permissionSettings in the initial rendering.

defaultMessage: 'Workspace privacy',
})}
// isInvalid={!!formErrors.features}
// error={formErrors.features?.message}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed.

label={i18n.translate('workspace.form.privacy.name.label', {
defaultMessage: 'Workspace privacy',
})}
// isInvalid={!!formErrors.features}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed.

},
];

useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed after move privacyType to useWorkspaceForm

@@ -145,7 +146,7 @@ export const WorkspaceCreator = (props: WorkspaceCreatorProps) => {
?.features[0].id;
// Redirect page after one second, leave one second time to show create successful toast.
window.setTimeout(() => {
if (isPermissionEnabled) {
if (isPermissionEnabled && goToCollaborators) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should fix the should redirect to workspace setting collaborators page if permission enabled in src/plugins/workspace/public/components/workspace_creator/workspace_creator.test.tsx unit test failed due to this change.

@@ -110,6 +114,15 @@ export const WorkspaceCreatorForm = (props: WorkspaceCreatorFormProps) => {
</EuiPanel>
</>
)}
<EuiSpacer size="m" />
{isDashboardAdmin && (
<WorkspacePrivacySettingPanel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add permission enabled check here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

initialFormData.permissionSettings &&
convertPermissionsToPrivacyType(newFormData.permissionSettings) !==
convertPermissionsToPrivacyType(initialFormData.permissionSettings)
) {
Copy link
Contributor

@wanglam wanglam Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This number of changes will be failed when permissionSettings not exists. How about refactor with code below:

    convertPermissionsToPrivacyType(newFormData.permissionSettings ?? []) !==
      convertPermissionsToPrivacyType(initialFormData.permissionSettings ?? [])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants