Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: useHydratedSession instead of built-in useSession #2407

Merged
merged 12 commits into from
Nov 20, 2024
2 changes: 1 addition & 1 deletion keep-ui/app/ai/ai.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
import { Card, List, ListItem, Title, Subtitle } from "@tremor/react";
import { useAIStats, usePollAILogs } from "utils/hooks/useAI";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { useApiUrl } from "utils/hooks/useConfig";
import { toast } from "react-toastify";
import { useEffect, useState, useRef, FormEvent } from "react";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/ViewAlertModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Modal from "@/components/ui/Modal"; // Ensure this path matches your proj
import { Button, Icon, Switch, Text } from "@tremor/react";
import { toast } from "react-toastify";
import { useApiUrl } from "utils/hooks/useConfig";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { XMarkIcon } from "@heroicons/react/24/outline";
import "./ViewAlertModal.css";
import React, { useState } from "react";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-assign-ticket-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button, TextInput, Text } from "@tremor/react";
import { PlusIcon } from "@heroicons/react/20/solid";
import { useForm, Controller, SubmitHandler } from "react-hook-form";
import { Providers } from "./../providers/providers";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { useApiUrl } from "utils/hooks/useConfig";
import { AlertDto } from "./models";
import Modal from "@/components/ui/Modal";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-associate-incident-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Modal from "@/components/ui/Modal";
import { Button, Divider, Title } from "@tremor/react";
import Select from "@/components/ui/Select";
import { CreateOrUpdateIncidentForm } from "@/features/create-or-update-incident";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
Kiryous marked this conversation as resolved.
Show resolved Hide resolved
import { FormEvent, useCallback, useEffect, useState } from "react";
import { toast } from "react-toastify";
import { useApiUrl } from "utils/hooks/useConfig";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-change-status-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Select, {
import { useState } from "react";
import { AlertDto, Status } from "./models";
import { useApiUrl } from "utils/hooks/useConfig";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { toast } from "react-toastify";
import {
CheckCircleIcon,
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-create-incident-ai-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import Modal from "@/components/ui/Modal";
import { Callout, Button, Title, Card } from "@tremor/react";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { toast } from "react-toastify";
import Loading from "../loading";
import { AlertDto } from "./models";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-dismiss-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import "react-quill/dist/quill.snow.css";
import { AlertDto } from "./models";
import { format, set, isSameDay, isAfter, addMinutes } from "date-fns";
import { useApiUrl } from "utils/hooks/useConfig";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { usePresets } from "utils/hooks/usePresets";
import { useAlerts } from "utils/hooks/useAlerts";
import { toast } from "react-toastify";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@heroicons/react/24/outline";
import { IoNotificationsOffOutline } from "react-icons/io5";

import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
Kiryous marked this conversation as resolved.
Show resolved Hide resolved
import { useApiUrl } from "utils/hooks/useConfig";
import Link from "next/link";
import { ProviderMethod } from "app/providers/providers";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-note-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ReactQuill =
import "react-quill/dist/quill.snow.css";
import { Button } from "@tremor/react";
import { useApiUrl } from "utils/hooks/useConfig";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { AlertDto } from "./models";
import Modal from "@/components/ui/Modal";

Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-presets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Modal from "@/components/ui/Modal";
import { Button, Subtitle, TextInput, Switch, Text } from "@tremor/react";
import { useApiUrl } from "utils/hooks/useConfig";
import { toast } from "react-toastify";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
Kiryous marked this conversation as resolved.
Show resolved Hide resolved
import { usePresets } from "utils/hooks/usePresets";
import { useTags } from "utils/hooks/useTags";
import { useRouter } from "next/navigation";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-push-alert-to-server-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
FieldValues,
} from "react-hook-form";
import Modal from "@/components/ui/Modal";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { useApiUrl } from "utils/hooks/useConfig";
import { useProviders } from "utils/hooks/useProviders";
import ImageWithFallback from "@/components/ImageWithFallback";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-run-workflow-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AlertDto } from "./models";
import Modal from "@/components/ui/Modal";
import { useWorkflows } from "utils/hooks/useWorkflows";
import { useState } from "react";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
Kiryous marked this conversation as resolved.
Show resolved Hide resolved
import { useApiUrl } from "utils/hooks/useConfig";
import { toast } from "react-toastify";
import { useRouter } from "next/navigation";
Expand Down
6 changes: 3 additions & 3 deletions keep-ui/app/alerts/alert-tab-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState } from "react";
import Modal from "@/components/ui/Modal";
import { Button, TextInput } from "@tremor/react";
import { AlertsRulesBuilder } from "app/alerts/alerts-rules-builder";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { useApiUrl } from "utils/hooks/useConfig";

