remote-content.ts
2.23 KB
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
import { configureStore } from '@reduxjs/toolkit'
import { createSlice } from '@reduxjs/toolkit'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
import { parseHtml } from './html.ts'
interface SiteConfig {
name: string,
baseUrl: string,
}
export const configSlice = createSlice({
name: 'config',
initialState: {
sites: {},
},
reducers: {
setSiteConfig(state, { payload: { name, baseUrl } }) {
if (!state.sites[ name ]) state.sites[ name ] = {}
Object.assign(state.sites[ name ], { baseUrl })
}
},
selectors: {
getSiteBaseUrl: (state, name) => state.sites?.[ name ]?.baseUrl,
},
})
const baseQuery = fetchBaseQuery()
const siteBaseQuery = async (args, api, options) => {
const { site, url } = args
const baseUrl = getSiteBaseUrl(site)
return baseQuery({ ...args, url: `${baseUrl ? `${baseUrl}/` : ''}${url}` }, api, options)
}
export const sitePageSlice = createApi({
reducerPath: 'pages',
tagTypes: ['Page'],
keepUnusedDataFor: 60,
refetchOnReconnect: true,
refetchOnMountOrArgChange: true,
baseQuery: siteBaseQuery,
endpoints: builder => ({
getPage: builder.query({
query: (args) => {
const { site, page } = args
return {
site,
url: page,
method: 'GET',
responseHandler: 'text',
}
},
providesTags: (result, err, args) => {
const { site, page } = args
return [ { type: 'Page', id: { site, page } } ]
},
}),
}),
})
export const store = configureStore({
reducer: {
[ configSlice.reducerPath ]: configSlice.reducer,
[ sitePageSlice.reducerPath ]: sitePageSlice.reducer,
},
middleware: getDefaultMiddleware => getDefaultMiddleware().concat([
sitePageSlice.middleware,
]),
})
export const getSitePage = async (site: string, page: string) => {
const result = await store.dispatch(sitePageSlice.endpoints.getPage.initiate({ site, page }))
return result
}
export const setSiteConfig = (siteConfig: SiteConfig) => {
return store.dispatch(configSlice.actions.setSiteConfig(siteConfig))
}
export const getSiteBaseUrl = (name: string): string => {
return configSlice.selectors.getSiteBaseUrl(store.getState(), name)
}
setSiteConfig({ name: 'direct' })