mirror of
https://github.com/outline/outline.git
synced 2026-11-23 06:11:04 +03:00
109 lines
3.1 KiB
TypeScript
109 lines
3.1 KiB
TypeScript
import { observer } from "mobx-react";
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import {
|
|
Drawer,
|
|
DrawerContent,
|
|
DrawerTitle,
|
|
DrawerTrigger,
|
|
} from "~/components/primitives/Drawer";
|
|
import {
|
|
Popover,
|
|
PopoverTrigger,
|
|
PopoverContent,
|
|
} from "~/components/primitives/Popover";
|
|
import useConsumeQueryParam from "~/hooks/useConsumeQueryParam";
|
|
import useMobile from "~/hooks/useMobile";
|
|
import { usePathnameChange } from "~/hooks/usePathnameChange";
|
|
import useStores from "~/hooks/useStores";
|
|
import { isTruthyQueryValue } from "~/utils/urls";
|
|
import Notifications from "./Notifications";
|
|
|
|
type Props = {
|
|
children?: React.ReactNode;
|
|
};
|
|
|
|
const NotificationsPopover: React.FC<Props> = ({ children }: Props) => {
|
|
const { t } = useTranslation();
|
|
const { notifications } = useStores();
|
|
const shouldOpen = isTruthyQueryValue(useConsumeQueryParam("notifications"));
|
|
const [open, setOpen] = useState(shouldOpen);
|
|
const isMobile = useMobile();
|
|
const scrollableRef = useRef<HTMLDivElement>(null);
|
|
const drawerContentRef = useRef<React.ElementRef<typeof DrawerContent>>(null);
|
|
|
|
useEffect(() => {
|
|
void notifications.fetchPage({ archived: false });
|
|
}, [notifications]);
|
|
|
|
const handleRequestClose = useCallback(() => {
|
|
setOpen(false);
|
|
}, []);
|
|
|
|
// Close on navigation because the trigger can be hidden on the new page.
|
|
usePathnameChange(handleRequestClose, open);
|
|
|
|
const handleAutoFocus = useCallback((event: Event) => {
|
|
// Prevent focus from moving to the popover content
|
|
event.preventDefault();
|
|
|
|
// Reset scroll position to the top when popover is opened
|
|
if (scrollableRef.current) {
|
|
scrollableRef.current.scrollTop = 0;
|
|
scrollableRef.current.focus();
|
|
}
|
|
}, []);
|
|
|
|
const enablePointerEvents = useCallback(() => {
|
|
if (drawerContentRef.current) {
|
|
drawerContentRef.current.style.pointerEvents = "auto";
|
|
}
|
|
}, []);
|
|
|
|
const disablePointerEvents = useCallback(() => {
|
|
if (drawerContentRef.current) {
|
|
drawerContentRef.current.style.pointerEvents = "none";
|
|
}
|
|
}, []);
|
|
|
|
const notificationsList = (
|
|
<Notifications onRequestClose={handleRequestClose} ref={scrollableRef} />
|
|
);
|
|
|
|
if (isMobile) {
|
|
return (
|
|
<Drawer open={open} onOpenChange={setOpen}>
|
|
<DrawerTrigger asChild>{children}</DrawerTrigger>
|
|
<DrawerContent
|
|
ref={drawerContentRef}
|
|
aria-label={t("Notifications")}
|
|
aria-describedby={undefined}
|
|
onAnimationStart={disablePointerEvents}
|
|
onAnimationEnd={enablePointerEvents}
|
|
>
|
|
<DrawerTitle hidden>{t("Notifications")}</DrawerTitle>
|
|
{notificationsList}
|
|
</DrawerContent>
|
|
</Drawer>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
<PopoverTrigger>{children}</PopoverTrigger>
|
|
<PopoverContent
|
|
aria-label={t("Notifications")}
|
|
side="top"
|
|
align="start"
|
|
onOpenAutoFocus={handleAutoFocus}
|
|
scrollable={false}
|
|
shrink
|
|
>
|
|
{notificationsList}
|
|
</PopoverContent>
|
|
</Popover>
|
|
);
|
|
};
|
|
|
|
export default observer(NotificationsPopover);
|