Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 16s
CodeQL / Analyze (python) (push) Failing after 3s
i18n sync check / check:sync (push) Failing after 3s
React Doctor / react-doctor (push) Failing after 16s
Branch Build CE / Build-Push Admin Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Admin Manifest (push) Has been cancelled
Branch Build CE / Build-Push Web Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Web Manifest (push) Has been cancelled
Branch Build CE / Build-Push Space Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Proxy Manifest (push) Has been cancelled
Branch Build CE / Merge Space Manifest (push) Has been cancelled
Branch Build CE / Prepare AIO Assets (push) Has been cancelled
Branch Build CE / Build Setup (push) Has been cancelled
Branch Build CE / Build-Push Live Collaboration Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Live Collaboration Manifest (push) Has been cancelled
Branch Build CE / Build-Push API Server Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge API Server Manifest (push) Has been cancelled
Branch Build CE / Build-Push Proxy Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Build-Push AIO Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge AIO Manifest (push) Has been cancelled
Branch Build CE / Upload Build Assets (push) Has been cancelled
Branch Build CE / Build Release (push) Has been cancelled
84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
/**
|
|
* Copyright (c) 2023-present Plane Software, Inc. and contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
* See the LICENSE file for details.
|
|
*/
|
|
|
|
import { createToastManager, ToastProvider } from "@makeplane/propel/components/toast";
|
|
import type { ToastData, ToastVariant } from "@makeplane/propel/components/toast";
|
|
import { IconButton } from "@makeplane/propel/components/icon-button";
|
|
import { Icon } from "@makeplane/propel/components/icon";
|
|
import { CloseOutline } from "@makeplane/propel/icons";
|
|
|
|
// Keep the call-site API the workspace @plane/propel toast exposed, backed by
|
|
// @makeplane/propel's toast manager, so consumers only change their import path.
|
|
export enum TOAST_TYPE {
|
|
SUCCESS = "success",
|
|
ERROR = "error",
|
|
INFO = "info",
|
|
WARNING = "warning",
|
|
}
|
|
|
|
const TYPE_TO_VARIANT: Record<TOAST_TYPE, ToastVariant> = {
|
|
[TOAST_TYPE.SUCCESS]: "success",
|
|
[TOAST_TYPE.ERROR]: "danger",
|
|
[TOAST_TYPE.INFO]: "info",
|
|
[TOAST_TYPE.WARNING]: "warning",
|
|
};
|
|
|
|
const toastManager = createToastManager();
|
|
|
|
type SetToastProps = {
|
|
type: TOAST_TYPE;
|
|
title: string;
|
|
message?: string;
|
|
};
|
|
|
|
export const setToast = (props: SetToastProps) =>
|
|
toastManager.add({
|
|
title: props.title,
|
|
description: props.message,
|
|
data: { variant: TYPE_TO_VARIANT[props.type] },
|
|
});
|
|
|
|
type PromiseToastData<TValue> = {
|
|
title: string;
|
|
message?: (data: TValue) => string;
|
|
};
|
|
|
|
type PromiseToastOptions<TValue> = {
|
|
loading?: string;
|
|
success: PromiseToastData<TValue>;
|
|
error: PromiseToastData<TValue>;
|
|
};
|
|
|
|
export const setPromiseToast = <TValue,>(promise: Promise<TValue>, options: PromiseToastOptions<TValue>): void => {
|
|
void toastManager.promise<TValue, ToastData>(promise, {
|
|
loading: {
|
|
title: options.loading ?? "Loading...",
|
|
data: { variant: "neutral" },
|
|
},
|
|
success: (data) => ({
|
|
title: options.success.title,
|
|
description: options.success.message?.(data),
|
|
data: { variant: "success" },
|
|
}),
|
|
error: (data) => ({
|
|
title: options.error.title,
|
|
description: options.error.message?.(data),
|
|
data: { variant: "danger" },
|
|
}),
|
|
});
|
|
};
|
|
|
|
export function ToastWithTheme() {
|
|
return (
|
|
<ToastProvider
|
|
toastManager={toastManager}
|
|
close={
|
|
<IconButton variant="ghost" size="sm" aria-label="Close notification" icon={<Icon icon={CloseOutline} />} />
|
|
}
|
|
/>
|
|
);
|
|
}
|