Added illogical relation warning.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup>
|
||||
import { format } from 'date-fns';
|
||||
import { format, startOfDay, startOfMonth, startOfYear } from 'date-fns';
|
||||
import { computed, onMounted, ref, useId } from 'vue';
|
||||
|
||||
import Dialog from '#/components/dialog/dialog.vue';
|
||||
@@ -47,6 +47,43 @@ const options = computed(() => props.types.flatMap(({ type, symmetric }) => (sym
|
||||
const hasQuery = computed(() => query.value.trim().length > 0);
|
||||
const showResults = computed(() => results.value.length > 0 || (hasQuery.value && !searching.value && searchCompleted.value));
|
||||
|
||||
// derivations and compilations are released after what they're based on, the source of the relation should not predate the target
|
||||
const chronologicalTypes = ['rerelease', 'remaster', 'compilation'];
|
||||
const datePrecisions = { year: startOfYear, month: startOfMonth };
|
||||
|
||||
// compare at the coarsest precision of the two, a scene known only by its year isn't necessarily earlier than one from later that year
|
||||
function getComparableDates(sceneA, sceneB) {
|
||||
const precision = ['year', 'month'].find((datePrecision) => [sceneA.datePrecision, sceneB.datePrecision].includes(datePrecision));
|
||||
const truncate = datePrecisions[precision] || startOfDay;
|
||||
|
||||
return [truncate(new Date(sceneA.date)), truncate(new Date(sceneB.date))];
|
||||
}
|
||||
|
||||
// only a hint, the dates may be wrong or the relation unusual, so it never blocks
|
||||
const inversion = computed(() => {
|
||||
if (!selectedScene.value || !selectedOption.value || !chronologicalTypes.includes(selectedOption.value.type)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { type, direction } = selectedOption.value;
|
||||
const [sourceScene, targetScene] = direction === 'target' ? [props.scene, selectedScene.value] : [selectedScene.value, props.scene];
|
||||
|
||||
if (!sourceScene.date || !targetScene.date) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [sourceDate, targetDate] = getComparableDates(sourceScene, targetScene);
|
||||
|
||||
if (sourceDate >= targetDate) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
isCurrentEarlier: sourceScene === props.scene,
|
||||
option: options.value.find((option) => option.type === type && option.direction === (direction === 'target' ? 'source' : 'target')),
|
||||
};
|
||||
});
|
||||
|
||||
const canSubmit = computed(() => !!selectedScene.value && !!selectedOption.value);
|
||||
const hasChanges = computed(() => !!selectedScene.value || !!selectedOption.value);
|
||||
|
||||
@@ -251,6 +288,22 @@ onMounted(() => focusInput());
|
||||
</template>
|
||||
</VDropdown>
|
||||
|
||||
<p
|
||||
v-if="inversion"
|
||||
class="inversion"
|
||||
>
|
||||
<Icon icon="warning2" />
|
||||
|
||||
<span>
|
||||
The source scene was released {{ inversion.isCurrentEarlier ? 'before' : 'after' }} the target scene, did you mean
|
||||
<button
|
||||
type="button"
|
||||
class="inversion-option"
|
||||
@click="selectedOption = inversion.option"
|
||||
>{{ inversion.option.label }}</button>?
|
||||
</span>
|
||||
</p>
|
||||
|
||||
<div class="dialog-actions">
|
||||
<button
|
||||
:disabled="!canSubmit"
|
||||
@@ -275,6 +328,35 @@ onMounted(() => focusInput());
|
||||
}
|
||||
}
|
||||
|
||||
.inversion {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
padding: .5rem;
|
||||
margin: 0;
|
||||
border-radius: .25rem;
|
||||
background: var(--warn);
|
||||
color: var(--text-light);
|
||||
font-size: .9rem;
|
||||
line-height: 1.25;
|
||||
|
||||
.icon {
|
||||
flex-shrink: 0;
|
||||
fill: var(--text-light);
|
||||
}
|
||||
}
|
||||
|
||||
.inversion-option {
|
||||
padding: 0;
|
||||
border: none;
|
||||
background: none;
|
||||
color: inherit;
|
||||
font: inherit;
|
||||
font-weight: bold;
|
||||
text-decoration: underline;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.dialog-actions {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
|
||||
Reference in New Issue
Block a user