From 6f83b8e2cd48fc8ca6f101c293d38f4a5c6c42ab Mon Sep 17 00:00:00 2001 From: dgreif Date: Mon, 11 Nov 2024 09:34:09 -0500 Subject: [PATCH] Fix homebridge ui by inlining js --- .changeset/orange-pumpkins-confess.md | 5 + .../public/homebridge-ring-ui.ts | 162 ---------------- .../homebridge-ui/public/index.html | 177 +++++++++++++++++- packages/homebridge-ring/package.json | 2 +- packages/homebridge-ring/tsconfig.json | 3 +- 5 files changed, 177 insertions(+), 172 deletions(-) create mode 100644 .changeset/orange-pumpkins-confess.md delete mode 100644 packages/homebridge-ring/homebridge-ui/public/homebridge-ring-ui.ts diff --git a/.changeset/orange-pumpkins-confess.md b/.changeset/orange-pumpkins-confess.md new file mode 100644 index 00000000..417cfbd8 --- /dev/null +++ b/.changeset/orange-pumpkins-confess.md @@ -0,0 +1,5 @@ +--- +'homebridge-ring': patch +--- + +Fix homebridge ui diff --git a/packages/homebridge-ring/homebridge-ui/public/homebridge-ring-ui.ts b/packages/homebridge-ring/homebridge-ui/public/homebridge-ring-ui.ts deleted file mode 100644 index 059433a3..00000000 --- a/packages/homebridge-ring/homebridge-ui/public/homebridge-ring-ui.ts +++ /dev/null @@ -1,162 +0,0 @@ -const { homebridge } = window -const newTokenButton = document.getElementById('ring-new-token') -const linkAccountHeader = document.getElementById('ring-link-account-header') - -if (!newTokenButton) { - homebridge.toast.error('no ring-new-token element found!') - throw new Error('no ring-new-token element found!') -} - -if (!linkAccountHeader) { - homebridge.toast.error('no ring-link-account-header element found!') - throw new Error('no ring-link-account-header element found!') -} - -newTokenButton.addEventListener('click', () => { - showLoginForm() -}) - -/** - * Render the correct form, based on whether we have an existing refresh token - */ -async function renderForm() { - const [config] = await homebridge.getPluginConfig() - const needToken = !config?.refreshToken - - homebridge.hideSpinner() - - if (needToken) { - showLoginForm() - } else { - showStandardForm() - } -} -renderForm() - -async function setRefreshToken(refreshToken: string) { - const [config, ...otherConfigs] = await homebridge.getPluginConfig() - await homebridge.updatePluginConfig([ - { ...config, refreshToken }, - ...otherConfigs, - ]) - homebridge.toast.success('Refresh Token Updated', 'Ring Login Successful') - showStandardForm() -} - -function showStandardForm() { - newTokenButton?.style.setProperty('display', 'block') - linkAccountHeader?.style.setProperty('display', 'none') - homebridge.showSchemaForm() -} - -function showLoginForm() { - // Hide the standard form - newTokenButton?.style.setProperty('display', 'none') - linkAccountHeader?.style.setProperty('display', 'block') - homebridge.hideSchemaForm() - - // Create a new login form - const form = homebridge.createForm( - { - schema: { - type: 'object', - properties: { - email: { - title: 'Email', - type: 'string', - 'x-schema-form': { - type: 'email', - }, - required: true, - }, - password: { - title: 'Password', - type: 'string', - 'x-schema-form': { - type: 'password', - }, - required: true, - }, - }, - }, - }, - {}, - 'Log In' - ) - - form.onSubmit(async ({ email, password }) => { - homebridge.showSpinner() - - try { - const response = (await homebridge.request('/send-code', { - email, - password, - })) as { codePrompt: string } | { refreshToken: string } - - if ('refreshToken' in response) { - // didn't need 2fa, return token without asking for code - setRefreshToken(response.refreshToken) - } else { - // Need a token, so show the token form - showTokenForm({ - email, - password, - codePrompt: response.codePrompt, - }) - } - } catch (e: any) { - // eslint-disable-next-line no-console - console.error(e) - homebridge.toast.error(e.message, 'Ring Login Failed') - } finally { - homebridge.hideSpinner() - } - }) -} - -function showTokenForm(loginInfo: { - email: string - password: string - codePrompt: string -}) { - const form = homebridge.createForm( - { - schema: { - type: 'object', - properties: { - code: { - title: 'Code', - type: 'string', - required: true, - description: loginInfo.codePrompt, - }, - }, - }, - }, - {}, - 'Link Account', - 'Change Email' - ) - - form.onSubmit(async ({ code }) => { - homebridge.showSpinner() - - try { - const { refreshToken } = await homebridge.request('/token', { - email: loginInfo.email, - password: loginInfo.password, - code, - }) - - setRefreshToken(refreshToken) - } catch (e: any) { - // eslint-disable-next-line no-console - console.error(e) - homebridge.toast.error(e.message, 'Failed to Link Account') - } finally { - homebridge.hideSpinner() - } - }) - - form.onCancel(() => showLoginForm()) -} diff --git a/packages/homebridge-ring/homebridge-ui/public/index.html b/packages/homebridge-ring/homebridge-ui/public/index.html index 6a0d3d9f..1ef5974a 100644 --- a/packages/homebridge-ring/homebridge-ui/public/index.html +++ b/packages/homebridge-ring/homebridge-ui/public/index.html @@ -1,17 +1,180 @@ - - diff --git a/packages/homebridge-ring/package.json b/packages/homebridge-ring/package.json index d24b53ee..98e4a985 100644 --- a/packages/homebridge-ring/package.json +++ b/packages/homebridge-ring/package.json @@ -5,7 +5,7 @@ "description": "Homebridge plugin for Ring doorbells, cameras, security alarm system and smart lighting", "main": "lib/index.js", "scripts": { - "build": "rm -rf lib && tsc && cp ./homebridge-ui/public/index.html ./lib/homebridge-ui/public/", + "build": "rm -rf lib && tsc && cp -r ./homebridge-ui/public ./lib/homebridge-ui/public/", "lint": "eslint . --ext .ts && tsc --noEmit", "dev": "concurrently -c yellow,blue --kill-others \"npm:dev:build\" \"npm:dev:run\" ", "dev:build": "tsc --watch --preserveWatchOutput", diff --git a/packages/homebridge-ring/tsconfig.json b/packages/homebridge-ring/tsconfig.json index 22590dac..63bde5f1 100644 --- a/packages/homebridge-ring/tsconfig.json +++ b/packages/homebridge-ring/tsconfig.json @@ -1,8 +1,7 @@ { "extends": "tsconfig/tsconfig.json", "compilerOptions": { - "outDir": "./lib", - "types": ["@homebridge/plugin-ui-utils/dist/ui.interface"], + "outDir": "lib" }, "include": ["**/*.ts"] }