2023-07-02 05:08:42 +00:00
|
|
|
import type { Shout, LoadShoutsOptions } from '../../graphql/types.gen'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2023-07-02 05:08:42 +00:00
|
|
|
import { createSignal } from 'solid-js'
|
|
|
|
|
2023-11-14 15:10:00 +00:00
|
|
|
import { apiClient } from '../../utils/apiClient'
|
|
|
|
|
2023-07-02 05:08:42 +00:00
|
|
|
export type LayoutType = 'article' | 'audio' | 'video' | 'image' | 'literature'
|
|
|
|
|
|
|
|
const [sortedLayoutShouts, setSortedLayoutShouts] = createSignal<Map<LayoutType, Shout[]>>(new Map())
|
|
|
|
|
|
|
|
const addLayoutShouts = (layout: LayoutType, shouts: Shout[]) => {
|
|
|
|
setSortedLayoutShouts((prevSorted: Map<LayoutType, Shout[]>) => {
|
|
|
|
const siblings = prevSorted.get(layout)
|
|
|
|
if (siblings) {
|
|
|
|
const uniqued = [...new Set([...siblings, ...shouts])]
|
|
|
|
prevSorted.set(layout, uniqued)
|
|
|
|
}
|
|
|
|
return prevSorted
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const resetSortedLayoutShouts = () => {
|
|
|
|
setSortedLayoutShouts(new Map())
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadLayoutShoutsBy = async (options: LoadShoutsOptions): Promise<{ hasMore: boolean }> => {
|
|
|
|
const newLayoutShouts = await apiClient.getShouts({
|
|
|
|
...options,
|
2023-11-14 15:10:00 +00:00
|
|
|
limit: options.limit + 1,
|
2023-07-02 05:08:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const hasMore = newLayoutShouts.length === options.limit + 1
|
|
|
|
|
|
|
|
if (hasMore) {
|
|
|
|
newLayoutShouts.splice(-1)
|
|
|
|
}
|
|
|
|
addLayoutShouts(options.filters.layout as LayoutType, newLayoutShouts)
|
|
|
|
|
|
|
|
return { hasMore }
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useLayoutsStore = (layout: LayoutType, initialData: Shout[]) => {
|
|
|
|
addLayoutShouts(layout, initialData || [])
|
|
|
|
|
|
|
|
return {
|
|
|
|
addLayoutShouts,
|
|
|
|
sortedLayoutShouts,
|
2023-11-14 15:10:00 +00:00
|
|
|
loadLayoutShoutsBy,
|
2023-07-02 05:08:42 +00:00
|
|
|
}
|
|
|
|
}
|