-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: nina992 <[email protected]> Co-authored-by: KaWaite <[email protected]>
- Loading branch information
1 parent
af7d3b3
commit 8c4b5b3
Showing
14 changed files
with
551 additions
and
112 deletions.
There are no files selected for viewing
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,36 @@ | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import URLField, { Props } from "."; | ||
|
||
const meta: Meta<Props> = { | ||
component: URLField, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof URLField>; | ||
|
||
export const AssetImageType: Story = { | ||
args: { | ||
name: "Asset", | ||
description: "Defaul Asset Uploader", | ||
fileType: "asset", | ||
assetType: "image", | ||
}, | ||
}; | ||
|
||
export const AssetFileType: Story = { | ||
args: { | ||
name: "Asset", | ||
description: "Defaul Asset Uploader", | ||
fileType: "asset", | ||
assetType: "file", | ||
}, | ||
}; | ||
|
||
export const URLType: Story = { | ||
args: { | ||
name: "URL", | ||
description: "Defaul URL Input wrapper", | ||
fileType: "URL", | ||
}, | ||
}; |
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,176 @@ | ||
import React, { useCallback, useEffect, useState } from "react"; | ||
|
||
import Button from "@reearth/beta/components/Button"; | ||
import Property from "@reearth/beta/components/fields"; | ||
import TextInput from "@reearth/beta/components/fields/common/TextInput"; | ||
import useHooks from "@reearth/beta/features/Assets/AssetsQueriesHook/hooks"; | ||
import { | ||
FILE_FORMATS, | ||
IMAGE_FORMATS, | ||
VIDEO_FORMATS, | ||
} from "@reearth/beta/features/Assets/constants"; | ||
import { Asset } from "@reearth/beta/features/Assets/types"; | ||
import { useManageAssets } from "@reearth/beta/features/Assets/useManageAssets/hooks"; | ||
import ChooseAssetModal from "@reearth/beta/features/Modals/ChooseAssetModal"; | ||
import { checkIfFileType } from "@reearth/beta/utils/util"; | ||
import { useT } from "@reearth/services/i18n"; | ||
import { useNotification, useWorkspace } from "@reearth/services/state"; | ||
import { styled } from "@reearth/services/theme"; | ||
|
||
export type Props = { | ||
value?: string; | ||
onChange?: (value: string | undefined) => void; | ||
name?: string; | ||
description?: string; | ||
fileType?: "asset" | "URL"; | ||
assetType?: "image" | "file"; | ||
}; | ||
|
||
const URLField: React.FC<Props> = ({ name, description, value, fileType, assetType, onChange }) => { | ||
const t = useT(); | ||
const [open, setOpen] = useState(false); | ||
const [currentWorkspace] = useWorkspace(); | ||
const [, setNotification] = useNotification(); | ||
const [currentValue, setCurrentValue] = useState(value); | ||
|
||
const handleChange = useCallback( | ||
(inputValue?: string) => { | ||
if (!inputValue) return; | ||
|
||
if ( | ||
fileType === "asset" && | ||
!(checkIfFileType(inputValue, FILE_FORMATS) || checkIfFileType(inputValue, IMAGE_FORMATS)) | ||
) { | ||
setNotification({ | ||
type: "error", | ||
text: t("Wrong file format"), | ||
}); | ||
setCurrentValue(undefined); | ||
return; | ||
} else if (fileType === "URL" && !checkIfFileType(inputValue, VIDEO_FORMATS)) { | ||
setNotification({ | ||
type: "error", | ||
text: t("wrong Video URL Format"), | ||
}); | ||
setCurrentValue(undefined); | ||
return; | ||
} | ||
|
||
setCurrentValue(inputValue); | ||
onChange?.(inputValue); | ||
}, | ||
[fileType, onChange, setNotification, t], | ||
); | ||
|
||
const { | ||
assets, | ||
isLoading, | ||
hasMoreAssets, | ||
searchTerm, | ||
selectedAssets, | ||
selectAsset, | ||
handleGetMoreAssets, | ||
handleFileSelect, | ||
handleSortChange, | ||
handleSearchTerm, | ||
} = useHooks({ workspaceId: currentWorkspace?.id, onAssetSelect: handleChange }); | ||
|
||
const { localSearchTerm, wrapperRef, onScrollToBottom, handleSearchInputChange, handleSearch } = | ||
useManageAssets({ | ||
selectedAssets, | ||
searchTerm, | ||
isLoading, | ||
hasMoreAssets, | ||
onGetMore: handleGetMoreAssets, | ||
onSortChange: handleSortChange, | ||
onSearch: handleSearchTerm, | ||
}); | ||
|
||
const handleReset = useCallback(() => { | ||
const selectedAsset = assets?.find(a => a.url === currentValue); | ||
if (selectedAsset) { | ||
selectAsset([selectedAsset]); | ||
} | ||
}, [currentValue, assets, selectAsset]); | ||
|
||
useEffect(() => { | ||
if (value) { | ||
setCurrentValue(value); | ||
} | ||
}, [value]); | ||
|
||
useEffect(() => { | ||
handleReset(); | ||
}, [handleReset]); | ||
|
||
const handleClose = useCallback(() => { | ||
setOpen(false); | ||
handleReset(); | ||
}, [handleReset]); | ||
|
||
const handleClick = useCallback(() => setOpen(!open), [open]); | ||
|
||
const handleSelect = useCallback( | ||
(asset?: Asset) => { | ||
if (!asset) return; | ||
selectAsset(!selectedAssets.includes(asset) ? [asset] : []); | ||
}, | ||
[selectedAssets, selectAsset], | ||
); | ||
|
||
return ( | ||
<Property name={name} description={description}> | ||
<TextInput value={currentValue} onChange={handleChange} placeholder={t("Not set")} /> | ||
{fileType === "asset" && ( | ||
<ButtonWrapper> | ||
<AssetButton | ||
icon={assetType === "image" ? "imageStoryBlock" : "file"} | ||
text={t("Choose")} | ||
iconPosition="left" | ||
onClick={handleClick} | ||
/> | ||
<AssetButton | ||
icon="uploadSimple" | ||
text={t("Upload")} | ||
iconPosition="left" | ||
onClick={handleFileSelect} | ||
/> | ||
</ButtonWrapper> | ||
)} | ||
{open && ( | ||
<ChooseAssetModal | ||
open={open} | ||
assetType={assetType} | ||
localSearchTerm={localSearchTerm} | ||
selectedAssets={selectedAssets} | ||
wrapperRef={wrapperRef} | ||
assets={assets} | ||
isLoading={isLoading} | ||
hasMoreAssets={hasMoreAssets} | ||
searchTerm={searchTerm} | ||
onClose={handleClose} | ||
handleSearch={handleSearch} | ||
handleSearchInputChange={handleSearchInputChange} | ||
onGetMore={handleGetMoreAssets} | ||
onScrollToBottom={onScrollToBottom} | ||
onSelectAsset={handleSelect} | ||
onSelect={handleChange} | ||
/> | ||
)} | ||
</Property> | ||
); | ||
}; | ||
|
||
const AssetButton = styled(Button)<{ active?: boolean }>` | ||
cursor: pointer; | ||
padding: 4px; | ||
flex: 1; | ||
`; | ||
|
||
const ButtonWrapper = styled.div` | ||
display: flex; | ||
align-items: space-between; | ||
gap: 4px; | ||
`; | ||
|
||
export default URLField; |
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,5 @@ | ||
export const FILE_FORMATS = ".kml,.czml,.topojson,.geojson,.json,.gltf,.glb"; | ||
|
||
export const IMAGE_FORMATS = ".jpg,.jpeg,.png,.gif,.svg,.tiff,.webp"; | ||
|
||
export const VIDEO_FORMATS = ".mp4"; |
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,10 @@ | ||
export type Asset = { | ||
id: string; | ||
teamId: string; | ||
name: string; | ||
size: number; | ||
url: string; | ||
contentType: string; | ||
}; | ||
|
||
export type SortType = "date" | "name" | "size"; |
Oops, something went wrong.