Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

use optimistic update

Steven Teye06a83021cbd
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
index 1e67c9ebd2..42ed15f9ed 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
@@ -1,12 +1,12 @@
import SettingsLayout from "@/ui/layout/settings-layout";
import {
+ CircleInfo,
Gear2,
Globe,
Key,
Receipt2,
ShieldCheck,
Users6,
- WindowSettings,
} from "@dub/ui/src/icons";
import { ReactNode } from "react";
@@ -46,15 +46,22 @@ export default function WorkspaceSettingsLayout({
icon: ShieldCheck,
segment: "security",
},
+ ];
+
+ const extraTabs = [
{
name: "Notifications",
- icon: WindowSettings,
+ icon: CircleInfo,
segment: "notifications",
},
];
return (
- <SettingsLayout tabs={tabs} tabContainerClassName="top-16">
+ <SettingsLayout
+ tabs={tabs}
+ extraTabs={extraTabs}
+ tabContainerClassName="top-16"
+ >
{children}
</SettingsLayout>
);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/notifications/preferences.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/notifications/preferences.tsx
index 7b56d742b9..d911e6dc00 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/notifications/preferences.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/notifications/preferences.tsx
@@ -2,12 +2,8 @@
import { updateNotificationPreference } from "@/lib/actions/update-notification-preference";
import useWorkspace from "@/lib/swr/use-workspace";
-import { Switch } from "@dub/ui";
-import { LoadingSpinner } from "@dub/ui/src/icons";
-import { fetcher } from "@dub/utils";
+import { Switch, useOptimisticUpdate } from "@dub/ui";
import { useAction } from "next-safe-action/hooks";
-import { toast } from "sonner";
-import useSWR from "swr";
const notifications = [
{
@@ -20,13 +16,6 @@ const notifications = [
},
];
-interface PreferenceProps {
- type: string;
- description: string;
- enabled: boolean;
- onChange: (value: boolean) => void;
-}
-
export const NotificationPreferences = () => {
const { id: workspaceId } = useWorkspace();
const { executeAsync } = useAction(updateNotificationPreference);
@@ -34,14 +23,35 @@ export const NotificationPreferences = () => {
const {
data: preferences,
isLoading,
- mutate,
- } = useSWR<{
+ update,
+ } = useOptimisticUpdate<{
linkUsageSummary: boolean;
domainConfigurationUpdates: boolean;
- }>(`/api/workspaces/${workspaceId}/notification-preferences`, fetcher, {
- dedupingInterval: 30000,
+ }>(`/api/workspaces/${workspaceId}/notification-preferences`, {
+ loading: "Updating notification preference...",
+ success: "Notification preference updated.",
+ error: "Failed to update notification preference.",
});
+ const handleUpdate = async ({
+ type,
+ value,
+ }: {
+ type: string;
+ value: boolean;
+ }) => {
+ await executeAsync({
+ workspaceId: workspaceId!,
+ type: type as "linkUsageSummary" | "domainConfigurationUpdates",
+ value,
+ });
+
+ return {
+ ...preferences,
+ [type]: value,
+ };
+ };
+
return (
<>
<div className="rounded-lg border border-gray-200 bg-white">
@@ -56,51 +66,34 @@ export const NotificationPreferences = () => {
</div>
<div className="flex flex-col gap-3 p-5 sm:p-10">
- {isLoading ? (
- <LoadingSpinner />
- ) : (
- notifications.map((notification) => (
- <Preference
- {...notification}
- enabled={preferences ? preferences[notification.type] : false}
- key={notification.type}
- onChange={async (value) => {
- if (!workspaceId) return;
-
- const response = await executeAsync({
- workspaceId,
- type: notification.type as any,
- value,
- });
-
- if (response?.data) {
- mutate();
- toast.success("Notification preference saved.");
- } else {
- toast.error(response?.serverError?.serverError);
- }
- }}
- />
- ))
- )}
+ {notifications.map((notification) => (
+ <div key={notification.type}>
+ <label className="flex items-center font-medium text-gray-700">
+ <Switch
+ checked={preferences?.[notification.type] ?? false}
+ disabled={isLoading}
+ fn={(checked: boolean) => {
+ update(
+ () =>
+ handleUpdate({
+ type: notification.type,
+ value: checked,
+ }),
+ {
+ ...preferences,
+ [notification.type]: checked,
+ },
+ );
+ }}
+ />
+ <span className="ml-2 text-sm text-gray-500">
+ {notification.description}
+ </span>
+ </label>
+ </div>
+ ))}
</div>
</div>
</>
);
};
-
-const Preference = ({
- type,
- description,
- enabled,
- onChange,
-}: PreferenceProps) => {
- return (
- <div key={type}>
- <label className="flex items-center font-medium text-gray-700">
- <Switch checked={enabled} fn={(value) => onChange(value)} />
- <span className="ml-2 text-sm text-gray-500">{description}</span>
- </label>
- </div>
- );
-};
diff --git a/apps/web/scripts/update-notifications-preferences.ts b/apps/web/scripts/update-notifications-preferences.ts
index deedd7d8d3..00b90fbe0f 100644
--- a/apps/web/scripts/update-notifications-preferences.ts
+++ b/apps/web/scripts/update-notifications-preferences.ts
@@ -8,11 +8,15 @@ async function main() {
user: {
isMachine: false,
},
+ notificationPreference: null,
+ project: {
+ slug: "acme",
+ },
},
select: {
id: true,
},
- take: 500,
+ take: 1000,
skip: 0,
});
diff --git a/apps/web/ui/layout/settings-layout.tsx b/apps/web/ui/layout/settings-layout.tsx
index 30a2f0b111..b97e6be7d0 100644
--- a/apps/web/ui/layout/settings-layout.tsx
+++ b/apps/web/ui/layout/settings-layout.tsx
@@ -3,16 +3,21 @@ import { Icon, MaxWidthWrapper } from "@dub/ui";
import { cn } from "@dub/utils";
import { ReactNode } from "react";
+interface TabProps {
+ name: string;
+ icon: Icon;
+ segment: string | null;
+}
+[];
+
export default function SettingsLayout({
tabs,
+ extraTabs,
tabContainerClassName,
children,
}: {
- tabs: {
- name: string;
- icon: Icon;
- segment: string | null;
- }[];
+ tabs: TabProps[];
+ extraTabs?: TabProps[];
tabContainerClassName?: string;
children: ReactNode;
}) {
@@ -30,6 +35,16 @@ export default function SettingsLayout({
{name}
</NavLink>
))}
+ {extraTabs && (
+ <>
+ <div className="my-4 border-t border-gray-200" />
+ {extraTabs.map(({ name, segment, icon }) => (
+ <NavLink segment={segment} icon={icon}>
+ {name}
+ </NavLink>
+ ))}
+ </>
+ )}
</div>
<div className="grid gap-5 lg:col-span-4">{children}</div>
</MaxWidthWrapper>
diff --git a/packages/ui/src/switch.tsx b/packages/ui/src/switch.tsx
index 0ecb8a4c56..c8b115f6ca 100644
--- a/packages/ui/src/switch.tsx
+++ b/packages/ui/src/switch.tsx
@@ -35,8 +35,9 @@ export function Switch({
{...(fn && { onCheckedChange: fn })}
disabled={switchDisabled}
className={cn(
- "radix-state-checked:bg-blue-500 radix-state-unchecked:bg-gray-200 relative inline-flex h-4 w-8 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus-visible:ring focus-visible:ring-blue-500 focus-visible:ring-opacity-75",
- switchDisabled && "cursor-not-allowed",
+ "radix-state-checked:bg-blue-500 radix-state-unchecked:bg-gray-200 relative inline-flex h-4 w-8 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out",
+ "focus:outline-none focus-visible:ring focus-visible:ring-blue-500 focus-visible:ring-opacity-75",
+ "data-[disabled]:cursor-not-allowed",
trackDimensions,
)}
>