-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create page with all icons from the sprite * add info about file size * refactoring
- Loading branch information
Showing
28 changed files
with
227 additions
and
25 deletions.
There are no files selected for viewing
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import fs from 'fs'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import path from 'path'; | ||
|
||
import config from 'configs/app'; | ||
|
||
const ROOT_DIR = './icons'; | ||
const NAME_PREFIX = ROOT_DIR.replace('./', '') + '/'; | ||
|
||
interface IconInfo { | ||
name: string; | ||
fileSize: number; | ||
} | ||
|
||
const getIconName = (filePath: string) => filePath.replace(NAME_PREFIX, '').replace('.svg', ''); | ||
|
||
function collectIconNames(dir: string) { | ||
const files = fs.readdirSync(dir, { withFileTypes: true }); | ||
let icons: Array<IconInfo> = []; | ||
|
||
files.forEach((file) => { | ||
const filePath = path.join(dir, file.name); | ||
const stats = fs.statSync(filePath); | ||
|
||
file.name.endsWith('.svg') && icons.push({ | ||
name: getIconName(filePath), | ||
fileSize: stats.size, | ||
}); | ||
|
||
if (file.isDirectory()) { | ||
icons = [ ...icons, ...collectIconNames(filePath) ]; | ||
} | ||
}); | ||
|
||
return icons; | ||
} | ||
|
||
export default async function spriteHandler(req: NextApiRequest, res: NextApiResponse) { | ||
|
||
if (!config.app.isDev) { | ||
return res.status(404).json({ error: 'Not found' }); | ||
} | ||
|
||
const icons = collectIconNames(ROOT_DIR); | ||
|
||
res.status(200).json({ | ||
icons, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { NextPage } from 'next'; | ||
import React from 'react'; | ||
|
||
import PageNextJs from 'nextjs/PageNextJs'; | ||
|
||
import Sprite from 'ui/pages/Sprite'; | ||
|
||
const Page: NextPage = () => { | ||
return ( | ||
<PageNextJs pathname="/sprite"> | ||
<Sprite/> | ||
</PageNextJs> | ||
); | ||
}; | ||
|
||
export default Page; | ||
|
||
export { dev as getServerSideProps } from 'nextjs/getServerSideProps'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Flex, Box, Tooltip, useClipboard, useColorModeValue } from '@chakra-ui/react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import React from 'react'; | ||
|
||
import type { StaticRoute } from 'nextjs-routes'; | ||
import { route } from 'nextjs-routes'; | ||
|
||
import useFetch from 'lib/hooks/useFetch'; | ||
import ContentLoader from 'ui/shared/ContentLoader'; | ||
import DataFetchAlert from 'ui/shared/DataFetchAlert'; | ||
import EmptySearchResult from 'ui/shared/EmptySearchResult'; | ||
import FilterInput from 'ui/shared/filters/FilterInput'; | ||
import type { IconName } from 'ui/shared/IconSvg'; | ||
import IconSvg from 'ui/shared/IconSvg'; | ||
import PageTitle from 'ui/shared/Page/PageTitle'; | ||
|
||
const formatFileSize = (fileSizeInBytes: number) => `${ (fileSizeInBytes / 1_024).toFixed(2) } Kb`; | ||
|
||
interface IconInfo { | ||
name: string; | ||
fileSize: number; | ||
} | ||
|
||
const Item = ({ name, fileSize, bgColor }: IconInfo & { bgColor: string }) => { | ||
const { hasCopied, onCopy } = useClipboard(name, 1000); | ||
const [ copied, setCopied ] = React.useState(false); | ||
|
||
React.useEffect(() => { | ||
if (hasCopied) { | ||
setCopied(true); | ||
} else { | ||
setCopied(false); | ||
} | ||
}, [ hasCopied ]); | ||
|
||
return ( | ||
<Flex | ||
flexDir="column" | ||
alignItems="center" | ||
whiteSpace="pre-wrap" | ||
wordBreak="break-word" | ||
maxW="100px" | ||
textAlign="center" | ||
onClick={ onCopy } | ||
cursor="pointer" | ||
> | ||
<IconSvg name={ name as IconName } boxSize="100px" bgColor={ bgColor } borderRadius="base"/> | ||
<Tooltip label={ copied ? 'Copied' : 'Copy to clipboard' } isOpen={ copied }> | ||
<Box fontWeight={ 500 } mt={ 2 }>{ name }</Box> | ||
</Tooltip> | ||
<Box color="text_secondary">{ formatFileSize(fileSize) }</Box> | ||
</Flex> | ||
); | ||
}; | ||
|
||
const Sprite = () => { | ||
const [ searchTerm, setSearchTerm ] = React.useState(''); | ||
const bgColor = useColorModeValue('blackAlpha.100', 'whiteAlpha.100'); | ||
|
||
const fetch = useFetch(); | ||
const { data, isFetching, isError } = useQuery({ | ||
queryKey: [ 'sprite' ], | ||
queryFn: () => { | ||
const url = route({ pathname: '/node-api/sprite' as StaticRoute<'/api/sprite'>['pathname'] }); | ||
return fetch<{ icons: Array<IconInfo> }, unknown>(url); | ||
}, | ||
}); | ||
|
||
const content = (() => { | ||
if (isFetching) { | ||
return <ContentLoader/>; | ||
} | ||
|
||
if (isError || !data || !('icons' in data)) { | ||
return <DataFetchAlert/>; | ||
} | ||
|
||
const items = data.icons.filter((icon) => icon.name.includes(searchTerm)); | ||
|
||
if (items.length === 0) { | ||
return <EmptySearchResult text="No icons found"/>; | ||
} | ||
|
||
return ( | ||
<Flex flexWrap="wrap" fontSize="sm" columnGap={ 5 } rowGap={ 5 } justifyContent="flex-start"> | ||
{ items.map((item) => <Item key={ item.name } { ...item } bgColor={ bgColor }/>) } | ||
</Flex> | ||
); | ||
})(); | ||
|
||
const total = React.useMemo(() => { | ||
if (!data || !('icons' in data)) { | ||
return; | ||
} | ||
return data?.icons.reduce((result, item) => { | ||
result.num++; | ||
result.fileSize += item.fileSize; | ||
return result; | ||
}, { num: 0, fileSize: 0 }); | ||
}, [ data ]); | ||
|
||
const searchInput = <FilterInput placeholder="Search by name..." onChange={ setSearchTerm } isLoading={ isFetching } minW={{ base: '100%', lg: '300px' }}/>; | ||
const totalEl = total ? <Box ml="auto">Items: { total.num } / Size: { formatFileSize(total.fileSize) }</Box> : null; | ||
|
||
const contentAfter = ( | ||
<> | ||
{ totalEl } | ||
{ searchInput } | ||
</> | ||
); | ||
|
||
return ( | ||
<div> | ||
<PageTitle title="SVG sprite 🥤" contentAfter={ contentAfter }/> | ||
{ content } | ||
</div> | ||
); | ||
}; | ||
|
||
export default React.memo(Sprite); |
Oops, something went wrong.