|
| 1 | +import { useEffect, useReducer } from 'react' |
| 2 | +import type { SubscribeTypeToBitMap } from '@mx-space/api-client' |
| 3 | +import type { FC } from 'react' |
| 4 | + |
| 5 | +import { Input } from '~/components/ui/input/Input' |
| 6 | +import { useStateToRef } from '~/hooks/common/use-state-ref' |
| 7 | +import { toast } from '~/lib/toast' |
| 8 | +import { useAggregationData } from '~/providers/root/aggregation-data-provider' |
| 9 | +import { apiClient } from '~/utils/request' |
| 10 | + |
| 11 | +import { useSubscribeStatusQuery } from './hooks' |
| 12 | + |
| 13 | +interface SubscribeModalProps { |
| 14 | + onConfirm: () => void |
| 15 | + defaultTypes?: (keyof typeof SubscribeTypeToBitMap)[] |
| 16 | +} |
| 17 | + |
| 18 | +const subscibeTextMap: Record<string, string> = { |
| 19 | + post_c: '文章', |
| 20 | + note_c: '手记', |
| 21 | + say_c: '说说', |
| 22 | + recently_c: '速记', |
| 23 | +} |
| 24 | + |
| 25 | +const initialState = { |
| 26 | + email: '', |
| 27 | + types: { |
| 28 | + post_c: false, |
| 29 | + note_c: false, |
| 30 | + say_c: false, |
| 31 | + recently_c: false, |
| 32 | + }, |
| 33 | +} |
| 34 | + |
| 35 | +type Action = |
| 36 | + | { type: 'set'; data: Partial<typeof initialState> } |
| 37 | + | { type: 'reset' } |
| 38 | + |
| 39 | +const useFormData = () => { |
| 40 | + const [state, dispatch] = useReducer( |
| 41 | + (state: typeof initialState, payload: Action) => { |
| 42 | + switch (payload.type) { |
| 43 | + case 'set': |
| 44 | + return { ...state, ...payload.data } |
| 45 | + case 'reset': |
| 46 | + return initialState |
| 47 | + } |
| 48 | + }, |
| 49 | + { ...initialState }, |
| 50 | + ) |
| 51 | + return [state, dispatch] as const |
| 52 | +} |
| 53 | + |
| 54 | +export const SubscribeModal: FC<SubscribeModalProps> = ({ |
| 55 | + onConfirm, |
| 56 | + defaultTypes, |
| 57 | +}) => { |
| 58 | + const [state, dispatch] = useFormData() |
| 59 | + |
| 60 | + const stateRef = useStateToRef(state) |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (!defaultTypes || !defaultTypes.length) { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + dispatch({ |
| 68 | + type: 'set', |
| 69 | + data: { |
| 70 | + types: defaultTypes.reduce( |
| 71 | + (acc, type) => { |
| 72 | + // @ts-ignore |
| 73 | + acc[type] = true |
| 74 | + return acc |
| 75 | + }, |
| 76 | + { ...stateRef.current.types }, |
| 77 | + ), |
| 78 | + }, |
| 79 | + }) |
| 80 | + }, []) |
| 81 | + |
| 82 | + const query = useSubscribeStatusQuery() |
| 83 | + |
| 84 | + const handleSubList = async () => { |
| 85 | + const { email, types } = state |
| 86 | + await apiClient.subscribe.subscribe( |
| 87 | + email, |
| 88 | + // @ts-ignore |
| 89 | + Object.keys(types).filter((name) => state.types[name]) as any[], |
| 90 | + ) |
| 91 | + |
| 92 | + toast('订阅成功,谢谢你!', 'success') |
| 93 | + dispatch({ type: 'reset' }) |
| 94 | + onConfirm() |
| 95 | + } |
| 96 | + const aggregation = useAggregationData() |
| 97 | + if (!aggregation) return null |
| 98 | + const { |
| 99 | + seo: { title }, |
| 100 | + } = aggregation |
| 101 | + return ( |
| 102 | + <form action="#" onSubmit={handleSubList} className="flex flex-col gap-5"> |
| 103 | + <p className="text-gray-1 text-sm"> |
| 104 | + 欢迎订阅「{title} |
| 105 | + 」,我会定期推送最新的内容到你的邮箱。 |
| 106 | + </p> |
| 107 | + <Input |
| 108 | + type="text" |
| 109 | + placeholder="留下你的邮箱哦 *" |
| 110 | + required |
| 111 | + value={state.email} |
| 112 | + onChange={(e) => { |
| 113 | + dispatch({ type: 'set', data: { email: e.target.value } }) |
| 114 | + }} |
| 115 | + /> |
| 116 | + <div className="flex gap-10"> |
| 117 | + {Object.keys(state.types) |
| 118 | + .filter((type) => query.data?.allowTypes.includes(type as any)) |
| 119 | + .map((name) => ( |
| 120 | + <fieldset |
| 121 | + className="children:cursor-pointer inline-flex items-center text-lg" |
| 122 | + key={name} |
| 123 | + > |
| 124 | + <input |
| 125 | + className="checkbox-accent checkbox mr-2" |
| 126 | + type="checkbox" |
| 127 | + onChange={(e) => { |
| 128 | + dispatch({ |
| 129 | + type: 'set', |
| 130 | + data: { |
| 131 | + types: { |
| 132 | + ...state.types, |
| 133 | + [name]: e.target.checked, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }) |
| 137 | + }} |
| 138 | + // @ts-ignore |
| 139 | + checked={state.types[name]} |
| 140 | + id={name} |
| 141 | + /> |
| 142 | + <label htmlFor={name} className="text-shizuku"> |
| 143 | + {subscibeTextMap[name]} |
| 144 | + </label> |
| 145 | + </fieldset> |
| 146 | + ))} |
| 147 | + </div> |
| 148 | + |
| 149 | + <p className="text-gray-1 -mt-2 text-sm"> |
| 150 | + 或者你也可以通过{' '} |
| 151 | + <a href="/feed" className="text-green" target="_blank"> |
| 152 | + /feed |
| 153 | + </a>{' '} |
| 154 | + 订阅「{title}」的 RSS 流。 |
| 155 | + </p> |
| 156 | + <button className="btn-accent btn-md btn" disabled={!state.email}> |
| 157 | + 订阅 |
| 158 | + </button> |
| 159 | + </form> |
| 160 | + ) |
| 161 | +} |
0 commit comments