webapp/src/stores/zine/layouts.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-11-28 13:18:25 +00:00
import type { Shout, LoadShoutsOptions } from '../../graphql/schema/core.gen'
import { createSignal } from 'solid-js'
2023-11-28 13:18:25 +00:00
import { apiClient } from '../../graphql/client/core'
export type LayoutType = 'article' | 'audio' | 'video' | 'image' | 'literature'
const [sortedLayoutShouts, setSortedLayoutShouts] = createSignal<Map<LayoutType, Shout[]>>(new Map())
2023-11-28 15:36:00 +00:00
const addLayoutShouts = (layouts: LayoutType[], shouts: Shout[]) => {
setSortedLayoutShouts((prevSorted: Map<LayoutType, Shout[]>) => {
2023-11-28 15:36:00 +00:00
layouts.forEach((layout: LayoutType) => {
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 }> => {
2023-11-29 08:23:08 +00:00
options.limit += 1
const newLayoutShouts = await apiClient.getShouts({ options })
const hasMore = newLayoutShouts.length === options.limit + 1
if (hasMore) {
newLayoutShouts.splice(-1)
}
2023-11-28 15:36:00 +00:00
addLayoutShouts(options.filters.layouts as LayoutType[], newLayoutShouts)
return { hasMore }
}
2023-11-28 15:36:00 +00:00
export const useLayoutsStore = (layouts: LayoutType[], initialData: Shout[]) => {
addLayoutShouts(layouts, initialData || [])
return {
addLayoutShouts,
sortedLayoutShouts,
loadLayoutShoutsBy,
}
}