fix: Fix flaky ratings on mobile (#7916)

This commit is contained in:
Michael Genson
2026-07-20 14:39:20 -05:00
committed by GitHub
parent 4979adafa3
commit 2bae99e731

View File

@@ -1,38 +1,36 @@
<template>
<div @click.prevent>
<!-- User Rating -->
<v-hover v-slot="{ isHovering, props: hoverProps }">
<v-rating
v-if="isOwnGroup && (userRating || isHovering || !ratingsLoaded)"
v-bind="hoverProps"
:model-value="userRating"
active-color="secondary"
color="secondary-lighten-3"
length="5"
:density="small ? 'compact' : 'default'"
:size="small ? 'x-small' : undefined"
hover
clearable
@update:model-value="updateRating(+$event)"
/>
<!-- Group Rating -->
<v-rating
v-else
v-bind="hoverProps"
:model-value="groupRating"
:half-increments="true"
active-color="grey-darken-1"
color="secondary-lighten-3"
length="5"
:density="small ? 'compact' : 'default'"
:size="small ? 'x-small' : undefined"
hover
/>
</v-hover>
<!-- User Rating: shows the group average until the user rates the recipe themselves -->
<v-rating
v-if="isOwnGroup"
:model-value="displayRating"
:active-color="showGroupAverage ? 'grey-darken-1' : 'secondary'"
color="secondary-lighten-3"
length="5"
:half-increments="showGroupAverage"
:density="small ? 'compact' : 'default'"
:size="small ? 'x-small' : undefined"
:hover="canHover"
:clearable="!!userRatingDisplayValue"
@update:model-value="updateRating(+$event)"
/>
<!-- Group Rating -->
<v-rating
v-else
:model-value="groupRating"
:half-increments="true"
active-color="grey-darken-1"
color="secondary-lighten-3"
length="5"
:density="small ? 'compact' : 'default'"
:size="small ? 'x-small' : undefined"
readonly
/>
</div>
</template>
<script setup lang="ts">
import { useMediaQuery } from "@vueuse/core";
import { useLoggedInState } from "~/composables/use-logged-in-state";
import { useUserSelfRatings } from "~/composables/use-users";
@@ -51,42 +49,61 @@ const props = withDefaults(defineProps<Props>(), {
});
const modelValue = defineModel<number>({ default: 0 });
const groupRating = computed(() => modelValue.value);
const { isOwnGroup } = useLoggedInState();
const { userRatings, setRating, ready: ratingsLoaded } = useUserSelfRatings();
const { userRatings, setRating } = useUserSelfRatings();
const userRating = computed(() => {
return userRatings.value.find(r => r.recipeId === props.recipeId)?.rating ?? undefined;
// on touch devices a tap fires mouseenter without a matching mouseleave, which leaves v-rating
// rendering the stuck hover value instead of the model value
const canHover = useMediaQuery("(hover: hover) and (pointer: fine)");
const userRating = computed<number | null>(() => {
return userRatings.value.find(r => r.recipeId === props.recipeId)?.rating ?? null;
});
// if a user unsets their rating, we don't want to fall back to the group rating since it's out of sync
const hideGroupRating = ref(!!userRating.value);
watch(
() => userRating.value,
() => {
if (userRating.value) {
hideGroupRating.value = true;
}
},
);
const localUserRating = ref(userRating.value);
const groupRating = computed(() => {
return hideGroupRating.value ? 0 : modelValue.value;
// while a write is in flight a refetch may still report the pre-click value, so ignore it
const pendingWrites = ref(0);
watch(userRating, (value) => {
if (!pendingWrites.value) {
localUserRating.value = value;
}
});
function updateRating(val?: number) {
const userRatingDisplayValue = computed(() => localUserRating.value ?? 0);
const showGroupAverage = computed(() => localUserRating.value === null);
const displayRating = computed(() => {
return showGroupAverage.value ? groupRating.value : userRatingDisplayValue.value;
});
async function updateRating(val?: number) {
if (!isOwnGroup.value) {
return;
}
if (val === userRating.value) {
val = 0;
// the group average can be a half value, but user ratings are always whole stars
let rating = Math.round(val ?? 0);
if (rating === localUserRating.value) {
rating = 0;
}
if (!props.emitOnly) {
setRating(props.slug, val || 0, null);
localUserRating.value = rating;
if (props.emitOnly) {
modelValue.value = rating;
return;
}
pendingWrites.value++;
try {
await setRating(props.slug, rating, null);
}
finally {
pendingWrites.value--;
}
modelValue.value = val ?? 0;
}
</script>