Skip to content

Commit

Permalink
Chore: refactor notifications and enable permanent notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
avivash committed Jan 4, 2024
1 parent c9c46ae commit f0e000e
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const copyLink = async () => {
await clipboardCopy(connectionLink)
addNotification('Copied to clipboard', 'success')
addNotification({ msg: 'Copied to clipboard', type: 'success' })
}
</script>

Expand Down
2 changes: 1 addition & 1 deletion src/components/auth/link-device/LinkDevice.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
const copyCode = async () => {
await clipboardCopy(pin)
addNotification('Copied to clipboard', 'success')
addNotification({ msg: 'Copied to clipboard', type: 'success' })
}
</script>

Expand Down
4 changes: 2 additions & 2 deletions src/components/home/Public.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
await register(encodedUsername)
addNotification('Account created!', 'success')
addNotification({ msg: 'Account created!', type: 'success' })
} catch (error) {
console.error(error)
addNotification('Failed to register account', 'error')
addNotification({ msg: 'Failed to register account', type: 'error' })
}
loading = false
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/settings/NodeInfo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
const handleCopyToClipboard = async (value: string): Promise<void> => {
await clipboardCopy(value)
addNotification('Copied to clipboard', 'success')
addNotification({ msg: 'Copied to clipboard', type: 'success' })
}
onMount(async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/settings/Username.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const handleCopyUsername = async (): Promise<void> => {
await clipboardCopy($sessionStore.username.full)
addNotification('Copied to clipboard', 'success')
addNotification({ msg: 'Copied to clipboard', type: 'success' })
}
</script>

Expand Down
4 changes: 2 additions & 2 deletions src/lib/account-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ export const uploadAvatarToWNFS = async (image: File): Promise<void> => {
// Announce the changes to the server
await fs.publish()

addNotification(`Your avatar has been updated!`, 'success')
addNotification({ msg: `Your avatar has been updated!`, type: 'success' })
} catch (error) {
addNotification(error.message, 'error')
addNotification({ msg: error.message, type: 'error' })
console.error(error)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const subscribNetworkEvents = async (): Promise<void> => {
})
} catch (error) {
console.error(error)
addNotification('Failed to subscribe to network events', 'error')
addNotification({ msg: 'Failed to subscribe to network events', type: 'error' })
}

// await homestar2.networkEvents(result => {
Expand Down
28 changes: 18 additions & 10 deletions src/lib/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ export const removeNotification: (id: string) => void = id => {
)
}

export const addNotification: (
msg: string,
type?: NotificationType,
export const addNotification = ({
msg,
permanent = false,
timeout = 5000,
type = 'info',
}: {
msg: string
permanent?: boolean
timeout?: number
) => void = (msg, type = 'info', timeout = 5000) => {
type?: NotificationType
}): string => {
// uuid for each notification
const id = crypto.randomUUID()

Expand All @@ -30,15 +36,17 @@ export const addNotification: (
id,
msg,
type,
timeout,
timeout
}
])

// removing the notification after a specified timeout
const timer = setTimeout(() => {
removeNotification(id)
clearTimeout(timer)
}, timeout)
if (!permanent) {
// removing the notification after a specified timeout
const timer = setTimeout(() => {
removeNotification(id)
clearTimeout(timer)
}, timeout)
}

// return the id
return id
Expand Down
2 changes: 1 addition & 1 deletion src/lib/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getHomestarClient = (): Client => {
return homestar
} catch (error) {
console.error(error)
addNotification(error, 'error')
addNotification({ msg: error, type: 'error' })
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/workflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export const handleMessage = async (message: Message): Promise<void> => {
}
} catch (error) {
console.error(error)
addNotification('Run failed', 'error')
addNotification({ msg: 'Run failed', type: 'error' })
}
}

