MCPcopy Create free account
hub / github.com/dailydotdev/apps / useMarkdownInput

Function useMarkdownInput

packages/shared/src/hooks/input/useMarkdownInput.ts:114–667  ·  view source on GitHub ↗
({
  textareaRef,
  postId,
  sourceId,
  onSubmit,
  onValueUpdate,
  initialContent = '',
  enabledCommand = {},
  editCommentId,
  parentCommentId,
}: UseMarkdownInputProps)

Source from the content-addressed store, hash-verified

112};
113
114export const useMarkdownInput = ({
115 textareaRef,
116 postId,
117 sourceId,
118 onSubmit,
119 onValueUpdate,
120 initialContent = '',
121 enabledCommand = {},
122 editCommentId,
123 parentCommentId,
124}: UseMarkdownInputProps): UseMarkdownInput => {
125 const dirtyRef = useRef(false);
126 const textarea = textareaRef?.current;
127 const isLinkEnabled = enabledCommand[MarkdownCommand.Link];
128 const isUploadEnabled = enabledCommand[MarkdownCommand.Upload];
129 const isMentionEnabled = enabledCommand[MarkdownCommand.Mention];
130 const isEmojiEnabled = enabledCommand[MarkdownCommand.Emoji];
131 const isGifEnabled = enabledCommand[MarkdownCommand.Gif];
132 const [command, setCommand] = useState<TextareaCommand | null>(null);
133
134 // Generate unique storage key for this comment context
135 const draftStorageKey = useMemo(() => {
136 if (!postId) {
137 return null;
138 }
139 const identifier = editCommentId || parentCommentId || postId;
140 return generateStorageKey(StorageTopic.Comment, 'draft', identifier);
141 }, [postId, editCommentId, parentCommentId]);
142
143 // Load draft from storage or use initialContent
144 const getInitialValue = useCallback(() => {
145 if (initialContent) {
146 return initialContent;
147 }
148 if (!draftStorageKey) {
149 return '';
150 }
151
152 const savedDraft = storageWrapper.getItem(draftStorageKey);
153 return savedDraft || '';
154 }, [initialContent, draftStorageKey]);
155
156 const [input, setInput] = useState(getInitialValue);
157 const [query, setQuery] = useState<string | undefined>(undefined);
158 const [emojiQuery, setEmojiQuery] = useState<string | undefined>(undefined);
159 const [offset, setOffset] = useState([0, 0]);
160 const [selected, setSelected] = useState(0);
161 const [selectedEmoji, setSelectedEmoji] = useState(0);
162 const { requestMethod } = useRequestProtocol();
163 const key = ['user', query, postId, sourceId];
164 const { user } = useAuthContext();
165 const { displayToast } = useToastNotification();
166 const saveTimeoutRef = useRef<ReturnType<typeof setTimeout>>();
167
168 const getCommand = useCallback((): TextareaCommand => {
169 if (!command) {
170 throw new Error('Markdown command is not initialized');
171 }

Callers 2

MarkdownInputFunction · 0.90
renderMarkdownInputHookFunction · 0.90

Calls 13

generateStorageKeyFunction · 0.90
useRequestProtocolFunction · 0.90
useAuthContextFunction · 0.90
useToastNotificationFunction · 0.90
useSyncUploaderFunction · 0.90
getTemporaryUploadStringFunction · 0.90
getCursorTypeFunction · 0.90
setInputFunction · 0.85
displayToastFunction · 0.85
requestMethodFunction · 0.85
replaceWordMethod · 0.80
onReplaceUploadMethod · 0.80

Tested by 1

renderMarkdownInputHookFunction · 0.72