Skip to content

Commit

Permalink
refactor(frontend): use helper func instead of hook for json export
Browse files Browse the repository at this point in the history
  • Loading branch information
MiracleHorizon committed May 24, 2024
1 parent 2145915 commit aa035ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import type { ComponentPropsWithoutRef } from 'react'

import { ButtonDownload } from '@ui/ButtonDownload'
import { GallerySlideDetailItem } from './GallerySlideDetailItem'
import { useExportJSON } from '@hooks/useExportJSON'
import { createAndDownloadJSONFile } from '@utility/json-file'
import { geistMono } from '@app/fonts'
import { SITE_TITLE } from '@site/config'
import type { ClassNameProps } from '@app-types/ClassNameProps'
import styles from './GallerySlideInfo.module.css'

Expand Down Expand Up @@ -35,11 +36,9 @@ export const GallerySlideInfo = ({ index, label, settings, details, className }:
)

const ExportSettings = ({ index, settings }: Pick<Props, 'index' | 'settings'>) => {
const { handleExportJSON } = useExportJSON()

const handleExport = () =>
handleExportJSON({
fileName: `scissors-slide-${index + 1}-settings`,
createAndDownloadJSONFile({
fileName: `${SITE_TITLE.toLowerCase()}-slide-${index + 1}-settings`,
payload: settings
})

Expand Down
20 changes: 0 additions & 20 deletions apps/frontend/src/hooks/useExportJSON.ts

This file was deleted.

8 changes: 3 additions & 5 deletions apps/frontend/src/stores/hooks/useExportSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { useCallback } from 'react'

import { useConvertSettings } from './useConvertSettings'
import { useResizeSettings } from './useResizeSettings'
import { useExportJSON } from '@hooks/useExportJSON'
import { TOOLBAR_TAB, type ToolbarTab } from '@stores/tabs'
import { createAndDownloadJSONFile } from '@utility/json-file'

export const useExportSettings = (selectedTab: ToolbarTab) => {
const { handleExportJSON } = useExportJSON()

const convertSettings = useConvertSettings()
const resizeSettings = useResizeSettings()

Expand All @@ -34,11 +32,11 @@ export const useExportSettings = (selectedTab: ToolbarTab) => {

const { fileName, settings: payload } = exportPayload

handleExportJSON({
createAndDownloadJSONFile({
fileName,
payload
})
}, [collectExportPayload, handleExportJSON])
}, [collectExportPayload])

return { exportSettings }
}
21 changes: 21 additions & 0 deletions apps/frontend/src/utility/json-file.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { downloadFile } from './export'

/**
* @param file - JSON file to read
* @returns parsed JSON object or rejects with error
Expand Down Expand Up @@ -34,3 +36,22 @@ export const createJSONBlob = <T>(payload: T): Blob => {
type: 'application/json'
})
}

/**
* @param payload - any value to convert to JSON
* @param fileName - name of the file without extension
*/
export const createAndDownloadJSONFile = ({
payload,
fileName
}: {
payload: Record<string, any>
fileName: string
}) => {
const blob = createJSONBlob(payload)

downloadFile({
blob,
download: `${fileName}.json`
})
}

0 comments on commit aa035ee

Please sign in to comment.