interface AlertTabModalProps {
Expand Down Expand Up @@ -114,8 +114,8 @@ const AlertTabModal = ({
!newTabName
? "Tab name is required"
: !newTabFilter
? "Tab filter is required (notice you need to click 'enter' to apply the filter)"
: ""
? "Tab filter is required (notice you need to click 'enter' to apply the filter)"
: ""
}
>
Add Tab
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/alerts/alert-tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import AlertTabModal from "./alert-tab-modal";
import { evalWithContext } from "./alerts-rules-builder";
import { XMarkIcon } from "@heroicons/react/24/outline";
import { useApiUrl } from "utils/hooks/useConfig";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
interface Tab {
id?: string;
name: string;
Expand Down
4 changes: 1 addition & 3 deletions keep-ui/app/alerts/alerts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import { useRouter, useSearchParams } from "next/navigation";
import AlertChangeStatusModal from "./alert-change-status-modal";
import { useAlertPolling } from "utils/hooks/usePusher";
import NotFound from "@/app/not-found";
import { useMounted } from "@/shared/lib/hooks/useMounted";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";

const defaultPresets: Preset[] = [
{
Expand Down Expand Up @@ -111,7 +110,6 @@ export default function Alerts({ presetName }: AlertsProps) {
error: alertsError,
} = usePresetAlerts(selectedPreset ? selectedPreset.name : "");

// const isMounted = useMounted();
const { status: sessionStatus } = useSession();
const isLoading = isAsyncLoading || sessionStatus === "loading";

Expand Down
17 changes: 15 additions & 2 deletions keep-ui/app/auth-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
"use client";

import { Session } from "next-auth";
import { SessionProvider } from "next-auth/react";

declare global {
interface Window {
__NEXT_AUTH_SESSION__?: Session | null;
}
}

type Props = {
children?: React.ReactNode;
session?: Session | null;
};

export const NextAuthProvider = ({ children }: Props) => {
return <SessionProvider>{children}</SessionProvider>
export const NextAuthProvider = ({ children, session }: Props) => {
// Hydrate session on mount
if (typeof window !== "undefined" && !!session) {
window.__NEXT_AUTH_SESSION__ = session;
}

return <SessionProvider>{children}</SessionProvider>;
};
65 changes: 45 additions & 20 deletions keep-ui/app/dashboard/[id]/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
"use client";
import {useParams} from "next/navigation";
import {ChangeEvent, useEffect, useState} from "react";
import { useParams } from "next/navigation";
import { ChangeEvent, useEffect, useState } from "react";
import GridLayout from "../GridLayout";
import WidgetModal from "../WidgetModal";
import {Button, Card, Icon, Subtitle, TextInput} from "@tremor/react";
import {GenericsMetrics, LayoutItem, Threshold, WidgetData, WidgetType} from "../types";
import {Preset} from "app/alerts/models";
import {FiEdit2, FiSave} from "react-icons/fi";
import {useSession} from "next-auth/react";
import {useDashboards} from "utils/hooks/useDashboards";
import {useApiUrl} from "utils/hooks/useConfig";
import { Button, Card, Icon, Subtitle, TextInput } from "@tremor/react";
import {
GenericsMetrics,
LayoutItem,
Threshold,
WidgetData,
WidgetType,
} from "../types";
import { Preset } from "app/alerts/models";
import { FiEdit2, FiSave } from "react-icons/fi";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { useDashboards } from "utils/hooks/useDashboards";
import { useApiUrl } from "utils/hooks/useConfig";
import "./../styles.css";
import {toast} from "react-toastify";
import {GenericFilters} from "@/components/filters/GenericFilters";
import {useDashboardPreset} from "utils/hooks/useDashboardPresets";
import {MetricsWidget, useDashboardMetricWidgets} from '@/utils/hooks/useDashboardMetricWidgets';
import { toast } from "react-toastify";
import { GenericFilters } from "@/components/filters/GenericFilters";
import { useDashboardPreset } from "utils/hooks/useDashboardPresets";
import {
MetricsWidget,
useDashboardMetricWidgets,
} from "@/utils/hooks/useDashboardMetricWidgets";

const DASHBOARD_FILTERS = [
{
Expand All @@ -33,7 +42,7 @@ const DashboardPage = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
const [layout, setLayout] = useState<LayoutItem[]>([]);
const [widgetData, setWidgetData] = useState<WidgetData[]>([]);
const {widgets: allMetricWidgets} = useDashboardMetricWidgets(true);
const { widgets: allMetricWidgets } = useDashboardMetricWidgets(true);
const [editingItem, setEditingItem] = useState<WidgetData | null>(null);
const [dashboardName, setDashboardName] = useState(decodeURIComponent(id));
const [isEditingName, setIsEditingName] = useState(false);
Expand All @@ -59,7 +68,9 @@ const DashboardPage = () => {
const closeModal = () => setIsModalOpen(false);

const handleAddWidget = (
name: string, widgetType: WidgetType, preset?: Preset ,
name: string,
widgetType: WidgetType,
preset?: Preset,
thresholds?: Threshold[],
metric?: MetricsWidget,
genericMetrics?: GenericsMetrics
Expand All @@ -69,10 +80,25 @@ const DashboardPage = () => {
i: uniqueId,
x: (layout.length % 12) * 2,
y: Math.floor(layout.length / 12) * 2,
w: widgetType === WidgetType.GENERICS_METRICS ? 12 : widgetType === WidgetType.METRIC ? 6 : 3,
h: widgetType === WidgetType.GENERICS_METRICS ? 20 : widgetType === WidgetType.METRIC ? 8 : 3,
w:
widgetType === WidgetType.GENERICS_METRICS
? 12
: widgetType === WidgetType.METRIC
? 6
: 3,
h:
widgetType === WidgetType.GENERICS_METRICS
? 20
: widgetType === WidgetType.METRIC
? 8
: 3,
minW: widgetType === WidgetType.GENERICS_METRICS ? 10 : 2,
minH: widgetType === WidgetType.GENERICS_METRICS ? 15 : widgetType === WidgetType.METRIC ? 7 : 3,
minH:
widgetType === WidgetType.GENERICS_METRICS
? 15
: widgetType === WidgetType.METRIC
? 7
: 3,
static: false,
};
const newWidget: WidgetData = {
Expand All @@ -82,15 +108,14 @@ const DashboardPage = () => {
name,
widgetType,
genericMetrics,
metric
metric,
};
setLayout((prevLayout) => [...prevLayout, newItem]);
setWidgetData((prevData) => [...prevData, newWidget]);
};

const handleEditWidget = (id: string, update?: WidgetData) => {
let itemToEdit = widgetData.find((d) => d.i === id) || null;
console.log(itemToEdit, update)
if (itemToEdit && update) {
setEditingItem({ ...itemToEdit, ...update });
} else {
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/deduplication/DeduplicationSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
InformationCircleIcon,
} from "@heroicons/react/24/outline";
import { useApiUrl } from "utils/hooks/useConfig";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { KeyedMutator } from "swr";

interface ProviderOption {
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/deduplication/DeduplicationTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import DeduplicationSidebar from "app/deduplication/DeduplicationSidebar";
import { TrashIcon, PauseIcon, PlusIcon } from "@heroicons/react/24/outline";
import Image from "next/image";
import { useApiUrl } from "utils/hooks/useConfig";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";

const columnHelper = createColumnHelper<DeduplicationRule>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Switch,
Badge,
} from "@tremor/react";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { FormEvent, useEffect, useState } from "react";
import { toast } from "react-toastify";
import { useApiUrl } from "utils/hooks/useConfig";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/extraction/extractions-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
useReactTable,
} from "@tanstack/react-table";
import { MdRemoveCircle, MdModeEdit } from "react-icons/md";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { useApiUrl } from "utils/hooks/useConfig";
import { useMappings } from "utils/hooks/useMappingRules";
import { toast } from "react-toastify";
Expand Down
4 changes: 2 additions & 2 deletions keep-ui/app/frigade-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import * as Frigade from "@frigade/react";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
export const FrigadeProvider = ({
children,
}: {
Expand All @@ -14,7 +14,7 @@ export const FrigadeProvider = ({
userId={
session?.user.email === "keep"
? undefined
: session?.user.email ?? session?.user.name
: (session?.user.email ?? session?.user.name)
}
theme={{
colors: {
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/incidents/[id]/activity/incident-activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
usePollIncidentComments,
} from "@/utils/hooks/useIncidents";
import { useAlerts } from "@/utils/hooks/useAlerts";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { IncidentActivityItem } from "./ui/IncidentActivityItem";
import { IncidentActivityComment } from "./ui/IncidentActivityComment";
import { useMemo } from "react";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IncidentDto } from "@/entities/incidents/model";
import { AuditEvent } from "@/utils/hooks/useAlerts";
import { useApiUrl } from "@/utils/hooks/useConfig";
import { TextInput, Button } from "@tremor/react";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { useState, useCallback, useEffect } from "react";
import { toast } from "react-toastify";
import { KeyedMutator } from "swr";
Expand Down
2 changes: 1 addition & 1 deletion keep-ui/app/incidents/[id]/alerts/incident-alert-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Icon } from "@tremor/react";
import { AlertDto } from "app/alerts/models";
import { useSession } from "next-auth/react";
import { useHydratedSession as useSession } from "@/shared/lib/hooks/useHydratedSession";
import { toast } from "react-toastify";
import { useApiUrl } from "utils/hooks/useConfig";
import { useIncidentAlerts } from "utils/hooks/useIncidents";
Expand Down
Loading
Loading