Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 4x 39x 39x 39x 39x 39x 39x 39x 24x 24x 24x 24x 24x 20x 19x 24x 24x 3x 2x 3x 23x 21x 4x 4x 4x 4x 4x 4x 4x 6x 4x 4x 4x 4x 4x 2x 2x 6x 6x 2x 2x 2x 4x 4x 4x 4x 3x 3x 4x 4x 3x 3x 8x 6x 4x 39x | import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
// The full item record — same shape as an item from GET /v1/entities/{id}/items
// (the standard envelope plus a dynamic set of field values keyed by fieldId) —
// so it can be rendered with the same item-card component used everywhere else.
export interface FeedItem {
id: string
entityId: string
flowId?: string
createdAt: string
updatedAt?: string
createdBy?: string
likeCount?: number
likedByCurrentUser?: boolean
// Only present when the item's entity accepts comments
commentCount?: number
[fieldId: string]: unknown
}
export interface FeedPage {
items: FeedItem[]
nextToken: string | null
hasMore: boolean
}
// Recent-activity feed across the caller's subscribed public entities (see
// entities store's subscribeToEntity/unsubscribeFromEntity). A simple rolling
// list, not a notification inbox — no read/unread state.
export const useFeedStore = defineStore('feed', () => {
const items = ref<FeedItem[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
const nextToken = ref<string | null>(null)
const hasMore = ref(false)
let latestLoadMoreRequestId = 0
// Bumped by reset(): a feed fetch started before a flow switch must not
// write the previous flow's items once it resolves.
let generation = 0
async function fetchFeed() {
const requestGeneration = generation
loading.value = true
error.value = null
try {
const response = await api.get<FeedPage>('/v1/feed')
if (requestGeneration !== generation) return
items.value = response.data.items || []
nextToken.value = response.data.nextToken
hasMore.value = response.data.hasMore
} catch (err: any) {
if (requestGeneration !== generation) return
error.value = err.response?.data?.error || 'Failed to load feed'
throw err
} finally {
if (requestGeneration === generation) {
loading.value = false
}
}
}
/**
* The feed includes the active flow's private entities, so a flow switch
* drops it (and any request still in flight) until it is fetched again.
*/
function reset() {
generation++
latestLoadMoreRequestId++
items.value = []
loading.value = false
error.value = null
nextToken.value = null
hasMore.value = false
}
// Mirrors catalog.ts/entities.ts's loadMore: `loading` also covers this
// request so VirtualCardGrid's own scroll-triggered infinite loading (which
// checks :is-loading before firing another @load-more) doesn't fire twice.
async function loadMore() {
if (!hasMore.value || loading.value || !nextToken.value) return
const requestId = ++latestLoadMoreRequestId
loading.value = true
error.value = null
try {
const response = await api.get<FeedPage>('/v1/feed', {
params: { nextToken: nextToken.value }
})
Iif (requestId !== latestLoadMoreRequestId) return
items.value = [...items.value, ...(response.data.items || [])]
nextToken.value = response.data.nextToken
hasMore.value = response.data.hasMore
} catch (err: any) {
Iif (requestId !== latestLoadMoreRequestId) return
error.value = err.response?.data?.error || 'Failed to load more feed items'
throw err
} finally {
Eif (requestId === latestLoadMoreRequestId) {
loading.value = false
}
}
}
// Mutates a feed item in place (rather than replacing it) so displayItems'
// shallow copy in HomePage.vue — which shares the same item object
// references as `items` — reflects the change without a resync.
function markAsLiked(itemId: string) {
const item = items.value.find((i) => i.id === itemId)
if (item) {
item.likedByCurrentUser = true
item.likeCount = (item.likeCount ?? 0) + 1
}
}
function markAsUnliked(itemId: string) {
const item = items.value.find((i) => i.id === itemId)
if (item) {
item.likedByCurrentUser = false
item.likeCount = Math.max(0, (item.likeCount ?? 0) - 1)
}
}
function adjustCommentCount(itemId: string, delta: number) {
const item = items.value.find((i) => i.id === itemId)
if (item && typeof item.commentCount === 'number') {
item.commentCount = Math.max(0, item.commentCount + delta)
}
}
return {
items,
loading,
error,
hasMore,
fetchFeed,
reset,
loadMore,
markAsLiked,
markAsUnliked,
adjustCommentCount
}
})
|