Skip to content

Commit f269aef

Browse files
authored
Merge pull request matrix-org#4855 from matrix-org/t3chguy/download_toast
Add Generic Expiring Toast and timing hooks
2 parents 446f3e3 + 1a1b7e5 commit f269aef

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2020 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from "react";
18+
19+
import ToastStore from "../../../stores/ToastStore";
20+
import GenericToast, { IProps as IGenericToastProps } from "./GenericToast";
21+
import {useExpiringCounter} from "../../../hooks/useTimeout";
22+
23+
interface IProps extends IGenericToastProps {
24+
toastKey: string;
25+
numSeconds: number;
26+
dismissLabel: string;
27+
onDismiss?();
28+
}
29+
30+
const SECOND = 1000;
31+
32+
const GenericExpiringToast: React.FC<IProps> = ({description, acceptLabel, dismissLabel, onAccept, onDismiss, toastKey, numSeconds}) => {
33+
const onReject = () => {
34+
if (onDismiss) onDismiss();
35+
ToastStore.sharedInstance().dismissToast(toastKey);
36+
};
37+
const counter = useExpiringCounter(onReject, SECOND, numSeconds);
38+
39+
let rejectLabel = dismissLabel;
40+
if (counter > 0) {
41+
rejectLabel += ` (${counter})`;
42+
}
43+
44+
return <GenericToast
45+
description={description}
46+
acceptLabel={acceptLabel}
47+
onAccept={onAccept}
48+
rejectLabel={rejectLabel}
49+
onReject={onReject}
50+
/>;
51+
};
52+
53+
export default GenericExpiringToast;

src/components/views/toasts/GenericToast.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import React, {ReactChild} from "react";
1919
import FormButton from "../elements/FormButton";
2020
import {XOR} from "../../../@types/common";
2121

22-
interface IProps {
22+
export interface IProps {
2323
description: ReactChild;
2424
acceptLabel: string;
2525

src/hooks/useTimeout.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2020 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import {useEffect, useRef, useState} from "react";
18+
19+
type Handler = () => void;
20+
21+
// Hook to simplify timeouts in functional components
22+
export const useTimeout = (handler: Handler, timeoutMs: number) => {
23+
// Create a ref that stores handler
24+
const savedHandler = useRef<Handler>();
25+
26+
// Update ref.current value if handler changes.
27+
useEffect(() => {
28+
savedHandler.current = handler;
29+
}, [handler]);
30+
31+
// Set up timer
32+
useEffect(() => {
33+
const timeoutID = setTimeout(() => {
34+
savedHandler.current();
35+
}, timeoutMs);
36+
return () => clearTimeout(timeoutID);
37+
}, [timeoutMs]);
38+
};
39+
40+
// Hook to simplify intervals in functional components
41+
export const useInterval = (handler: Handler, intervalMs: number) => {
42+
// Create a ref that stores handler
43+
const savedHandler = useRef<Handler>();
44+
45+
// Update ref.current value if handler changes.
46+
useEffect(() => {
47+
savedHandler.current = handler;
48+
}, [handler]);
49+
50+
// Set up timer
51+
useEffect(() => {
52+
const intervalID = setInterval(() => {
53+
savedHandler.current();
54+
}, intervalMs);
55+
return () => clearInterval(intervalID);
56+
}, [intervalMs]);
57+
};
58+
59+
// Hook to simplify a variable counting down to 0, handler called when it reached 0
60+
export const useExpiringCounter = (handler: Handler, intervalMs: number, initialCount: number) => {
61+
const [count, setCount] = useState(initialCount);
62+
useInterval(() => setCount(c => c - 1), intervalMs);
63+
if (count === 0) {
64+
handler();
65+
}
66+
return count;
67+
};

src/stores/ToastStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface IToast<C extends keyof JSX.IntrinsicElements | JSXElementConstr
2424
title: string;
2525
icon?: string;
2626
component: C;
27-
props?: React.ComponentProps<C>;
27+
props?: Omit<React.ComponentProps<C>, "toastKey">; // toastKey is injected by ToastContainer
2828
}
2929

3030
/**

0 commit comments

Comments
 (0)