| 204 | } |
| 205 | |
| 206 | async updateMessage<p>(key: string, props: p) { |
| 207 | const rawMessageContainer = await Phelia.Storage.get(key); |
| 208 | if (!rawMessageContainer) { |
| 209 | throw TypeError(`Could not find a message with key ${key}.`); |
| 210 | } |
| 211 | |
| 212 | const container: PheliaMessageContainer = JSON.parse(rawMessageContainer); |
| 213 | |
| 214 | if (container.isEphemeral === true) { |
| 215 | throw TypeError("Ephemeral messages cannot be updated."); |
| 216 | } |
| 217 | |
| 218 | /** A hook to create some state for a component */ |
| 219 | function useState<t>(key: string): [t, (value: t) => void] { |
| 220 | return [ |
| 221 | container.state[key], |
| 222 | (newState: t) => (container.state[key] = newState), |
| 223 | ]; |
| 224 | } |
| 225 | |
| 226 | /** A hook to create a modal for a component */ |
| 227 | function useModal(): (title: string, props?: any) => Promise<void> { |
| 228 | return async () => null; |
| 229 | } |
| 230 | |
| 231 | const message = await render( |
| 232 | React.createElement(this.messageCache.get(container.name) as any, { |
| 233 | useState, |
| 234 | useModal, |
| 235 | props, |
| 236 | }) |
| 237 | ); |
| 238 | |
| 239 | await this.client.chat.update({ |
| 240 | ...message, |
| 241 | channel: container.channelID, |
| 242 | ts: container.ts, |
| 243 | }); |
| 244 | |
| 245 | await Phelia.Storage.set( |
| 246 | container.viewID, |
| 247 | JSON.stringify({ |
| 248 | message: JSON.stringify(message), |
| 249 | name: this.homeComponent.name, |
| 250 | state: container.state, |
| 251 | type: "home", |
| 252 | viewID: container.viewID, |
| 253 | user: container.user, |
| 254 | }) |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | registerComponents(components: (PheliaMessage | PheliaModal)[]) { |
| 259 | const pheliaComponents = loadMessagesFromArray(components); |