From a8ef4e2097d3d6db056ef64353eb9821430289fb Mon Sep 17 00:00:00 2001 From: shumadrid <195762734+shumadrid@users.noreply.github.com> Date: Fri, 11 Apr 2025 14:31:11 +0200 Subject: [PATCH] fix: properly convert timezones in PrepareCompareModal --- src/GitChangelogPlugin.svelte.ts | 14 ++-- .../CompareRepoCommits/CompareRepoCommits.ts | 17 ++-- .../CompareRepoCommits/PrepareCompareModal.ts | 78 ++++++++++--------- src/commands.ts | 8 +- 4 files changed, 64 insertions(+), 53 deletions(-) diff --git a/src/GitChangelogPlugin.svelte.ts b/src/GitChangelogPlugin.svelte.ts index 3e58d25..22a5e33 100644 --- a/src/GitChangelogPlugin.svelte.ts +++ b/src/GitChangelogPlugin.svelte.ts @@ -85,8 +85,8 @@ export class GitChangelogPlugin extends PluginBase { public fileChangelogManager = $state(); public statusBarStats?: StatusBarStats; public cachedActiveGitFile: string | undefined; - public compareVersionsNewerDate: string | undefined; - public compareVersionsOlderDate: string | undefined; + public compareVersionsUtcNewerDate: string | undefined; + public compareVersionsUtcOlderDate: string | undefined; public localeSafe = $state('en-US'); // Properly loaded in onLayoutReady @@ -238,12 +238,12 @@ export class GitChangelogPlugin extends PluginBase { }); this.registerView(COMPARE_REPO_COMMITS_VIEW_CONFIG.type, (leaf) => { - return new CompareRepoCommitsView( + return new CompareRepoCommitsView({ leaf, - this, - assertNotNull(this.compareVersionsOlderDate), - assertNotNull(this.compareVersionsNewerDate) - ); + plugin: this, + utcOlderDate: assertNotNull(this.compareVersionsUtcOlderDate), + utcNewerDate: assertNotNull(this.compareVersionsUtcNewerDate) + }); }); addCommands(this); diff --git a/src/Views/CompareRepoCommits/CompareRepoCommits.ts b/src/Views/CompareRepoCommits/CompareRepoCommits.ts index ce1a0a3..41bfd9c 100644 --- a/src/Views/CompareRepoCommits/CompareRepoCommits.ts +++ b/src/Views/CompareRepoCommits/CompareRepoCommits.ts @@ -17,12 +17,17 @@ export class CompareRepoCommitsView public utcNewerDate: string; private _view: CompareRepoCommitsComponent | undefined = undefined; - public constructor( - leaf: WorkspaceLeaf, - plugin: GitChangelogPlugin, - utcOlderDate: string, - utcNewerDate: string - ) { + public constructor({ + leaf, + plugin, + utcOlderDate, + utcNewerDate + }: { + leaf: WorkspaceLeaf; + plugin: GitChangelogPlugin; + utcOlderDate: string; + utcNewerDate: string; + }) { super(leaf); this.plugin = plugin; this.utcOlderDate = utcOlderDate; diff --git a/src/Views/CompareRepoCommits/PrepareCompareModal.ts b/src/Views/CompareRepoCommits/PrepareCompareModal.ts index 0b2753a..6864c3d 100644 --- a/src/Views/CompareRepoCommits/PrepareCompareModal.ts +++ b/src/Views/CompareRepoCommits/PrepareCompareModal.ts @@ -7,8 +7,6 @@ import { ButtonComponent, moment } from 'obsidian'; import { CssClass } from 'obsidian-dev-utils/CssClass'; import { ModalBase } from 'obsidian-dev-utils/obsidian/Modals/ModalBase'; import { SettingEx } from 'obsidian-dev-utils/obsidian/SettingEx'; -import { getTimeZone } from 'settings/ui/CustomTimeZone.ts'; -import spacetime from 'spacetime'; import { assertNotNull } from 'utils.ts'; export class CompareVersionsModal extends ModalBase< @@ -17,8 +15,9 @@ export class CompareVersionsModal extends ModalBase< > { public plugin: GitChangelogPlugin; - private olderDate: Date | undefined; - private newerDate: Date; + // Date-time component automatically converts dates to the system timezone in the UI and stores them as UTC + private utcOlderDate: Date | undefined; + private utcNewerDate: Date; private compareRepoCommitsViewEphemeralState: | CompareRepoCommitsViewState | undefined; @@ -27,16 +26,27 @@ export class CompareVersionsModal extends ModalBase< options, resolve, modalCssClass, - plugin + plugin, + utcOlderDateString, + utcNewerDateString }: { options: ModalOptionsBase; resolve: PromiseResolve; modalCssClass: string; plugin: GitChangelogPlugin; + utcOlderDateString: string | undefined; + utcNewerDateString: string | undefined; }) { super(options, resolve, modalCssClass); this.plugin = plugin; - this.newerDate = spacetime.now(getTimeZone(this.plugin)).toNativeDate(); + + // Load the newer date from the previous modal or if this is the first time the user is comparing in this Obsidian session then assign the current date + this.utcNewerDate = utcNewerDateString + ? new Date(utcNewerDateString) + : new Date(); + if (utcOlderDateString) { + this.utcOlderDate = new Date(utcOlderDateString); + } } public override onClose(): void { @@ -50,24 +60,33 @@ export class CompareVersionsModal extends ModalBase< this.contentEl.addClass('git-changelog-checkpoint-modal'); const titleElement = new SettingEx(this.contentEl); - titleElement.setName('Compare points in git history'); + titleElement.setName('Compare two vault states in git history'); titleElement.setDesc( - `The nearest commit that came before the specified date will be used. Specify the dates in the timezone that's configured in your settings` + createFragment((fragment) => { + fragment.appendText( + `The nearest commit that came before the each specified date will be used.` + ); + fragment.createEl('br'); + fragment.appendText( + `Specify the dates in your system timezone (not the one configured in the plugin settings).` + ); + }) ); this.createTimeInput({ text: 'Newer date', onChange: (value) => { - this.newerDate = value; + this.utcNewerDate = value; }, - startValue: this.newerDate + startValue: this.utcNewerDate }); this.createTimeInput({ text: 'Older date', onChange: (value) => { - this.olderDate = value; - } + this.utcOlderDate = value; + }, + startValue: this.utcOlderDate }); this.createCompareButton(); @@ -80,7 +99,7 @@ export class CompareVersionsModal extends ModalBase< }: { onChange: (value: Date) => void; text: string; - startValue?: Date; + startValue: Date | undefined; }): void { const setting = new SettingEx(this.contentEl); @@ -93,23 +112,22 @@ export class CompareVersionsModal extends ModalBase< } private validateDates(): string | undefined { - if (!this.olderDate || !this.newerDate) { + if (!this.utcOlderDate || !this.utcNewerDate) { return 'Both dates must be selected.'; } - // Try parsing both dates - Date.parse returns NaN for invalid dates - - if (!moment.isDate(this.olderDate)) { + // Try parsing both dates + if (!moment.isDate(this.utcOlderDate)) { return 'Invalid start date.'; } - if (!moment.isDate(this.newerDate)) { + if (!moment.isDate(this.utcNewerDate)) { return 'Invalid end date.'; } // Check if the older date is before the newer date - return this.olderDate < this.newerDate + return this.utcOlderDate < this.utcNewerDate ? undefined - : 'Start date must be before end date.'; + : 'Older date must be before the newer date.'; } private createCompareButton(): void { @@ -122,24 +140,10 @@ export class CompareVersionsModal extends ModalBase< if (validationMessage) { this.plugin.displayNotice(validationMessage); } else { - const timeZone = getTimeZone(this.plugin); - - // Swap the timezone, but keep the same date-time - // Date() doesn't store timezone information. - const timeZoneAppliedOlderDate = spacetime( - assertNotNull(this.olderDate) - ).timezone(timeZone); - const timeZoneAppliedNewerDate = spacetime( - assertNotNull(this.newerDate) - ).timezone(timeZone); - - // Assumes input dates are in the configured timezone, so we need to convert them to UTC, so that git can process them accurately. - const utcOlderDate = timeZoneAppliedOlderDate.goto('utc'); - const utcNewerDate = timeZoneAppliedNewerDate.goto('utc'); - + // Input dates are in UTC, so that git can process them accurately. this.compareRepoCommitsViewEphemeralState = { - utcOlderDate: utcOlderDate.toNativeDate().toISOString(), - utcNewerDate: utcNewerDate.toNativeDate().toISOString() + utcOlderDate: assertNotNull(this.utcOlderDate).toISOString(), + utcNewerDate: this.utcNewerDate.toISOString() }; this.close(); diff --git a/src/commands.ts b/src/commands.ts index 796d7f7..d2cc4a6 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -96,7 +96,9 @@ function addCompareRepoVersionsCommand(plugin: GitChangelogPlugin): void { options: { app: plugin.app, cssClass: CssClass.ConfirmModal - } + }, + utcOlderDateString: plugin.compareVersionsUtcOlderDate, + utcNewerDateString: plugin.compareVersionsUtcNewerDate }) ); @@ -104,9 +106,9 @@ function addCompareRepoVersionsCommand(plugin: GitChangelogPlugin): void { // Close any existing COMPARE_REPO_STATES_VIEW views removeCompareVersionsView(plugin); - plugin.compareVersionsNewerDate = + plugin.compareVersionsUtcNewerDate = compareRepoCommitsViewState.utcNewerDate; - plugin.compareVersionsOlderDate = + plugin.compareVersionsUtcOlderDate = compareRepoCommitsViewState.utcOlderDate; await plugin.app.workspace.ensureSideLeaf(