Skip to content

Commit

Permalink
fix: add addinitonal media and watched and sort order
Browse files Browse the repository at this point in the history
  • Loading branch information
sircharlo committed Nov 22, 2024
1 parent cbae2c8 commit 4436c05
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 27 deletions.
91 changes: 77 additions & 14 deletions src/helpers/jw-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,82 @@ const exportDayToFolder = async (
}
};

export const mapOrder =
(sortOrder: string | string[] | undefined) =>
(a: DynamicMediaObject, b: DynamicMediaObject) => {
try {
const key = 'uniqueId';
if (!sortOrder || sortOrder.length === 0) return 0;
return sortOrder.indexOf(a[key]) > sortOrder.indexOf(b[key]) ? 1 : -1;
} catch (e) {
errorCatcher(e);
return 0;
}
};

export const exportAllDays = async () => {
try {
const jwStore = useJwStore();
const currentStateStore = useCurrentStateStore();
if (
!currentStateStore.currentSettings?.enableMediaAutoExport ||
!currentStateStore.currentSettings?.mediaAutoExportFolder
)
return;
const daysToExport = (
jwStore.lookupPeriod[currentStateStore.currentCongregation] || []
).map((d) => {
return {
date: d.date,
dynamicMedia: d.dynamicMedia,
};
});
for (const day of daysToExport) {
const dateString = formatDate(day.date, 'YYYY/MM/DD');
day.dynamicMedia.push(
...((jwStore.additionalMediaMaps[dateString] ||
[]) as DynamicMediaObject[]),
);
day.dynamicMedia.push(
...((currentStateStore.watchFolderMedia[dateString] ||
[]) as DynamicMediaObject[]),
);
const seenFileUrls = new Set();
day.dynamicMedia = day.dynamicMedia
.filter((m) => {
if (!m.fileUrl || seenFileUrls.has(m.fileUrl)) {
return false;
}
seenFileUrls.add(m.fileUrl);
return true;
})
.sort(
mapOrder(
jwStore.mediaSort[currentStateStore.currentCongregation]?.[
dateString
] || [],
),
)
.sort((a, b) => {
const sectionOrder = [
'additional',
'tgw',
'ayfm',
'lac',
'wt',
'circuitOverseer',
];
return (
sectionOrder.indexOf(a.section) - sectionOrder.indexOf(b.section)
);
});
await exportDayToFolder(day.date, day.dynamicMedia);
}
} catch (error) {
errorCatcher(error);
}
};

const fetchMedia = async () => {
try {
const currentStateStore = useCurrentStateStore();
Expand Down Expand Up @@ -323,20 +399,7 @@ const fetchMedia = async () => {
}
}
await queue.onIdle();

const daysToExport =
jwStore.lookupPeriod[currentStateStore.currentCongregation]?.filter(
(d) => d.dynamicMedia.length,
) || [];
console.log('daysToExport', daysToExport);

// TODO: add other media here too (additionalMedia, watched)
// TODO: apply sort order before exporting

for (const day of daysToExport) {
console.log('day', day);
await exportDayToFolder(day.date, day.dynamicMedia);
}
exportAllDays();
} catch (error) {
errorCatcher(error);
}
Expand Down
24 changes: 11 additions & 13 deletions src/pages/MediaCalendarPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,10 @@ import { sorter } from 'src/helpers/general';
import {
addJwpubDocumentMediaToFiles,
downloadFileIfNeeded,
// exportAllDays,
fetchMedia,
getPublicationInfoFromDb,
mapOrder,
sanitizeId,
} from 'src/helpers/jw-media';
import {
Expand Down Expand Up @@ -697,19 +699,6 @@ watch(
},
);
const mapOrder =
(sortOrder: string | string[] | undefined) =>
(a: DynamicMediaObject, b: DynamicMediaObject) => {
try {
const key = 'uniqueId';
if (!sortOrder || sortOrder.length === 0) return 0;
return sortOrder.indexOf(a[key]) > sortOrder.indexOf(b[key]) ? 1 : -1;
} catch (e) {
errorCatcher(e);
return 0;
}
};
const updateMediaSortPlugin: DNDPlugin = (parent) => {
const parentData = parents.get(parent);
if (!parentData) return;
Expand Down Expand Up @@ -1223,6 +1212,15 @@ const sortedMediaIds = computed(() => {
].map((m) => m.uniqueId);
});
// const arraysAreIdentical = (a: string[], b: string[]) =>
// a.length === b.length && a.every((element, index) => element === b[index]);
// watch(sortedMediaIds, (newSortedMediaIds, oldSortedMediaIds) => {
// if (arraysAreIdentical(newSortedMediaIds, oldSortedMediaIds)) return;
// console.log('sortedMediaIds', newSortedMediaIds);
// exportAllDays();
// });
const lastPlayedMediaUniqueId = ref<string>('');
const nextMediaUniqueId = computed(() => {
Expand Down

0 comments on commit 4436c05

Please sign in to comment.