Skip to content

Commit

Permalink
#68 Added github emojis support.
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed Feb 3, 2022
1 parent dc335fd commit 591aa80
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/redux/api/github/getEmojis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { withCancellation } from "@/redux/utils";
import getClient from './getClient';
import { parseRateLimit } from "./utils";

/**
* Gets list of emojis
*/
export const getEmojis = () =>
withCancellation(async (signal) => {
const client = getClient();

const data = await client.emojis.get({
request: {
signal,
},
});

return {
data: data?.data,
rateLimit: parseRateLimit(data?.headers),
};
});
22 changes: 22 additions & 0 deletions src/redux/api/githubGQL/getEmojis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { withCancellation } from "@/redux/utils";
import getClient from './getClient';
import { parseRateLimit } from "./utils";

/**
* Gets list of emojis
*/
export const getEmojis = () =>
withCancellation(async (signal) => {
const client = getClient();

const data = await client.emojis.get({
request: {
signal,
},
});

return {
data: data?.data,
rateLimit: parseRateLimit(data?.headers),
};
});
39 changes: 39 additions & 0 deletions src/shared/components/GithubEmoje/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from "react";
import slice from '@/redux/modules/emojis';
import { makeId } from "@/shared/utils";
import PropTypes from 'prop-types';
import { useSelector } from "react-redux";
import styled from "styled-components";

const Image = styled.img`
height: 1em;
`;

const reg = /(:[\w\d_+]+:)/g;
const GithubEmoji = ({ text }) => {
const { items } = useSelector(slice.selectors.getState);

if (!text) {
return null;
}

if (!reg.test(text)) {
return text;
}

return text.split(reg).map((part) => {
const has = reg.test(part) && items[part.replaceAll(':', '')];

return has ? (<Image alt={part} src={has} key={makeId()} />) : part;
});
};

GithubEmoji.propTypes = {
text: PropTypes.string,
};

GithubEmoji.defaultProps = {
text: '',
};

export default GithubEmoji;

0 comments on commit 591aa80

Please sign in to comment.