-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bce687a
commit e86a238
Showing
17 changed files
with
462 additions
and
195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.errors { | ||
position: fixed; | ||
top: 5px; | ||
right: 5px; | ||
z-index: 1000; | ||
|
||
&__notification { | ||
position: relative; | ||
background-color: #f44336; | ||
color: #fff; | ||
padding: 10px 20px; | ||
border-radius: 5px; | ||
margin-bottom: 10px; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); | ||
} | ||
|
||
&__notification-close { | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
cursor: pointer; | ||
} | ||
} |
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,28 @@ | ||
import React from 'react'; | ||
import { useErrors } from '../../util/errors'; | ||
import { Stack } from '../stack/stack'; | ||
import './errors.scss'; | ||
|
||
export function Errors(): JSX.Element { | ||
const errors = useErrors(); | ||
return ( | ||
<div className="errors"> | ||
<Stack size={4}> | ||
{errors.map((e) => ( | ||
<div className="errors__notification" key={e.message}> | ||
<button | ||
className="errors__notification-close" | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
e.confirm(); | ||
}} | ||
> | ||
X | ||
</button> | ||
{e.message} | ||
</div> | ||
))} | ||
</Stack> | ||
</div> | ||
); | ||
} |
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,6 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
export function useRefresh(): () => void { | ||
const [, setUpdate] = useState(0); | ||
return useCallback(() => setUpdate((u) => u + 1), [setUpdate]); | ||
} |
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,59 @@ | ||
import { useEffect } from 'react'; | ||
import { useRefresh } from './basic_hooks'; | ||
|
||
interface DisplayedError { | ||
message: string; | ||
error: unknown; | ||
confirm: () => void; | ||
} | ||
|
||
class ErrorManager { | ||
static _instance: ErrorManager; | ||
|
||
static get instance() { | ||
if (!this._instance) { | ||
this._instance = new ErrorManager(); | ||
} | ||
return this._instance; | ||
} | ||
|
||
errors: DisplayedError[] = []; | ||
listeners: (() => void)[] = []; | ||
|
||
handleError(error: unknown) { | ||
const message = error instanceof Error ? error.message : String(error); | ||
const displayedError: DisplayedError = { | ||
message, | ||
error, | ||
confirm: () => {}, | ||
}; | ||
displayedError.confirm = () => { | ||
this.errors = this.errors.filter((e) => e !== displayedError); | ||
this.listeners.forEach((listener) => listener()); | ||
}; | ||
this.errors.push(displayedError); | ||
this.listeners.forEach((listener) => listener()); | ||
} | ||
|
||
addListener(listener: () => void) { | ||
this.listeners.push(listener); | ||
} | ||
|
||
removeListener(listener: () => void) { | ||
this.listeners = this.listeners.filter((l) => l !== listener); | ||
} | ||
} | ||
|
||
export function useErrors(): DisplayedError[] { | ||
const refresh = useRefresh(); | ||
const errors = ErrorManager.instance.errors; | ||
useEffect(() => { | ||
ErrorManager.instance.addListener(refresh); | ||
return () => ErrorManager.instance.removeListener(refresh); | ||
}, []); | ||
return errors; | ||
} | ||
|
||
export function handleError(error: unknown) { | ||
ErrorManager.instance.handleError(error); | ||
} |
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 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 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { defineConfig } from 'vite'; | ||
import Rails from 'vite-plugin-rails'; | ||
import react from '@vitejs/plugin-react'; | ||
|
||
export default defineConfig({ | ||
plugins: [Rails()], | ||
plugins: [Rails(), react()], | ||
}); |
Oops, something went wrong.