@ombori/ga-settings
Advanced tools
Comparing version 4.1.13 to 4.1.14
@@ -6,2 +6,13 @@ # Change Log | ||
## 4.1.14 (2024-08-25) | ||
### Bug Fixes | ||
* update ([e68dc51](https://github.com/ombori/gridapp/commit/e68dc51a92207cc1be4222543a7288fb5a17e86c)) | ||
## 4.1.13 (2024-08-25) | ||
@@ -8,0 +19,0 @@ |
@@ -13,2 +13,38 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -23,2 +59,112 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var types_1 = require("./types"); | ||
var pouchdb_browser_1 = __importDefault(require("pouchdb-browser")); | ||
var mediaDb = new pouchdb_browser_1.default('mediastorage'); | ||
// Helper function to recursively traverse the settings object and update blob URLs | ||
var updateBlobUrlsInSettings = function (settings) { return __awaiter(void 0, void 0, void 0, function () { | ||
var updatedSettings, recursivelyUpdateUrls; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
updatedSettings = __assign({}, settings); | ||
recursivelyUpdateUrls = function (obj) { return __awaiter(void 0, void 0, void 0, function () { | ||
var i, _a, _b, _i, key, blobId, newBlobUrl; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (!Array.isArray(obj)) return [3 /*break*/, 5]; | ||
i = 0; | ||
_c.label = 1; | ||
case 1: | ||
if (!(i < obj.length)) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, recursivelyUpdateUrls(obj[i])]; | ||
case 2: | ||
_c.sent(); | ||
_c.label = 3; | ||
case 3: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [3 /*break*/, 12]; | ||
case 5: | ||
if (!(typeof obj === 'object' && obj !== null)) return [3 /*break*/, 12]; | ||
_a = []; | ||
for (_b in obj) | ||
_a.push(_b); | ||
_i = 0; | ||
_c.label = 6; | ||
case 6: | ||
if (!(_i < _a.length)) return [3 /*break*/, 12]; | ||
key = _a[_i]; | ||
if (!obj.hasOwnProperty(key)) return [3 /*break*/, 11]; | ||
if (!(key === 'url' && | ||
typeof obj[key] === 'string' && | ||
obj[key].startsWith('blob:https://screen.omborigrid.com/'))) return [3 /*break*/, 9]; | ||
blobId = extractBlobId(obj[key]); | ||
if (!blobId) return [3 /*break*/, 8]; | ||
return [4 /*yield*/, fetchMediaBlobUrl(blobId)]; | ||
case 7: | ||
newBlobUrl = _c.sent(); | ||
if (newBlobUrl) { | ||
obj[key] = newBlobUrl; // Update with the new blob URL | ||
} | ||
_c.label = 8; | ||
case 8: return [3 /*break*/, 11]; | ||
case 9: | ||
if (!(typeof obj[key] === 'object' && obj[key] !== null)) return [3 /*break*/, 11]; | ||
// Recursively update nested objects | ||
return [4 /*yield*/, recursivelyUpdateUrls(obj[key])]; | ||
case 10: | ||
// Recursively update nested objects | ||
_c.sent(); | ||
_c.label = 11; | ||
case 11: | ||
_i++; | ||
return [3 /*break*/, 6]; | ||
case 12: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
// Start recursion with the full settings object | ||
return [4 /*yield*/, recursivelyUpdateUrls(updatedSettings)]; | ||
case 1: | ||
// Start recursion with the full settings object | ||
_a.sent(); | ||
return [2 /*return*/, updatedSettings]; | ||
} | ||
}); | ||
}); }; | ||
// Function to extract blob ID from a blob URL | ||
var extractBlobId = function (blobUrl) { | ||
var matches = blobUrl.match(/blob:https:\/\/screen\.omborigrid\.com\/(.+)/); | ||
return matches ? matches[1] : null; | ||
}; | ||
// Fetches a media blob from PouchDB and creates a blob URL | ||
var fetchMediaBlobUrl = function (id) { return __awaiter(void 0, void 0, void 0, function () { | ||
var doc, attachment, blob, error_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, mediaDb.get(id, { attachments: true, binary: true })]; | ||
case 1: | ||
doc = _a.sent(); | ||
// Ensure doc._attachments is not undefined and has the correct format | ||
if (doc && doc._attachments && doc._attachments.media) { | ||
attachment = doc._attachments.media; | ||
// Type assertion to correctly access 'data' property on Attachment | ||
if ('data' in attachment) { | ||
blob = new Blob([attachment.data], { | ||
type: attachment.content_type, | ||
}); | ||
return [2 /*return*/, URL.createObjectURL(blob)]; | ||
} | ||
} | ||
return [2 /*return*/, null]; | ||
case 2: | ||
error_1 = _a.sent(); | ||
console.error('Error fetching media blob:', error_1); | ||
return [2 /*return*/, null]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
var DEVELOPMENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXX'; | ||
@@ -84,2 +230,3 @@ var settingsKey = document.location.pathname.split('/').splice(0, 4).join('/'); | ||
function useSettings(props) { | ||
var _this = this; | ||
if (props === void 0) { props = {}; } | ||
@@ -89,62 +236,65 @@ var hasLoadedSettings = react_1.useRef(false); | ||
react_1.useEffect(function () { | ||
var handler = function () { | ||
// settings are ready for use! | ||
var gridapp = window.gridapp; | ||
if (gridapp && !hasLoadedSettings.current) { | ||
log('Settings ready3: ', { gridapp: gridapp, boom: gridapp.getSettings() }); | ||
var gridappSettings = gridapp.getSettings(); | ||
console.log({ gridappSettings: gridappSettings }); | ||
console.log('To set in localstorage: ', JSON.stringify(gridappSettings)); | ||
var settingsOverride = {}; | ||
var installationType = getInstallationTypeFromSettings(gridappSettings); | ||
switch (installationType) { | ||
case types_1.InstallationTypeEnum.SCREEN: | ||
console.log('InstallationTypeEnum.SCREEN'); | ||
var device = getDeviceInfo(gridappSettings); | ||
if (device) { | ||
var devicesSettings = (gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._devicesSettings) || {}; | ||
var spacesSettings = (gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._spacesSettings) || {}; | ||
var deviceId = device.id, spaceId = device.spaceId; | ||
if (devicesSettings[deviceId]) { | ||
settingsOverride = devicesSettings[deviceId]; | ||
var handler = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var gridapp, gridappSettings, settingsOverride, installationType, device, devicesSettings, spacesSettings, deviceId, spaceId, mobileEndpoint, mobileEndpointsSettings, spacesSettings, mobileEndpointId, spaceId; | ||
return __generator(this, function (_a) { | ||
gridapp = window.gridapp; | ||
if (gridapp && !hasLoadedSettings.current) { | ||
log('Settings ready3: ', { gridapp: gridapp, boom: gridapp.getSettings() }); | ||
gridappSettings = gridapp.getSettings(); | ||
console.log({ gridappSettings: gridappSettings }); | ||
console.log('To set in localstorage: ', JSON.stringify(gridappSettings)); | ||
settingsOverride = {}; | ||
installationType = getInstallationTypeFromSettings(gridappSettings); | ||
switch (installationType) { | ||
case types_1.InstallationTypeEnum.SCREEN: | ||
console.log('InstallationTypeEnum.SCREEN'); | ||
device = getDeviceInfo(gridappSettings); | ||
if (device) { | ||
devicesSettings = (gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._devicesSettings) || {}; | ||
spacesSettings = (gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._spacesSettings) || {}; | ||
deviceId = device.id, spaceId = device.spaceId; | ||
if (devicesSettings[deviceId]) { | ||
settingsOverride = devicesSettings[deviceId]; | ||
} | ||
else if (spacesSettings[spaceId]) { | ||
settingsOverride = spacesSettings[spaceId]; | ||
} | ||
} | ||
else if (spacesSettings[spaceId]) { | ||
settingsOverride = spacesSettings[spaceId]; | ||
break; | ||
case types_1.InstallationTypeEnum.MOBILE: | ||
console.log('InstallationTypeEnum.MOBILE'); | ||
mobileEndpoint = gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._mobileEndpointInfo; | ||
if (mobileEndpoint && !utils_1.isObjectEmpty(mobileEndpoint)) { | ||
mobileEndpointsSettings = (gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._mobileEndpointsSettings) || {}; | ||
spacesSettings = (gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._spacesSettings) || {}; | ||
mobileEndpointId = mobileEndpoint.id, spaceId = mobileEndpoint.space; | ||
if (mobileEndpointsSettings[mobileEndpointId]) { | ||
settingsOverride = mobileEndpointsSettings[mobileEndpointId]; | ||
} | ||
else if (spacesSettings[spaceId]) { | ||
settingsOverride = spacesSettings[spaceId]; | ||
} | ||
} | ||
} | ||
break; | ||
case types_1.InstallationTypeEnum.MOBILE: | ||
console.log('InstallationTypeEnum.MOBILE'); | ||
var mobileEndpoint = gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._mobileEndpointInfo; | ||
if (mobileEndpoint && !utils_1.isObjectEmpty(mobileEndpoint)) { | ||
var mobileEndpointsSettings = (gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._mobileEndpointsSettings) || {}; | ||
var spacesSettings = (gridappSettings === null || gridappSettings === void 0 ? void 0 : gridappSettings._spacesSettings) || {}; | ||
var mobileEndpointId = mobileEndpoint.id, spaceId = mobileEndpoint.space; | ||
if (mobileEndpointsSettings[mobileEndpointId]) { | ||
settingsOverride = mobileEndpointsSettings[mobileEndpointId]; | ||
} | ||
else if (spacesSettings[spaceId]) { | ||
settingsOverride = spacesSettings[spaceId]; | ||
} | ||
} | ||
break; | ||
default: | ||
console.log('InstallationTypeEnum.DEFAULT'); | ||
break; | ||
break; | ||
default: | ||
console.log('InstallationTypeEnum.DEFAULT'); | ||
break; | ||
} | ||
console.log('Setting settings:', { | ||
settings: __assign(__assign({}, gridappSettings), settingsOverride), | ||
}); | ||
setSettings(__assign(__assign({}, gridappSettings), settingsOverride)); | ||
hasLoadedSettings.current = true; | ||
console.log('Handler hasLoadedSettings.current true', { | ||
gridapp: window.gridapp, | ||
settings: window.gridapp.getSettings(), | ||
}); | ||
} | ||
console.log('Setting settings:', { | ||
settings: __assign(__assign({}, gridappSettings), settingsOverride), | ||
}); | ||
setSettings(__assign(__assign({}, gridappSettings), settingsOverride)); | ||
hasLoadedSettings.current = true; | ||
console.log('Handler hasLoadedSettings.current true', { | ||
console.log('Handler hasLoadedSettings.current false', { | ||
gridapp: window.gridapp, | ||
settings: window.gridapp.getSettings(), | ||
}); | ||
} | ||
console.log('Handler hasLoadedSettings.current false', { | ||
gridapp: window.gridapp, | ||
settings: window.gridapp.getSettings(), | ||
return [2 /*return*/]; | ||
}); | ||
}; | ||
}); }; | ||
if (utils_1.isDev) { | ||
@@ -157,23 +307,30 @@ setSettings(getDevSettings(props.devSettings)); | ||
try { | ||
var localStorageSettings_1 = JSON.parse(localStorageGridAppSettings); | ||
console.log({ localStorageSettings: localStorageSettings_1 }); | ||
window.gridParams = { | ||
id: 'app-container', | ||
root: settingsKey, | ||
settings: localStorageSettings_1, | ||
}; | ||
window.gridapp = { | ||
addEventListener: function (event, listener) { | ||
window.addEventListener(event, listener); | ||
}, | ||
removeEventListener: function (event, listener) { | ||
window.removeEventListener(event, listener); | ||
}, | ||
send: function (_event) { | ||
// window.send(event); | ||
}, | ||
getSettings: function () { return localStorageSettings_1; }, | ||
}; | ||
window.dispatchEvent(new CustomEvent('ready')); | ||
window.dispatchEvent(new Event('GridappReady')); | ||
var localStorageSettingsTmp = JSON.parse(localStorageGridAppSettings); | ||
// Recursively update blob URLs in settings | ||
updateBlobUrlsInSettings(localStorageSettingsTmp) | ||
.then(function (localStorageSettings) { | ||
console.log({ localStorageSettings: localStorageSettings }); | ||
window.gridParams = { | ||
id: 'app-container', | ||
root: settingsKey, | ||
settings: localStorageSettings, | ||
}; | ||
window.gridapp = { | ||
addEventListener: function (event, listener) { | ||
window.addEventListener(event, listener); | ||
}, | ||
removeEventListener: function (event, listener) { | ||
window.removeEventListener(event, listener); | ||
}, | ||
send: function (_event) { | ||
// window.send(event); | ||
}, | ||
getSettings: function () { return localStorageSettings; }, | ||
}; | ||
window.dispatchEvent(new CustomEvent('ready')); | ||
window.dispatchEvent(new Event('GridappReady')); | ||
}) | ||
.catch(function (error) { | ||
console.error('Error updating blob URLs in settings:', error); | ||
}); | ||
} | ||
@@ -180,0 +337,0 @@ catch (err) { |
{ | ||
"name": "@ombori/ga-settings", | ||
"version": "4.1.13", | ||
"version": "4.1.14", | ||
"main": "dist/index.js", | ||
@@ -19,3 +19,6 @@ "license": "UNLICENSED", | ||
"test:coverage": "npm run test -- --coverage --watchAll=false", | ||
"gitHead": "7af23c0aca7e57d1c765888cb359cc9c1130b77c" | ||
"dependencies": { | ||
"pouchdb-browser": "^9.0.0" | ||
}, | ||
"gitHead": "695c6761272a20d4c6e1a9cd7ce124e615a92d2b" | ||
} |
126
src/index.ts
@@ -14,3 +14,78 @@ import { useEffect, useState, useRef } from 'react'; | ||
} from './types'; | ||
import PouchDB from 'pouchdb-browser'; | ||
const mediaDb = new PouchDB('mediastorage'); | ||
// Helper function to recursively traverse the settings object and update blob URLs | ||
const updateBlobUrlsInSettings = async (settings: any) => { | ||
const updatedSettings = { ...settings }; | ||
const recursivelyUpdateUrls = async (obj: any) => { | ||
if (Array.isArray(obj)) { | ||
// Iterate through array elements | ||
for (let i = 0; i < obj.length; i++) { | ||
await recursivelyUpdateUrls(obj[i]); | ||
} | ||
} else if (typeof obj === 'object' && obj !== null) { | ||
// Iterate through object properties | ||
for (const key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
if ( | ||
key === 'url' && | ||
typeof obj[key] === 'string' && | ||
obj[key].startsWith('blob:https://screen.omborigrid.com/') | ||
) { | ||
// Extract blob ID from the blob URL | ||
const blobId = extractBlobId(obj[key]); | ||
if (blobId) { | ||
const newBlobUrl = await fetchMediaBlobUrl(blobId); | ||
if (newBlobUrl) { | ||
obj[key] = newBlobUrl; // Update with the new blob URL | ||
} | ||
} | ||
} else if (typeof obj[key] === 'object' && obj[key] !== null) { | ||
// Recursively update nested objects | ||
await recursivelyUpdateUrls(obj[key]); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
// Start recursion with the full settings object | ||
await recursivelyUpdateUrls(updatedSettings); | ||
return updatedSettings; | ||
}; | ||
// Function to extract blob ID from a blob URL | ||
const extractBlobId = (blobUrl: any) => { | ||
const matches = blobUrl.match(/blob:https:\/\/screen\.omborigrid\.com\/(.+)/); | ||
return matches ? matches[1] : null; | ||
}; | ||
// Fetches a media blob from PouchDB and creates a blob URL | ||
const fetchMediaBlobUrl = async (id: string): Promise<string | null> => { | ||
try { | ||
const doc = await mediaDb.get(id, { attachments: true, binary: true }); | ||
// Ensure doc._attachments is not undefined and has the correct format | ||
if (doc && doc._attachments && doc._attachments.media) { | ||
const attachment = doc._attachments.media; | ||
// Type assertion to correctly access 'data' property on Attachment | ||
if ('data' in attachment) { | ||
const blob = new Blob([attachment.data as BlobPart], { | ||
type: attachment.content_type, | ||
}); | ||
return URL.createObjectURL(blob); | ||
} | ||
} | ||
return null; | ||
} catch (error) { | ||
console.error('Error fetching media blob:', error); | ||
return null; | ||
} | ||
}; | ||
const DEVELOPMENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXX'; | ||
@@ -89,3 +164,3 @@ | ||
useEffect(() => { | ||
const handler = () => { | ||
const handler = async () => { | ||
// settings are ready for use! | ||
@@ -171,23 +246,30 @@ const gridapp = window.gridapp; | ||
try { | ||
const localStorageSettings = JSON.parse(localStorageGridAppSettings); | ||
console.log({ localStorageSettings }); | ||
(window as any).gridParams = { | ||
id: 'app-container', | ||
root: settingsKey, | ||
settings: localStorageSettings, | ||
}; | ||
window.gridapp = { | ||
addEventListener: (event: any, listener: any) => { | ||
window.addEventListener(event, listener); | ||
}, | ||
removeEventListener: (event: any, listener: any) => { | ||
window.removeEventListener(event, listener); | ||
}, | ||
send: (_event: any) => { | ||
// window.send(event); | ||
}, | ||
getSettings: () => localStorageSettings, | ||
}; | ||
window.dispatchEvent(new CustomEvent('ready')); | ||
window.dispatchEvent(new Event('GridappReady')); | ||
const localStorageSettingsTmp = JSON.parse(localStorageGridAppSettings); | ||
// Recursively update blob URLs in settings | ||
updateBlobUrlsInSettings(localStorageSettingsTmp) | ||
.then((localStorageSettings) => { | ||
console.log({ localStorageSettings }); | ||
(window as any).gridParams = { | ||
id: 'app-container', | ||
root: settingsKey, | ||
settings: localStorageSettings, | ||
}; | ||
window.gridapp = { | ||
addEventListener: (event: any, listener: any) => { | ||
window.addEventListener(event, listener); | ||
}, | ||
removeEventListener: (event: any, listener: any) => { | ||
window.removeEventListener(event, listener); | ||
}, | ||
send: (_event: any) => { | ||
// window.send(event); | ||
}, | ||
getSettings: () => localStorageSettings, | ||
}; | ||
window.dispatchEvent(new CustomEvent('ready')); | ||
window.dispatchEvent(new Event('GridappReady')); | ||
}) | ||
.catch((error) => { | ||
console.error('Error updating blob URLs in settings:', error); | ||
}); | ||
} catch (err) { | ||
@@ -194,0 +276,0 @@ console.log('localStorageGridAppSettings error:', err); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87091
1383
2
+ Addedpouchdb-browser@^9.0.0
+ Addedpouchdb-browser@9.0.0(transitive)
+ Addedspark-md5@3.0.2(transitive)
+ Addeduuid@8.3.2(transitive)
+ Addedvuvuzela@1.0.3(transitive)