Skip to content
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

feat: Add printer url in search params so that URLs become bookmarkable #1574

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import sanitizeEndpoint from './util/sanitize-endpoint'
import webSocketWrapper from './util/web-socket-wrapper'
import promiseAny from './util/promise-any'
import sleep from './util/sleep'
import md5 from 'md5'

// Load API configuration
/**
Expand All @@ -32,11 +33,19 @@ const getHostConfig = async () => {
}
}

const getApiConfig = async (hostConfig: HostConfig): Promise<ApiConfig | InstanceConfig> => {
const getApiConfig = async (hostConfig: HostConfig, apiUrlHash?: string | null): Promise<ApiConfig | InstanceConfig> => {
// Local storage load
if (Globals.LOCAL_INSTANCES_STORAGE_KEY in localStorage) {
const instances = JSON.parse(localStorage[Globals.LOCAL_INSTANCES_STORAGE_KEY]) as InstanceConfig[]
if (instances && instances.length) {
if (apiUrlHash) {
for (const config of instances) {
if (md5(config.apiUrl) === apiUrlHash) {
consola.debug('API Config from Local Storage', config)
return config
}
}
}
for (const config of instances) {
if (config.active) {
consola.debug('API Config from Local Storage', config)
Expand Down Expand Up @@ -158,9 +167,22 @@ export const appInit = async (apiConfig?: ApiConfig, hostConfig?: HostConfig): P
}
}

const locationUrl = new URL(window.location.href)

// Check if we have a printer url hash in search params
const apiUrlHash = locationUrl.searchParams.get('printer')

// Load the API Config
if (!apiConfig) {
apiConfig = await getApiConfig(hostConfig)
apiConfig = await getApiConfig(hostConfig, apiUrlHash)
}

if (apiConfig.apiUrl) {
// Set the printer url hash in the search params so that the url is bookmarkable

locationUrl.searchParams.set('printer', md5(apiConfig.apiUrl))

window.history.replaceState(window.history.state, '', locationUrl)
}

// Setup axios
Expand Down
Loading