Skip to content

Commit

Permalink
Update links server actions + add copy to clipboard hook & format dat…
Browse files Browse the repository at this point in the history
…e utility
  • Loading branch information
pheralb committed Mar 7, 2024
1 parent 778a9e2 commit 79a8ef1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/hooks/useCopyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useState } from "react";

type CopiedValue = string | null;
type CopyFn = (text: string) => Promise<boolean>; // Return success

export function useCopyToClipboard(): [CopiedValue, CopyFn] {
const [copiedText, setCopiedText] = useState<CopiedValue>(null);

const copy: CopyFn = async (text) => {
if (!navigator?.clipboard) {
console.warn("Clipboard not supported");
return false;
}

// Try to save to clipboard then save it in the state if worked
try {
await navigator.clipboard.writeText(text);
setCopiedText(text);
return true;
} catch (error) {
console.warn("Copy failed", error);
setCopiedText(null);
return false;
}
};

return [copiedText, copy];
}
3 changes: 3 additions & 0 deletions src/server/actions/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { CreateLinkSchema, LinkSchema } from "@/server/schemas";

import { auth } from "@/server/auth";
import { db } from "@/server/db";
import { revalidatePath } from "next/cache";

/**
* Get links created by user.
Expand Down Expand Up @@ -91,6 +92,8 @@ export const createLink = async (values: z.infer<typeof CreateLinkSchema>) => {
},
});

revalidatePath("/dashboard");

return result;
};

Expand Down
10 changes: 10 additions & 0 deletions src/types/link.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface LinksProps {
id: number;
url: string;
slug: string;
description: string | null;
createdAt: Date;
creatorId: string;
folder?: string | null;
tagId: number | null;
}
5 changes: 5 additions & 0 deletions src/utils/formatDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { format } from "date-fns";
export const formatDate = (date: Date) => {
// Return "23 January 2021"
return format(date, "dd MMMM, yyyy");
};

0 comments on commit 79a8ef1

Please sign in to comment.