| 1 | import { |
| 2 | AutocompleteClipboardSnippet, |
| 3 | AutocompleteSnippet, |
| 4 | AutocompleteSnippetType, |
| 5 | } from "../snippets/types"; |
| 6 | |
| 7 | const MAX_CLIPBOARD_AGE = 5 * 60 * 1000; |
| 8 | |
| 9 | const isValidClipboardSnippet = ( |
| 10 | snippet: AutocompleteClipboardSnippet, |
| 11 | ): boolean => { |
| 12 | const currDate = new Date(); |
| 13 | |
| 14 | const isTooOld = |
| 15 | currDate.getTime() - new Date(snippet.copiedAt).getTime() > |
| 16 | MAX_CLIPBOARD_AGE; |
| 17 | |
| 18 | return !isTooOld; |
| 19 | }; |
| 20 | |
| 21 | export const isValidSnippet = (snippet: AutocompleteSnippet): boolean => { |
| 22 | if (snippet.content.trim() === "") return false; |
| 23 | |
| 24 | if (snippet.type === AutocompleteSnippetType.Clipboard) { |
| 25 | return isValidClipboardSnippet(snippet); |
| 26 | } |
| 27 | |
| 28 | return true; |
| 29 | }; |