convert type to union

This commit is contained in:
ewwwwwwww 2022-07-04 20:59:39 -07:00
parent 8d8cf53ac4
commit bd9a33201a
3 changed files with 11 additions and 10 deletions

View file

@ -20,7 +20,7 @@ const AutosuggestEmoji: React.FC<IAutosuggestEmoji> = ({ emoji }) => {
if (isCustomEmoji(emoji)) { if (isCustomEmoji(emoji)) {
url = emoji.imageUrl; url = emoji.imageUrl;
alt = emoji.colons; alt = emoji.colons;
} else if (isNativeEmoji(emoji)) { } else {
const mapping = unicodeMapping[emoji.native] || unicodeMapping[emoji.native.replace(/\uFE0F$/, '')]; const mapping = unicodeMapping[emoji.native] || unicodeMapping[emoji.native.replace(/\uFE0F$/, '')];
if (!mapping) { if (!mapping) {
@ -29,8 +29,6 @@ const AutosuggestEmoji: React.FC<IAutosuggestEmoji> = ({ emoji }) => {
url = joinPublicPath(`packs/emoji/${mapping.unified}.svg`); url = joinPublicPath(`packs/emoji/${mapping.unified}.svg`);
alt = emoji.native; alt = emoji.native;
} else {
return <div />
} }
return ( return (

View file

@ -21,22 +21,23 @@ import type { Emoji as EmojiMart, CustomEmoji as EmojiMartCustom } from 'emoji-m
* and one type that is used everywhere that the above two are converted into * and one type that is used everywhere that the above two are converted into
*/ */
export interface Emoji { export interface CustomEmoji {
id: string, id: string,
colons: string, colons: string,
custom?: boolean,
}
export interface CustomEmoji extends Emoji {
custom: true, custom: true,
imageUrl: string, imageUrl: string,
} }
export interface NativeEmoji extends Emoji { export interface NativeEmoji {
id: string,
colons: string,
custom?: boolean,
unified: string, unified: string,
native: string, native: string,
} }
export type Emoji = CustomEmoji | NativeEmoji;
export function isCustomEmoji(emoji: Emoji): emoji is CustomEmoji { export function isCustomEmoji(emoji: Emoji): emoji is CustomEmoji {
return (emoji as CustomEmoji).imageUrl !== undefined; return (emoji as CustomEmoji).imageUrl !== undefined;
} }

View file

@ -2,6 +2,8 @@ import { Index } from 'flexsearch';
import data from './data'; import data from './data';
import { Emoji as EmojiType } from './index';
import type { Emoji, CustomEmoji } from 'emoji-mart'; import type { Emoji, CustomEmoji } from 'emoji-mart';
const index = new Index({ const index = new Index({
@ -25,7 +27,7 @@ export const addCustomToPool = (customEmojis: Emoji<CustomEmoji>[]) => {
} }
}; };
const search = (str: string, options: searchOptions, custom_emojis: any) => { const search = (str: string, options: searchOptions, custom_emojis: any): EmojiType[] => {
return index.search(str, options.maxResults) return index.search(str, options.maxResults)
.flatMap(id => { .flatMap(id => {
if (Number.isInteger(id)) { if (Number.isInteger(id)) {