Skip to content

Commit 3859697

Browse files
committed
sessionDate sync bug fix
1 parent f9fb04e commit 3859697

5 files changed

Lines changed: 97 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.3.11] - 2025-01-15
11+
1012
### Fixed
11-
- Restored compatibility with PopOut! by cloning sidebar base options before overriding defaults so Combat Tracker and other stock tabs keep their pop-out buttons when realtime sync is enabled.
13+
- When syncing existing Session Recaps via the "Sync with Archivist" button, the `sessionDate` flag is now properly detected and updated when the `session_date` changes in Archivist, ensuring correct date display and chronological sorting in the Recaps folder.
1214

1315
## [1.3.10] - 2025-01-15
1416

module.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"email": "cameron.b.llewellyn@gmail.com"
99
}
1010
],
11-
"version": "1.3.10",
11+
"version": "1.3.11",
1212
"compatibility": {
1313
"minimum": "13.341",
1414
"verified": "13.346"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "archivist-sync",
3-
"version": "1.3.10",
3+
"version": "1.3.11",
44
"description": "A simple Foundry VTT module for fetching world data from an API endpoint using an API key.",
55
"type": "module",
66
"scripts": {

scripts/dialogs/sync-dialog.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,50 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi
214214
}
215215
}
216216

217+
// Reorder all recaps after any sessionDate changes (from diffs or imports)
218+
const hasRecapDiffs = selectedDiffs.some(
219+
(d) => d.type === 'Session' && d.changes?.sessionDate
220+
);
221+
if (hasRecapDiffs || selectedImports.some((i) => i.type === 'Session')) {
222+
try {
223+
const recapsFolderId = await Utils.ensureJournalFolder('Recaps');
224+
const entries = (game.journal?.contents || [])
225+
.filter((j) => (j.folder?.id || null) === (recapsFolderId || null))
226+
.filter(
227+
(j) =>
228+
String(
229+
(j.getFlag(CONFIG.MODULE_ID, 'archivist') || {}).sheetType ||
230+
''
231+
) === 'recap'
232+
);
233+
const withDates = entries.map((j) => ({
234+
j,
235+
dateMs: (() => {
236+
const iso = String(
237+
j.getFlag(CONFIG.MODULE_ID, 'sessionDate') || ''
238+
).trim();
239+
const t = iso ? new Date(iso).getTime() : NaN;
240+
return Number.isFinite(t) ? t : Number.POSITIVE_INFINITY; // undated go to end
241+
})(),
242+
}));
243+
withDates.sort((a, b) => a.dateMs - b.dateMs);
244+
let index = 0;
245+
for (const { j } of withDates) {
246+
const desired = index * 1000;
247+
index += 1;
248+
if (j.sort !== desired) {
249+
try {
250+
await j.update({ sort: desired }, { render: false });
251+
} catch (_) {
252+
/* ignore */
253+
}
254+
}
255+
}
256+
} catch (_) {
257+
/* ignore ordering failures */
258+
}
259+
}
260+
217261
ui.notifications?.info?.('Archivist sync applied.');
218262
// Force-refresh core directories and any open Archivist windows so UI reflects new docs
219263
await this._refreshUIAfterSync?.();
@@ -420,6 +464,20 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi
420464
if (flagImg !== archImg)
421465
changes.image = { from: flagImg, to: archImg };
422466
}
467+
// Session date diff: compare Archivist session_date with Foundry sessionDate flag
468+
if (type === 'Session' && arch.session_date) {
469+
try {
470+
const currentDate = String(
471+
j.getFlag(CONFIG.MODULE_ID, 'sessionDate') || ''
472+
).trim();
473+
const archDate = String(arch.session_date || '').trim();
474+
if (archDate && currentDate !== archDate) {
475+
changes.sessionDate = { from: currentDate || null, to: archDate };
476+
}
477+
} catch (_) {
478+
/* ignore */
479+
}
480+
}
423481
// Links diff: only outgoing links (from_id == this sheet's archivistId), ignore alias
424482
try {
425483
const wantList = outgoing.get(archId) || [];
@@ -586,6 +644,35 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi
586644
await j.setFlag(CONFIG.MODULE_ID, 'archivist', nextFlags);
587645
// Hub image flag removed
588646
}
647+
if (changes.sessionDate) {
648+
// Update sessionDate flag to match Archivist session_date (same as world setup)
649+
const archDate = String(changes.sessionDate.to || '').trim();
650+
if (archDate) {
651+
try {
652+
await j.setFlag(
653+
CONFIG.MODULE_ID,
654+
'sessionDate',
655+
archDate
656+
);
657+
// Also update sort order if this is a recap in the Recaps folder
658+
const sheetType = String(
659+
(j.getFlag(CONFIG.MODULE_ID, 'archivist') || {}).sheetType || ''
660+
).toLowerCase();
661+
if (sheetType === 'recap' || sheetType === 'session') {
662+
try {
663+
const sortValue = new Date(archDate).getTime();
664+
if (Number.isFinite(sortValue)) {
665+
await j.update({ sort: sortValue }, { render: false });
666+
}
667+
} catch (_) {
668+
/* ignore sort update failures */
669+
}
670+
}
671+
} catch (_) {
672+
/* ignore */
673+
}
674+
}
675+
}
589676
if (changes.links) {
590677
const buckets = {
591678
character: 'characters',

templates/sync-dialog.hbs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<th style="width: 80px; text-align: center;">Title</th>
3030
<th style="width: 100px; text-align: center;">Description</th>
3131
<th style="width: 80px; text-align: center;">Image</th>
32+
<th style="width: 80px; text-align: center;">Date</th>
3233
<th style="width: 80px; text-align: center;">Links</th>
3334
</tr>
3435
</thead>
@@ -54,6 +55,10 @@
5455
{{#if d.changes.image}}<i class="fas fa-check"
5556
style="color: var(--arch-success);"></i>{{/if}}
5657
</td>
58+
<td style="text-align: center;">
59+
{{#if d.changes.sessionDate}}<i class="fas fa-check"
60+
style="color: var(--arch-success);"></i>{{/if}}
61+
</td>
5762
<td style="text-align: center;">
5863
{{#if d.changes.links}}<i class="fas fa-check"
5964
style="color: var(--arch-success);"></i>{{/if}}

0 commit comments

Comments
 (0)