Skip to content

Commit

Permalink
feat: Redirect from dashboard setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed Sep 23, 2024
1 parent 0e47dfe commit 19fb049
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 18 deletions.
5 changes: 4 additions & 1 deletion public/locales/de/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@
"users": "Anzahl der Benutzer",
"version": "Liman-Version",
"most_used_extensions": "Meistgenutzte Erweiterungen",
"most_used_servers": "Meistgenutzte Server"
"most_used_servers": "Meistgenutzte Server",
"redirect": "Umleitungserweiterung",
"redirect_description": "Bestimmen Sie die Erweiterung, zu der Benutzer beim Anmelden bei Liman weitergeleitet werden. Fügen Sie diese Rolle den Benutzern sorgfältig hinzu.",
"redirect_placeholder": "Wählen Sie die Erweiterung zur Weiterleitung aus"
},
"liman": {
"title": "liman Genehmigungen",
Expand Down
5 changes: 4 additions & 1 deletion public/locales/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@
"users": "Number of Users",
"version": "Liman Version",
"most_used_extensions": "Most Used Extensions",
"most_used_servers": "Most Used Servers"
"most_used_servers": "Most Used Servers",
"redirect": "Redirect Extension",
"redirect_description": "Determine the extension to which users will be redirected when they log in to Liman. Add this role to users carefully.",
"redirect_placeholder": "Select the extension to be redirected"
},
"liman": {
"title": "Liman Permissions",
Expand Down
5 changes: 4 additions & 1 deletion public/locales/tr/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@
"users": "Kullanıcı Sayısı",
"version": "Liman Versiyonu",
"most_used_extensions": "En Çok Kullanılan Eklentiler",
"most_used_servers": "En Çok Kullanılan Sunucular"
"most_used_servers": "En Çok Kullanılan Sunucular",
"redirect": "Yönlendirilecek Eklenti",
"redirect_description": "Kullanıcılar Liman'a giriş yaptıklarında yönlendirilecekleri eklentiyi belirleyin. Bu rolü kullanıcılara dikkatli ekleyin.",
"redirect_placeholder": "Yönlendirilecek eklentiyi seçiniz"
},
"liman": {
"title": "Liman İzinleri",
Expand Down
16 changes: 12 additions & 4 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useMemo } from "react"
import dynamic from "next/dynamic"
import { useEffect, useMemo } from "react"
import { useTranslation } from "react-i18next"

import { DashboardEnum } from "@/types/user"
import { cn } from "@/lib/utils"
import { useCurrentUser } from "@/hooks/auth/useCurrentUser"
import DashboardCards from "@/components/dashboard/cards"
import FavoriteServers from "@/components/dashboard/favorite-servers"
import LatestLoggedInUsers from "@/components/dashboard/latest-logged-in-users"
import MostUsedExtensions from "@/components/dashboard/most-used-extensions"
import { useCurrentUser } from "@/hooks/auth/useCurrentUser"
import { cn } from "@/lib/utils"
import { useSidebarContext } from "@/providers/sidebar-provider"
import { DashboardEnum } from "@/types/user"