Expand Down
47 changes: 12 additions & 35 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import '../global.css'
import { instantiatePostHog } from '$lib/analytics'
import {
checkHomestarConnection,
checkIPFSConnection
} from '$lib/connections'
import subscribNetworkEvents from '$lib/network'
import { addNotification } from '$lib/notifications'
import { sessionStore, themeStore } from '$lib/stores'
Expand All @@ -29,7 +33,7 @@
const unsubscribeSessionStore = sessionStore.subscribe(session => {
if (session.error) {
const message = errorToMessage(session.error)
addNotification(message, 'error')
addNotification({ msg: message, type: 'error' })
}
})
Expand All @@ -48,41 +52,14 @@
init()
onMount(async () => {
// Check if Homestar node is connected
let homestarError = false
const ws = new WebSocket(import.meta.env.VITE_WEBSOCKET_ENDPOINT)
ws.addEventListener('error', () => {
// Connection closed
if (ws.readyState === 3) {
homestarError = true
console.error(`Error: couldn't connect to WebSocket`)
addNotification(
'Failed to connect to Homestar. Please check the WebSocket endpoint in your .env and ensure Homestar is running.',
'error',
15000
)
}
})
// Check if IPFS daemon is running
try {
await fetch('http://localhost:5001/debug/vars', {
method: 'GET',
mode: 'no-cors'
})
} catch (error) {
homestarError = true
console.error(`Error: IPFS daemon isn't running`)
addNotification(
"Your IPFS daemon isn't running. Please run 'ipfs daemon' in your terminal.",
'error',
15000
)
}
// Check for a Homestar WebSocket connection
checkHomestarConnection()
if (!homestarError) {
subscribNetworkEvents()
}
// Check if the IPFS daemon is running
await checkIPFSConnection()
// Subscribe to Homestar network events
subscribNetworkEvents()
// Instantiate PostHog Analytics
instantiatePostHog()
Expand Down
18 changes: 12 additions & 6 deletions src/routes/delegate-account/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@
if (fs) {
await setBackupStatus(fs, { created: true })
addNotification("You've connected a backup device!", 'success')
addNotification({
msg: "You've connected a backup device!",
type: 'success'
})
goto('/')
} else {
addNotification(
'Missing filesystem. Unable to create a backup device.',
'error'
)
addNotification({
msg: 'Missing filesystem. Unable to create a backup device.',
type: 'error'
})
}
}
})
Expand All @@ -109,7 +112,10 @@
const cancelConnection = () => {
rejectPin()
addNotification('The connection attempt was cancelled', 'info')
addNotification({
msg: 'The connection attempt was cancelled',
type: 'info'
})
goto('/')
}
Expand Down
12 changes: 9 additions & 3 deletions src/routes/link-device/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@
await waitForDataRoot(hashedUsername)
await loadAccount(hashedUsername, fullUsername)
addNotification("You're now connected!", 'success')
addNotification({ msg: "You're now connected!", type: 'success' })
goto('/')
} else {
addNotification('The connection attempt was cancelled', 'info')
addNotification({
msg: 'The connection attempt was cancelled',
type: 'info'
})
goto('/')
}
})
}
const cancelConnection = async () => {
addNotification('The connection attempt was cancelled', 'info')
addNotification({
msg: 'The connection attempt was cancelled',
type: 'info'
})
await accountLinkingConsumer?.cancel()
goto('/')
Expand Down
18 changes: 9 additions & 9 deletions src/routes/workflows/components/WorkflowBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
w.name.toLowerCase() === $workflowsStore.builder.name.toLowerCase()
)
) {
addNotification('Workflow name must be unique', 'error')
addNotification({ msg: 'Workflow name must be unique', type: 'error' })
saving = false
return
}
Expand All @@ -76,7 +76,7 @@
const errorId = document.querySelector('.input-error')?.id
const errorMessage = 'Invalid params'
if (errorId && !imageBitmap) {
addNotification(errorMessage, 'error')
addNotification({ msg: errorMessage, type: 'error' })
throw new Error(errorMessage)
}
Expand Down Expand Up @@ -111,16 +111,16 @@
(errorId && CROP_PARAMS.find(p => p.name === errorId)) ||
invalidCropError
) {
addNotification(
`Crop x + width must be less than ${imageBitmap.width} and crop y + height must be less than ${imageBitmap.height}`,
'error',
7000
)
addNotification({
msg: `Crop x + width must be less than ${imageBitmap.width} and crop y + height must be less than ${imageBitmap.height}`,
type: 'error',
timeout: 7000
})
throw new Error(errorMessage)
}
if (errorId) {
addNotification(errorMessage, 'error')
addNotification({ msg: errorMessage, type: 'error' })
throw new Error(errorMessage)
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@
handleCloseBuilder()
addNotification('Workflow created', 'success')
addNotification({ msg: 'Workflow created', type: 'success' })
} catch (error) {
saving = false
}
Expand Down
4 changes: 2 additions & 2 deletions src/routes/workflows/components/graph/Actions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@
})?.length
if ((errorId && CROP_PARAMS.find(p => p.name === errorId)) || invalidCropError) {
addNotification(`Crop x + width must be less than ${imageBitmap.width} and crop y + height must be less than ${imageBitmap.height}`, 'error', 7000)
addNotification({ msg: `Crop x + width must be less than ${imageBitmap.width} and crop y + height must be less than ${imageBitmap.height}`, type: 'error', timeout: 7000 })
throw new Error(errorMessage)
}
if (errorId) {
addNotification(errorMessage, 'error')
addNotification({ msg: errorMessage, type: 'error' })
throw new Error(errorMessage)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/workflows/components/graph/ImageNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
status = 'running'
if (files[0]?.type !== 'image/png') {
addNotification('Image must be a PNG', 'error')
addNotification({ msg: 'Image must be a PNG', type: 'error' })
status = 'ready'
return
}
Expand Down
18 changes: 9 additions & 9 deletions src/routes/workflows/components/graph/NewFunctionNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
(functionName === 'blur' && updatedArg < param?.min)
if (invalid) {
// addNotification(`Param must be less than ${param.max}`)
// addNotification({ msg: `Param must be less than ${param.max}` })
event.target.classList.add(errorClass)
return
}
Expand All @@ -102,16 +102,16 @@
.charAt(0)
.toUpperCase()}${param.name.slice(1)}`
if (invalidCropWidth) {
addNotification(
`${paramName} must be less than ${imageBitmap.width}`,
'error'
)
addNotification({
msg: `${paramName} must be less than ${imageBitmap.width}`,
type: 'error'
})
}
if (invalidCropHeight) {
addNotification(
`${paramName} must be less than ${imageBitmap.height}`,
'error'
)
addNotification({
msg: `${paramName} must be less than ${imageBitmap.height}`,
type: 'error'
})
}
return
Expand Down
Loading

0 comments on commit f0e000e

Please sign in to comment.