Skip to content

Commit

Permalink
Decoupled badge and notification context
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmit Goswami authored and Harmit Goswami committed Jan 15, 2025
1 parent ecb761a commit b1a6116
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 35 deletions.
22 changes: 17 additions & 5 deletions translate/src/context/BadgeTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { createContext, useContext } from 'react';
import { BadgeTooltipMessage, ShowBadgeTooltip } from './Notification';
import { createContext } from 'react';
import { useNotifications } from '~/hooks/useNotifications';

export type BadgeTooltipMessage = Readonly<{
badgeName: string | null;
badgeLevel: number | null;
}>;

export const BadgeTooltipMessage = createContext<BadgeTooltipMessage | null>(
null,
);

export const ShowBadgeTooltip = createContext<
(tooltip: BadgeTooltipMessage | null) => void
>(() => {});

export function BadgeTooltipProvider({
children,
}: {
children: React.ReactElement;
}) {
const badgeMessage = useContext(BadgeTooltipMessage);
const setBadgeMessage = useContext(ShowBadgeTooltip);
const { badgeMessage, setBadgeMessage } = useNotifications();

return (
<BadgeTooltipMessage.Provider value={badgeMessage}>
<ShowBadgeTooltip.Provider value={setBadgeMessage}>
<ShowBadgeTooltip.Provider value={(tooltip) => setBadgeMessage(tooltip)}>
{children}
</ShowBadgeTooltip.Provider>
</BadgeTooltipMessage.Provider>
Expand Down
26 changes: 4 additions & 22 deletions translate/src/context/Notification.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { createContext } from 'react';
import { useNotifications } from '../hooks/useNotification';
import { createContext } from 'react';
import { useNotifications } from '~/hooks/useNotifications';

type NotificationType = 'debug' | 'error' | 'info' | 'success' | 'warning';

Expand All @@ -16,35 +16,17 @@ export const ShowNotification = createContext<
(message: NotificationMessage | null) => void
>(() => {});

export type BadgeTooltipMessage = Readonly<{
badgeName: string | null;
badgeLevel: number | null;
}>;

export const BadgeTooltipMessage = createContext<BadgeTooltipMessage | null>(
null,
);

export const ShowBadgeTooltip = createContext<
(tooltip: BadgeTooltipMessage | null) => void
>(() => {});

export function NotificationProvider({
children,
}: {
children: React.ReactElement;
}) {
const { message, setMessage, badgeMessage, setBadgeMessage } =
useNotifications();
const { message, setMessage } = useNotifications();

return (
<NotificationMessage.Provider value={message}>
<ShowNotification.Provider value={setMessage}>
<BadgeTooltipMessage.Provider value={badgeMessage}>
<ShowBadgeTooltip.Provider value={setBadgeMessage}>
{children}
</ShowBadgeTooltip.Provider>
</BadgeTooltipMessage.Provider>
{children}
</ShowNotification.Provider>
</NotificationMessage.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useState, useEffect } from 'react';
import {
NotificationMessage,
BadgeTooltipMessage,
} from '../context/Notification';
import { NotificationMessage } from '~/context/Notification';
import { BadgeTooltipMessage } from '~/context/BadgeTooltip';

export function useNotifications() {
const [message, setMessage] = useState<NotificationMessage | null>(null);
const [badgeMessage, setBadgeMessage] = useState<BadgeTooltipMessage | null>(
null,
);

// If there's a notification in the DOM set by Django, show it.
// Note that we only show it once, and only when the UI has already
// been rendered, to make sure users do see it.
useEffect(() => {
const rootElt = document.getElementById('root');
if (rootElt?.dataset.notifications) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Localized } from '@fluent/react';
import React, { useCallback, useContext, useEffect, useRef } from 'react';

import { Location } from '~/context/Location';
import { ShowBadgeTooltip } from '~/context/Notification';
import { ShowBadgeTooltip } from '~/context/BadgeTooltip';
import { useAppDispatch, useAppSelector } from '~/hooks';

import { performAction, resetSelection, selectAll } from '../actions';
Expand Down
2 changes: 1 addition & 1 deletion translate/src/modules/editor/components/BadgeTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useCallback, useContext, useRef } from 'react';
import { Localized } from '@fluent/react';
import Pride from 'react-canvas-confetti/dist/presets/pride';

import { BadgeTooltipMessage, ShowBadgeTooltip } from '~/context/Notification';
import { BadgeTooltipMessage, ShowBadgeTooltip } from '~/context/BadgeTooltip';
import { useAppSelector } from '~/hooks';
import { useOnDiscard } from '~/utils';

Expand Down
3 changes: 2 additions & 1 deletion translate/src/modules/editor/hooks/useSendTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EntityView } from '~/context/EntityView';
import { FailedChecksData } from '~/context/FailedChecksData';
import { Locale } from '~/context/Locale';
import { Location } from '~/context/Location';
import { ShowNotification } from '~/context/Notification';
import { UnsavedActions } from '~/context/UnsavedChanges';
import { updateEntityTranslation } from '~/modules/entities/actions';
import { usePushNextTranslatable } from '~/modules/entities/hooks';
Expand All @@ -22,7 +23,7 @@ import { updateResource } from '~/modules/resource/actions';
import { updateStats } from '~/modules/stats/actions';
import { useAppDispatch, useAppSelector } from '~/hooks';
import { serializeEntry, getPlainMessage } from '~/utils/message';
import { ShowBadgeTooltip, ShowNotification } from '~/context/Notification';
import { ShowBadgeTooltip } from '~/context/BadgeTooltip';

/**
* Return a function to send a translation to the server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { EntityView } from '~/context/EntityView';
import { FailedChecksData } from '~/context/FailedChecksData';
import { HistoryData } from '~/context/HistoryData';
import { Location } from '~/context/Location';
import { ShowBadgeTooltip, ShowNotification } from '~/context/Notification';
import { ShowNotification } from '~/context/Notification';
import { ShowBadgeTooltip } from '~/context/BadgeTooltip';
import { updateEntityTranslation } from '~/modules/entities/actions';
import { usePushNextTranslatable } from '~/modules/entities/hooks';
import {
Expand Down

0 comments on commit b1a6116

Please sign in to comment.