const DateTimeView = dynamic(() => import("@/components/dashboard/date-time"), {
ssr: false,
Expand All @@ -17,6 +18,7 @@ const DateTimeView = dynamic(() => import("@/components/dashboard/date-time"), {
export default function IndexPage() {
const { t } = useTranslation("dashboard")
const user = useCurrentUser()
const { redirectNow } = useSidebarContext()

const viewPermissions = user.permissions.view

Expand All @@ -26,6 +28,12 @@ export default function IndexPage() {
"most_used_servers",
]

useEffect(() => {
if (viewPermissions.redirect) {
redirectNow(viewPermissions.redirect)
}
}, [viewPermissions.redirect])

const dashboardGridItems = useMemo(() => {
return viewPermissions.dashboard
.filter((item) => dashboardItems.includes(item))
Expand Down
59 changes: 52 additions & 7 deletions src/pages/settings/roles/[role_id]/views.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { ReactElement, useEffect } from "react"
import { useRouter } from "next/router"
import { NextPageWithLayout } from "@/pages/_app"
import { apiService } from "@/services"
import { zodResolver } from "@hookform/resolvers/zod"
import { Save } from "lucide-react"
import { useRouter } from "next/router"
import { ReactElement, useEffect, useState } from "react"
import { useForm } from "react-hook-form"
import { useTranslation } from "react-i18next"
import * as z from "zod"

import { setFormErrors } from "@/lib/utils"
import RoleLayout from "@/components/_layout/role_layout"
import { Form, FormField, FormMessage } from "@/components/form/form"
import { Button } from "@/components/ui/button"
import { Card, CardContent } from "@/components/ui/card"
import { Checkbox } from "@/components/ui/checkbox"
Expand All @@ -22,14 +23,16 @@ import {
SelectValue,
} from "@/components/ui/select"
import { useToast } from "@/components/ui/use-toast"
import RoleLayout from "@/components/_layout/role_layout"
import { Form, FormField, FormMessage } from "@/components/form/form"
import { setFormErrors } from "@/lib/utils"
import { IExtension } from "@/types/extension"

const RoleViewList: NextPageWithLayout = () => {
const router = useRouter()
const { t } = useTranslation("settings")
const { toast } = useToast()

const [extensionSelector, setExtensionSelector] = useState<IExtension[]>([])

const DashboardEnum = z.enum([
"servers",
"users",
Expand All @@ -41,6 +44,7 @@ const RoleViewList: NextPageWithLayout = () => {
const formSchema = z.object({
sidebar: z.enum(["servers", "extensions"]),
dashboard: z.array(DashboardEnum),
redirect: z.string().optional()
})

const form = useForm<z.infer<typeof formSchema>>({
Expand All @@ -50,10 +54,19 @@ const RoleViewList: NextPageWithLayout = () => {
const fetchData = () => {
apiService
.getInstance()
.get(`/settings/roles/${router.query.role_id}/views`)
.get(`/settings/roles/${router.query.role_id}/extensions`)
.then((res) => {
form.reset(res.data)
if (res.status === 200) {
setExtensionSelector(res.data.selected)
}
apiService
.getInstance()
.get(`/settings/roles/${router.query.role_id}/views`)
.then((res) => {
form.reset(res.data)
})
})

}

useEffect(() => {
Expand Down Expand Up @@ -158,6 +171,38 @@ const RoleViewList: NextPageWithLayout = () => {
</small>
</div>

<FormField
control={form.control}
name="redirect"
render={({ field }) => (
<div className="flex flex-col gap-2">
<Label htmlFor="redirect">{t("roles.views.redirect")}</Label>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
value={field.value}
>
<SelectTrigger>
<SelectValue
placeholder={t("roles.views.redirect_placeholder")}
/>
</SelectTrigger>
<SelectContent>
{extensionSelector.map((item) => (
<SelectItem key={item.id} value={item.id}>
{item.display_name}
</SelectItem>
))}
</SelectContent>
</Select>
<small className="italic text-muted-foreground">
{t("roles.views.redirect_description")}
</small>
<FormMessage className="mt-1" />
</div>
)}
/>

<div className="flex justify-end">
<Button type="submit">
<Save className="mr-2 size-4" />
Expand Down
42 changes: 38 additions & 4 deletions src/providers/sidebar-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as React from "react"
import { useRouter } from "next/router"
import { apiService } from "@/services"
import { useRouter } from "next/router"
import * as React from "react"

import { useCurrentUser } from "@/hooks/auth/useCurrentUser"
import { IExtension } from "@/types/extension"
import { IServer } from "@/types/server"
import { useCurrentUser } from "@/hooks/auth/useCurrentUser"

interface SidebarContextType {
selected: string
Expand All @@ -30,6 +30,9 @@ interface SidebarContextType {
collapsed: boolean
setCollapsed: React.Dispatch<React.SetStateAction<boolean>>
toggleSidebar: () => void
redirectNow: (id?: string) => void
redirectId: string
setRedirectId: React.Dispatch<React.SetStateAction<string>>
}

const SidebarContext = React.createContext<SidebarContextType | undefined>(
Expand Down Expand Up @@ -59,6 +62,9 @@ export const SidebarProvider = ({

const [collapsed, setCollapsed] = React.useState<boolean>(true)

const [redirectKey, setRedirectKey] = React.useState<number>(0)
const [redirectId, setRedirectId] = React.useState<string>("")

React.useEffect(() => {
if (router.asPath.startsWith("/settings")) {
setSettingsActive(true)
Expand Down Expand Up @@ -108,8 +114,30 @@ export const SidebarProvider = ({
.then((res) => {
setExtensions(res.data)
setExtensionsLoading(false)
if (redirectId) {
const extension = res.data.find((ext: IExtension) => ext.id === redirectId)
if (!extension) {
return
}
router.push(`/servers/${extension.server_id}/extensions/${extension.id}${extension.menus && extension.menus.length > 0
? extension.menus[0].url
: ""
}`)
}
})
}, [])
}, [redirectKey])

const redirectNow = React.useCallback(
(id?: string) => {
if (id) {
setRedirectId(id)
}
if (redirectId) {
setRedirectKey((prev) => prev + 1)
}
},
[redirectId]
)

const toggleSidebar = React.useCallback(() => {
setCollapsed((prev) => !prev)
Expand Down Expand Up @@ -139,6 +167,11 @@ export const SidebarProvider = ({
collapsed,
setCollapsed,
toggleSidebar,
redirectKey,
setRedirectKey,
redirectId,
setRedirectId,
redirectNow,
}),
[
selected,
Expand All @@ -154,6 +187,7 @@ export const SidebarProvider = ({
extensions,
refreshExtensions,
toggleSidebar,
redirectNow,
]
)

Expand Down
1 change: 1 addition & 0 deletions src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ export interface ILimanPermissions {
view: {
sidebar: "servers" | "extensions"
dashboard: DashboardEnum[]
redirect: string | null
}
}

0 comments on commit 19fb049

Please sign in to comment.