pleroma-fe/src/components/conversation/conversation.js

373 lines
11 KiB
JavaScript
Raw Normal View History

import { storeToRefs } from 'pinia'
2026-09-10 23:07:08 +03:00
import {
computed,
2026-09-16 15:54:02 +03:00
nextTick,
2026-09-10 23:07:08 +03:00
onUnmounted,
provide,
ref,
toRefs,
useTemplateRef,
watch,
} from 'vue'
2026-01-08 17:26:52 +02:00
import ChatMessageList from 'src/components/chat_message_list/chat_message_list.vue'
2026-07-21 18:25:56 +03:00
import PostStatusForm from 'src/components/post_status_form/post_status_form.vue'
import QuickFilterSettings from 'src/components/quick_filter_settings/quick_filter_settings.vue'
import QuickViewSettings from 'src/components/quick_view_settings/quick_view_settings.vue'
import RichContent from 'src/components/rich_content/rich_content.jsx'
2026-07-23 16:08:17 +03:00
import ThreadTree from 'src/components/thread_tree/thread_tree.vue'
2026-08-10 22:26:22 +03:00
import { useInterfaceStore } from 'src/stores/interface.js'
import { useMergedConfigStore } from 'src/stores/merged_config.js'
2026-01-29 20:40:00 +02:00
import { useConversation } from 'src/composables/useConversation.js'
2026-09-10 19:20:35 +03:00
import { useInterfaceSizes } from 'src/composables/useInterfaceSizes.js'
2026-09-09 19:41:45 +03:00
import { useScrollPosition } from 'src/composables/useScrollPosition.js'
import { useTreeConversationTopology } from 'src/composables/useTreeConversationTopology.js'
import { useVirtualScrolling } from 'src/composables/useVirtualScrolling.js'
2026-09-09 09:50:58 +03:00
2026-01-08 17:26:52 +02:00
import { library } from '@fortawesome/fontawesome-svg-core'
import {
faAngleDoubleDown,
faAngleDoubleLeft,
faChevronLeft,
2026-07-10 17:09:17 +03:00
faReply,
faTimes,
2026-01-08 17:26:52 +02:00
} from '@fortawesome/free-solid-svg-icons'
2026-07-21 18:25:56 +03:00
library.add(
faAngleDoubleDown,
faAngleDoubleLeft,
faChevronLeft,
faReply,
faTimes,
)
export default {
2026-06-30 14:26:24 +03:00
props: {
statusId: {
// Main thing
type: String,
required: true,
},
isPage: {
// Whether conversation is rendered as a standalone page
// as opposed to embedded into a timeline
type: Boolean,
default: false,
},
},
components: {
ThreadTree,
QuickFilterSettings,
QuickViewSettings,
ChatMessageList,
PostStatusForm,
RichContent,
2026-08-24 15:36:28 +03:00
},
2026-09-16 00:05:46 +03:00
emits: ['heightChange', 'suspendableStateChange', 'expanded', 'collapsed'],
2026-09-10 23:07:08 +03:00
setup(props, { emit }) {
2026-09-09 19:39:24 +03:00
const scroller = useScrollPosition()
2026-09-16 00:05:46 +03:00
const { statusId } = toRefs(props)
2026-09-09 18:22:18 +03:00
// # Main Configuration / global state
const { mergedConfig } = storeToRefs(useMergedConfigStore())
const displayStyle = computed(() => mergedConfig.value.conversationDisplay)
2026-09-08 17:17:24 +03:00
const { layoutType } = storeToRefs(useInterfaceStore())
const mobileLayout = computed(() => layoutType.value === 'mobile')
// # Conversation Expansion
const expanded = ref(false)
const { isPage } = toRefs(props)
const isExpanded = computed(() => !!(expanded.value || isPage.value))
2026-09-16 00:05:46 +03:00
const toggleExpanded = async () => {
const newVal = !expanded.value
if (newVal) {
virtualScrollingEnabled.value = newVal
await nextTick()
expanded.value = newVal
} else {
expanded.value = newVal
await nextTick()
virtualScrollingEnabled.value = newVal
}
}
provide('isExpanded', isExpanded)
provide('isPage', isPage)
2026-09-09 18:22:18 +03:00
provide('expandable', true)
2026-09-16 15:54:02 +03:00
watch(expanded, (val) => (val ? emit('expanded') : emit('collapsed')), {
flush: 'post',
})
// # Main things
const {
focusedId,
2026-09-16 00:05:46 +03:00
conversationId,
setFocused,
currentStatus,
conversation,
replies,
getReplies,
fetchConversation,
loadError,
} = useConversation(statusId, isExpanded)
2026-09-15 01:11:30 +03:00
const conversationLite = computed(() =>
conversation.value.map(({ id }) => ({ id })),
)
2026-09-16 20:44:41 +03:00
provide('focusedId', focusedId)
provide('conversation', conversation)
provide('replies', replies)
2026-09-08 17:17:24 +03:00
// Component created
if (isPage.value) {
fetchConversation()
}
// # Misc UI things
const firstStatus = computed(() => conversation.value[0])
const lastStatus = computed(
() => conversation.value[conversation.value.legnth - 1],
)
const getStatusClasses = (statusId, ancestor) => {
2026-09-11 18:17:59 +03:00
const result = {
'-first': statusId === firstStatus.value?.id,
'-last': statusId === lastStatus.value?.id,
2026-09-11 18:17:59 +03:00
}
if (ancestor) {
result['-ancestor'] = true
result['-fade'] = mergedConfig.value.conversationTreeFadeAncestors
}
return result
}
2026-09-16 14:39:56 +03:00
// # External virtual scrolling
2026-09-10 23:07:08 +03:00
const unsuspendableIds = ref(new Set())
const suspendable = computed(
() => !isExpanded.value && unsuspendableIds.value.size === 0,
)
const rootElement = useTemplateRef('root')
2026-09-16 00:05:46 +03:00
const rootElementMargin = ref(null)
2026-09-10 23:07:08 +03:00
const updateVirtualHeight = (e) => {
const [entry] = e
2026-09-16 00:05:46 +03:00
const rootCss = window.getComputedStyle(rootElement.value)
const rootMarginString = rootCss.getPropertyValue('margin-top')
const rootMargin = Number.parseInt(rootMarginString.slice(0, -2), 10)
rootElementMargin.value = rootMargin
2026-09-10 23:07:08 +03:00
emit('heightChange', {
id: statusId.value,
height: entry.contentRect.height,
element: rootElement,
})
}
const resizeObserver = ref(new ResizeObserver(updateVirtualHeight))
watch(rootElement, () => resizeObserver.value.observe(rootElement.value))
watch(suspendable, (value) =>
emit('suspendableStateChange', { suspendable: value, id: statusId.value }),
2026-09-10 23:07:08 +03:00
)
onUnmounted(() => resizeObserver.value.disconnect())
2026-09-16 14:39:56 +03:00
// # Internal virtual scrolling
2026-09-16 00:05:46 +03:00
const virtualScrollingEnabled = ref(isExpanded.value)
2026-09-10 23:07:08 +03:00
2026-09-10 16:15:19 +03:00
// Placeholder heights.
2026-09-16 00:05:46 +03:00
const { fontSize, navbarSize, panelHeaderSize } = useInterfaceSizes()
2026-09-10 16:15:19 +03:00
const normalStatusHeight = computed(() => fontSize.value * 10)
2026-09-16 14:19:44 +03:00
const getPlaceholderHeight = (id) => normalStatusHeight
2026-09-16 00:05:46 +03:00
const offset = computed(() => {
// The fontsize after navbar is the little gap between navbar and content
if (isPage.value) {
return navbarSize.value + fontSize.value + panelHeaderSize.value
} else if (expanded.value) {
2026-09-16 15:54:02 +03:00
return (
navbarSize.value +
fontSize.value +
panelHeaderSize.value * 2 +
rootElementMargin.value
)
2026-09-16 00:05:46 +03:00
} else {
return navbarSize.value + fontSize.value
}
})
2026-09-10 16:15:19 +03:00
// # Linear style stuff
const isLinearView = computed(() => displayStyle.value !== 'tree')
const linearElement = useTemplateRef('linear')
2026-09-15 01:11:30 +03:00
const linearScrollCompensation = computed(
2026-09-16 00:05:46 +03:00
() => virtualScrollingEnabled.value && isLinearView.value,
2026-09-15 01:11:30 +03:00
)
const {
heightChart: heightChartLinear,
changeSuspendState: changeSuspendStateLinear,
updateVirtualHeight: updateVirtualHeightLinear,
reset: resetLinearScrollVirtualization,
2026-09-16 00:05:46 +03:00
scrollTo: linearScrollTo,
2026-09-10 16:15:19 +03:00
} = useVirtualScrolling({
2026-09-16 00:05:46 +03:00
name: 'Linear',
enabled: linearScrollCompensation,
list: conversationLite,
2026-09-10 16:15:19 +03:00
body: linearElement,
2026-09-16 00:05:46 +03:00
offset,
2026-09-10 16:15:19 +03:00
scrollPositionInstance: scroller,
scrollCompensation: linearScrollCompensation,
getPlaceholderHeight,
})
2026-09-10 23:07:08 +03:00
const changeSuspendStateLinearLocal = (e) => {
changeSuspendStateLinear(e)
const { id, suspendable } = e
if (suspendable) {
2026-09-10 23:07:08 +03:00
unsuspendableIds.value.delete(id)
2026-09-16 00:31:47 +03:00
} else {
unsuspendableIds.value.add(id)
2026-09-10 23:07:08 +03:00
}
}
2025-02-04 15:23:21 +02:00
// # Tree style stuff
const isTreeView = computed(() => displayStyle.value === 'tree')
const {
topLevel,
currentAncestors,
threadDisplay,
showThreadRecursively,
resetThreadDisplay,
} = useTreeConversationTopology(conversation, replies, focusedId)
provide('threadDisplay', threadDisplay)
watch(
isExpanded,
(value) => {
if (!value) resetThreadDisplay()
},
{ flush: 'post' },
)
2026-09-15 01:11:30 +03:00
const currentAncestorsLite = computed(() =>
currentAncestors.value.map(({ id }) => ({ id })),
)
const ancestorsElement = useTemplateRef('ancestors')
2026-09-15 01:11:30 +03:00
const treeScrollCompensation = computed(
2026-09-16 00:05:46 +03:00
() => virtualScrollingEnabled.value && isTreeView.value,
2026-09-15 01:11:30 +03:00
)
const {
heightChart: heightChartAncestors,
changeSuspendState: changeSuspendStateAncestors,
updateVirtualHeight: updateVirtualHeightAncestors,
reset: resetTreeScrollVirtualization,
2026-09-10 16:15:19 +03:00
} = useVirtualScrolling({
2026-09-16 00:05:46 +03:00
name: 'Ancestors',
enabled: treeScrollCompensation,
list: currentAncestorsLite,
2026-09-10 16:15:19 +03:00
body: ancestorsElement,
2026-09-16 00:05:46 +03:00
offset,
2026-09-10 16:15:19 +03:00
scrollPositionInstance: scroller,
scrollCompensation: treeScrollCompensation,
2026-09-11 18:17:23 +03:00
collapseMode: 'height',
2026-09-10 16:15:19 +03:00
getPlaceholderHeight,
})
2026-09-10 23:07:08 +03:00
const changeSuspendStateAncestorsLocal = (e) => {
changeSuspendStateAncestors(e)
const { id, suspendable } = e
if (suspendable) {
2026-09-10 23:07:08 +03:00
unsuspendableIds.value.delete(id)
} else {
unsuspendableIds.value.add(id)
2026-09-10 23:07:08 +03:00
}
}
const changeSuspendStateCurrentLevelLocal = (e) => {
const { id, suspendable } = e
if (suspendable) {
2026-09-10 23:07:08 +03:00
unsuspendableIds.value.delete(id)
} else {
unsuspendableIds.value.add(id)
2026-09-10 23:07:08 +03:00
}
}
2026-09-16 00:05:46 +03:00
watch(conversationId, (neu, old) => {
resetLinearScrollVirtualization()
resetTreeScrollVirtualization()
})
const treeViewIsSimple = computed(
() => !mergedConfig.value.conversationTreeAdvanced,
)
const shouldShowAllConversationButton = computed(
() => currentAncestors.value.length > 0 && topLevel.value.length > 1,
)
const shouldShowAncestors = computed(
() => isExpanded.value && heightChartAncestors.value.length > 0,
)
2026-09-08 17:17:24 +03:00
// # Scrolling
2026-09-16 00:05:46 +03:00
const scrollTo = (ids) => {
2026-09-16 00:31:47 +03:00
if (isLinearView.value) {
2026-09-16 00:05:46 +03:00
return linearScrollTo(ids)
}
}
2026-09-16 14:39:30 +03:00
const diveIntoStatus = (id) => setFocused(id)
const diveToTopLevel = () => setFocused(currentAncestors.value[0].id)
2026-09-16 03:06:29 +03:00
watch(focusedId, async (neu, old) => {
2026-09-16 12:39:05 +03:00
// Ignoring initial update (null -> id) since that is handled by scroll compensation
if (old && neu) scrollTo(new Set([neu]))
2026-09-16 00:05:46 +03:00
})
return {
2026-09-08 17:17:24 +03:00
// # Misc
loadError,
2026-09-08 17:17:24 +03:00
mobileLayout,
2026-09-09 18:22:18 +03:00
// # Conversation Expansion
isPage,
isExpanded,
toggleExpanded,
2026-09-08 17:17:24 +03:00
// # Focus
focusedId,
setFocused,
2026-09-08 17:17:24 +03:00
// # Main things
conversation,
currentStatus,
2026-09-08 17:21:06 +03:00
getReplies,
2026-09-08 17:17:24 +03:00
// # Misc UI things
getStatusClasses,
2026-09-08 17:17:24 +03:00
// # Linear style stuff
isLinearView,
// ## Linear virtual scrolling
heightChartLinear,
2026-09-10 23:07:08 +03:00
changeSuspendStateLinearLocal,
updateVirtualHeightLinear,
2026-09-08 17:17:24 +03:00
// # Tree style stuff
isTreeView,
// ## Tree virtual scrolling
heightChartAncestors,
2026-09-10 23:07:08 +03:00
changeSuspendStateAncestorsLocal,
updateVirtualHeightAncestors,
2026-09-10 23:07:08 +03:00
changeSuspendStateCurrentLevelLocal,
2026-09-08 17:17:24 +03:00
// ## Tree state
// ### Topology
topLevel,
currentAncestors,
2026-09-08 17:17:24 +03:00
// ### Thread Display
showThreadRecursively,
// ### Derived values and config
2026-09-08 17:17:24 +03:00
treeViewIsSimple,
shouldShowAllConversationButton,
2026-09-08 17:17:24 +03:00
shouldShowAncestors,
// # Scrolling
diveToTopLevel,
diveIntoStatus,
2026-09-16 00:31:47 +03:00
unsuspendableIds,
}
},
}