Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('DraftHistoryEntryComponent', () => {
when(mockedTrainingDataService.queryTrainingDataAsync(anything(), anything())).thenResolve(
instance(trainingDataQuery)
);
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected()).thenReturn(true);
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected(anything())).thenReturn(true);
fixture = TestBed.createComponent(DraftHistoryEntryComponent);
component = fixture.componentInstance;
fixture.detectChanges();
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('DraftHistoryEntryComponent', () => {
}));

it('should show the USFM format option when the project is the latest draft', fakeAsync(() => {
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected()).thenReturn(false);
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected(anything())).thenReturn(false);
when(mockedDraftOptionsService.areFormattingOptionsSupportedForBuild(anything())).thenReturn(true);
const user = 'user-display-name';
const date = dateAfterFormattingSupported;
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('DraftHistoryEntryComponent', () => {
}));

it('should handle builds with additional info referencing a deleted user', fakeAsync(() => {
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected()).thenReturn(false);
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected(anything())).thenReturn(false);
when(mockedDraftOptionsService.areFormattingOptionsSupportedForBuild(anything())).thenReturn(true);
when(mockedI18nService.formatDate(anything())).thenReturn('formatted-date');
when(mockedI18nService.formatAndLocalizeScriptureRange('GEN')).thenReturn('Genesis');
Expand Down Expand Up @@ -353,7 +353,7 @@ describe('DraftHistoryEntryComponent', () => {
});

it('should show set draft format UI', fakeAsync(() => {
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected()).thenReturn(false);
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected(anything())).thenReturn(false);
when(mockedDraftOptionsService.areFormattingOptionsSupportedForBuild(anything())).thenReturn(true);
const date = dateAfterFormattingSupported;
component.entry = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ describe('DraftOptionsService', () => {
return doc;
}

function buildDtoWithDate(date: Date): BuildDto {
return {
additionalInfo: {
dateFinished: date.toJSON()
}
} as BuildDto;
}

const SUPPORTED_BUILD_ENTRY: BuildDto = buildDtoWithDate(new Date(FORMATTING_OPTIONS_SUPPORTED_DATE.getTime() + 1));

const PROJECT_DOC_BOTH_FORMATS: SFProjectProfileDoc = buildProjectDoc({
paragraphFormat: ParagraphBreakFormat.BestGuess,
quoteFormat: QuoteFormat.Normalized
Expand Down Expand Up @@ -83,23 +93,28 @@ describe('DraftOptionsService', () => {
describe('areFormattingOptionsAvailableButUnselected', () => {
it('returns true when flag enabled and both options missing', () => {
when(mockedActivatedProject.projectDoc).thenReturn(PROJECT_DOC_EMPTY_USFM);
expect(service.areFormattingOptionsAvailableButUnselected()).toBe(true);
expect(service.areFormattingOptionsAvailableButUnselected(SUPPORTED_BUILD_ENTRY)).toBe(true);
});

it('returns true when flag enabled and one option missing', () => {
when(mockedActivatedProject.projectDoc).thenReturn(PROJECT_DOC_QUOTE_ONLY);
expect(service.areFormattingOptionsAvailableButUnselected()).toBe(true);
expect(service.areFormattingOptionsAvailableButUnselected(SUPPORTED_BUILD_ENTRY)).toBe(true);
});

it('returns false when flag enabled and both options set', () => {
when(mockedActivatedProject.projectDoc).thenReturn(PROJECT_DOC_BOTH_FORMATS);
expect(service.areFormattingOptionsAvailableButUnselected()).toBe(false);
expect(service.areFormattingOptionsAvailableButUnselected(SUPPORTED_BUILD_ENTRY)).toBe(false);
});

it('returns false when flag disabled', () => {
when(mockedFeatureFlagService.usfmFormat).thenReturn(createTestFeatureFlag(false));
when(mockedActivatedProject.projectDoc).thenReturn(PROJECT_DOC_EMPTY_USFM);
expect(service.areFormattingOptionsAvailableButUnselected()).toBe(false);
expect(service.areFormattingOptionsAvailableButUnselected(SUPPORTED_BUILD_ENTRY)).toBe(false);
});

it('returns false when build entry unavailable', () => {
when(mockedActivatedProject.projectDoc).thenReturn(PROJECT_DOC_EMPTY_USFM);
expect(service.areFormattingOptionsAvailableButUnselected(undefined)).toBe(false);
});
});

Expand All @@ -109,7 +124,7 @@ describe('DraftOptionsService', () => {
if (date == null) {
return { additionalInfo: {} } as BuildDto;
}
return { additionalInfo: { dateFinished: date.toJSON() } } as BuildDto;
return buildDtoWithDate(date);
}

it('returns true when flag enabled and date after supported date', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export class DraftOptionsService {
);
}

areFormattingOptionsAvailableButUnselected(): boolean {
return (
this.featureFlags.usfmFormat.enabled &&
(this.activatedProjectService.projectDoc?.data?.translateConfig.draftConfig.usfmConfig?.paragraphFormat == null ||
this.activatedProjectService.projectDoc?.data?.translateConfig.draftConfig.usfmConfig?.quoteFormat == null)
);
areFormattingOptionsAvailableButUnselected(draftBuild: BuildDto | undefined): boolean {
const usfmConfig = this.activatedProjectService.projectDoc?.data?.translateConfig.draftConfig.usfmConfig;
const optionsSelected = usfmConfig?.paragraphFormat != null && usfmConfig?.quoteFormat != null;

const available = this.featureFlags.usfmFormat.enabled && this.areFormattingOptionsSupportedForBuild(draftBuild);
return available && !optionsSelected;
}

areFormattingOptionsSupportedForBuild(entry: BuildDto | undefined): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</mat-form-field>
}
<div class="apply-draft-button-container">
@if (featureFlags.usfmFormat.enabled) {
@if (showDraftOptionsButton$ | async) {
<span
[matTooltip]="t(doesLatestHaveDraft ? 'format_draft_can' : 'format_draft_cannot')"
[style.cursor]="doesLatestHaveDraft ? 'pointer' : 'not-allowed'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ describe('EditorDraftComponent', () => {
when(mockFeatureFlagService.usfmFormat).thenReturn(createTestFeatureFlag(true));
when(mockDraftGenerationService.pollBuildProgress(anything())).thenReturn(buildProgress$.asObservable());
buildProgress$.next({ state: BuildStates.Completed } as BuildDto);
when(mockActivatedProjectService.projectId$).thenReturn(of('targetProjectId'));
when(mockDraftGenerationService.getLastCompletedBuild(anything())).thenReturn(of(undefined));

fixture = TestBed.createComponent(EditorDraftComponent);
component = fixture.componentInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { BuildStates } from '../../../machine-api/build-states';
import { TextComponent } from '../../../shared/text/text.component';
import { DraftGenerationService } from '../../draft-generation/draft-generation.service';
import { DraftHandlingService } from '../../draft-generation/draft-handling.service';
import { DraftOptionsService } from '../../draft-generation/draft-options.service';
@Component({
selector: 'app-editor-draft',
templateUrl: './editor-draft.component.html',
Expand Down Expand Up @@ -82,6 +83,12 @@ export class EditorDraftComponent implements AfterViewInit, OnChanges {
this.onlineStatusService.onlineStatus$
]).pipe(map(([isLoading, isOnline]) => isLoading || !isOnline));

showDraftOptionsButton$: Observable<boolean> = this.activatedProjectService.projectId$.pipe(
filterNullish(),
switchMap(projectId => this.draftGenerationService.getLastCompletedBuild(projectId)),
map(build => this.draftOptionsService.areFormattingOptionsSupportedForBuild(build))
);

private draftDelta?: Delta;
private targetDelta?: Delta;

Expand All @@ -98,7 +105,8 @@ export class EditorDraftComponent implements AfterViewInit, OnChanges {
readonly onlineStatusService: OnlineStatusService,
private readonly noticeService: NoticeService,
private errorReportingService: ErrorReportingService,
private readonly router: Router
private readonly router: Router,
private readonly draftOptionsService: DraftOptionsService
) {}

get bookId(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4111,8 +4111,8 @@ describe('EditorComponent', () => {
Object.defineProperty(env.component, 'showSource', { get: () => true });
});
env.setupProject({ translateConfig: { draftConfig: {} } });
// Formatting options not selected, so draft tab should not be shown
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected()).thenReturn(true);
// Formatting options not selected, so draft tab should be blocked
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected(anything())).thenReturn(true);
when(mockedPermissionsService.canAccessDrafts(anything(), anything())).thenReturn(true);
env.wait();
env.routeWithParams({ projectId: 'project01', bookId: 'LUK', chapter: '1' });
Expand Down Expand Up @@ -4903,6 +4903,8 @@ class TestEnvironment {
).thenReturn(of([]));
when(mockedDraftGenerationService.getGeneratedDraftHistory(anything(), anything(), anything())).thenReturn(of([]));
when(mockedDraftGenerationService.draftExists(anything(), anything(), anything())).thenReturn(of(true));
when(mockedDraftGenerationService.getLastCompletedBuild(anything())).thenReturn(of({} as BuildDto));
when(mockedDraftOptionsService.areFormattingOptionsAvailableButUnselected(anything())).thenReturn(false);
when(mockedPermissionsService.isUserOnProject(anything())).thenResolve(true);
when(mockedFeatureFlagService.newDraftHistory).thenReturn(createTestFeatureFlag(false));
when(mockedLynxWorkspaceService.rawInsightSource$).thenReturn(of([]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1491,20 +1491,25 @@ export class EditorComponent extends DataLoadingComponent implements OnDestroy,
this.projectDoc,
this.userService.currentUserId
);
if (
((hasDraft && !draftApplied) || urlDraftActive) &&
canViewDrafts &&
!this.draftOptionsService.areFormattingOptionsAvailableButUnselected()
) {

const hasUnappliedDraft: boolean = hasDraft && !draftApplied;
const draftShouldBeVisible: boolean = hasUnappliedDraft || urlDraftActive;

let draftBuild: BuildDto | undefined;
// Only try to fetch the draft build if existing information indicates we should show the draft
if (draftShouldBeVisible && canViewDrafts && this.projectId != null) {
draftBuild = await firstValueFrom(this.draftGenerationService.getLastCompletedBuild(this.projectId));
}

const isBlockedByFormattingOptions: boolean =
this.draftOptionsService.areFormattingOptionsAvailableButUnselected(draftBuild);

if (draftShouldBeVisible && canViewDrafts && draftBuild != null && !isBlockedByFormattingOptions) {
// URL may indicate to select the 'draft' tab (such as when coming from generate draft page)
const groupIdToAddTo: EditorTabGroupType = this.showSource ? 'source' : 'target';

// Add to 'source' (or 'target' if showSource is false) tab group if no existing draft tab
if (existingDraftTab == null) {
const draftBuild: BuildDto | undefined = await firstValueFrom(
this.draftGenerationService.getLastCompletedBuild(this.projectId!)
);

this.tabState.addTab(
groupIdToAddTo,
this.editorTabFactory.createTab('draft', {
Expand Down
Loading
Loading