Skip to content

Commit

Permalink
refactor(frontend): improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
MiracleHorizon committed Jun 9, 2024
1 parent 6a2ba8c commit 2a15ca5
Show file tree
Hide file tree
Showing 60 changed files with 105 additions and 93 deletions.
2 changes: 1 addition & 1 deletion apps/frontend/src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

--layout-header-height: 49px; /* var(--space-8) + 1px */
--layout-content-height: calc(100dvh - var(--layout-header-height) + 1px);
--app-footer-height: 64px; /* var(--space-9) */
--processing-footer-height: 64px; /* var(--space-9) */
}

* {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.root {
--content-height: calc(var(--layout-content-height) - var(--app-footer-height));
--content-height: calc(var(--layout-content-height) - var(--processing-footer-height));

height: var(--layout-content-height);
}
Expand Down
29 changes: 29 additions & 0 deletions apps/frontend/src/app/home/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Flex, ScrollArea } from '@radix-ui/themes'

import { ImagePreview } from '@widgets/ImagePreview'
import { SettingsPanel } from '@widgets/SettingsPanel'
import { ProcessingFooter } from '@widgets/ProcessingFooter'
import type { FlexDirection } from '@lib/theme'
import styles from './page.module.css'

const mainDirection: FlexDirection = {
initial: 'column',
md: 'row'
} as const

const HomePage = () => (
<Flex width='100%' align='center' direction='column' className={styles.root}>
<ScrollArea scrollbars='vertical' size='1' className={styles.scrollArea}>
<Flex asChild justify='end' direction={mainDirection} width='100%' className={styles.main}>
<main>
<ImagePreview />
<SettingsPanel />
</main>
</Flex>
</ScrollArea>

<ProcessingFooter />
</Flex>
)

export default HomePage
29 changes: 1 addition & 28 deletions apps/frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1 @@
import { Flex, ScrollArea } from '@radix-ui/themes'

import { Preview } from '@widgets/Preview'
import { SettingsPanel } from '@widgets/SettingsPanel'
import { AppFooter } from '@widgets/AppFooter'
import type { FlexDirection } from '@lib/theme'
import styles from './page.module.css'

const mainDirection: FlexDirection = {
initial: 'column',
md: 'row'
} as const

const HomePage = () => (
<Flex width='100%' align='center' direction='column' className={styles.root}>
<ScrollArea scrollbars='vertical' size='1' className={styles.scrollArea}>
<Flex asChild justify='end' direction={mainDirection} width='100%' className={styles.main}>
<main>
<Preview />
<SettingsPanel />
</main>
</Flex>
</ScrollArea>
<AppFooter />
</Flex>
)

