Skip to content

Commit

Permalink
Feat/Refactor: EventBus and Notifications (#4869)
Browse files Browse the repository at this point in the history
* initial commit

* import fix

* persist link notifications
  • Loading branch information
briangregoryholmes authored May 10, 2024
1 parent 5abee16 commit 0251b9f
Show file tree
Hide file tree
Showing 36 changed files with 238 additions and 368 deletions.
6 changes: 3 additions & 3 deletions web-admin/src/features/bookmarks/Bookmarks.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import EditBookmarkDialog from "@rilldata/web-admin/features/bookmarks/EditBookmarkDialog.svelte";
import { getBookmarkDataForDashboard } from "@rilldata/web-admin/features/bookmarks/getBookmarkDataForDashboard";
import type { BookmarkEntry } from "@rilldata/web-admin/features/bookmarks/selectors";
import { notifications } from "@rilldata/web-common/components/notifications";
import { useDashboardStore } from "@rilldata/web-common/features/dashboards/stores/dashboard-stores";
import { ResourceKind } from "@rilldata/web-common/features/entity-management/resource-selectors";
import { runtime } from "@rilldata/web-common/runtime-client/runtime-store";
import { useQueryClient } from "@tanstack/svelte-query";
import { BookmarkIcon } from "lucide-svelte";
import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus";
$: metricsViewName = $page.params.dashboard;
Expand Down Expand Up @@ -50,7 +50,7 @@
await homeBookmarkModifier(
getBookmarkDataForDashboard($dashboardStore, false, false),
);
notifications.send({
eventBus.emit("notification", {
message: "Home bookmark created",
});
return queryClient.refetchQueries(
Expand All @@ -67,7 +67,7 @@
await $bookmarkDeleter.mutateAsync({
bookmarkId: bookmark.resource.id,
});
notifications.send({
eventBus.emit("notification", {
message: `Bookmark ${bookmark.resource.displayName} deleted`,
});
return queryClient.refetchQueries(
Expand Down
6 changes: 3 additions & 3 deletions web-admin/src/features/bookmarks/CreateBookmarkDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
} from "@rilldata/web-admin/client";
import { Button } from "@rilldata/web-common/components/button";
import { getBookmarkDataForDashboard } from "@rilldata/web-admin/features/bookmarks/getBookmarkDataForDashboard";
import { notifications } from "@rilldata/web-common/components/notifications";
import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus";
import { useDashboardStore } from "@rilldata/web-common/features/dashboards/stores/dashboard-stores";
import { ResourceKind } from "@rilldata/web-common/features/entity-management/resource-selectors";
import { useQueryClient } from "@tanstack/svelte-query";
Expand Down Expand Up @@ -57,14 +57,14 @@
},
});
handleReset();
queryClient.refetchQueries(
await queryClient.refetchQueries(
getAdminServiceListBookmarksQueryKey({
projectId: $projectId.data ?? "",
resourceKind: ResourceKind.MetricsView,
resourceName: metricsViewName,
}),
);
notifications.send({
eventBus.emit("notification", {
message: "Bookmark created",
});
handleClose();
Expand Down
4 changes: 2 additions & 2 deletions web-admin/src/features/bookmarks/EditBookmarkDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { Button } from "@rilldata/web-common/components/button";
import { getBookmarkDataForDashboard } from "@rilldata/web-admin/features/bookmarks/getBookmarkDataForDashboard";
import type { BookmarkEntry } from "@rilldata/web-admin/features/bookmarks/selectors";
import { notifications } from "@rilldata/web-common/components/notifications";
import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus";
import { useDashboardStore } from "@rilldata/web-common/features/dashboards/stores/dashboard-stores";
import { ResourceKind } from "@rilldata/web-common/features/entity-management/resource-selectors";
import { useQueryClient } from "@tanstack/svelte-query";
Expand Down Expand Up @@ -63,7 +63,7 @@
resourceName: metricsViewName,
}),
);
notifications.send({
eventBus.emit("notification", {
message: "Bookmark updated",
});
handleClose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import Button from "@rilldata/web-common/components/button/Button.svelte";
import * as DropdownMenu from "@rilldata/web-common/components/dropdown-menu";
import Link from "@rilldata/web-common/components/icons/Link.svelte";
import { notifications } from "@rilldata/web-common/components/notifications";
import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus";
function handleCopyLink() {
async function handleCopyLink() {
// Copy the current URL to the clipboard
navigator.clipboard.writeText(window.location.href);
await navigator.clipboard.writeText(window.location.href);
notifications.send({
eventBus.emit("notification", {
message: "Link copied to clipboard",
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { createAdminServiceTriggerReport } from "@rilldata/web-admin/client";
import { Button } from "@rilldata/web-common/components/button";
import { notifications } from "@rilldata/web-common/components/notifications";
import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus";
import Tooltip from "@rilldata/web-common/components/tooltip/Tooltip.svelte";
import TooltipContent from "@rilldata/web-common/components/tooltip/TooltipContent.svelte";
import { ResourceKind } from "@rilldata/web-common/features/entity-management/resource-selectors";
Expand All @@ -28,7 +28,7 @@
data: undefined,
});
notifications.send({
eventBus.emit("notification", {
message: "Triggered an ad-hoc run of this report.",
type: "success",
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* This component will render the label bound on the TimestampDetail.svelte graph.
* It also enables a shift + click to copy the bound as a query-ready timestamp.
*/
import { notifications } from "@rilldata/web-common/components/notifications";
import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus";
import Shortcut from "@rilldata/web-common/components/tooltip/Shortcut.svelte";
import StackingWord from "@rilldata/web-common/components/tooltip/StackingWord.svelte";
import Tooltip from "@rilldata/web-common/components/tooltip/Tooltip.svelte";
Expand Down Expand Up @@ -45,7 +45,9 @@
if (valueWithoutOffset === undefined) return;
const exportedValue = `TIMESTAMP '${valueWithoutOffset.toISOString()}'`;
await navigator.clipboard.writeText(exportedValue);
notifications.send({ message: `copied ${exportedValue} to clipboard` });
eventBus.emit("notification", {
message: `copied ${exportedValue} to clipboard`,
});
// update this to set the active animation in the tooltip text
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* The graph will contain an unsmoothed series (showing noise * abnormalities) by default, and
* a smoothed series (showing the trend) if the time series merits it.
*/
import { notifications } from "@rilldata/web-common/components/notifications";
import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus";
import Tooltip from "@rilldata/web-common/components/tooltip/Tooltip.svelte";
import { createShiftClickAction } from "@rilldata/web-common/lib/actions/shift-click-action";
import { guidGenerator } from "@rilldata/web-common/lib/guid";
Expand Down Expand Up @@ -316,7 +316,7 @@
).toISOString()}'`;
await navigator.clipboard.writeText(exportedValue);
setTimeout(() => {
notifications.send({
eventBus.emit("notification", {
message: `copied ${exportedValue} to clipboard`,
});
}, 200);
Expand Down
195 changes: 89 additions & 106 deletions web-common/src/components/notifications/Notification.svelte
Original file line number Diff line number Diff line change
@@ -1,142 +1,125 @@
<script>
<script lang="ts">
import { scale } from "svelte/transition";
export let width = undefined;
export let level = "info"; // info, error.
export let xOffset = undefined;
export let yOffset = undefined;
// positioning elements.
export let location;
// if specified, set the width variable.
function makeStyle({ width, xOffset, yOffset }) {
let styles = [
width ? `--width: ${width}` : undefined,
xOffset ? `--x-offset: ${xOffset}` : undefined,
yOffset ? `--y-offset: ${yOffset}` : undefined,
];
return styles.filter((d) => d !== undefined).join("; ");
}
$: style = makeStyle({ width, xOffset, yOffset });
import { portal } from "@rilldata/web-common/lib/actions/portal";
import type { NotificationMessage } from "@rilldata/web-common/lib/event-bus/events";
import Close from "../icons/Close.svelte";
import Check from "../icons/Check.svelte";
import Button from "../button/Button.svelte";
import { onMount } from "svelte";
import WarningIcon from "../icons/WarningIcon.svelte";
const NOTIFICATION_TIMEOUT = 3500;
export let location: "top" | "bottom" | "middle" = "bottom";
export let justify: "left" | "right" | "center" = "center";
export let notification: NotificationMessage;
export let onClose: () => void;
$: ({ message, link, type, detail, options } = notification);
onMount(() => {
if (!options?.persisted || link) {
setTimeout(onClose, NOTIFICATION_TIMEOUT);
}
});
</script>

<aside
transition:scale|global={{ duration: 200, start: 0.98, opacity: 0 }}
{style}
class="whitespace-pre radius-sm notification-{level}
{location !== undefined
? `notification-floating notification-floating-${location}`
: ''}"
use:portal
transition:scale={{ duration: 200, start: 0.98, opacity: 0 }}
class="{location} {justify}"
>
<div class="icon centered">
<slot name="icon" />
</div>
<div class="body centered">
<slot name="body" />
<slot />
</div>
<div class="cta centered">
<slot name="cta" />
<div class="main-section">
<div class="message-container" class:font-medium={detail}>
{#if type === "success"}
<Check size="18px" className="text-white" />
{:else if type == "error"}
<WarningIcon />
{/if}

{message}
</div>

{#if link}
<div class="link-container">
<a href={link.href} on:click={onClose}>{link.text}</a>
</div>
{/if}

{#if options?.persisted}
<div class="px-2 py-2 border-l">
<Button on:click={onClose} square>
<Close size="18px" color="#fff" />
</Button>
</div>
{/if}
</div>

{#if detail}
<hr />
<div class="detail">
{detail}
</div>
{/if}
</aside>

<style lang="postcss">
* {
@apply border-gray-600;
}
aside {
--width: max-content;
padding: 8px 14px;
width: var(--width);
box-shadow: var(--rally-box-shadow-sm);
font-style: normal;
/* font-weight: 600; */
font-size: 15px;
line-height: 22px;
display: grid;
grid-column-gap: 10px;
grid-template-columns: max-content auto max-content;
align-items: center;
align-content: center;
@apply shadow-lg rounded-md;
@apply absolute w-fit z-50 flex flex-col text-sm;
@apply bg-gray-900 text-gray-50 p-0 rounded-md shadow-lg;
}
.notification-info,
.notification-info > * {
color: #0c0c0d;
@apply bg-slate-800 text-slate-100;
.main-section {
@apply flex;
}
.notification-error,
.notification-error > * {
color: var(--color-white);
background-color: var(--color-red-60);
.detail {
@apply px-4 pt-2 pb-3;
}
.icon {
font-size: 24px;
height: 24px;
.link-container {
@apply px-4 py-2 border-l items-center flex;
}
.centered {
height: max-content;
display: grid;
align-self: center;
align-items: center;
align-content: center;
.message-container {
@apply flex items-center px-4 py-2 gap-x-2;
}
.centered > * {
height: max-content;
display: grid;
align-self: center;
align-items: center;
align-content: center;
a {
@apply text-primary-300;
}
.notification-floating {
position: fixed;
--pad: 20px;
/* stylelint-disable */
--x-offset: 0px;
--y-offset: 0px;
/* stylelint-enable */
--x-pad: calc(var(--pad) + var(--x-offset));
--y-pad: calc(var(--pad) + var(--y-offset));
a:hover {
@apply text-primary-200;
}
.notification-floating-bottom,
.notification-floating-bottom-center {
bottom: var(--y-pad);
left: calc(50% + var(--x-offset));
transform: translateX(-50%);
.top {
@apply top-[30px];
}
.notification-floating-bottom-right {
right: var(--x-pad);
bottom: var(--y-pad);
.bottom {
@apply bottom-[30px];
}
.notification-floating-bottom-left {
left: var(--x-pad);
bottom: var(--y-pad);
.middle {
@apply top-1/2;
@apply -translate-y-1/2;
}
.notification-floating-top,
.notification-floating-top-center {
top: var(--y-pad);
left: calc(50% + var(--x-offset));
transform: translateX(-50%);
.left {
@apply left-4;
}
.notification-floating-top-left {
left: var(--x-pad);
top: var(--y-pad);
.right {
@apply right-4;
}
.notification-floating-top-right {
right: var(--x-pad);
top: var(--y-pad);
.center {
@apply left-1/2;
@apply -translate-x-1/2;
}
</style>
Loading

1 comment on commit 0251b9f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.