|
| 1 | +--- |
| 2 | +id: persist_input_text_in_localstorage |
| 3 | +sidebar_position: 6 |
| 4 | +slug: /guides/persist-input-text-in-localstorage/ |
| 5 | +title: Storing message drafts |
| 6 | +--- |
| 7 | + |
| 8 | +In this recipe, we would like to demonstrate how you can start storing unsent user's messages as drafts. The whole implementation turns around the use of `MessageInput`'s prop `getDefaultValue` and custom change event handler. We will store the messages in localStorage. |
| 9 | + |
| 10 | + |
| 11 | +## Building the draft storage logic |
| 12 | +Below, we have a simple logic to store all the message text drafts in a localStorage object under the key `@chat/drafts`. |
| 13 | + |
| 14 | +```ts |
| 15 | +const STORAGE_KEY = '@chat/drafts'; |
| 16 | + |
| 17 | +const getDrafts = () => JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}'); |
| 18 | + |
| 19 | +const removeDraft = (key: string) => { |
| 20 | + const drafts = getDrafts(); |
| 21 | + |
| 22 | + if (drafts[key]) { |
| 23 | + delete drafts[key]; |
| 24 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(drafts)) |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +const updateDraft = (key: string, value: string) => { |
| 29 | + const drafts = getDrafts(); |
| 30 | + |
| 31 | + if (!value) { |
| 32 | + delete drafts[key]; |
| 33 | + } else { |
| 34 | + drafts[key] = value |
| 35 | + } |
| 36 | + |
| 37 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(drafts)) |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +On top of this logic we build a hook that exposes the change handler functions for both thread and main `MessageInput` components as well as functions for `MessageInput`'s `getDefaultValue` prop. We also have to override the `MessageInput`'s default submit handler, because we want to remove the draft from storage when a message is sent. |
| 42 | + |
| 43 | +```ts |
| 44 | +import { ChangeEvent, useCallback } from 'react'; |
| 45 | +import { |
| 46 | + MessageToSend, |
| 47 | + useChannelActionContext, |
| 48 | + useChannelStateContext, |
| 49 | +} from 'stream-chat-react'; |
| 50 | +import type { |
| 51 | + Message |
| 52 | +} from 'stream-chat'; |
| 53 | + |
| 54 | +const STORAGE_KEY = '@chat/drafts'; |
| 55 | + |
| 56 | +const getDrafts = () => JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}'); |
| 57 | + |
| 58 | +const removeDraft = (key: string) => { |
| 59 | + const drafts = getDrafts(); |
| 60 | + |
| 61 | + if (drafts[key]) { |
| 62 | + delete drafts[key]; |
| 63 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(drafts)) |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +const updateDraft = (key: string, value: string) => { |
| 68 | + const drafts = getDrafts(); |
| 69 | + |
| 70 | + if (!value) { |
| 71 | + delete drafts[key]; |
| 72 | + } else { |
| 73 | + drafts[key] = value |
| 74 | + } |
| 75 | + |
| 76 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(drafts)) |
| 77 | +} |
| 78 | + |
| 79 | +// highlight-start |
| 80 | +const useDraftAPI = () => { |
| 81 | + const { channel, thread } = useChannelStateContext(); |
| 82 | + const { sendMessage } = useChannelActionContext(); |
| 83 | + |
| 84 | + const handleInputChange = useCallback((e: ChangeEvent<HTMLTextAreaElement>) => { |
| 85 | + updateDraft(channel.cid, e.target.value); |
| 86 | + }, [channel.cid]) |
| 87 | + |
| 88 | + const handleThreadInputChange = useCallback((e: ChangeEvent<HTMLTextAreaElement>) => { |
| 89 | + if (!thread) return; |
| 90 | + updateDraft(`${channel.cid}:${thread.id}`, e.target.value); |
| 91 | + }, [channel.cid, thread]); |
| 92 | + |
| 93 | + const getMainInputDraft = useCallback(() => { |
| 94 | + const drafts = getDrafts(); |
| 95 | + return drafts[channel.cid] || ''; |
| 96 | + }, [channel.cid]); |
| 97 | + |
| 98 | + const getThreadInputDraft = useCallback(() => { |
| 99 | + if (!thread) return; |
| 100 | + const drafts = getDrafts(); |
| 101 | + return drafts[`${channel.cid}:${thread.id}`] || ''; |
| 102 | + }, [channel.cid, thread]); |
| 103 | + |
| 104 | + const overrideSubmitHandler = useCallback( |
| 105 | + async (message: MessageToSend, channelCid: string, customMessageData?: Partial<Message>,) => { |
| 106 | + await sendMessage(message, customMessageData); |
| 107 | + const key = message.parent ? `${channelCid}:${message.parent.id}` : channelCid; |
| 108 | + removeDraft(key); |
| 109 | + }, [sendMessage]) |
| 110 | + |
| 111 | + return { |
| 112 | + getMainInputDraft, |
| 113 | + getThreadInputDraft, |
| 114 | + handleInputChange, |
| 115 | + handleThreadInputChange, |
| 116 | + overrideSubmitHandler, |
| 117 | + } |
| 118 | +} |
| 119 | +// highlight-end |
| 120 | +``` |
| 121 | + |
| 122 | +## Plugging it in |
| 123 | + |
| 124 | +Now it is time to access the API in our React component. The component has to be a descendant of `Channel` component, because `useDraftAPI` accesses the `ChannelStateContext` and `ChannelActionContext` through corresponding consumers. In our example we call this component `ChannelWindow`. |
| 125 | + |
| 126 | +```tsx |
| 127 | +import { ChannelFilters, ChannelOptions, ChannelSort, StreamChat } from 'stream-chat'; |
| 128 | +import { useDraftAPI } from './useDraftAPI'; |
| 129 | +import type { StreamChatGenerics } from './types'; |
| 130 | + |
| 131 | +const ChannelWindow = () => { |
| 132 | + const { |
| 133 | + getMainInputDraft, |
| 134 | + getThreadInputDraft, |
| 135 | + handleInputChange, |
| 136 | + handleThreadInputChange, |
| 137 | + overrideSubmitHandler, |
| 138 | + } = useDraftAPI() |
| 139 | + |
| 140 | + return ( |
| 141 | + <> |
| 142 | + <Window> |
| 143 | + <TruncateButton/> |
| 144 | + <ChannelHeader/> |
| 145 | + <MessageList/> |
| 146 | + <MessageInput |
| 147 | + // highlight-start |
| 148 | + additionalTextareaProps={{onChange: handleInputChange}} |
| 149 | + getDefaultValue={getMainInputDraft} |
| 150 | + overrideSubmitHandler={overrideSubmitHandler} |
| 151 | + // highlight-end |
| 152 | + focus |
| 153 | + /> |
| 154 | + </Window> |
| 155 | + <Thread additionalMessageInputProps={{ |
| 156 | + // highlight-start |
| 157 | + additionalTextareaProps: {onChange: handleThreadInputChange}, |
| 158 | + getDefaultValue: getThreadInputDraft, |
| 159 | + overrideSubmitHandler, |
| 160 | + // highlight-end |
| 161 | + }}/> |
| 162 | + </> |
| 163 | + ) |
| 164 | +} |
| 165 | + |
| 166 | +// In your application you will probably initiate the client in a React effect. |
| 167 | +const chatClient = StreamChat.getInstance<StreamChatGenerics>('<YOUR_API_KEY>'); |
| 168 | + |
| 169 | +// User your own filters, options, sort if needed |
| 170 | +const filters: ChannelFilters = { type: 'messaging', members: { $in: ['<YOUR_USER_ID>'] } }; |
| 171 | +const options: ChannelOptions = { state: true, presence: true, limit: 10 }; |
| 172 | +const sort: ChannelSort = { last_message_at: -1, updated_at: -1 }; |
| 173 | + |
| 174 | +const App = () => { |
| 175 | + return ( |
| 176 | + <Chat client={chatClient}> |
| 177 | + <ChannelList filters={filters} sort={sort} options={options} showChannelSearch/> |
| 178 | + <Channel> |
| 179 | + <ChannelWindow/> |
| 180 | + </Channel> |
| 181 | + </Chat> |
| 182 | + ); |
| 183 | +}; |
| 184 | +``` |
| 185 | + |
| 186 | +Now once you start typing, you should be able to see the drafts in the `localStorage` under the key `@chat/drafts`. Despite changing channels or threads, the unsent message text should be kept in the textarea. |
0 commit comments