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

Function useMarkdownInput

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

Source from the content-addressed store, hash-verified

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

Callers 1

MarkdownInputFunction · 0.90

Calls 12

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
replaceWordMethod · 0.80
onReplaceUploadMethod · 0.80
onUpdateFunction · 0.70

Tested by

no test coverage detected