Skip to content

Commit

Permalink
deploy: ready-to-deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Davronov-Alimardon committed Dec 11, 2024
1 parent 709c149 commit 3206b17
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 79 deletions.
1 change: 1 addition & 0 deletions liveblocks.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare global {
// Example properties, for useSelf, useUser, useOthers, etc.
name: string;
avatar: string;
color: string;
};
};

Expand Down
8 changes: 7 additions & 1 deletion src/app/api/liveblocks-auth/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ export async function POST(req: Request) {
return new Response("Unauthorized", { status: 401 });
}

const name = user.fullName ?? user.primaryEmailAddress?.emailAddress ?? "Anonymous";
const nameToNumber = name.split("").reduce((acc, char) => acc + char.charCodeAt(0), 0);
const hue = Math.abs(nameToNumber) % 360
const color = `hsl(${hue}, 80%, 60%)`;

const session = liveblocks.prepareSession(user.id, {
userInfo: {
name: user.fullName ?? user.primaryEmailAddress?.emailAddress ?? "Anonymous",
name,
avatar: user.imageUrl,
color,
},
});
session.allow(room, session.FULL_ACCESS);
Expand Down
1 change: 1 addition & 0 deletions src/app/documents/[documentId]/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export async function getUsers() {
id: user.id,
name: user.fullName ?? user.primaryEmailAddress?.emailAddress ?? "Anonymous",
avatar: user.imageUrl,
color: "",
}));

return users;
Expand Down
2 changes: 1 addition & 1 deletion src/app/documents/[documentId]/inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DropdownMenuContent,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Skeleton } from "@/components/ui/skeleton";
// import { Skeleton } from "@/components/ui/skeleton";
import { Separator } from "@/components/ui/separator";

export const Inbox = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/documents/[documentId]/room.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { toast } from "sonner";
import { Id } from "../../../../convex/_generated/dataModel";
import { LEFT_MARGIN_DEFAULT, RIGHT_MARGIN_DEFAULT } from "@/constants/margins";

type User = { id: string; name: string; avatar: string };
type User = { id: string; name: string; avatar: string; color: string; };

export function Room({ children }: { children: ReactNode }) {
const params = useParams();
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ function Calendar({
...classNames,
}}
components={{
IconLeft: ({ ...props }) => <ChevronLeft className="h-4 w-4" />,
IconRight: ({ ...props }) => <ChevronRight className="h-4 w-4" />,
IconLeft: () => <ChevronLeft className="h-4 w-4" />,
IconRight: () => <ChevronRight className="h-4 w-4" />,
}}
{...props}
/>
Expand Down
12 changes: 6 additions & 6 deletions src/components/ui/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import * as React from "react"
import * as RechartsPrimitive from "recharts"
import {
NameType,
Payload,
ValueType,
} from "recharts/types/component/DefaultTooltipContent"
// import {
// NameType,
// Payload,
// ValueType,
// } from "recharts/types/component/DefaultTooltipContent"

import { cn } from "@/lib/utils"

Expand Down Expand Up @@ -74,7 +74,7 @@ ChartContainer.displayName = "Chart"

const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
const colorConfig = Object.entries(config).filter(
([_, config]) => config.theme || config.color
([, config]) => config.theme || config.color
)

if (!colorConfig.length) {
Expand Down
132 changes: 64 additions & 68 deletions src/hooks/use-toast.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,102 @@
"use client"
/* eslint-disable @typescript-eslint/no-unused-vars */
"use client";

// Inspired by react-hot-toast library
import * as React from "react"
import * as React from "react";

import type {
ToastActionElement,
ToastProps,
} from "@/components/ui/toast"
import type { ToastActionElement, ToastProps } from "@/components/ui/toast";

const TOAST_LIMIT = 1
const TOAST_REMOVE_DELAY = 1000000
const TOAST_LIMIT = 1;
const TOAST_REMOVE_DELAY = 1000000;

type ToasterToast = ToastProps & {
id: string
title?: React.ReactNode
description?: React.ReactNode
action?: ToastActionElement
}
id: string;
title?: React.ReactNode;
description?: React.ReactNode;
action?: ToastActionElement;
};

const actionTypes = {
ADD_TOAST: "ADD_TOAST",
UPDATE_TOAST: "UPDATE_TOAST",
DISMISS_TOAST: "DISMISS_TOAST",
REMOVE_TOAST: "REMOVE_TOAST",
} as const
} as const;

let count = 0
let count = 0;

