make site navigation bar configurable
TODO: drag to reorder
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "sites" ADD COLUMN "navigation" JSONB;
|
||||
@@ -89,6 +89,7 @@ model Site {
|
||||
primaryDomainId String? @unique
|
||||
description String?
|
||||
twitter String?
|
||||
navigation Json?
|
||||
deletedAt DateTime?
|
||||
memberships Membership[]
|
||||
|
||||
|
||||
@@ -15,6 +15,10 @@ export const SettingsLayout: React.FC<{
|
||||
type === "site"
|
||||
? [
|
||||
{ text: "General", href: `/dashboard/${subdomain}/settings/general` },
|
||||
{
|
||||
text: "Navigation",
|
||||
href: `/dashboard/${subdomain}/settings/navigation`,
|
||||
},
|
||||
{ text: "Domains", href: `/dashboard/${subdomain}/settings/domains` },
|
||||
]
|
||||
: [{ text: "Profile", href: `/dashboard/${subdomain}/account/profile` }]
|
||||
|
||||
@@ -12,12 +12,12 @@ import { IS_PROD } from "~/lib/constants"
|
||||
import { OUR_DOMAIN } from "~/lib/env"
|
||||
import { useRouter } from "next/router"
|
||||
import { SEOHead } from "../common/SEOHead"
|
||||
import { SiteNavigationItem } from "~/lib/types"
|
||||
|
||||
type MenuLink = {
|
||||
text: string
|
||||
href?: string
|
||||
label: string
|
||||
url?: string
|
||||
onClick?: () => void
|
||||
hide?: boolean
|
||||
}
|
||||
|
||||
const Header: React.FC<{
|
||||
@@ -91,11 +91,11 @@ const Header: React.FC<{
|
||||
<div className="text-sm text-gray-400">
|
||||
<div className="flex items-center space-x-5">
|
||||
{links.map((link, i) => {
|
||||
const active = router.asPath === link.href
|
||||
const active = router.asPath === link.url
|
||||
return (
|
||||
<UniLink
|
||||
key={`${link.text}${i}`}
|
||||
href={link.href}
|
||||
key={`${link.label}${i}`}
|
||||
href={link.url}
|
||||
onClick={link.onClick}
|
||||
className={clsx(
|
||||
`h-10 flex items-center border-b-2 hover:border-gray-500 hover:text-gray-700`,
|
||||
@@ -104,7 +104,7 @@ const Header: React.FC<{
|
||||
: `border-transparent`
|
||||
)}
|
||||
>
|
||||
{link.text}
|
||||
{link.label}
|
||||
</UniLink>
|
||||
)
|
||||
})}
|
||||
@@ -121,6 +121,7 @@ export type SiteLayoutProps = {
|
||||
name: string
|
||||
description?: string | null
|
||||
icon?: string | null
|
||||
navigation?: SiteNavigationItem[] | null
|
||||
}
|
||||
isLoggedIn: boolean
|
||||
subscription?: { telegram?: boolean; email?: boolean } | null
|
||||
@@ -139,27 +140,20 @@ export const SiteLayout: React.FC<SiteLayoutProps> = ({
|
||||
const setLoginModalOpened = useStore((store) => store.setLoginModalOpened)
|
||||
|
||||
const links: MenuLink[] = [
|
||||
{ text: "Home", href: "/" },
|
||||
{
|
||||
text: "About",
|
||||
href: "/about",
|
||||
},
|
||||
{ text: "Archives", hide: true, href: "/archives" },
|
||||
{ label: "Home", url: "/" },
|
||||
...(site.navigation || []),
|
||||
!isLoggedIn && {
|
||||
text: "Log in",
|
||||
hide: true,
|
||||
label: "Log in",
|
||||
onClick() {
|
||||
setLoginModalOpened(true)
|
||||
},
|
||||
},
|
||||
isLoggedIn && {
|
||||
text: "Dashboard",
|
||||
hide: true,
|
||||
href: `${IS_PROD ? "https" : "http"}://${OUR_DOMAIN}/dashboard`,
|
||||
label: "Dashboard",
|
||||
url: `${IS_PROD ? "https" : "http"}://${OUR_DOMAIN}/dashboard`,
|
||||
},
|
||||
isLoggedIn && {
|
||||
text: "Log out",
|
||||
hide: true,
|
||||
label: "Log out",
|
||||
onClick() {
|
||||
logout()
|
||||
},
|
||||
|
||||
@@ -5,6 +5,7 @@ export const Button: React.FC<
|
||||
React.ButtonHTMLAttributes<HTMLButtonElement> & {
|
||||
isLoading?: boolean
|
||||
isBlock?: boolean
|
||||
isDisabled?: boolean
|
||||
variant?: "primary" | "secondary" | "text"
|
||||
variantColor?: "green" | "red" | "gray"
|
||||
size?: "sm"
|
||||
@@ -15,6 +16,7 @@ export const Button: React.FC<
|
||||
children,
|
||||
className,
|
||||
isLoading,
|
||||
isDisabled,
|
||||
isBlock,
|
||||
variant,
|
||||
variantColor,
|
||||
@@ -26,12 +28,14 @@ export const Button: React.FC<
|
||||
<button
|
||||
{...props}
|
||||
type={type || "button"}
|
||||
disabled={isDisabled}
|
||||
className={clsx(
|
||||
className,
|
||||
"button",
|
||||
isLoading && "is-loading",
|
||||
isBlock && `is-block`,
|
||||
variantColor && `is-${variantColor}`,
|
||||
isDisabled && `is-disabled`,
|
||||
size && `is-${size}`,
|
||||
`is-${variant || "primary"}`,
|
||||
rounded === "full" ? "rounded-full" : "rounded-lg"
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
@apply shadow-button;
|
||||
@apply whitespace-nowrap;
|
||||
@apply font-medium;
|
||||
|
||||
@apply transition-colors;
|
||||
}
|
||||
|
||||
@@ -31,6 +30,11 @@
|
||||
@apply opacity-50;
|
||||
}
|
||||
|
||||
.button[disabled] {
|
||||
@apply opacity-50;
|
||||
@apply cursor-not-allowed;
|
||||
}
|
||||
|
||||
.button.is-loading:before {
|
||||
margin-right: 0.5rem;
|
||||
height: 1rem;
|
||||
@@ -85,6 +89,7 @@
|
||||
@apply h-7;
|
||||
@apply px-2;
|
||||
@apply text-xs;
|
||||
@apply min-w-[auto];
|
||||
}
|
||||
|
||||
.input {
|
||||
|
||||
+28
-16
@@ -1,27 +1,39 @@
|
||||
import { PrismaClient, type Prisma } from "@prisma/client"
|
||||
import { IS_PROD } from "./constants"
|
||||
import { IS_BROWSER, IS_PROD } from "./constants"
|
||||
import { singleton } from "./singleton.server"
|
||||
|
||||
const logLevel: Prisma.LogLevel[] = IS_PROD
|
||||
? ["info", "error", "warn"]
|
||||
: ["info", "warn", "error", "query"]
|
||||
|
||||
export const prisma = /* @__PURE__ */ singleton("prisma-read", () => {
|
||||
const client = new PrismaClient({
|
||||
log: logLevel,
|
||||
})
|
||||
export const prisma = /* @__PURE__ */ singleton(
|
||||
"prisma",
|
||||
() => {
|
||||
if (!IS_PROD && !IS_BROWSER) {
|
||||
for (const k in require.cache) {
|
||||
if (k.includes("prisma")) {
|
||||
delete require.cache[k]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
client.$use(async (params, next) => {
|
||||
const now = Date.now()
|
||||
const result = await next(params)
|
||||
const end = Date.now()
|
||||
const duration = Math.floor(end - now)
|
||||
console.log("query took", duration, "ms")
|
||||
// See results here
|
||||
return result
|
||||
})
|
||||
const client = new PrismaClient({
|
||||
log: logLevel,
|
||||
})
|
||||
|
||||
return client
|
||||
})
|
||||
client.$use(async (params, next) => {
|
||||
const now = Date.now()
|
||||
const result = await next(params)
|
||||
const end = Date.now()
|
||||
const duration = Math.floor(end - now)
|
||||
console.log("query took", duration, "ms")
|
||||
// See results here
|
||||
return result
|
||||
})
|
||||
|
||||
return client
|
||||
},
|
||||
IS_PROD
|
||||
)
|
||||
|
||||
export type { Prisma }
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
// @ts-nocheck
|
||||
globalThis._singletons = globalThis._singletons || {}
|
||||
|
||||
export const singleton = <T>(id: string, factory: () => T): T => {
|
||||
if (!globalThis._singletons[id]) {
|
||||
export const singleton = <T>(
|
||||
id: string,
|
||||
factory: () => T,
|
||||
enableSingleton = true
|
||||
): T => {
|
||||
if (!globalThis._singletons[id] || !enableSingleton) {
|
||||
globalThis._singletons[id] = factory()
|
||||
}
|
||||
return globalThis._singletons[id]
|
||||
@@ -11,9 +15,9 @@ export const singleton = <T>(id: string, factory: () => T): T => {
|
||||
export const singletonAsync = <T>(
|
||||
id: string,
|
||||
factory: () => Promise<T>,
|
||||
singleton = true
|
||||
enableSingleton = true
|
||||
): { readonly value: T; wait: Promise<void> } => {
|
||||
if (globalThis._singletons[id] && singleton) {
|
||||
if (globalThis._singletons[id] && enableSingleton) {
|
||||
return {
|
||||
wait: Promise.resolve(),
|
||||
|
||||
|
||||
@@ -64,3 +64,9 @@ export type SubscribeFormData = {
|
||||
telegram?: boolean
|
||||
siteId: string
|
||||
}
|
||||
|
||||
export type SiteNavigationItem = {
|
||||
id: string
|
||||
label: string
|
||||
url: string
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { prisma } from "~/lib/db.server"
|
||||
import { isUUID } from "~/lib/uuid"
|
||||
import { MembershipRole, PageType } from "@prisma/client"
|
||||
import { MembershipRole, PageType, Prisma, Site } from "@prisma/client"
|
||||
import { Gate } from "~/lib/gate.server"
|
||||
import dayjs from "dayjs"
|
||||
import { sendLoginEmail } from "~/lib/mailgun.server"
|
||||
import { SubscribeFormData } from "~/lib/types"
|
||||
import { SiteNavigationItem, SubscribeFormData } from "~/lib/types"
|
||||
import { nanoid } from "nanoid"
|
||||
|
||||
export const checkSubdomain = async ({
|
||||
subdomain,
|
||||
@@ -80,7 +81,9 @@ export const getSite = async (input: string) => {
|
||||
throw new Error(`Site not found`)
|
||||
}
|
||||
|
||||
return site
|
||||
return site as Omit<Site, "navigation"> & {
|
||||
navigation: SiteNavigationItem[] | null
|
||||
}
|
||||
}
|
||||
|
||||
export const getMembership = async (data: {
|
||||
@@ -141,6 +144,7 @@ export async function updateSite(
|
||||
description?: string
|
||||
icon?: string | null
|
||||
subdomain?: string
|
||||
navigation?: SiteNavigationItem[]
|
||||
}
|
||||
) {
|
||||
const site = await getSite(payload.site)
|
||||
@@ -165,6 +169,7 @@ export async function updateSite(
|
||||
subdomain: payload.subdomain,
|
||||
description: payload.description,
|
||||
icon: payload.icon,
|
||||
navigation: payload.navigation,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -181,10 +186,23 @@ export async function createSite(
|
||||
const user = gate.getUser(true)
|
||||
await checkSubdomain({ subdomain: payload.subdomain })
|
||||
|
||||
const navigation: SiteNavigationItem[] = [
|
||||
{
|
||||
id: nanoid(),
|
||||
label: "About",
|
||||
url: "/about",
|
||||
},
|
||||
{
|
||||
id: nanoid(),
|
||||
label: "Archives",
|
||||
url: "/archives",
|
||||
},
|
||||
]
|
||||
const site = await prisma.site.create({
|
||||
data: {
|
||||
name: payload.name,
|
||||
subdomain: payload.subdomain,
|
||||
navigation,
|
||||
memberships: {
|
||||
create: {
|
||||
role: MembershipRole.OWNER,
|
||||
|
||||
@@ -233,7 +233,7 @@ export default function SubdomainEditor() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="h-full overflow-auto flex-shrink-0 w-[280px] border-l bg-gray-50 p-5">
|
||||
<div className="h-full overflow-auto flex-shrink-0 w-[280px] border-l bg-zinc-50 p-5">
|
||||
<div>
|
||||
<Input
|
||||
name="slug"
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import { Input } from "~/components/ui/Input"
|
||||
import { FormEvent, useEffect, useState } from "react"
|
||||
import toast from "react-hot-toast"
|
||||
import { SettingsLayout } from "~/components/dashboard/SettingsLayout"
|
||||
import { useRouter } from "next/router"
|
||||
import { DashboardLayout } from "~/components/dashboard/DashboardLayout"
|
||||
import { trpc } from "~/lib/trpc"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { SiteNavigationItem } from "~/lib/types"
|
||||
import { nanoid } from "nanoid"
|
||||
|
||||
export default function SiteSettingsNavigationPage() {
|
||||
const router = useRouter()
|
||||
|
||||
const [itemsModified, setItemsModified] = useState(false)
|
||||
const subdomain = router.query.subdomain as string
|
||||
|
||||
const siteResult = trpc.useQuery(["site", { site: subdomain }], {
|
||||
enabled: !!subdomain,
|
||||
})
|
||||
const updateSite = trpc.useMutation("site.updateSite")
|
||||
|
||||
const [items, setItems] = useState<SiteNavigationItem[]>([])
|
||||
|
||||
const updateItem = (id: string, newItem: Partial<SiteNavigationItem>) => {
|
||||
setItems((items) => {
|
||||
return items.map((item) => {
|
||||
if (item.id === id) {
|
||||
return { ...item, ...newItem }
|
||||
}
|
||||
return item
|
||||
})
|
||||
})
|
||||
setItemsModified(true)
|
||||
}
|
||||
|
||||
const newEmptyItem = () => {
|
||||
setItems((items) => [...items, { id: nanoid(), label: "", url: "" }])
|
||||
}
|
||||
|
||||
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault()
|
||||
updateSite.mutate({
|
||||
site: siteResult.data!.id,
|
||||
navigation: items,
|
||||
})
|
||||
}
|
||||
|
||||
const removeItem = (id: string) => {
|
||||
setItems((items) => items.filter((item) => item.id !== id))
|
||||
setItemsModified(true)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!siteResult.isPreviousData && siteResult.data?.navigation) {
|
||||
setItems(siteResult.data.navigation)
|
||||
}
|
||||
}, [siteResult.isPreviousData, siteResult.data])
|
||||
|
||||
useEffect(() => {
|
||||
if (updateSite.isSuccess) {
|
||||
updateSite.reset()
|
||||
toast.success("Saved")
|
||||
setItemsModified(true)
|
||||
}
|
||||
}, [updateSite])
|
||||
|
||||
return (
|
||||
<DashboardLayout>
|
||||
<SettingsLayout title="Site Settings" type="site">
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="bg-zinc-50 rounded-lg p-5">
|
||||
{items.length === 0 && (
|
||||
<div className="text-center text-zinc-500">
|
||||
No navigation items yet
|
||||
</div>
|
||||
)}
|
||||
{items.map((item) => {
|
||||
return (
|
||||
<div
|
||||
key={item.id}
|
||||
className="flex space-x-5 border-b last:border-0 mb-5 pb-5 last:pb-0 last:mb-0"
|
||||
>
|
||||
<Input
|
||||
label="Label"
|
||||
required
|
||||
id={`${item.id}-label`}
|
||||
value={item.label}
|
||||
onChange={(e) =>
|
||||
updateItem(item.id, { label: e.target.value })
|
||||
}
|
||||
/>
|
||||
<Input
|
||||
label="URL"
|
||||
required
|
||||
id={`${item.id}-url`}
|
||||
type="text"
|
||||
value={item.url}
|
||||
pattern="(https?://|/).+"
|
||||
title="URL must start with / or http:// or https://"
|
||||
onChange={(e) =>
|
||||
updateItem(item.id, { url: e.target.value })
|
||||
}
|
||||
/>
|
||||
<div className="flex items-end relative -top-[5px]">
|
||||
<Button
|
||||
onClick={() => removeItem(item.id)}
|
||||
variantColor="red"
|
||||
>
|
||||
Remove
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
<div className="border-t pt-5 mt-10 space-x-3 flex items-center">
|
||||
<Button
|
||||
type="submit"
|
||||
isLoading={updateSite.isLoading}
|
||||
isDisabled={!itemsModified}
|
||||
>
|
||||
Save
|
||||
</Button>
|
||||
<Button variant="secondary" onClick={newEmptyItem}>
|
||||
New Item
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</SettingsLayout>
|
||||
</DashboardLayout>
|
||||
)
|
||||
}
|
||||
@@ -20,6 +20,15 @@ export const appRouter = trpc
|
||||
description: z.string().nullable(),
|
||||
icon: z.string().nullable(),
|
||||
subdomain: z.string(),
|
||||
navigation: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
label: z.string(),
|
||||
url: z.string(),
|
||||
})
|
||||
)
|
||||
.nullable(),
|
||||
}),
|
||||
async resolve({ input }) {
|
||||
const site = await getSite(input.site)
|
||||
|
||||
@@ -117,6 +117,20 @@ export const siteRouter = createRouter()
|
||||
description: z.string().optional(),
|
||||
icon: z.string().nullish().optional(),
|
||||
subdomain: z.string().optional(),
|
||||
navigation: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
label: z.string(),
|
||||
url: z
|
||||
.string()
|
||||
.regex(
|
||||
/^(https?:\/\/|\/)/,
|
||||
"URL must start with / or http:// or https://"
|
||||
),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
}),
|
||||
output: z.object({
|
||||
site: z.object({
|
||||
|
||||
Reference in New Issue
Block a user