|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const VERSION = 'v2'; |
|
|
|
|
|
|
|
const V1_IMAGE_IDS = [ |
|
'1', '48', '43', '22', '2', '3', '4', '5', '6', '7', '8', '9', |
|
'10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', |
|
'23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', |
|
'35', '36', '37', '38', '39', '40', '41', '42', '44', '45', '46', '47', |
|
'49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60' |
|
]; |
|
|
|
|
|
|
|
|
|
export interface State { |
|
|
|
modelName: string; |
|
|
|
imageId: string; |
|
|
|
prompts: string[]; |
|
} |
|
|
|
|
|
|
|
|
|
export const getUrl = |
|
(modelName: string, imageId: string, prompts: string[]): string => { |
|
let href = window.location.href; |
|
if (href.indexOf('#') !== -1) { |
|
href = href.substring(0, href.indexOf('#')); |
|
} |
|
const parts = [ |
|
VERSION, |
|
modelName, |
|
imageId, |
|
...prompts, |
|
]; |
|
return href + '#' + parts.map(encodeURIComponent).join('|'); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
export const parseUrl = (): State|undefined => { |
|
const hash = window.location.hash.substring(1); |
|
if (!hash) return; |
|
const parts = hash.split(/\|/g); |
|
if (parts.length < 4) { |
|
throw new Error(`Invalid URL: "${hash}"`); |
|
} |
|
let [version, modelName, imageId, ...texts] = parts; |
|
if (version === VERSION) { |
|
} else if (version === 'v1') { |
|
const idx = Number(imageId); |
|
if (isNaN(idx)) throw new Error(`Expected idx="${idx}" to be numerical!`); |
|
imageId = V1_IMAGE_IDS[idx]; |
|
} else { |
|
throw new Error(`Incompatible version: ${version} (supported: ${VERSION})`); |
|
} |
|
return { |
|
modelName, |
|
imageId, |
|
prompts: texts.map(decodeURIComponent), |
|
}; |
|
}; |
|
|