export default HomePage
export { default } from './home/page'
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const CookieConsentBanner = () => {
<Card
size='2'
style={{
bottom: isHomePage ? 'calc(var(--app-footer-height) + 20px)' : '20px'
bottom: isHomePage ? 'calc(var(--ProcessingFooter-height) + 20px)' : '20px'
}}
className={styles.root}
>
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/design-system/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { type HTMLAttributes, memo, useId } from 'react'
import { Checkbox as RadixCheckbox, Flex, Text } from '@radix-ui/themes'
import { clsx } from 'clsx'

import styles from './Checkbox.module.css'
import type { JustifyContent } from '@lib/theme'
import styles from './Checkbox.module.css'

export const rootTestId = 'checkbox-root'

Expand Down
10 changes: 7 additions & 3 deletions apps/frontend/src/hooks/useEscapeAction.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
'use client'

import { useEffect } from 'react'

const ESCAPE_KEY = 'Escape'

export const useEscapeAction = (
callback: VoidFunction,
options?: boolean | AddEventListenerOptions
) => {
useEffect(() => {
const handleEscapeKeydown = (ev: KeyboardEvent) => {
if (ev.key !== 'Escape') return
if (ev.key !== ESCAPE_KEY) return

callback()
}

window.addEventListener('keydown', handleEscapeKeydown, options)
document.addEventListener('keydown', handleEscapeKeydown, options)

return () => {
window.removeEventListener('keydown', handleEscapeKeydown, options)
document.removeEventListener('keydown', handleEscapeKeydown, options)
}
}, [callback, options])
}
6 changes: 3 additions & 3 deletions apps/frontend/src/hooks/useEscapeBlur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import type { RefObject } from 'react'

import { useEscapeAction } from './useEscapeAction'

interface Params<T extends HTMLElement> {
ref: RefObject<T>
interface Params<El extends HTMLElement> {
ref: RefObject<El>
options?: boolean | AddEventListenerOptions
}

export const useEscapeBlur = <T extends HTMLElement>({ ref, options }: Params<T>) => {
export const useEscapeBlur = <El extends HTMLElement>({ ref, options }: Params<El>) => {
useEscapeAction(() => {
if (!ref.current) return

Expand Down
2 changes: 2 additions & 0 deletions apps/frontend/src/lib/ui/ColorField/ColorField.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client'

// https://github.com/radix-ui/website/blob/67f9736819e5a03f863e8e56f366fa51637845f7/components/ColorField.tsx

import {
Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/src/stores/__tests__/output.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// vitest-environment jsdom

import pick from 'lodash.pick'

import { IMAGE_FILE_FORMAT } from '@scissors/sharp'

import { createOutputStore, defaultState, type DownloadPayload } from '@stores/output'
import pick from 'lodash.pick'

describe('@stores/output', () => {
const store = createOutputStore({
Expand Down
7 changes: 5 additions & 2 deletions apps/frontend/src/stores/hooks/useExportSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { useConvertSettings } from './useConvertSettings'
import { useResizeSettings } from './useResizeSettings'
import { TOOLBAR_TAB, type ToolbarTab } from '@stores/tabs'
import { createAndDownloadJSONFile } from '@utility/json-file'
import { SITE_TITLE } from '@site/config'

const FILE_NAME_PREFIX = `${SITE_TITLE}-settings`

export const useExportSettings = (selectedTab: ToolbarTab) => {
const convertSettings = useConvertSettings()
Expand All @@ -13,12 +16,12 @@ export const useExportSettings = (selectedTab: ToolbarTab) => {
switch (selectedTab) {
case TOOLBAR_TAB.CONVERT:
return {
fileName: 'scissors-settings-convert',
fileName: `${FILE_NAME_PREFIX}-convert`,
settings: convertSettings
}
case TOOLBAR_TAB.RESIZE:
return {
fileName: 'scissors-settings-resize',
fileName: `${FILE_NAME_PREFIX}-resize`,
settings: resizeSettings
}
default:
Expand Down
13 changes: 6 additions & 7 deletions apps/frontend/src/stores/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ImageFileFormat } from '@scissors/sharp'
import { isValidFileName } from '@helpers/file/isValidFileName'
import { cropFileName } from '@helpers/file/cropFileName'
import { cropImageFileType } from '@helpers/file/cropImageFileType'
import { SITE_TITLE } from '@site/config'

export interface DownloadPayload {
link: string
Expand Down Expand Up @@ -151,12 +152,12 @@ const outputStoreCreator: StateCreator<Store> = (set, get) => ({
})

export const createOutputStore = ({
withPersist
}: CreateStoreParams): UseBoundStore<StoreApi<Store>> => {
withPersist = true
}: CreateStoreParams | undefined = {}): UseBoundStore<StoreApi<Store>> => {
if (withPersist) {
return create(
persist<Store>(outputStoreCreator, {
name: 'scissors-output-store',
name: `${SITE_TITLE}-output-store`,
merge: <State>(persistedState: unknown, currentState: State): State => {
if (!persistedState || typeof persistedState !== 'object') {
return currentState
Expand All @@ -179,9 +180,7 @@ export const createOutputStore = ({
}

interface CreateStoreParams {
withPersist: boolean
withPersist?: boolean
}

export const useOutputStore = createOutputStore({
withPersist: true
})
export const useOutputStore = createOutputStore()
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
RESIZE_OPERATION,
RESIZE_POSITION
} from '@scissors/sharp'

import { hexValidationRegex } from '@helpers/colors'

export const booleanSchema = boolean().defined()
Expand Down
18 changes: 0 additions & 18 deletions apps/frontend/src/widgets/AppFooter/AppFooter.tsx

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion apps/frontend/src/widgets/AppFooter/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { UploadedFileLoading } from './UploadedFile/UploadedFileLoading'
import { ImageUploaderLoading } from '@components/ImageUploader/ImageUploaderLoading'
import { BackgroundGrid } from '@ui/BackgroundGrid'
import { useOutputStore } from '@stores/output'
import styles from './Preview.module.css'
import styles from './ImagePreview.module.css'

const ImageUploader = dynamic(
() => import('@components/ImageUploader').then(mod => mod.ImageUploader),
Expand All @@ -22,7 +22,7 @@ const UploadedFile = dynamic(() => import('./UploadedFile').then(mod => mod.Uplo
loading: () => <UploadedFileLoading />
})

export const Preview = () => {
export const ImagePreview = () => {
const file = useOutputStore(state => state.file)

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.root {
position: relative;
height: calc(var(--layout-content-height) - var(--app-footer-height) - 200px);
height: calc(var(--layout-content-height) - var(--processing-footer-height) - 200px);
}

.image {
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/src/widgets/ImagePreview/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ImagePreview } from './ImagePreview'
1 change: 0 additions & 1 deletion apps/frontend/src/widgets/Preview/index.ts

This file was deleted.

18 changes: 18 additions & 0 deletions apps/frontend/src/widgets/ProcessingFooter/ProcessingFooter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import dynamic from 'next/dynamic'
import { Box } from '@radix-ui/themes'

import { ProcessingFooterContentSkeleton } from './ProcessingFooterContentSkeleton'
import styles from './ProcessingFooter.module.css'

const ProcessingFooterContent = dynamic(() => import('./ProcessingFooterContent'), {
ssr: false,
loading: () => <ProcessingFooterContentSkeleton />
})

export const ProcessingFooter = () => (
<Box asChild height='var(--processing-footer-height)' width='100%' px='4' className={styles.root}>
<footer>
<ProcessingFooterContent />
</footer>
</Box>
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import dynamic from 'next/dynamic'
import { Flex, Separator } from '@radix-ui/themes'

import { ButtonDownloadSkeleton, ButtonRequestSkeleton } from '../AppFooterContentSkeleton'
import { ButtonDownloadImageSkeleton, ButtonRequestSkeleton } from '../ProcessingFooterContentSkeleton'
import { TOOLBAR_TAB, useTabsStore } from '@stores/tabs'

const ButtonDownload = dynamic(
() => import('./buttons/ButtonDownload').then(mod => mod.ButtonDownload),
const ButtonDownloadImage = dynamic(
() => import('./buttons/ButtonDownloadImage').then(mod => mod.ButtonDownloadImage),
{
ssr: false,
loading: () => <ButtonDownloadSkeleton />
loading: () => <ButtonDownloadImageSkeleton />
}
)
const ButtonConvert = dynamic(
Expand All @@ -32,12 +32,12 @@ const ButtonMetadata = dynamic(
}
)

const AppFooterContent = () => {
const ProcessingFooterContent = () => {
const selectedTab = useTabsStore(state => state.selectedTab)

return (
<Flex align='center' justify='end' gap='3' height='100%' width='100%'>
<ButtonDownload />
<ButtonDownloadImage />

<Separator orientation='vertical' size='2' />

Expand All @@ -51,4 +51,4 @@ const AppFooterContent = () => {
/*
* Default export is required to import a client component inside a server component using next/dynamic.
*/
export default AppFooterContent
export default ProcessingFooterContent
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useRef } from 'react'
import { Button, Link as RadixLink } from '@radix-ui/themes'
import { Button } from '@radix-ui/themes'
import MediaQuery from 'react-responsive'

import { DownloadIcon } from '@scissors/react-icons/DownloadIcon'

import { useOutputStore } from '@stores/output'
import { TOUR_STEP } from '@lib/tour'

export const ButtonDownload = () => {
export const ButtonDownloadImage = () => {
const linkRef = useRef<HTMLAnchorElement>(null)

const downloadPayload = useOutputStore(state => state.downloadPayload)
Expand Down Expand Up @@ -36,7 +36,7 @@ export const ButtonDownload = () => {
</Button>

{downloadPayload && (
<RadixLink
<a
ref={linkRef}
href={downloadPayload.link}
download={downloadPayload.fileName}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './ProcessingFooterContent'
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Flex, Separator, Skeleton } from '@radix-ui/themes'

import styles from './AppFooterContentSkeleton.module.css'
import styles from './ProcessingFooterContentSkeleton.module.css'

export const ButtonDownloadSkeleton = () => (
export const ButtonDownloadImageSkeleton = () => (
<Skeleton height='40px' className={styles.buttonDownload} />
)

export const ButtonRequestSkeleton = () => (
<Skeleton height='40px' className={styles.buttonRequest} />
)

export const AppFooterContentSkeleton = () => (
export const ProcessingFooterContentSkeleton = () => (
<Flex align='center' justify='end' gap='3' height='100%' width='100%'>
<ButtonDownloadSkeleton />
<ButtonDownloadImageSkeleton />
<Separator orientation='vertical' size='2' />
<ButtonRequestSkeleton />
</Flex>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
ProcessingFooterContentSkeleton,
ButtonDownloadImageSkeleton,
ButtonRequestSkeleton
} from './ProcessingFooterContentSkeleton'
Loading

0 comments on commit 2a15ca5

Please sign in to comment.