Skip to content

Commit

Permalink
feat: integrate fix space into clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Jul 15, 2024
1 parent 65b5e62 commit fc54847
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 86 deletions.
2 changes: 1 addition & 1 deletion frontend/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@
"cwd": "${workspaceRoot}/appflowy_tauri/"
},
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DatabaseTabBarBloc
_createLinkedView(layout.layoutType, name ?? layout.layoutName);
},
deleteView: (String viewId) async {
final result = await ViewBackendService.delete(viewId: viewId);
final result = await ViewBackendService.deleteView(viewId: viewId);
result.fold(
(l) {},
(r) => Log.error(r),
Expand Down
16 changes: 12 additions & 4 deletions frontend/appflowy_flutter/lib/shared/feature_flags.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ enum FeatureFlag {
// used for controlling whether to show plan+billing options in settings
planBilling,

// used for space design
spaceDesign,

// used for ignore the conflicted feature flag
unknown;

Expand Down Expand Up @@ -88,6 +91,8 @@ enum FeatureFlag {

bool get isOn {
if ([
// release this feature in version 0.6.1
FeatureFlag.spaceDesign,
// release this feature in version 0.5.9
FeatureFlag.search,
// release this feature in version 0.5.6
Expand All @@ -105,15 +110,16 @@ enum FeatureFlag {
}

switch (this) {
case FeatureFlag.search:
case FeatureFlag.syncDocument:
case FeatureFlag.syncDatabase:
case FeatureFlag.spaceDesign:
return true;
case FeatureFlag.collaborativeWorkspace:
case FeatureFlag.membersSettings:
case FeatureFlag.planBilling:
case FeatureFlag.unknown:
return false;
case FeatureFlag.search:
case FeatureFlag.syncDocument:
case FeatureFlag.syncDatabase:
return true;
}
}

Expand All @@ -131,6 +137,8 @@ enum FeatureFlag {
return 'if it\'s on, the command palette and search button will be available';
case FeatureFlag.planBilling:
return 'if it\'s on, plan and billing pages will be available in Settings';
case FeatureFlag.spaceDesign:
return 'if it\'s on, the space design feature will be available';
case FeatureFlag.unknown:
return '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class SpaceBloc extends Bloc<SpaceEvent, SpaceState> {
isExpanded: isExpanded,
shouldShowUpgradeDialog: shouldShowUpgradeDialog,
isInitialized: true,
issueViews: [],
),
);

Expand Down Expand Up @@ -142,7 +141,7 @@ class SpaceBloc extends Bloc<SpaceEvent, SpaceState> {
if (deletedSpace == null) {
return;
}
await ViewBackendService.delete(viewId: deletedSpace.id);
await ViewBackendService.deleteView(viewId: deletedSpace.id);
},
rename: (space, name) async {
add(SpaceEvent.update(name: name));
Expand Down Expand Up @@ -330,12 +329,6 @@ class SpaceBloc extends Bloc<SpaceEvent, SpaceState> {

emit(state.copyWith(isDuplicatingSpace: false));
},
reassignIssueViews: () async {
await _reassignIssueViews();
},
updateIssueViews: (issueViews) async {
emit(state.copyWith(issueViews: issueViews));
},
);
},
);
Expand Down Expand Up @@ -440,6 +433,7 @@ class SpaceBloc extends Bloc<SpaceEvent, SpaceState> {
workspaceId: workspaceId,
)..start(
sectionChanged: (result) async {
Log.info('did receive section views changed');
add(const SpaceEvent.didReceiveSpaceUpdate());
},
);
Expand Down Expand Up @@ -680,28 +674,6 @@ class SpaceBloc extends Bloc<SpaceEvent, SpaceState> {

return newSpace;
}

Future<void> _reassignIssueViews() async {
final issueViews = state.issueViews;
if (issueViews.isEmpty) {
return;
}
for (final view in issueViews) {
final result = await ViewBackendService.moveViewV2(
viewId: view.id,
newParentId: view.parentViewId,
prevViewId: null,
);
result.fold(
(_) {
Log.info('space: reassign issue view: ${view.name}(${view.id})');
},
(error) {
Log.error('space: failed to reassign issue view: $error');
},
);
}
}
}

@freezed
Expand Down Expand Up @@ -743,9 +715,6 @@ class SpaceEvent with _$SpaceEvent {
) = _Reset;
const factory SpaceEvent.migrate() = _Migrate;
const factory SpaceEvent.switchToNextSpace() = _SwitchToNextSpace;
const factory SpaceEvent.reassignIssueViews() = _ReassignIssueViews;
const factory SpaceEvent.updateIssueViews(List<ViewPB> issueViews) =
_UpdateIssueViews;
}

@freezed
Expand All @@ -760,7 +729,6 @@ class SpaceState with _$SpaceState {
@Default(false) bool shouldShowUpgradeDialog,
@Default(false) bool isDuplicatingSpace,
@Default(false) bool isInitialized,
@Default([]) List<ViewPB> issueViews,
}) = _SpaceState;

