Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap usage of useSensitiveMedia with computed #2982

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions frontend/src/components/VAudioThumbnail/VAudioThumbnail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</template>

<script lang="ts">
import { ref, onMounted, defineComponent, PropType } from "vue"
import { toRefs, ref, onMounted, defineComponent, PropType } from "vue"

import { rand, hash } from "~/utils/prng"
import { lerp, dist, bezier, Point } from "~/utils/math"
Expand All @@ -60,7 +60,8 @@ export default defineComponent({
},
},
setup(props) {
const { isHidden: shouldBlur } = useSensitiveMedia(props.audio)
const { audio } = toRefs(props)
const { isHidden: shouldBlur } = useSensitiveMedia(audio)

const i18n = useI18n()
const helpText = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</template>

<script lang="ts">
import { defineComponent, PropType } from "vue"
import { toRefs, defineComponent, PropType } from "vue"

import type { AudioDetail } from "~/types/media"

Expand All @@ -42,7 +42,8 @@ export default defineComponent({
},
},
setup(props) {
const { isHidden: shouldBlur } = useSensitiveMedia(props.audio)
const { audio } = toRefs(props)
const { isHidden: shouldBlur } = useSensitiveMedia(audio)

return {
shouldBlur,
Expand Down
25 changes: 17 additions & 8 deletions frontend/src/composables/use-sensitive-media.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed } from "vue"
import { computed, unref, Ref } from "vue"

import type { Media } from "~/types/media"

Expand All @@ -7,13 +7,15 @@ import { useAnalytics } from "~/composables/use-analytics"

import type { SensitiveMediaVisibility } from "~/constants/content-safety"

type SensitiveFields = Pick<Media, "id" | "sensitivity" | "isSensitive">

/**
* A helper composable for working with sensitive media.
* Provides the current visibility of a sensitive media,
* along with toggles for hiding/revealing the media.
*/
export function useSensitiveMedia(
media: Pick<Media, "id" | "sensitivity" | "isSensitive"> | null
rawMedia: SensitiveFields | Ref<SensitiveFields> | null
) {
const uiStore = useUiStore()
const { sendCustomEvent } = useAnalytics()
Expand All @@ -24,6 +26,7 @@ export function useSensitiveMedia(
* updated by user interactions.
*/
const visibility = computed<SensitiveMediaVisibility>(() => {
const media = unref(rawMedia)
if (!media) {
return "non-sensitive"
} else if (media.isSensitive) {
Expand All @@ -40,6 +43,7 @@ export function useSensitiveMedia(
})

function reveal() {
const media = unref(rawMedia)
if (media && !uiStore.revealedSensitiveResults.includes(media.id)) {
uiStore.revealedSensitiveResults.push(media.id)
sendCustomEvent("UNBLUR_SENSITIVE_RESULT", {
Expand All @@ -50,6 +54,7 @@ export function useSensitiveMedia(
}

function hide() {
const media = unref(rawMedia)
if (!media) return
const index = uiStore.revealedSensitiveResults.indexOf(media.id)
if (index > -1) {
Expand All @@ -61,21 +66,25 @@ export function useSensitiveMedia(
})
}

const isHidden = computed(
() =>
const isHidden = computed(() => {
const media = unref(rawMedia)
return (
media &&
uiStore.shouldBlurSensitive &&
media.isSensitive &&
visibility.value === "sensitive-hidden"
)
)
})

const canBeHidden = computed(
() =>
const canBeHidden = computed(() => {
const media = unref(rawMedia)
return (
media &&
uiStore.shouldBlurSensitive &&
media.isSensitive &&
visibility.value === "sensitive-shown"
)
)
})

return { visibility, reveal, hide, isHidden, canBeHidden }
}