Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex replacer component #22

Merged
merged 4 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,55 @@
"$schema": "https://biomejs.dev/schemas/1.6.3/schema.json",
"formatter": {
"enabled": true,
"formatWithErrors": false,
"formatWithErrors": true,
"indentStyle": "space",
"indentWidth": 2,
"lineEnding": "lf",
"lineWidth": 80,
"attributePosition": "auto"
"attributePosition": "auto",
"ignore": [
"**/node_modules/**",
"**/dist/**",
"**/build/**",
"**/coverage/**",
"**/.git/**",
"**/.vscode/**",
"**/.github/**",
"**/.yarn/**",
"**/.pnp/**",
"**/.cache/**",
"**/.next/**",
"**/.nuxt/**",
"**/out/**",
"**/public/**",
"**/static"
]
},
"organizeImports": { "enabled": true },
"linter": { "enabled": true, "rules": { "recommended": true } },
"linter": {
"enabled": true,
"rules": { "recommended": true },
"ignore": [
"**/node_modules/**",
"**/dist/**",
"**/build/**",
"**/coverage/**",
"**/.git/**",
"**/.vscode/**",
"**/.github/**",
"**/.yarn/**",
"**/.pnp/**",
"**/.cache/**",
"**/.next/**",
"**/.nuxt/**",
"**/out/**",
"**/public/**",
"**/static"
]
},
"javascript": {
"formatter": {
"enabled": true,
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded",
"trailingComma": "es5",
Expand Down
72 changes: 22 additions & 50 deletions components/AliasedEmails.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AliasType } from "@/components/types"
import type { AliasType } from "@/components/types"
import { colorSelector } from "@/utils/colorSelector"
import { faker } from "@faker-js/faker"
import {
Expand Down Expand Up @@ -26,9 +26,8 @@ import {
IconX,
} from "@tabler/icons-react"
import aliasedEmail from "aliased-email"
import { useAtom } from "jotai"
import { useCallback, useEffect, useState } from "react"
import { localCopyHistoryAtom } from "./global/CopyHistory"
import { CopyComponent } from "@/components/global/CopyComponent"

type Props = { extension?: boolean }

Expand All @@ -43,7 +42,6 @@ export default function InputCreator({ extension }: Props) {
defaultValue: "",
})
const [finalEmail, setFinalEmail] = useState("")
const [copiedEmail, setCopiedEmail] = useState("")
const [timestampEnabled, setTimestampEnabled] = useLocalStorage({
key: "timestampEnabled",
defaultValue: true,
Expand All @@ -65,12 +63,12 @@ export default function InputCreator({ extension }: Props) {
}

const handleCreateAlias = (query: string) => {
query = query.trim().replaceAll(/\W/g, "")
if (aliases.find((a) => a.value === query)) return
if (query.length === 0) return
const trimmedQuery = query.trim().replaceAll(/\W/g, "")
if (aliases.find((a) => a.value === trimmedQuery)) return
if (trimmedQuery.length === 0) return
setAliases([
...aliases,
{ label: query, value: query.trim().replaceAll(/\W/g, "") },
{ label: trimmedQuery, value: trimmedQuery.trim().replaceAll(/\W/g, "") },
])
}
const [editingAliases, setEditingAliases] = useState(false)
Expand All @@ -84,24 +82,6 @@ export default function InputCreator({ extension }: Props) {
}
}

const [copyHistory, setCopyHistory] = useAtom(localCopyHistoryAtom)
const uniqueCopyHistory = new Set(copyHistory.map((item) => item.value))

const handleCopyEmail = () => {
setCopiedEmail(finalEmail)

if (uniqueCopyHistory.has(finalEmail)) return
setCopyHistory((history) => [
...history,
{
id: history.length,
type: "email",
value: finalEmail,
timestamp: new Date().getTime(),
},
])
}

const useRealtimeTimestamp = () => {
const [realtimeTimestamp, setRealtimeTimestamp] = useState("")

Expand Down Expand Up @@ -206,7 +186,7 @@ export default function InputCreator({ extension }: Props) {
Random Alias
</Button>
</Grid.Col>
<Grid.Col span={2}></Grid.Col>
<Grid.Col span={2}>{}</Grid.Col>
<Grid.Col span={5} my={"xs"}>
<Button
fullWidth
Expand Down Expand Up @@ -265,7 +245,7 @@ export default function InputCreator({ extension }: Props) {
<Text lineClamp={1} size={extension ? "xs" : "sm"}>
{email.indexOf("@") === -1
? email.slice(0, email.length)
: email.slice(0, email.indexOf("@")) + "+"}
: `${email.slice(0, email.indexOf("@"))}+`}
</Text>
</Box>
<Box px={rem(4)}>
Expand Down Expand Up @@ -306,7 +286,7 @@ export default function InputCreator({ extension }: Props) {
{selectedAlias && timestampEnabled && "-"}
<Box px={extension ? rem(1) : rem(6)}>
<Chip
data-cy={"timestampEnabled"}
data-test={"timestampEnabled"}
variant="light"
checked={timestampEnabled}
size={extension ? "xs" : "sm"}
Expand Down Expand Up @@ -335,27 +315,19 @@ export default function InputCreator({ extension }: Props) {
</Grid.Col>
)}

<Grid.Col span={12} onClick={handleCopyEmail}>
<CopyButton value={finalEmail}>
{({ copied, copy }) => (
<>
<Button
id="copyEmail"
size={extension ? "xs" : "md"}
h={80}
maw={extension ? "330px" : "100%"}
fullWidth
variant={copied ? "light" : "outline"}
onClick={copy}
rightIcon={<IconCopy />}
disabled={!validateEmail(email) || email.length === 0}
color={colorSelector("email")}
>
{copied ? `Copied ${copiedEmail}` : `${finalEmail}`}
</Button>
</>
)}
</CopyButton>
<Grid.Col span={12}>
<CopyComponent
value={finalEmail}
label={finalEmail}
type="email"
size={extension ? "xs" : "md"}
fullWidth
id="copyEmail"
h={80}
maw={extension ? "330px" : "100%"}
rightIcon={<IconCopy />}
disabled={!validateEmail(email) || email.length === 0}
/>
</Grid.Col>
</Grid>
</Card>
Expand Down
146 changes: 113 additions & 33 deletions components/RegexReplacer.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,130 @@
import { Box, Button, CopyButton, Input, Text } from "@mantine/core"
import { Box, Card, Grid, Input, Text, Tooltip } from "@mantine/core"
import { useInputState } from "@mantine/hooks"
import React, { useEffect, useState } from "react"
import { regexReplacer } from "@/utils/regexReplacer"
import { stringToRegex } from "@/utils/transformStringToRegex"
import { CopyComponent } from "@/components/global/CopyComponent"
import { IconAlertCircle, IconRegex } from "@tabler/icons-react"

type Props = {}
type Props = {
extension?: boolean
}

const RegexReplacer = (props: Props) => {
const RegexReplacer = ({ extension }: Props) => {
const [regexInput, setRegexInput] = useInputState("")
const [replaceValue, setReplaceValue] = useInputState("")
const [testText, setTestText] = useInputState("")
const [preview, setPreview] = useInputState("")

//convert the regexInput to regex
const [regex, setRegex] = useState<RegExp | null>(null)

useEffect(() => {
setRegex(stringToRegex(regexInput))
setPreview(regexReplacer(regexInput, replaceValue, testText))
}, [regexInput, replaceValue, testText])
}, [regexInput, replaceValue, testText, setPreview])

return (
<Box>
<Input.Wrapper label="Test text">
<Input
value={testText}
onChange={setTestText}
placeholder="test text"
/>
</Input.Wrapper>
<Input.Wrapper label="Regex">
<Input
value={regexInput}
onChange={setRegexInput}
placeholder="regex"
/>
</Input.Wrapper>
<Input.Wrapper label="replacement text">
<Input
value={replaceValue}
onChange={setReplaceValue}
placeholder="replacement text"
/>
</Input.Wrapper>

<CopyButton value={regexInput}>
{({ copied, copy }) => <Button>{preview}</Button>}
</CopyButton>
</Box>
<Card
shadow="sm"
padding={extension ? "xs" : "lg"}
radius="md"
maw={extension ? "380px" : "100%"}
>
<Grid gutter={extension ? 0 : "xs"}>
<Grid.Col span={12}>
<Text mx={8}> Regex Replace Tester</Text>
</Grid.Col>
<Grid.Col span={12}>
<Input.Wrapper
description={
"Enter a regex pattern to match text. Use the test text input to see the preview. This is still a work in progress."
}
descriptionProps={
extension ? { display: "none" } : { display: "block" }
}
>
<Input
value={regexInput}
onChange={setRegexInput}
placeholder="regex"
type="text"
icon={<IconRegex size="1rem" />}
radius={"xl"}
autoComplete="off"
autoSave="off"
data-test="regex-input"
rightSection={
<Tooltip
label="Remember to use \ to escape special characters"
position="left"
>
<Box>
<IconAlertCircle
size="1rem"
style={{ display: "block", opacity: 0.5 }}
/>
</Box>
</Tooltip>
}
/>
</Input.Wrapper>
</Grid.Col>
<Grid.Col span={6} data-name={"Text Text"}>
<Input.Wrapper label="Test text">
<Input
value={testText}
onChange={setTestText}
placeholder="test text"
data-test="regex-test-text"
/>
</Input.Wrapper>
</Grid.Col>
<Grid.Col span={6} data-name={"Replacement Text"}>
<Input.Wrapper label="replacement text">
<Input
value={replaceValue}
onChange={setReplaceValue}
placeholder="replacement text"
data-test="regex-replacement-text"
/>
</Input.Wrapper>
</Grid.Col>
<Grid.Col span={6} data-name={"Copy Regex"}>
<CopyComponent
label={regex?.toString()}
type="regex"
value={regex?.toString()}
fullWidth
h={60}
disabled={
regex && regexInput.length > 0 && regex.toString() !== "/(?:)/"
? false
: true
}
data-test="regex-copy"
/>
</Grid.Col>
<Grid.Col span={6} data-name={"Copy Preview"}>
<CopyComponent
label={preview}
type="string"
value={preview}
fullWidth
h={60}
disabled={preview.length > 0 ? false : true}
data-test="regex-preview"
/>
</Grid.Col>
<Grid.Col span={12} data-name={"replaceFunction"}>
<CopyComponent
label={`.replace(${regex}, "${replaceValue}")`}
value={`.replace(${regex}, "${replaceValue}")`}
type="function"
fullWidth
data-test="regex-function"
/>
</Grid.Col>
</Grid>
</Card>
)
}

Expand Down
Loading
Loading