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.

update folder switcher

Kiran K2e500504955d
diff --git a/apps/web/lib/swr/use-folder.ts b/apps/web/lib/swr/use-folder.ts
index cee2c5f0bc..1201cd8897 100644
--- a/apps/web/lib/swr/use-folder.ts
+++ b/apps/web/lib/swr/use-folder.ts
@@ -6,14 +6,19 @@ import useWorkspace from "./use-workspace";
export default function useFolder({ folderId }: { folderId?: string | null }) {
const { id: workspaceId, plan, flags } = useWorkspace();
+ const enabled =
+ folderId &&
+ folderId !== "unsorted" &&
+ workspaceId &&
+ flags?.linkFolders &&
+ plan !== "free";
+
const {
data: folder,
isValidating,
isLoading,
} = useSWR<Folder>(
- folderId && workspaceId && flags?.linkFolders && plan !== "free"
- ? `/api/folders/${folderId}?workspaceId=${workspaceId}`
- : null,
+ enabled ? `/api/folders/${folderId}?workspaceId=${workspaceId}` : null,
fetcher,
{
dedupingInterval: 60000,
diff --git a/apps/web/lib/swr/use-links-count.ts b/apps/web/lib/swr/use-links-count.ts
index 9e3661f334..20caa5dbe5 100644
--- a/apps/web/lib/swr/use-links-count.ts
+++ b/apps/web/lib/swr/use-links-count.ts
@@ -18,8 +18,9 @@ export default function useLinksCount<T = any>({
ignoreParams?: boolean;
enabled?: boolean;
} = {}) {
- const { id: workspaceId } = useWorkspace();
- const { getQueryString } = useRouterStuff();
+ const { id: workspaceId, defaultFolderId } = useWorkspace();
+ const { getQueryString, searchParams } = useRouterStuff();
+ const { isMegaFolder } = useIsMegaFolder();
const [admin, setAdmin] = useState(false);
useEffect(() => {
@@ -27,14 +28,25 @@ export default function useLinksCount<T = any>({
setAdmin(true);
}
}, []);
- const { isMegaFolder } = useIsMegaFolder();
+
+ // Decide on the folderId to use
+ let folderId = searchParams.get("folderId");
+ if (!folderId && defaultFolderId) {
+ folderId = defaultFolderId;
+ } else if (folderId) {
+ folderId = folderId !== "unsorted" ? folderId : "";
+ } else {
+ folderId = "";
+ }
const { data, error } = useSWR<any>(
workspaceId && !isMegaFolder && enabled
? `/api/links/count${getQueryString(
{
workspaceId,
+
...query,
+ folderId,
},
ignoreParams
? { include: [] }
diff --git a/apps/web/lib/swr/use-links.ts b/apps/web/lib/swr/use-links.ts
index ee09c21ac0..548ac25b84 100644
--- a/apps/web/lib/swr/use-links.ts
+++ b/apps/web/lib/swr/use-links.ts
@@ -13,8 +13,8 @@ export default function useLinks(
opts: z.infer<typeof partialQuerySchema> = {},
swrOpts: SWRConfiguration = {},
) {
- const { id: workspaceId } = useWorkspace();
- const { getQueryString } = useRouterStuff();
+ const { id: workspaceId, defaultFolderId } = useWorkspace();
+ const { getQueryString, searchParams } = useRouterStuff();
const [admin, setAdmin] = useState(false);
useEffect(() => {
@@ -23,6 +23,16 @@ export default function useLinks(
}
}, []);
+ // Decide on the folderId to use
+ let folderId = searchParams.get("folderId");
+ if (!folderId && defaultFolderId) {
+ folderId = defaultFolderId;
+ } else if (folderId) {
+ folderId = folderId !== "unsorted" ? folderId : "";
+ } else {
+ folderId = "";
+ }
+
const {
data: links,
isValidating,
@@ -39,6 +49,7 @@ export default function useLinks(
includeUser: "true",
includeWebhooks: "true",
includeDashboard: "true",
+ folderId,
...opts,
},
{
diff --git a/apps/web/ui/folders/folder-dropdown.tsx b/apps/web/ui/folders/folder-dropdown.tsx
index d9b97ed71d..4bd6bcd9e4 100644
--- a/apps/web/ui/folders/folder-dropdown.tsx
+++ b/apps/web/ui/folders/folder-dropdown.tsx
@@ -110,6 +110,7 @@ export const FolderDropdown = ({
? [selectedFolderData]
: []),
];
+
return [
...allFolders.map((folder) => ({
value: folder.id,
@@ -172,7 +173,7 @@ export const FolderDropdown = ({
onFolderSelect
? onFolderSelect(folder)
: router.push(
- `/${slug}${folder.id === "unsorted" ? "" : `?folderId=${folder.id}`}`,
+ `/${slug}${folder.id === "unsorted" ? "?folderId=unsorted" : `?folderId=${folder.id}`}`,
);
}
}}