-
Notifications
You must be signed in to change notification settings - Fork 0
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
Implement Validation for Filtering #86
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
358b5ad
feat: added validation
gpalmer27 9bcf55e
fix: added toast feature
gpalmer27 e04ce1b
fix: fixed formatting
gpalmer27 98605a7
fix: added more to toast
gpalmer27 8799929
feat: add toast
gpalmer27 2784465
merge main
gpalmer27 54c8201
fixed formatting
gpalmer27 b79bca6
deleted extra line
gpalmer27 c3c630d
fixed bug
gpalmer27 3de1b4e
fixed bug
gpalmer27 311f1a3
fixed bugs
gpalmer27 101d169
fixed bugs
gpalmer27 3c0ac8d
fixed bugs
gpalmer27 c5f5838
fixed style
gpalmer27 580e813
fixed formatting
gpalmer27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { Toaster } from "@cooper/ui/toaster"; | ||
|
||
import HeaderLayout from "~/app/_components/header-layout"; | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
return <HeaderLayout>{children}</HeaderLayout>; | ||
return ( | ||
<HeaderLayout> | ||
{children} | ||
<Toaster /> | ||
</HeaderLayout> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
"use client"; | ||
|
||
// Inspired by react-hot-toast library | ||
import * as React from "react"; | ||
|
||
import type { ToastActionElement, ToastProps } from "../toast"; | ||
|
||
const TOAST_LIMIT = 1; | ||
const TOAST_REMOVE_DELAY = 1000000; | ||
|
||
type ToasterToast = ToastProps & { | ||
id: string; | ||
title?: React.ReactNode; | ||
description?: React.ReactNode; | ||
action?: ToastActionElement; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const actionTypes = { | ||
ADD_TOAST: "ADD_TOAST", | ||
UPDATE_TOAST: "UPDATE_TOAST", | ||
DISMISS_TOAST: "DISMISS_TOAST", | ||
REMOVE_TOAST: "REMOVE_TOAST", | ||
} as const; | ||
|
||
let count = 0; | ||
|
||
function genId() { | ||
count = (count + 1) % Number.MAX_SAFE_INTEGER; | ||
return count.toString(); | ||
} | ||
|
||
type ActionType = typeof actionTypes; | ||
|
||
type Action = | ||
| { | ||
type: ActionType["ADD_TOAST"]; | ||
toast: ToasterToast; | ||
} | ||
| { | ||
type: ActionType["UPDATE_TOAST"]; | ||
toast: Partial<ToasterToast>; | ||
} | ||
| { | ||
type: ActionType["DISMISS_TOAST"]; | ||
toastId?: ToasterToast["id"]; | ||
} | ||
| { | ||
type: ActionType["REMOVE_TOAST"]; | ||
toastId?: ToasterToast["id"]; | ||
}; | ||
|
||
interface State { | ||
toasts: ToasterToast[]; | ||
} | ||
|
||
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>(); | ||
|
||
const addToRemoveQueue = (toastId: string) => { | ||
if (toastTimeouts.has(toastId)) { | ||
return; | ||
} | ||
|
||
const timeout = setTimeout(() => { | ||
toastTimeouts.delete(toastId); | ||
dispatch({ | ||
type: "REMOVE_TOAST", | ||
toastId: toastId, | ||
}); | ||
}, TOAST_REMOVE_DELAY); | ||
|
||
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, | ||
), | ||
}; | ||
|
||
case "DISMISS_TOAST": { | ||
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); | ||
} else { | ||
state.toasts.forEach((toast) => { | ||
addToRemoveQueue(toast.id); | ||
}); | ||
} | ||
|
||
return { | ||
...state, | ||
toasts: state.toasts.map((t) => | ||
t.id === toastId || toastId === undefined | ||
? { | ||
...t, | ||
open: false, | ||
} | ||
: t, | ||
), | ||
}; | ||
} | ||
case "REMOVE_TOAST": | ||
if (action.toastId === undefined) { | ||
return { | ||
...state, | ||
toasts: [], | ||
}; | ||
} | ||
return { | ||
...state, | ||
toasts: state.toasts.filter((t) => t.id !== action.toastId), | ||
}; | ||
} | ||
}; | ||
|
||
const listeners: ((state: State) => void)[] = []; | ||
|
||
let memoryState: State = { toasts: [] }; | ||
|
||
function dispatch(action: Action) { | ||
memoryState = reducer(memoryState, action); | ||
listeners.forEach((listener) => { | ||
listener(memoryState); | ||
}); | ||
} | ||
|
||
type Toast = Omit<ToasterToast, "id">; | ||
|
||
function toast({ ...props }: Toast) { | ||
const id = genId(); | ||
|
||
const update = (props: ToasterToast) => | ||
dispatch({ | ||
type: "UPDATE_TOAST", | ||
toast: { ...props, id }, | ||
}); | ||
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id }); | ||
|
||
dispatch({ | ||
type: "ADD_TOAST", | ||
toast: { | ||
...props, | ||
id, | ||
open: true, | ||
onOpenChange: (open) => { | ||
if (!open) dismiss(); | ||
}, | ||
}, | ||
}); | ||
|
||
return { | ||
id: id, | ||
dismiss, | ||
update, | ||
}; | ||
} | ||
|
||
function useToast() { | ||
const [state, setState] = React.useState<State>(memoryState); | ||
|
||
React.useEffect(() => { | ||
listeners.push(setState); | ||
return () => { | ||
const index = listeners.indexOf(setState); | ||
if (index > -1) { | ||
listeners.splice(index, 1); | ||
} | ||
}; | ||
}, [state]); | ||
|
||
return { | ||
...state, | ||
toast, | ||
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }), | ||
}; | ||
} | ||
|
||
export { useToast, toast }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can fix this warning by doing something like this: