-
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.
chore(web): basic publish tab in beta (#606)
- Loading branch information
Showing
17 changed files
with
1,081 additions
and
103 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
121 changes: 121 additions & 0 deletions
121
web/src/beta/features/Editor/tabs/publish/Nav/PublishModal/hooks.ts
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,121 @@ | ||
import { useState, useEffect, useCallback } from "react"; | ||
|
||
import { publishingType } from "@reearth/classic/components/molecules/EarthEditor/Header/index"; | ||
import generateRandomString from "@reearth/classic/util/generate-random-string"; | ||
|
||
export type PublishStatus = "published" | "limited" | "unpublished"; | ||
|
||
export type Validation = "too short" | "not match"; | ||
export type CopiedItemKey = { | ||
url?: boolean; | ||
embedCode?: boolean; | ||
}; | ||
|
||
export default ( | ||
publishing?: publishingType, | ||
publishStatus?: PublishStatus, | ||
defaultAlias?: string, | ||
onPublish?: (publishStatus: PublishStatus) => Promise<void>, | ||
onClose?: () => void, | ||
onAliasValidate?: (alias: string) => void, | ||
onCopyToClipBoard?: () => void, | ||
) => { | ||
const [copiedKey, setCopiedKey] = useState<CopiedItemKey>(); | ||
const [alias, changeAlias] = useState(defaultAlias); | ||
const [validation, changeValidation] = useState<Validation>(); | ||
const [statusChanged, setStatusChange] = useState(false); | ||
const [showOptions, setOptions] = useState(!defaultAlias); | ||
const [searchIndex, setSearchIndex] = useState(false); | ||
|
||
useEffect(() => { | ||
setSearchIndex(!!(publishStatus === "published")); | ||
}, [publishStatus]); | ||
|
||
const handleSearchIndexChange = useCallback(() => { | ||
setSearchIndex(!searchIndex); | ||
}, [searchIndex]); | ||
|
||
const handleCopyToClipBoard = useCallback( | ||
(key: keyof CopiedItemKey, value: string | undefined) => | ||
(_: React.MouseEvent<HTMLSpanElement, MouseEvent>) => { | ||
if (!value) return; | ||
setCopiedKey(prevState => ({ | ||
...prevState, | ||
[key]: true, | ||
})); | ||
navigator.clipboard.writeText(value); | ||
onCopyToClipBoard?.(); | ||
}, | ||
[onCopyToClipBoard], | ||
); | ||
|
||
const validate = useCallback( | ||
(a?: string) => { | ||
if (!a) { | ||
changeValidation(undefined); | ||
return; | ||
} | ||
if (a.length < 5) { | ||
changeValidation("too short"); | ||
} else if (!/^[A-Za-z0-9_-]*$/.test(a)) { | ||
changeValidation("not match"); | ||
} else { | ||
changeValidation(undefined); | ||
onAliasValidate?.(a); | ||
} | ||
}, | ||
[onAliasValidate], | ||
); | ||
|
||
const onAliasChange = useCallback( | ||
(value?: string) => { | ||
const a = value || generateAlias(); | ||
changeAlias(a); | ||
validate(a); | ||
}, | ||
[validate], // eslint-disable-line react-hooks/exhaustive-deps | ||
); | ||
|
||
const handleClose = useCallback(() => { | ||
onClose?.(); | ||
onAliasChange(defaultAlias); | ||
setStatusChange(false); | ||
setOptions(defaultAlias ? false : true); | ||
}, [onClose, defaultAlias]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
const generateAlias = useCallback(() => { | ||
const str = generateRandomString(10); | ||
changeAlias(str); | ||
return str; | ||
}, []); | ||
|
||
useEffect(() => { | ||
onAliasChange(defaultAlias); | ||
}, [defaultAlias]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
const handlePublish = useCallback(async () => { | ||
if (!publishing) return; | ||
const mode = | ||
publishing === "unpublishing" ? "unpublished" : !searchIndex ? "limited" : "published"; | ||
await onPublish?.(mode); | ||
if (publishing === "unpublishing") { | ||
handleClose?.(); | ||
} else { | ||
setStatusChange(true); | ||
} | ||
}, [onPublish, publishing, searchIndex, setStatusChange, handleClose]); | ||
|
||
return { | ||
statusChanged, | ||
alias, | ||
validation, | ||
copiedKey, | ||
showOptions, | ||
searchIndex, | ||
handlePublish, | ||
handleClose, | ||
handleCopyToClipBoard, | ||
handleSearchIndexChange, | ||
setOptions, | ||
}; | ||
}; |
Oops, something went wrong.