factory SpaceState.initial() => const SpaceState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
// unpublish the page and all its child pages if they are published
await _unpublishPage(view);

final result = await ViewBackendService.delete(viewId: view.id);
final result = await ViewBackendService.deleteView(viewId: view.id);

emit(
result.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ class ViewBackendService {
});
}

static Future<FlowyResult<void, FlowyError>> delete({
static Future<FlowyResult<void, FlowyError>> deleteView({
required String viewId,
}) {
final request = RepeatedViewIdPB.create()..items.add(viewId);
return FolderEventDeleteView(request).send();
}

static Future<FlowyResult<void, FlowyError>> deleteView({
required String viewId,
static Future<FlowyResult<void, FlowyError>> deleteViews({
required List<String> viewIds,
}) {
final request = RepeatedViewIdPB.create()..items.add(viewId);
final request = RepeatedViewIdPB.create()..items.addAll(viewIds);
return FolderEventDeleteView(request).send();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import 'package:appflowy/workspace/presentation/home/menu/sidebar/shared/sidebar
import 'package:appflowy/workspace/presentation/home/menu/sidebar/space/sidebar_space.dart';
import 'package:appflowy/workspace/presentation/home/menu/sidebar/space/space_migration.dart';
import 'package:appflowy/workspace/presentation/home/menu/sidebar/workspace/sidebar_workspace.dart';
import 'package:appflowy/workspace/presentation/widgets/dialogs.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/workspace.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
show UserProfilePB;
Expand Down Expand Up @@ -140,8 +139,7 @@ class HomeSideBar extends StatelessWidget {
BlocListener<SpaceBloc, SpaceState>(
listenWhen: (p, c) =>
p.lastCreatedPage?.id != c.lastCreatedPage?.id ||
p.isDuplicatingSpace != c.isDuplicatingSpace ||
p.issueViews != c.issueViews,
p.isDuplicatingSpace != c.isDuplicatingSpace,
listener: (context, state) {
final page = state.lastCreatedPage;
if (page == null || page.id.isEmpty) {
Expand All @@ -166,20 +164,6 @@ class HomeSideBar extends StatelessWidget {
_duplicateSpaceLoading?.stop();
_duplicateSpaceLoading = null;
}

if (state.issueViews.isNotEmpty) {
showConfirmDialog(
context: context,
title: 'Some errors happened',
description:
'There are 2 pages (Module 3, Module 4) that have not been synced to the space. Click OK to sync them.',
onConfirm: () {
context.read<SpaceBloc>().add(
const SpaceEvent.reassignIssueViews(),
);
},
);
}
},
),
BlocListener<ActionNavigationBloc, ActionNavigationState>(
Expand All @@ -193,7 +177,8 @@ class HomeSideBar extends StatelessWidget {
if (actionType == UserWorkspaceActionType.create ||
actionType == UserWorkspaceActionType.delete ||
actionType == UserWorkspaceActionType.open) {
if (context.read<SpaceBloc>().state.spaces.isEmpty) {
if (!FeatureFlag.spaceDesign.isOn ||
context.read<SpaceBloc>().state.spaces.isEmpty) {
context.read<SidebarSectionsBloc>().add(
SidebarSectionsEvent.reload(
userProfile,
Expand Down Expand Up @@ -390,7 +375,8 @@ class _SidebarState extends State<_Sidebar> {
context.read<SpaceBloc>().add(const SpaceEvent.didReceiveSpaceUpdate());
}

return !containsSpace ||
return !FeatureFlag.spaceDesign.isOn ||
!containsSpace ||
spaceState.spaces.isEmpty ||
!workspaceState.isCollabWorkspaceOn
? Expanded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,15 @@ class ConfirmPopup extends StatefulWidget {
required this.title,
required this.description,
required this.onConfirm,
this.confirmLabel,
this.confirmButtonColor,
});

final String title;
final String description;
final VoidCallback onConfirm;
final String? confirmLabel;
final Color? confirmButtonColor;
final ConfirmPopupStyle style;

@override
Expand Down Expand Up @@ -376,8 +380,9 @@ class _ConfirmPopupState extends State<ConfirmPopup> {
widget.onConfirm();
Navigator.of(context).pop();
},
confirmButtonName: LocaleKeys.button_ok.tr(),
confirmButtonColor: Theme.of(context).colorScheme.primary,
confirmButtonName: widget.confirmLabel ?? LocaleKeys.button_ok.tr(),
confirmButtonColor: widget.confirmButtonColor ??
Theme.of(context).colorScheme.primary,
);
case ConfirmPopupStyle.cancelAndOk:
return SpaceCancelOrConfirmButton(
Expand All @@ -386,8 +391,10 @@ class _ConfirmPopupState extends State<ConfirmPopup> {
widget.onConfirm();
Navigator.of(context).pop();
},
confirmButtonName: LocaleKeys.space_delete.tr(),
confirmButtonColor: Theme.of(context).colorScheme.error,
confirmButtonName:
widget.confirmLabel ?? LocaleKeys.space_delete.tr(),
confirmButtonColor:
widget.confirmButtonColor ?? Theme.of(context).colorScheme.error,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ class WorkspaceDataManager {
List<ViewPB>? allViews,
bool dryRun = true,
}) async {
final List<ViewPB> issues = [];
// Views whose parent view does not have the view in its child views
final List<ViewPB> unlistedChildViews = [];
// Views whose parent is not in allViews
final List<ViewPB> orphanViews = [];

try {
if (workspace == null || allViews == null) {
Expand All @@ -129,12 +132,16 @@ class WorkspaceDataManager {
}

for (final view in allViews) {
if (view.parentViewId == '') {
continue;
}

final parentView = allViews.firstWhereOrNull(
(e) => e.id == view.parentViewId,
);

if (parentView == null) {
Log.info('found an issue: can not find the parent view: $view');
orphanViews.add(view);
continue;
}

Expand All @@ -143,17 +150,50 @@ class WorkspaceDataManager {
.getOrThrow();
final result = childViewsOfParent.any((e) => e.id == view.id);
if (!result) {
Log.info(
'found an issue: view is not in the parent view\'s child views: $view',
);
issues.add(view);
unlistedChildViews.add(view);
}
}
} catch (e) {
Log.error('Failed to check space health: $e');
return [];
}

for (final view in unlistedChildViews) {
Log.info(
'[workspace] found an issue: view is not in the parent view\'s child views, view: ${view.toProto3Json()}}',
);
}

for (final view in orphanViews) {
Log.debug('[workspace] orphanViews: ${view.toProto3Json()}');
}

if (!dryRun && unlistedChildViews.isNotEmpty) {
Log.info(
'[workspace] start to fix ${unlistedChildViews.length} unlistedChildViews ...',
);
for (final view in unlistedChildViews) {
// move the view to the parent view if it is not in the parent view's child views
Log.info(
'[workspace] move view: $view to its parent view ${view.parentViewId}',
);
await ViewBackendService.moveViewV2(
viewId: view.id,
newParentId: view.parentViewId,
prevViewId: null,
);
}

Log.info('[workspace] end to fix unlistedChildViews');
}

if (unlistedChildViews.isEmpty && orphanViews.isEmpty) {
Log.info('[workspace] all views are healthy');
}

return issues;
Log.info('[workspace] done checking view health');

return unlistedChildViews;
}

static void dumpViews(String prefix, List<ViewPB> views) {
Expand Down
Loading

0 comments on commit fc54847

Please sign in to comment.