function genId() {
count = (count + 1) % Number.MAX_SAFE_INTEGER
return count.toString()
count = (count + 1) % Number.MAX_SAFE_INTEGER;
return count.toString();
}

type ActionType = typeof actionTypes
type ActionType = typeof actionTypes;

type Action =
| {
type: ActionType["ADD_TOAST"]
toast: ToasterToast
type: ActionType["ADD_TOAST"];
toast: ToasterToast;
}
| {
type: ActionType["UPDATE_TOAST"]
toast: Partial<ToasterToast>
type: ActionType["UPDATE_TOAST"];
toast: Partial<ToasterToast>;
}
| {
type: ActionType["DISMISS_TOAST"]
toastId?: ToasterToast["id"]
type: ActionType["DISMISS_TOAST"];
toastId?: ToasterToast["id"];
}
| {
type: ActionType["REMOVE_TOAST"]
toastId?: ToasterToast["id"]
}
type: ActionType["REMOVE_TOAST"];
toastId?: ToasterToast["id"];
};

interface State {
toasts: ToasterToast[]
toasts: ToasterToast[];
}

const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>();

const addToRemoveQueue = (toastId: string) => {
if (toastTimeouts.has(toastId)) {
return
return;
}

const timeout = setTimeout(() => {
toastTimeouts.delete(toastId)
toastTimeouts.delete(toastId);
dispatch({
type: "REMOVE_TOAST",
toastId: toastId,
})
}, TOAST_REMOVE_DELAY)
});
}, TOAST_REMOVE_DELAY);

toastTimeouts.set(toastId, timeout)
}
toastTimeouts.set(toastId, timeout);
};

export const reducer = (state: State, action: Action): State => {
switch (action.type) {
case "ADD_TOAST":
return {
...state,
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
}
};

case "UPDATE_TOAST":
return {
...state,
toasts: state.toasts.map((t) =>
t.id === action.toast.id ? { ...t, ...action.toast } : t
),
}
toasts: state.toasts.map((t) => (t.id === action.toast.id ? { ...t, ...action.toast } : t)),
};

case "DISMISS_TOAST": {
const { toastId } = action
const { toastId } = action;

// ! Side effects ! - This could be extracted into a dismissToast() action,
// but I'll keep it here for simplicity
if (toastId) {
addToRemoveQueue(toastId)
addToRemoveQueue(toastId);
} else {
state.toasts.forEach((toast) => {
addToRemoveQueue(toast.id)
})
addToRemoveQueue(toast.id);
});
}

return {
Expand All @@ -113,44 +109,44 @@ export const reducer = (state: State, action: Action): State => {
}
: t
),
}
};
}
case "REMOVE_TOAST":
if (action.toastId === undefined) {
return {
...state,
toasts: [],
}
};
}
return {
...state,
toasts: state.toasts.filter((t) => t.id !== action.toastId),
}
};
}
}
};

const listeners: Array<(state: State) => void> = []
const listeners: Array<(state: State) => void> = [];

let memoryState: State = { toasts: [] }
let memoryState: State = { toasts: [] };

function dispatch(action: Action) {
memoryState = reducer(memoryState, action)
memoryState = reducer(memoryState, action);
listeners.forEach((listener) => {
listener(memoryState)
})
listener(memoryState);
});
}

type Toast = Omit<ToasterToast, "id">
type Toast = Omit<ToasterToast, "id">;

function toast({ ...props }: Toast) {
const id = genId()
const id = genId();

const update = (props: ToasterToast) =>
dispatch({
type: "UPDATE_TOAST",
toast: { ...props, id },
})
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id })
});
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id });

dispatch({
type: "ADD_TOAST",
Expand All @@ -159,36 +155,36 @@ function toast({ ...props }: Toast) {
id,
open: true,
onOpenChange: (open) => {
if (!open) dismiss()
if (!open) dismiss();
},
},
})
});

return {
id: id,
dismiss,
update,
}
};
}

function useToast() {
const [state, setState] = React.useState<State>(memoryState)
const [state, setState] = React.useState<State>(memoryState);

React.useEffect(() => {
listeners.push(setState)
listeners.push(setState);
return () => {
const index = listeners.indexOf(setState)
const index = listeners.indexOf(setState);
if (index > -1) {
listeners.splice(index, 1)
listeners.splice(index, 1);
}
}
}, [state])
};
}, [state]);

return {
...state,
toast,
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
}
};
}

export { useToast, toast }
export { useToast, toast };

0 comments on commit 3206b17

Please sign in to comment.