Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cross-platform emoji compatibility #1449

Merged
merged 18 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"afterSign": "scripts/notarize.js"
},
"dependencies": {
"@discordapp/twemoji": "15.0.3",
"@electron/remote": "2.1.2",
"@primer/octicons-react": "19.11.0",
"axios": "1.7.4",
Expand Down
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/__mocks__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function setPlatform(platform: NodeJS.Platform) {
Object.defineProperty(process, 'platform', {
value: platform,
});
import * as helpers from '../utils/helpers';

export function mockDirectoryPath() {
jest.spyOn(helpers, 'getDirectoryPath').mockReturnValue('/mocked/dir/name');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/components/AccountNotifications.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { mockGitHubCloudAccount, mockSettings } from '../__mocks__/state-mocks';
import { ensureStableEmojis } from '../__mocks__/utils';
import { ensureStableEmojis, mockDirectoryPath } from '../__mocks__/utils';
import { AppContext } from '../context/App';
import { GroupBy } from '../types';
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
Expand All @@ -14,6 +14,7 @@ jest.mock('./RepositoryNotifications', () => ({
describe('components/AccountNotifications.tsx', () => {
beforeEach(() => {
ensureStableEmojis();
mockDirectoryPath();
});

it('should render itself - group notifications by repositories', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/AllRead.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { render } from '@testing-library/react';
import { ensureStableEmojis } from '../__mocks__/utils';
import { ensureStableEmojis, mockDirectoryPath } from '../__mocks__/utils';
import { AllRead } from './AllRead';

describe('components/AllRead.tsx', () => {
beforeEach(() => {
ensureStableEmojis();
mockDirectoryPath();
});

it('should render itself & its children', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/components/AllRead.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type FC, useMemo } from 'react';
import { Constants } from '../utils/constants';
import { EmojiText } from './EmojiText';

export const AllRead: FC = () => {
const emoji = useMemo(
Expand All @@ -12,9 +13,11 @@ export const AllRead: FC = () => {

return (
<div className="flex flex-1 flex-col items-center justify-center bg-white p-4 text-black dark:bg-gray-dark dark:text-white">
<h1 className="mt-2 mb-5 text-5xl">{emoji}</h1>
<div className="mt-2 mb-5 text-5xl">
<EmojiText text={emoji} />
</div>

<h2 className="mb-2 text-xl font-semibold">No new notifications.</h2>
<div className="mb-2 text-xl font-semibold">No new notifications.</div>
</div>
);
};
17 changes: 17 additions & 0 deletions src/components/EmojiText.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { render } from '@testing-library/react';
import { mockDirectoryPath } from '../__mocks__/utils';
import { EmojiText, type IEmojiText } from './EmojiText';

describe('components/icons/Emoji.tsx', () => {
beforeEach(() => {
mockDirectoryPath();
});

it('should render', () => {
const props: IEmojiText = {
text: '🍺',
};
const tree = render(<EmojiText {...props} />);
expect(tree).toMatchSnapshot();
});
});
37 changes: 37 additions & 0 deletions src/components/EmojiText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import path from 'node:path';
import twemoji from '@discordapp/twemoji';
import { type FC, useEffect, useRef } from 'react';
import { getDirectoryPath } from '../utils/helpers';

export interface IEmojiText {
text: string;
}

type TwemojiOptions = {
base: string;
size: string;
ext: string;
};

export const EmojiText: FC<IEmojiText> = ({ text }) => {
const ref = useRef(null);

useEffect(() => {
if (ref.current) {
ref.current.innerHTML = twemoji.parse(text, {
folder: 'svg',
ext: '.svg',
callback: (icon: string, options: TwemojiOptions, _variant: string) => {
const source = path.resolve(
getDirectoryPath(),
'../../node_modules/@discordapp/twemoji/dist',
);

return ''.concat(source, '/', options.size, '/', icon, options.ext);
},
});
}
}, [text]);

return <span ref={ref} />;
};
5 changes: 5 additions & 0 deletions src/components/Oops.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { render } from '@testing-library/react';
import { mockDirectoryPath } from '../__mocks__/utils';
import { Oops } from './Oops';

describe('components/Oops.tsx', () => {
beforeEach(() => {
mockDirectoryPath();
});

it('should render itself & its children', () => {
const mockError = {
title: 'Error title',
Expand Down
7 changes: 5 additions & 2 deletions src/components/Oops.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type FC, useMemo } from 'react';
import type { GitifyError } from '../types';
import { EmojiText } from './EmojiText';

interface IOops {
error: GitifyError;
Expand All @@ -13,9 +14,11 @@ export const Oops: FC<IOops> = ({ error }: IOops) => {

return (
<div className="flex flex-1 flex-col items-center justify-center p-4">
<h1 className="mt-2 mb-5 text-5xl">{emoji}</h1>
<div className="mt-2 mb-5 text-5xl">
<EmojiText text={`${emoji} foo`} />
</div>

<h2 className="mb-2 text-xl font-semibold">{error.title}</h2>
<div className="mb-2 text-xl font-semibold">{error.title}</div>
{error.descriptions.map((description, i) => {
return (
// biome-ignore lint/suspicious/noArrayIndexKey: using index for key to keep the error constants clean
Expand Down
70 changes: 50 additions & 20 deletions src/components/__snapshots__/AccountNotifications.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading