Feat/tag selector (#485)
This commit is contained in:
+33
-13
@@ -1,4 +1,5 @@
|
||||
import { forwardRef } from "react"
|
||||
import { forwardRef, useCallback, useMemo } from "react"
|
||||
import type { ReactElement } from "react"
|
||||
|
||||
import { cn } from "~/lib/utils"
|
||||
|
||||
@@ -12,8 +13,11 @@ type InputProps<TMultiline extends boolean> = {
|
||||
error?: string
|
||||
help?: React.ReactNode
|
||||
multiline?: TMultiline
|
||||
renderInput?: (props: Omit<InputProps<false>, "renderInput">) => ReactElement
|
||||
} & React.ComponentPropsWithRef<TMultiline extends true ? "textarea" : "input">
|
||||
|
||||
export type CustomInputProps = Omit<InputProps<false>, "renderInput">
|
||||
|
||||
export const Input = forwardRef(function Input<
|
||||
TMultiline extends boolean = false,
|
||||
>(
|
||||
@@ -26,6 +30,7 @@ export const Input = forwardRef(function Input<
|
||||
error,
|
||||
help,
|
||||
multiline,
|
||||
renderInput,
|
||||
...inputProps
|
||||
}: InputProps<TMultiline>,
|
||||
ref: TMultiline extends true
|
||||
@@ -34,7 +39,32 @@ export const Input = forwardRef(function Input<
|
||||
) {
|
||||
const hasAddon = !!addon
|
||||
const hasPrefix = !!prefix
|
||||
const Component = (multiline ? "textarea" : "input") as any
|
||||
|
||||
const inputComponentProps = useMemo(
|
||||
() =>
|
||||
({
|
||||
...inputProps,
|
||||
ref,
|
||||
className: cn(
|
||||
"input",
|
||||
hasAddon && `has-addon`,
|
||||
hasPrefix && `has-prefix`,
|
||||
isBlock && `is-block`,
|
||||
className,
|
||||
),
|
||||
} as any),
|
||||
[className, hasAddon, hasPrefix, inputProps, isBlock, ref],
|
||||
)
|
||||
|
||||
const renderInputComponent = useCallback(() => {
|
||||
if (renderInput) {
|
||||
return renderInput(inputComponentProps)
|
||||
} else if (multiline) {
|
||||
return <textarea {...inputComponentProps} />
|
||||
} else {
|
||||
return <input {...inputComponentProps} />
|
||||
}
|
||||
}, [inputComponentProps, multiline, renderInput])
|
||||
|
||||
return (
|
||||
<div>
|
||||
@@ -45,17 +75,7 @@ export const Input = forwardRef(function Input<
|
||||
{prefix}
|
||||
</span>
|
||||
)}
|
||||
<Component
|
||||
{...inputProps}
|
||||
ref={ref as any}
|
||||
className={cn(
|
||||
"input",
|
||||
hasAddon && `has-addon`,
|
||||
hasPrefix && `has-prefix`,
|
||||
isBlock && `is-block`,
|
||||
className,
|
||||
)}
|
||||
/>
|
||||
{renderInputComponent()}
|
||||
{addon && (
|
||||
<span className="flex items-center px-3 text-gray-600 bg-gray-50 h-10 border border-l-0 rounded-r-lg relative -z-10">
|
||||
{addon}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import { Fragment, useState } from "react"
|
||||
import { toast } from "react-hot-toast"
|
||||
import { shallow } from "zustand/shallow"
|
||||
|
||||
import { Combobox, Transition } from "@headlessui/react"
|
||||
import { ChevronUpDownIcon, XMarkIcon } from "@heroicons/react/20/solid"
|
||||
|
||||
import { useEditorState } from "~/hooks/useEdtiorState"
|
||||
import { cn } from "~/lib/utils"
|
||||
|
||||
import { CustomInputProps } from "./Input"
|
||||
|
||||
type Props = CustomInputProps & {
|
||||
userTags: string[]
|
||||
onTagChange: (tags: string) => void
|
||||
}
|
||||
|
||||
export function TagInput({
|
||||
userTags = [],
|
||||
value,
|
||||
onTagChange,
|
||||
...restProps
|
||||
}: Props) {
|
||||
const [selected, setSelected] = useState("")
|
||||
const [query, setQuery] = useState("")
|
||||
|
||||
const { editorTags, setValues } = useEditorState(
|
||||
(state) => ({
|
||||
editorTags: state.tags === "" ? [] : state.tags?.split(","),
|
||||
setValues: state.setValues,
|
||||
}),
|
||||
shallow,
|
||||
)
|
||||
|
||||
const onChange = (type: "add" | "delete", value: string) => {
|
||||
let tags = ""
|
||||
if (type === "add") {
|
||||
if (editorTags.includes(value)) {
|
||||
toast.error("Duplicate tag")
|
||||
return
|
||||
}
|
||||
tags = [...editorTags, value].join(",")
|
||||
} else {
|
||||
tags = editorTags.filter((tag) => tag !== value).join(",")
|
||||
}
|
||||
|
||||
setValues({ tags })
|
||||
onTagChange(tags)
|
||||
}
|
||||
|
||||
const onComboboxChange = (value: string) => {
|
||||
onChange("add", value)
|
||||
}
|
||||
|
||||
const onDel = (value: string) => {
|
||||
onChange("delete", value)
|
||||
}
|
||||
|
||||
const filteredTags =
|
||||
query === ""
|
||||
? userTags
|
||||
: Array.from(new Set([query, ...userTags])).filter((tag) =>
|
||||
tag
|
||||
.toLowerCase()
|
||||
.replace(/\s+/g, "")
|
||||
.includes(query.toLowerCase().replace(/\s+/g, "")),
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="w-full">
|
||||
<div className="flex flex-wrap gap-2 mb-2">
|
||||
{editorTags.map((tag) => (
|
||||
<div
|
||||
className="text-xs flex items-center font-semibold p-1 rounded-md bg-accent text-white"
|
||||
key={tag}
|
||||
>
|
||||
<span>{tag}</span>
|
||||
<XMarkIcon className="h-5 w-5" onClick={() => onDel(tag)} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<Combobox value={selected} onChange={onComboboxChange}>
|
||||
<div className="relative mt-1">
|
||||
<div className="relative flex w-full cursor-default overflow-hidden rounded-lg bg-white text-left sm:text-sm">
|
||||
<Combobox.Input
|
||||
{...restProps}
|
||||
onChange={(event) => setQuery(event.target.value)}
|
||||
autoComplete="off"
|
||||
/>
|
||||
<Combobox.Button className="absolute inset-y-0 right-0 flex items-center pr-2">
|
||||
<ChevronUpDownIcon
|
||||
className="h-5 w-5 text-black"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
</Combobox.Button>
|
||||
</div>
|
||||
<Transition
|
||||
as={Fragment}
|
||||
leave="transition ease-in duration-100"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
afterLeave={() => setQuery("")}
|
||||
>
|
||||
<Combobox.Options className="absolute max-h-60 w-full overflow-auto rounded-md bg-white p-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 sm:text-sm">
|
||||
{filteredTags.map((tag) => (
|
||||
<Combobox.Option
|
||||
key={tag}
|
||||
className={({ active }) =>
|
||||
cn(
|
||||
"relative cursor-default select-none py-2 px-4",
|
||||
active ? "bg-accent text-white" : "text-black",
|
||||
)
|
||||
}
|
||||
value={tag}
|
||||
>
|
||||
<span className={cn("block truncate font-normal")}>
|
||||
{tag}
|
||||
</span>
|
||||
</Combobox.Option>
|
||||
))}
|
||||
</Combobox.Options>
|
||||
</Transition>
|
||||
</div>
|
||||
</Combobox>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { create } from "zustand"
|
||||
|
||||
export const initialEditorState = {
|
||||
title: "",
|
||||
publishedAt: new Date().toISOString(),
|
||||
published: false,
|
||||
excerpt: "",
|
||||
slug: "",
|
||||
tags: "",
|
||||
content: "",
|
||||
}
|
||||
export type Values = typeof initialEditorState
|
||||
|
||||
export const useEditorState = create<
|
||||
typeof initialEditorState & {
|
||||
setValues: (values: Partial<typeof initialEditorState>) => void
|
||||
}
|
||||
>((set) => ({
|
||||
...initialEditorState,
|
||||
setValues: (values: any) => set(values),
|
||||
}))
|
||||
@@ -11,12 +11,12 @@ import {
|
||||
memo,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react"
|
||||
import type { ReactElement } from "react"
|
||||
import toast from "react-hot-toast"
|
||||
import { create } from "zustand"
|
||||
import { shallow } from "zustand/shallow"
|
||||
|
||||
import type { EditorView } from "@codemirror/view"
|
||||
@@ -33,9 +33,15 @@ import { CodeMirrorEditor } from "~/components/ui/CodeMirror"
|
||||
import { EditorToolbar } from "~/components/ui/EditorToolbar"
|
||||
import { Input } from "~/components/ui/Input"
|
||||
import { Modal } from "~/components/ui/Modal"
|
||||
import { TagInput } from "~/components/ui/TagInput"
|
||||
import { UniLink } from "~/components/ui/UniLink"
|
||||
import { toolbars } from "~/editor"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
import {
|
||||
Values,
|
||||
initialEditorState,
|
||||
useEditorState,
|
||||
} from "~/hooks/useEdtiorState"
|
||||
import { useGetState } from "~/hooks/useGetState"
|
||||
import { useIsMobileLayout } from "~/hooks/useMobileLayout"
|
||||
import { useSyncOnce } from "~/hooks/useSyncOnce"
|
||||
@@ -51,7 +57,11 @@ import { PageVisibilityEnum } from "~/lib/types"
|
||||
import { cn, pick } from "~/lib/utils"
|
||||
import { Rendered, renderPageContent } from "~/markdown"
|
||||
import { checkPageSlug } from "~/models/page.model"
|
||||
import { useCreateOrUpdatePage, useGetPage } from "~/queries/page"
|
||||
import {
|
||||
useCreateOrUpdatePage,
|
||||
useGetPage,
|
||||
useGetPagesBySiteLite,
|
||||
} from "~/queries/page"
|
||||
import { useGetSite } from "~/queries/site"
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = serverSidePropsHandler(
|
||||
@@ -71,27 +81,6 @@ const getInputDatetimeValue = (date: Date | string, dayjs: any) => {
|
||||
return str.substring(0, ((str.indexOf("T") | 0) + 6) | 0)
|
||||
}
|
||||
|
||||
const initialEditorState = {
|
||||
title: "",
|
||||
publishedAt: new Date().toISOString(),
|
||||
published: false,
|
||||
excerpt: "",
|
||||
slug: "",
|
||||
tags: "",
|
||||
content: "",
|
||||
}
|
||||
|
||||
const useEditorState = create<
|
||||
typeof initialEditorState & {
|
||||
setValues: (values: Partial<typeof initialEditorState>) => void
|
||||
}
|
||||
>((set) => ({
|
||||
...initialEditorState,
|
||||
setValues: (values: any) => set(values),
|
||||
}))
|
||||
|
||||
type Values = typeof initialEditorState
|
||||
|
||||
export default function SubdomainEditor() {
|
||||
const router = useRouter()
|
||||
const queryClient = useQueryClient()
|
||||
@@ -137,6 +126,30 @@ export default function SubdomainEditor() {
|
||||
handle: subdomain,
|
||||
})
|
||||
|
||||
const { data: posts = { pages: [] } } = useGetPagesBySiteLite({
|
||||
characterId: site.data?.characterId,
|
||||
limit: 100,
|
||||
type: "post",
|
||||
visibility: PageVisibilityEnum.Published,
|
||||
})
|
||||
|
||||
const userTags = useMemo(() => {
|
||||
const result = new Set<string>()
|
||||
|
||||
if (posts?.pages?.length) {
|
||||
for (const page of posts.pages) {
|
||||
for (const post of page.list) {
|
||||
post.metadata?.content?.tags?.forEach((tag) => {
|
||||
if (tag !== "post" && tag !== "page") {
|
||||
result.add(tag)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(result)
|
||||
}, [posts.pages])
|
||||
|
||||
const [visibility, setVisibility] = useState<PageVisibilityEnum>()
|
||||
|
||||
useEffect(() => {
|
||||
@@ -506,6 +519,7 @@ export default function SubdomainEditor() {
|
||||
updateValue={updateValue}
|
||||
isPost={isPost}
|
||||
subdomain={subdomain}
|
||||
userTags={userTags}
|
||||
/>
|
||||
)
|
||||
|
||||
@@ -667,6 +681,7 @@ export default function SubdomainEditor() {
|
||||
defaultSlug={defaultSlug}
|
||||
updateValue={updateValue}
|
||||
isPost={isPost}
|
||||
userTags={userTags}
|
||||
subdomain={subdomain}
|
||||
/>
|
||||
)}
|
||||
@@ -744,7 +759,8 @@ const EditorExtraProperties: FC<{
|
||||
|
||||
subdomain: string
|
||||
defaultSlug: string
|
||||
}> = memo(({ isPost, updateValue, subdomain, defaultSlug }) => {
|
||||
userTags: string[]
|
||||
}> = memo(({ isPost, updateValue, subdomain, defaultSlug, userTags }) => {
|
||||
const values = useEditorState(
|
||||
(state) => pick(state, ["publishedAt", "slug", "excerpt", "tags"]),
|
||||
shallow,
|
||||
@@ -819,10 +835,13 @@ const EditorExtraProperties: FC<{
|
||||
label={t("Tags") || ""}
|
||||
id="tags"
|
||||
isBlock
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
||||
updateValue("tags", e.target.value)
|
||||
}
|
||||
help={t("Separate multiple tags with English commas") + ` ","`}
|
||||
renderInput={(props) => (
|
||||
<TagInput
|
||||
{...props}
|
||||
userTags={userTags}
|
||||
onTagChange={(value: string) => updateValue("tags", value)}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user