-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: One store for Notifications + dev button to create notis
- Loading branch information
Showing
7 changed files
with
91 additions
and
49 deletions.
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
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
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
import type { UnlistenFn } from '@tauri-apps/api/event'; | ||
import type { NotificationData } from '~ui/components/overlay/notifications/NotificationComponent'; | ||
import { events } from '@onelauncher/client/bindings'; | ||
import { type Accessor, type Context, createContext, createSignal, onCleanup, onMount, type ParentProps, type Signal, useContext } from 'solid-js'; | ||
|
||
type Notifications = Record<string, NotificationData>; | ||
|
||
interface HookReturn { | ||
list: Accessor<Notifications>; | ||
set: (id: string, data: NotificationData) => void; | ||
clear: () => void; | ||
} | ||
|
||
const NotificationContext = createContext() as Context<Signal<Notifications>>; | ||
|
||
export function NotificationProvider(props: ParentProps) { | ||
const [notifications, setNotifications] = createSignal<Notifications>({}); | ||
let unlisten: UnlistenFn | undefined; | ||
|
||
onMount(() => { | ||
events.ingressPayload.listen((e) => { | ||
setNotifications(notifications => ({ | ||
...notifications, | ||
[e.payload.ingress_uuid]: { | ||
title: e.payload.event.type.replaceAll('_', ' '), | ||
message: e.payload.message, | ||
fraction: e.payload.fraction ?? undefined, | ||
}, | ||
})); | ||
}).then(u => unlisten = u); | ||
}); | ||
|
||
onCleanup(() => { | ||
unlisten?.(); | ||
}); | ||
|
||
return ( | ||
<NotificationContext.Provider value={[notifications, setNotifications]}> | ||
{props.children} | ||
</NotificationContext.Provider> | ||
); | ||
} | ||
|
||
function useNotifications(): HookReturn { | ||
const [notifications, setNotifications] = useContext(NotificationContext); | ||
|
||
return { | ||
list: notifications, | ||
set: (id, data) => setNotifications(notifications => ({ ...notifications, [id]: data })), | ||
clear: () => setNotifications({}), | ||
}; | ||
} | ||
|
||
export default useNotifications; |
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