Skip to content

Commit

Permalink
Add aliased-email library for email aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
mikikiv committed Jan 10, 2024
1 parent 39e5fbf commit a7175e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
24 changes: 10 additions & 14 deletions components/AliasedEmails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useAtom } from "jotai"
import { colorSelector } from "@/utils/colorSelector"
import { localCopyHistoryAtom } from "./global/CopyHistory"
import { AliasType } from "@/components/types"
import aliasedEmail, { aliasedEmailObject } from "aliased-email"

type Props = { extension?: boolean }

Expand All @@ -53,12 +54,10 @@ export default function InputCreator({ extension }: Props) {

const updateTimestamp = () => {
const updatingTimestamp = new Date(Date.now())
.toLocaleString("en-US", {
hour12: false,
})
.replaceAll(/[:\,]/g, "")
.replaceAll(/[\/]/g, "_")
.replace(/\s/g, "-")
.toLocaleString("en-US", { hourCycle: "h24" })
.replace(/[:\/]+/g, ".")
.replace(/,/g, "-")
.replace(/\s+/g, "")
setRealtimeTimestamp(updatingTimestamp)
}

Expand All @@ -68,22 +67,19 @@ export default function InputCreator({ extension }: Props) {
return () => clearInterval(interval)
}, [timestampEnabled])

const addAliasToEmail = (email: string, alias: string) => {
if (!email) return ""
return email.split("@").join(alias ? "+" + alias + "@" : "" + "@")
}

useEffect(() => {
//every time the selected alias changes, update the aliased email
setFinalEmail(
timestampEnabled
? addAliasToEmail(
? aliasedEmail(
email,
selectedAlias
? `${selectedAlias}-${realtimeTimestamp}`
: realtimeTimestamp
)
: addAliasToEmail(email, selectedAlias)
: selectedAlias
? aliasedEmailObject(email, selectedAlias).email
: aliasedEmailObject(email).email
)
}, [email, selectedAlias, realtimeTimestamp, timestampEnabled])

Expand All @@ -95,7 +91,7 @@ export default function InputCreator({ extension }: Props) {
const handleChangeEmail = (e: React.ChangeEvent<HTMLInputElement>) => {
setEmail(e.target.value)
setFinalEmail(
addAliasToEmail(
aliasedEmail(
e.target.value,
selectedAlias || new Date(timestamp).getTime().toString()
)
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/react": "18.0.38",
"@types/react-dom": "18.0.11",
"@vercel/analytics": "^1.0.1",
"aliased-email": "^1.1.2",
"analytics": "github:vercel/analytics",
"eslint": "8.39.0",
"eslint-config-next": "13.3.1",
Expand Down

1 comment on commit a7175e4

@vercel
Copy link

@vercel vercel bot commented on a7175e4 Jan 10, 2024

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.