Files
api/web/store/server.ts
Zack Spear 309d221542 feat / OEM whitelabel support (#973)
* feat: begin fixing dark mode in the webcomponents

* feat: lots of progress on colors

* feat: begin nuking alpha beta gamma

* feat: set background color on webcomponents

* fix: more color work

* feat: eliminate all alpha beta gamma variable usage

* feat: move variable declarations to theme.ts

* feat: begin fixing dark mode in the webcomponents

* feat: lots of progress on colors

* feat: begin nuking alpha beta gamma

* feat: set background color on webcomponents

* fix: more color work

* feat: eliminate all alpha beta gamma variable usage

* feat: download nodejs and install on legacy OS versions

* feat: do not move upgradepkg

* feat: array iteration for restoring files

* feat: separate install process

* feat: extract node to usr/local/

* feat: move variable declarations to theme.ts

* feat: remove nghttp3 and only bundle nodejs

* feat: validate entries correctly

* fix: upgradepkg

* feat: copy only needed files for nodejs

* fix: install syntax error

* fix: strip components from tar line

* feat: error when nodejs download fails

* feat(php): add OEM data extraction functionality

* feat(web): WIP add OEM activation modal and integrate OEM data handling

* refactor: replace oem etamology with activation

* feat(web): enhance activation modal with header and main title options

* refactor(web): update activation modal title to handle partner name conditionally

* feat: make partnerName optional in activation code data

* refactor: remove activationCodeStore from UserProfile component

* feat: integrate activation code data into server store

* feat: enhance activation code store to include callback actions and improve modal visibility logic

* refactor: remove unused theme reference from ColorSwitcher component

* fix: update partnerName in activationCodeData for clarity

* refactor: adjust gap spacing in Modal component for improved layout

* feat: enhance Activation Modal component with dynamic title and description, and add documentation buttons

* feat: implement localization for Activation Modal component titles and button texts

* feat: add new translations for activation prompts and welcome messages in the WebComponentTranslations class

* feat: implement key sequence to close Activation Modal and persist visibility state

* feat: update activation logic to conditionally activate or redeem based on activation code presence

* feat(plg): plg install / remove partner banner

* feat(plg): on install update config to enable display settings banner and on remove banner setting if no custom banner used

* fix(plg): plg remove, display banner no if no custom image

* feat(plg): implement partner banner handling and system model identification from activation JSON

* feat(web): add loading and error states to notification sidebar

* refactor(web): add container for loading & error states

* feat(web): add count labels to notification tabs

* refactor(web): lift notifications overview query to Sidebar from Indicator

* fix(web): refetch notifications for sidebar when new notifications arrive

* feat(web): move notification indicator icons to top-right of bell icon

previously, icons were placed next to bell icon because the status indicators
were not accessible to color-blind users. this commit replaces circular
status indicators with the icons.

* feat(web): remove notification indicator pulse

the pulse was initially added to provide visual feedback when:

1. a new notification arrived
2. an alert notification was unread

because we began using the legacy notify script, we now get a toast
on new notifications. re:2, feedback on the pulse was mixed, so i'm
removing it.

* refactor(plg): improve conditional for activation & partner setup

* feat(plg): add case model icon handling based on activation JSON

* feat(plg): enhance partner setup process with detailed logging and system identification updates and setup flag

* feat(plg): add setup flag file creation and logging for activation changes

* feat(plg): update activation setup flag checks for banner, case model icon, and system identity

* fix(plg): add error handling for invalid activation JSON and improve conditional checks for setup flags

* fix(plg): improve partner banner and case model icon setup logic with enhanced checks and comments

* fix(plg): update case model icon configuration to prevent newline issues in parsing

* fix(plg): remove unnecessary echo debug statements and improve setup flag handling for partner details

* fix(plg): remove unnecessary registration page check from activation code modal logic

* fix(plg): add overlay opacity prop to modal components for customizable background transparency

* fix(plg): enhance modal component with vertical centering option and adjust max-width

* fix(plg): enhance configuration handling by dynamically updating multiple header parameters in the config file

* fix(plg): improve comments and enhance setup flag handling for custom icons in activation logic

* fix(translations): correct capitalization and punctuation in user prompts for consistency

* fix(store): enhance activation code data structure with additional properties and correct partner URL reference

* fix(modal): enhance activation modal with dynamic partner logo and improved title handling

* fix(store): update activation code data structure with additional properties and correct partner details

* fix(index): refactor badge and button color handling with typed constants for improved maintainability

* fix(dependencies): add vue-tsc for improved TypeScript support in Vue projects

* refactor(plugin): remove unnecessary comments and clean up code structure

* refactor(plugin): remove todo comment for custom header logo

* fix(Modal): add partnerUrl handling for logo link and improve code structure

* fix(serverState): update partnerLogoPath to use a placeholder image for development

* feat(plugin): add rsync activation script for testing activation modal & installs

* refactor(activationCode): remove debug log for modal visibility check

* feat(plugin): enhance activation process by adding logo and case model handling

* refactor(activationCode): rename partnerLogoPath to partnerLogo and update handling for logo file type

* refactor(activationCode): remove debug log for partnerLogo watcher

* chore(nuxt.config): add ignore rule for webGui images directory

* refactor(plugin): rename params array to DISPLAY_PARAMS and update handling for configuration updates

* fix(web): partner logo handling for themes

* fix(plg): activation oem, custom case icon cfg handling

* fix(plugin): replace sed with awk for config file updates in DISPLAY_PARAMS handling

* fix(web): add target and rel attributes to partner logo link for security

* feat(plugin): add theme parameter to DISPLAY_PARAMS and activation code interface

* fix(plugin): replace sed with parameter expansion and awk for config file updates

* fix(plugin): reboot logic for plg install to prevent settings overwrite

* feat(plugin): streamline activation and partner setup by abstracting scripts for better maintainability

* fix(plugin): update script source paths for activation and partner setup

* fix(plugin): update script source paths for activation and partner setup to use relative paths

* fix(plugin): update script source paths for activation and partner setup to use absolute paths

* fix(plugin): add debug mode support to activation scripts and update setup flag handling

* fix(plugin): enhance activation scripts with debug mode and conditional execution for safety

* refactor(plugin): reposition execution of activation_code setup / remove scripts

* fix(plugin): add checks for activation_code_setup script existence and improve deletion logging

* fix(plugin): add option to remove setup flag after script execution

* fix(plugin): streamline activation script execution by removing unnecessary checks

* fix(plugin): remove duplicate activation_code_setup script sourcing

* feat(modal): add overlay color prop and enhance class binding for modal background

* feat(activation): update modal overlay color and opacity

* feat(plg): add WebComponentsExtractor class for managing JS file retrieval

* feat(plugin): myservers1.phpintegrate WebComponentsExtractor for dynamic script tag generation

* feat(activation): enhance getData method to support JSON output and add getDataForHtmlAttr for HTML-safe JSON

* refactor(activation): simplify getData method by removing parameters

* feat(plugin): add welcome-modal.php for displaying server state and web components

* refactor(web): activation code store rename modal visibility variables for clarity

* feat(web): add WelcomeModal component for user onboarding and server setup

* feat(plugin): activation_code setup and remove script support welcome modal

* refactor(plugin): streamline activation code setup script by removing some comments

* fix(plugin): update activation code setup script to prepend items to auth request allow list array

* feat: enhance ActivationCodeExtractor with partner logo handling and metadata extraction

- Added constants for document root and web GUI images directory.
- Introduced properties for partner name, URL, logo path, and logo file type.
- Implemented methods to retrieve partner logo path, render logo string, partner name, and URL.
- Enhanced data extraction logic to include partner-related information from JSON data.
- Added debug method for outputting internal state for troubleshooting.

* refactor: update activation-data.php to include debug output for ActivationCodeExtractor

- Removed JSON response and replaced it with a debug output wrapped in <pre> tags.
- This change allows for easier troubleshooting by displaying internal state information directly on the page.

* feat: inject partner logo into DefaultPageLayout and update JS file paths

- Modified the activation code setup script to change the path of JavaScript files to be relative to the web GUI's webroot.
- Added functionality to inject a partner logo into the DefaultPageLayout by replacing the existing logo string.
- Implemented a conditional backup mechanism for the DefaultPageLayout file before making changes.
- Included debug output to confirm successful injection or report errors during the process.

* feat: add partner logo display functionality and style adjustments

- Introduced a new file for displaying the partner logo, which retrieves the logo and URL based on the activation code.
- Added CSS styles to adjust the display properties of the partner logo in the header, ensuring proper sizing and alignment.
- Enhanced the integration of the partner logo into the existing plugin structure.

* refactor: simplify partner logo injection debug output in activation code setup script

- Removed syntax check for the DefaultPageLayout file after injecting the partner logo.
- Streamlined debug output to confirm successful injection without additional checks, enhancing clarity and reducing complexity.

* style: adjust partner logo height in myservers1.php

* style: update partner logo width in PartnerLogoImg.vue

- Changed the CSS class for the partner logo image from a max height constraint to a fixed width of 72 units, ensuring better responsiveness and alignment in the layout.

* fix: update modal title attribute for conditional close behavior

- Modified the title attribute of the modal overlay to conditionally display the close instruction based on the showCloseX property. This change improves accessibility by ensuring the title is only set when the close button is visible.

* refactor: update partner logo handling in activation code scripts and extractor

- Changed the partner logo file name to 'partner-logo.svg' in ActivationCodeExtractor.
- Simplified logo path handling by removing unnecessary file type checks and directly using the SVG file.
- Updated CSS in myservers1.php to target only the SVG logo.
- Modified activation code setup and removal scripts to consistently reference the SVG logo, improving clarity and maintainability.

* feat: enhance activation code setup script to include server name handling

- Added logic to retrieve and set the server name from the activation JSON if the current system model or comment is not set, or if the current name is "Tower".
- Improved debug output to include the partner server name, enhancing visibility during the activation process.
- Sanitized the partner server name to remove quotes and backslashes for better handling.

* feat: validate activation JSON before proceeding with setup

- Added validation for the activation JSON file to ensure it is correctly formatted before executing the setup process.
- Enhanced debug output to indicate whether the JSON is valid or not, improving troubleshooting capabilities.
- The setup will now only proceed if the activation JSON is confirmed to be valid, preventing potential errors during execution.

* fix(plg): improve warning message for invalid activation JSON in setup script

* feat: enhance activation code handling with partner logo integration

- Added a new optional property 'partnerLogo' to the ActivationCodeExtractor class and updated related interfaces to support boolean values for logo display.
- Removed the PNG logo reference from the activation code setup script, streamlining the logo handling process.
- Updated the server state to utilize the new 'partnerLogo' property, ensuring consistent logo display logic across the application.
- Adjusted computed properties in the activation code store to reflect the changes in logo handling, improving clarity and maintainability.

* fix: correct activation JSON validation in setup script

- Updated the activation code setup script to use 'jq empty' for validating the activation JSON file, ensuring proper error handling and validation.
- This change improves the robustness of the setup process by accurately checking the JSON format before proceeding.

* fix: correct comment formatting in dynamix.unraid.net.plg

- Fixed a formatting issue in the comment regarding the use of myservers.cfg values to prevent conflicts during installation. This change improves code readability and clarity.

* chore: update rsync activation script comments for clarity

- Revised comments in the rsync-activation-dir.sh script to better describe its purpose and usage.
- Enhanced documentation with an example usage to improve user understanding of the script's functionality.

* refactor: clean up activation data and extractor files

- Removed outdated copyright comments from activation-data.php and activation-code-extractor.php for improved readability.
- Simplified the debug output in activation-data.php by using short PHP tags.
- Updated the comment in the ActivationCodeExtractor class to be more concise, enhancing clarity for future developers.

* fix: remove unnecessary CSS properties in myservers1.php

- Eliminated redundant display and margin properties from the CSS in myservers1.php to streamline the styling and improve code clarity.

* refactor: streamline WebComponentsExtractor class and update method names

- Removed outdated copyright comments from web-components-extractor.php for improved readability.
- Renamed method getJsFileUrl() to getJSFileRelativePath() to enhance clarity and consistency in naming conventions.
- Adjusted indentation for better code formatting and readability.

* refactor: simplify partner logo handling in activation code script

- Removed unnecessary variable initialization and included only essential requires for improved clarity.
- Streamlined the code in partner-logo.php to focus on the core functionality of displaying the partner logo.
- Enhanced maintainability by reducing complexity in the script.

* refactor: clean up activation and server state files

- Removed outdated copyright comments and unnecessary variable initializations from activation-data.php and server-state.php for improved readability.
- Streamlined the code by eliminating redundant require statements, focusing on essential functionality.
- Enhanced maintainability and clarity by simplifying the structure of both files.

* feat: add password creation prompts to translations

- Introduced new translation strings for password creation in both PHP and JSON files.
- Enhanced user guidance by providing detailed messages about the importance of the password for system access and management.
- Improved overall user experience by ensuring clarity in the password setup process.

* refactor: update activation code setup script comments

* refactor: enhance activation code removal script functionality

- Updated the activation code removal script to delete additional related files, including PHP and setup scripts, for a more thorough cleanup.
- Improved comments for clarity on script usage and options, particularly regarding the self-delete functionality.
- Streamlined the deletion process by using an array to manage files to be removed, enhancing maintainability and readability.

* fix: update WelcomeModal to disable close button

* refactor: reorganize activation code setup and improve script clarity

- Moved the activation and partner setup section to follow the web component timestamp check to ensure correct targeting during setup.
- Updated comments for better clarity regarding the activation code setup process.
- Retained the warning message about not closing the window yet for user guidance.

* refactor: enhance activation code setup script with improved comments and logic

- Updated comments to clarify the purpose of server name, model, and description checks.
- Modified conditional logic to include additional checks for server identification.
- Added a TODO note regarding the necessity of updating the ident.cfg file, ensuring future review for potential optimizations.

* refactor: update activation code scripts to use .done flag

* refactor: introduce constant for activation modal storage key

- Added a new constant for the activation code modal hidden storage key to improve code maintainability and clarity.
- Updated the activation code store to utilize the new constant, replacing hardcoded string references.

* feat: enhance user onboarding and modal components

- Added new translation strings for activating Unraid licenses and creating Unraid.net accounts to improve user onboarding experience.
- Updated the WelcomeModal and Activation Modal components to reflect new messaging and improved styling options, including the ability to disable shadows.
- Implemented a workaround in the WelcomeModal to address font-size inconsistencies between login and authenticated pages.
- Refactored the index page to correctly pass server data to the WelcomeModal component.

* chore: comment out WelcomeModalCe component for testing

* feat: add disableOverlayClose prop to Modal and WelcomeModal components

- Introduced a new prop `disableOverlayClose` to the Modal component, allowing users to prevent closing the modal by clicking on the overlay.
- Updated the WelcomeModal component to utilize the new `disableOverlayClose` prop, enhancing modal behavior customization.

* refactor: update activation code removal script to delete by default

- Changed the activation code removal script to use a dry-run flag instead of a self-delete flag, enhancing safety during execution.
- Updated the plugin file to reflect the removal of the --delete option, ensuring consistency with the new script behavior.

---------

Co-authored-by: Eli Bosley <ekbosley@gmail.com>
Co-authored-by: Pujit Mehrotra <pujit@lime-technology.com>
2025-01-09 16:06:10 -05:00

1515 lines
50 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @todo Check OS and Connect Plugin versions against latest via API every session
*/
import dayjs from "dayjs";
import { defineStore, createPinia, setActivePinia } from "pinia";
import prerelease from "semver/functions/prerelease";
import {
ArrowPathIcon,
ArrowRightOnRectangleIcon,
CogIcon,
GlobeAltIcon,
InformationCircleIcon,
KeyIcon,
QuestionMarkCircleIcon,
} from "@heroicons/vue/24/solid";
import { useLazyQuery } from "@vue/apollo-composable";
import { SERVER_CLOUD_FRAGMENT, SERVER_STATE_QUERY } from "./server.fragment";
import { useFragment } from "~/composables/gql/fragment-masking";
import { WebguiState, WebguiUpdateIgnore } from "~/composables/services/webgui";
import {
WEBGUI_SETTINGS_MANAGMENT_ACCESS,
WEBGUI_TOOLS_REGISTRATION,
WEBGUI_TOOLS_UPDATE,
} from "~/helpers/urls";
import { useAccountStore } from "~/store/account";
import { useActivationCodeStore } from '~/store/activationCode';
import { useErrorsStore, type Error } from "~/store/errors";
import { usePurchaseStore } from "~/store/purchase";
import { useThemeStore, type Theme } from "~/store/theme";
import { useUnraidApiStore } from "~/store/unraidApi";
import type { ApolloQueryResult } from "@apollo/client/core/index.js";
import type {
Config,
PartialCloudFragment,
serverStateQuery,
} from "~/composables/gql/graphql";
import type {
Server,
ServerAccountCallbackSendPayload,
ServerKeyTypeForPurchase,
ServerPurchaseCallbackSendPayload,
ServerState,
ServerStateData,
ServerStateDataAction,
ServerconnectPluginInstalled,
ServerDateTimeFormat,
ServerStateDataKeyActions,
ServerStateArray,
ServerOsVersionBranch,
ServerUpdateOsResponse,
} from "~/types/server";
/**
* @see https://stackoverflow.com/questions/73476371/using-pinia-with-vue-js-web-components
* @see https://github.com/vuejs/pinia/discussions/1085
*/
setActivePinia(createPinia());
export const useServerStore = defineStore("server", () => {
const accountStore = useAccountStore();
const errorsStore = useErrorsStore();
const purchaseStore = usePurchaseStore();
const themeStore = useThemeStore();
const unraidApiStore = useUnraidApiStore();
/**
* State
*/
const apiKey = ref<string>("");
const apiVersion = ref<string>("");
const array = ref<ServerStateArray | undefined>();
// helps to display warning next to array status
const arrayWarning = computed(
() => !!(stateDataError.value || serverConfigError.value)
);
const computedArray = computed(() => {
if (arrayWarning.value) {
if (array.value?.state === "Stopped") {
return "Stopped. The Array will not start until the above issue is resolved.";
}
return "Started. If stopped, the Array will not restart until the above issue is resolved.";
}
return array.value?.state;
});
const avatar = ref<string>(""); // @todo potentially move to a user store
const caseModel = ref<string>("");
const cloud = ref<PartialCloudFragment | undefined>();
const config = ref<Config | undefined>();
const connectPluginInstalled = ref<ServerconnectPluginInstalled>("");
const connectPluginVersion = ref<string>("");
const csrf = ref<string>(""); // required to make requests to Unraid webgui
const dateTimeFormat = ref<ServerDateTimeFormat | undefined>();
const description = ref<string>("");
const deviceCount = ref<number>(0);
const email = ref<string>("");
const expireTime = ref<number>(0);
const flashBackupActivated = ref<boolean>(false);
const flashProduct = ref<string>("");
const flashVendor = ref<string>("");
const guid = ref<string>("");
const guidBlacklisted = ref<boolean>();
const guidRegistered = ref<boolean>();
const guidReplaceable = ref<boolean | undefined>();
const inIframe = ref<boolean>(window.self !== window.top);
const keyfile = ref<string>("");
const lanIp = ref<string>("");
const license = ref<string>("");
const locale = ref<string>("");
const name = ref<string>("");
const osVersion = ref<string>("");
const osVersionBranch = ref<ServerOsVersionBranch>("stable");
const rebootType = ref<
"thirdPartyDriversDownloading" | "downgrade" | "update" | ""
>("");
const rebootVersion = ref<string | undefined>();
const registered = ref<boolean>();
const regDevs = ref<number>(0); // use computedRegDevs to ensure it includes Basic, Plus, and Pro
const computedRegDevs = computed(() => {
if (regDevs.value > 0) {
return regDevs.value;
}
switch (regTy.value) {
case "Starter":
case "Basic":
return 6;
case "Plus":
return 12;
case "Unleashed":
case "Lifetime":
case "Pro":
case "Trial":
return -1; // unlimited
default:
return 0;
}
});
const regGen = ref<number>(0);
const regGuid = ref<string>("");
const regTm = ref<number>(0);
const regTo = ref<string>("");
const regTy = ref<string>("");
const regExp = ref<number>(0);
const parsedRegExp = computed(() =>
regExp.value ? dayjs(regExp.value).format("YYYY-MM-DD") : null
);
const regUpdatesExpired = computed(() => {
if (!regExp.value) {
return false;
}
const today = dayjs();
const parsedUpdateExpirationDate = dayjs(regExp.value);
return today.isAfter(parsedUpdateExpirationDate, "day");
});
const site = ref<string>("");
const state = ref<ServerState>();
const theme = ref<Theme>();
watch(theme, (newVal) => {
if (newVal) {
themeStore.setTheme(newVal);
}
});
const updateOsResponse = ref<ServerUpdateOsResponse>();
const updateOsIgnoredReleases = ref<string[]>([]);
const updateOsNotificationsEnabled = ref<boolean>(false);
const uptime = ref<number>(0);
const username = ref<string>(""); // @todo potentially move to a user store
const wanFQDN = ref<string>("");
const combinedKnownOrigins = ref<string[]>([]);
const apiServerStateRefresh =
ref<
(
variables?: Record<string, never> | undefined
) => Promise<ApolloQueryResult<serverStateQuery>> | undefined
>();
/**
* Getters
*/
const isRemoteAccess = computed(
() =>
wanFQDN.value ||
(site.value &&
site.value.includes("www.") &&
site.value.includes("unraid.net"))
);
/**
* @todo configure
*/
const pluginOutdated = computed((): boolean => {
return false;
});
const isOsVersionStable = computed(() => {
const hasPrerelease = prerelease(osVersion.value);
return !hasPrerelease;
}); // used to determine if we should look for stable or next releases
const server = computed((): Server => {
return {
apiKey: apiKey.value,
apiVersion: apiVersion.value,
array: array.value,
avatar: avatar.value,
connectPluginVersion: connectPluginVersion.value,
connectPluginInstalled: connectPluginInstalled.value,
description: description.value,
deviceCount: deviceCount.value,
email: email.value,
expireTime: expireTime.value,
flashProduct: flashProduct.value,
flashVendor: flashVendor.value,
guid: guid.value,
inIframe: inIframe.value,
keyfile: keyfile.value,
lanIp: lanIp.value,
license: license.value,
locale: locale.value,
name: name.value,
osVersion: osVersion.value,
osVersionBranch: osVersionBranch.value,
rebootType: rebootType.value,
rebootVersion: rebootVersion.value,
registered: registered.value,
regDevs: computedRegDevs.value,
regGen: regGen.value,
regGuid: regGuid.value,
regExp: regExp.value,
regUpdatesExpired: regUpdatesExpired.value,
site: site.value,
state: state.value,
theme: theme.value,
uptime: uptime.value,
username: username.value,
wanFQDN: wanFQDN.value,
};
});
const serverPurchasePayload = computed(
(): ServerPurchaseCallbackSendPayload => {
/** @todo refactor out. Just parse state on craft site to determine */
let keyTypeForPurchase: ServerKeyTypeForPurchase = "Trial";
switch (state.value) {
case "BASIC":
keyTypeForPurchase = "Basic";
break;
case "PLUS":
keyTypeForPurchase = "Plus";
break;
case "PRO":
keyTypeForPurchase = "Pro";
break;
case "STARTER":
keyTypeForPurchase = "Starter";
break;
case "UNLEASHED":
keyTypeForPurchase = "Unleashed";
break;
}
const server: ServerPurchaseCallbackSendPayload = {
apiVersion: apiVersion.value,
connectPluginVersion: connectPluginVersion.value,
deviceCount: deviceCount.value,
email: email.value,
guid: guid.value,
inIframe: inIframe.value,
keyTypeForPurchase,
locale: locale.value,
osVersion: osVersion.value,
osVersionBranch: osVersionBranch.value,
registered: registered.value ?? false,
regExp: regExp.value,
regTy: regTy.value,
regUpdatesExpired: regUpdatesExpired.value,
state: state.value,
site: site.value,
};
const { code, partnerName } = storeToRefs(useActivationCodeStore());
if (code.value) {
server['activationCodeData'] = {
code: code.value,
};
if (partnerName.value) {
server['activationCodeData']['partnerName'] = partnerName.value;
}
}
return server;
}
);
const serverAccountPayload = computed(
(): ServerAccountCallbackSendPayload => {
return {
apiVersion: apiVersion.value,
caseModel: caseModel.value,
connectPluginVersion: connectPluginVersion.value,
deviceCount: deviceCount.value,
description: description.value,
expireTime: expireTime.value,
flashBackupActivated: flashBackupActivated.value,
flashProduct: flashProduct.value,
flashVendor: flashVendor.value,
guid: guid.value,
inIframe: inIframe.value,
keyfile: keyfile.value,
lanIp: lanIp.value,
name: name.value,
osVersion: osVersion.value,
osVersionBranch: osVersionBranch.value,
rebootType: rebootType.value,
rebootVersion: rebootVersion.value,
registered: registered.value ?? false,
regGuid: regGuid.value,
regExp: regExp.value,
regTy: regTy.value,
regUpdatesExpired: regUpdatesExpired.value,
site: site.value,
state: state.value,
wanFQDN: wanFQDN.value,
};
}
);
const serverDebugPayload = computed((): Server => {
const payload = {
apiKey:
apiKey.value && typeof apiKey.value === "string"
? `${apiKey.value.substring(0, 6)}__[REDACTED]`
: "", // so we don't send full api key in email
apiVersion: apiVersion.value,
avatar: avatar.value,
connectPluginInstalled: connectPluginInstalled.value,
connectPluginVersion: connectPluginVersion.value,
description: description.value,
deviceCount: deviceCount.value,
email: email.value,
expireTime: expireTime.value,
flashProduct: flashProduct.value,
flashVendor: flashVendor.value,
guid: guid.value,
inIframe: inIframe.value,
lanIp: lanIp.value,
locale: locale.value,
name: name.value,
osVersion: osVersion.value,
osVersionBranch: osVersionBranch.value,
rebootType: rebootType.value,
rebootVersion: rebootVersion.value,
registered: registered.value,
regGen: regGen.value,
regGuid: regGuid.value,
regTy: regTy.value,
site: site.value,
state: state.value,
uptime: uptime.value,
username: username.value,
wanFQDN: wanFQDN.value,
};
// remove any empty values from object
return Object.fromEntries(
Object.entries(payload).filter(
([_, v]) => v !== null && v !== undefined && v !== ""
)
);
});
const serverActionsDisable = computed(() => {
const disable = !!(
connectPluginInstalled.value &&
(unraidApiStore.unraidApiStatus !== "online" ||
unraidApiStore.prioritizeCorsError)
);
return {
disable,
title: disable
? "Requires the local unraid-api to be running successfully"
: "",
};
});
const purchaseAction = computed((): ServerStateDataAction => {
return {
click: () => {
purchaseStore.purchase();
},
disabled: serverActionsDisable.value.disable,
external: true,
icon: KeyIcon,
name: "purchase",
text: "Purchase Key",
title: serverActionsDisable.value.title,
};
});
const upgradeAction = computed((): ServerStateDataAction => {
return {
click: () => {
purchaseStore.upgrade();
},
disabled: serverActionsDisable.value.disable,
external: true,
icon: KeyIcon,
name: "upgrade",
text: "Upgrade Key",
title: serverActionsDisable.value.title,
};
});
const recoverAction = computed((): ServerStateDataAction => {
return {
click: () => {
accountStore.recover();
},
disabled: serverActionsDisable.value.disable,
external: true,
icon: KeyIcon,
name: "recover",
text: "Recover Key",
title: serverActionsDisable.value.title,
};
});
const redeemAction = computed((): ServerStateDataAction => {
const { code } = storeToRefs(useActivationCodeStore());
return {
click: () => {
code.value ? purchaseStore.activate() : purchaseStore.redeem();
},
disabled: serverActionsDisable.value.disable,
external: true,
icon: KeyIcon,
name: code.value ? "activate" : "redeem",
text: code.value ? "Activate Now" : "Redeem Activation Code",
title: serverActionsDisable.value.title,
};
});
const renewAction = computed((): ServerStateDataAction => {
return {
click: () => {
purchaseStore.renew();
},
disabled: serverActionsDisable.value.disable,
external: true,
icon: KeyIcon,
name: "renew",
text: "Extend License to Enable OS Updates",
title: serverActionsDisable.value.title,
};
});
const replaceAction = computed((): ServerStateDataAction => {
return {
click: () => {
accountStore.replace();
},
external: true,
icon: KeyIcon,
name: "replace",
text: "Replace Key",
};
});
const signInAction = computed((): ServerStateDataAction => {
return {
click: () => {
accountStore.signIn();
},
disabled: serverActionsDisable.value.disable,
external: true,
icon: GlobeAltIcon,
name: "signIn",
text: "Sign In with Unraid.net Account",
title: serverActionsDisable.value.title,
};
});
/**
* The Sign Out action is a computed property because it depends on the state of the keyfile & unraid-api being online
*/
const signOutAction = computed((): ServerStateDataAction => {
const disabled: boolean =
!keyfile.value || serverActionsDisable.value.disable;
let title = "";
if (!keyfile.value) {
title = "Sign Out requires a keyfile";
}
if (serverActionsDisable.value.disable) {
title = serverActionsDisable.value.title;
}
return {
click: () => {
accountStore.signOut();
},
disabled,
external: true,
icon: ArrowRightOnRectangleIcon,
name: "signOut",
text: "Sign Out of Unraid.net",
title,
};
});
const trialExtendAction = computed((): ServerStateDataAction => {
return {
click: () => {
accountStore.trialExtend();
},
disabled: serverActionsDisable.value.disable,
external: true,
icon: KeyIcon,
name: "trialExtend",
text: "Extend Trial",
title: serverActionsDisable.value.title,
};
});
const trialStartAction = computed((): ServerStateDataAction => {
return {
click: () => {
accountStore.trialStart();
},
disabled: serverActionsDisable.value.disable,
external: true,
icon: KeyIcon,
name: "trialStart",
text: "Start Free 30 Day Trial",
title: serverActionsDisable.value.title,
};
});
let messageEGUID = "";
const stateData = computed((): ServerStateData => {
switch (state.value) {
case "ENOKEYFILE":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[
trialStartAction.value,
purchaseAction.value,
redeemAction.value,
recoverAction.value,
],
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
humanReadable: "No Keyfile",
heading: "Let's Unleash Your Hardware",
message:
'<p>Choose an option below, then use our <a href="https://unraid.net/getting-started" target="_blank" rel="noreffer noopener">Getting Started Guide</a> to configure your array in less than 15 minutes.</p>',
};
case "TRIAL":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[purchaseAction.value, redeemAction.value],
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
humanReadable: "Trial",
heading: "Thank you for choosing Unraid OS!",
message:
"<p>Your <em>Trial</em> key includes all the functionality and device support of an <em>Unleashed</em> key.</p><p>After your <em>Trial</em> has reached expiration, your server <strong>still functions normally</strong> until the next time you Stop the array or reboot your server.</p><p>At that point you may either purchase a license key or request a <em>Trial</em> extension.</p>",
};
case "EEXPIRED":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[purchaseAction.value, redeemAction.value],
...(trialExtensionEligible.value ? [trialExtendAction.value] : []),
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
error: true,
humanReadable: "Trial Expired",
heading: "Your Trial has expired",
message: trialExtensionEligible.value
? "<p>To continue using Unraid OS you may purchase a license key. Alternately, you may request a Trial extension.</p>"
: "<p>You have used all your Trial extensions. To continue using Unraid OS you may purchase a license key.</p>",
};
case "BASIC":
case "STARTER":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...(regUpdatesExpired.value ? [renewAction.value] : []),
...[upgradeAction.value],
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
humanReadable: state.value === "BASIC" ? "Basic" : "Starter",
heading: "Thank you for choosing Unraid OS!",
message:
!registered.value && connectPluginInstalled.value
? "<p>Register for Connect by signing in to your Unraid.net account</p>"
: guidRegistered.value
? "<p>To support more storage devices as your server grows, click Upgrade Key.</p>"
: "",
};
case "PLUS":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[upgradeAction.value],
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
humanReadable: "Plus",
heading: "Thank you for choosing Unraid OS!",
message:
!registered.value && connectPluginInstalled.value
? "<p>Register for Connect by signing in to your Unraid.net account</p>"
: guidRegistered.value
? "<p>To support more storage devices as your server grows, click Upgrade Key.</p>"
: "",
};
case "PRO":
case "LIFETIME":
case "UNLEASHED":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...(regUpdatesExpired.value ? [renewAction.value] : []),
...(state.value === "UNLEASHED" ? [upgradeAction.value] : []),
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
humanReadable:
state.value === "PRO"
? "Pro"
: state.value === "LIFETIME"
? "Lifetime"
: "Unleashed",
heading: "Thank you for choosing Unraid OS!",
message:
!registered.value && connectPluginInstalled.value
? "<p>Register for Connect by signing in to your Unraid.net account</p>"
: "",
};
case "EGUID":
if (guidReplaceable.value) {
messageEGUID =
"<p>Your Unraid registration key is ineligible for replacement as it has been replaced within the last 12 months.</p>";
} else if (guidReplaceable.value === false && guidBlacklisted.value) {
messageEGUID =
"<p>The license key file does not correspond to the USB Flash boot device. Please copy the correct key file to the /config directory on your USB Flash boot device or choose Purchase Key.</p><p>Your Unraid registration key is ineligible for replacement as it is blacklisted.</p>";
} else if (guidReplaceable.value === false && !guidBlacklisted.value) {
messageEGUID =
"<p>The license key file does not correspond to the USB Flash boot device. Please copy the correct key file to the /config directory on your USB Flash boot device or choose Purchase Key.</p><p>Your Unraid registration key is ineligible for replacement as it has been replaced within the last 12 months.</p>";
} else {
// basically guidReplaceable.value === null
messageEGUID =
"<p>The license key file does not correspond to the USB Flash boot device. Please copy the correct key file to the /config directory on your USB Flash boot device.</p><p>You may also attempt to Purchase or Replace your key.</p>";
}
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[replaceAction.value, purchaseAction.value, redeemAction.value],
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
error: true,
humanReadable: "Flash GUID Error",
heading: "Registration key / USB Flash GUID mismatch",
message: messageEGUID,
};
case "EGUID1":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[purchaseAction.value, redeemAction.value],
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
error: true,
humanReadable: "Multiple License Keys Present",
heading: "Multiple License Keys Present",
message:
"<p>There are multiple license key files present on your USB flash device and none of them correspond to the USB Flash boot device. Please remove all key files, except the one you want to replace, from the /config directory on your USB Flash boot device.</p><p>Alternately you may purchase a license key for this USB flash device.</p><p>If you want to replace one of your license keys with a new key bound to this USB Flash device, please first remove all other key files first.</p>",
// signInToFix: true, // @todo is this needed?
};
case "ENOKEYFILE2":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[recoverAction.value, purchaseAction.value, redeemAction.value],
...(registered.value ? [signOutAction.value] : []),
],
error: true,
humanReadable: "Missing key file",
heading: "Missing key file",
message: connectPluginInstalled.value
? "<p>Your license key file is corrupted or missing. The key file should be located in the /config directory on your USB Flash boot device.</p><p>You may attempt to recover your key with your Unraid.net account.</p><p>If this was an expired Trial installation, you may purchase a license key.</p>"
: "<p>Your license key file is corrupted or missing. The key file should be located in the /config directory on your USB Flash boot device.</p><p>If you do not have a backup copy of your license key file you may attempt to recover your key.</p><p>If this was an expired Trial installation, you may purchase a license key.</p>",
};
case "ETRIAL":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[purchaseAction.value, redeemAction.value],
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
error: true,
humanReadable: "Invalid installation",
heading: "Invalid installation",
message:
"<p>It is not possible to use a Trial key with an existing Unraid OS installation.</p><p>You may purchase a license key corresponding to this USB Flash device to continue using this installation.</p>",
};
case "ENOKEYFILE1":
return {
actions: [
...(!registered.value && connectPluginInstalled.value
? [signInAction.value]
: []),
...[purchaseAction.value, redeemAction.value],
...(registered.value && connectPluginInstalled.value
? [signOutAction.value]
: []),
],
error: true,
humanReadable: "No Keyfile",
heading: "No USB flash configuration data",
message: "<p>There is a problem with your USB Flash device</p>",
};
case "ENOFLASH":
case "ENOFLASH1":
case "ENOFLASH2":
case "ENOFLASH3":
case "ENOFLASH4":
case "ENOFLASH5":
case "ENOFLASH6":
case "ENOFLASH7":
return {
error: true,
humanReadable: "No Flash",
heading: "Cannot access your USB Flash boot device",
message:
"<p>There is a physical problem accessing your USB Flash boot device</p>",
};
case "EBLACKLISTED":
return {
error: true,
humanReadable: "BLACKLISTED",
heading: "Blacklisted USB Flash GUID",
message:
"<p>This USB Flash boot device has been blacklisted. This can occur as a result of transferring your license key to a replacement USB Flash device, and you are currently booted from your old USB Flash device.</p><p>A USB Flash device may also be blacklisted if we discover the serial number is not unique this is common with USB card readers.</p>",
};
case "EBLACKLISTED1":
return {
error: true,
humanReadable: "BLACKLISTED",
heading: "USB Flash device error",
message:
"<p>This USB Flash device has an invalid GUID. Please try a different USB Flash device</p>",
};
case "EBLACKLISTED2":
return {
error: true,
humanReadable: "BLACKLISTED",
heading: "USB Flash has no serial number",
message:
"<p>This USB Flash boot device has been blacklisted. This can occur as a result of transferring your license key to a replacement USB Flash device, and you are currently booted from your old USB Flash device.</p><p>A USB Flash device may also be blacklisted if we discover the serial number is not unique this is common with USB card readers.</p>",
};
case "ENOCONN":
return {
error: true,
humanReadable: "Trial Requires Internet Connection",
heading: "Cannot validate Unraid Trial key",
message:
'<p>Your Trial key requires an internet connection.</p><p><a href="/Settings/NetworkSettings" class="underline">Please check Settings > Network</a></p>',
};
default:
return {
error: true,
humanReadable: "Stale",
heading: "Stale Server",
message:
"<p>Please refresh the page to ensure you load your latest configuration</p>",
};
}
});
const stateDataError = computed((): Error | undefined => {
if (!stateData.value?.error) {
return undefined;
}
return {
actions: [
{
click: () => {
errorsStore.openTroubleshoot({
email: email.value,
includeUnraidApiLogs: !!connectPluginInstalled.value,
});
},
icon: QuestionMarkCircleIcon,
text: "Contact Support",
},
],
debugServer: serverDebugPayload.value,
heading: stateData.value?.heading ?? "",
level: "error",
message: stateData.value?.message ?? "",
ref: `stateDataError__${state.value}`,
type: "serverState",
};
});
watch(stateDataError, (newVal, oldVal) => {
if (oldVal && oldVal.ref) {
errorsStore.removeErrorByRef(oldVal.ref);
}
if (newVal) {
errorsStore.setError(newVal);
}
});
const authActionsNames = ["signIn", "signOut"];
// Extract sign in / out from actions so we can display seperately as needed
const authAction = computed((): ServerStateDataAction | undefined => {
if (!stateData.value.actions) {
return;
}
return stateData.value.actions.find((action) =>
authActionsNames.includes(action.name)
);
});
// Remove sign in / out from actions so we can display them separately
const keyActions = computed((): ServerStateDataAction[] | undefined => {
if (!stateData.value.actions) {
return;
}
return stateData.value.actions.filter(
(action) => !authActionsNames.includes(action.name)
);
});
const trialExtensionEligible = computed(
() => !regGen.value || regGen.value < 2
);
const serverConfigError = computed((): Error | undefined => {
if (!config.value?.valid && config.value?.error) {
switch (config.value?.error) {
// case 'UNKNOWN_ERROR':
// return {
// heading: 'Unknown Error',
// level: 'error',
// message: 'An unknown internal error occurred.',
// ref: 'configError',
// type: 'server',
// };
//@ts-expect-error - causing a build-breaking type error, but seems plausible, so i kept it around - pujitm
case "INELIGIBLE":
return {
heading: "Ineligible for OS Version",
level: "error",
message:
"Your License Key does not support this OS Version. OS build date greater than key expiration. Please consider extending your registration key.",
actions: [
{
href: WEBGUI_TOOLS_REGISTRATION.toString(),
icon: CogIcon,
text: "Learn More at Tools > Registration",
},
],
ref: "configError",
type: "server",
};
case "INVALID":
return {
heading: "Too Many Devices",
level: "error",
message:
"You have exceeded the number of devices allowed for your license. Please remove a device to start the array, or upgrade your key to support more devices.",
ref: "configError",
type: "server",
};
case "NO_KEY_SERVER":
return {
heading: "Check Network Connection",
level: "error",
message:
"Unable to validate your trial key. Please check your network connection.",
ref: "configError",
type: "server",
};
case "WITHDRAWN":
return {
heading: "OS Version Withdrawn",
level: "error",
message: "This OS release should not be run. OS Update Required.",
actions: [
{
href: WEBGUI_TOOLS_UPDATE.toString(),
icon: ArrowPathIcon,
text: "Check for Update",
},
],
ref: "configError",
type: "server",
};
}
return undefined;
}
});
watch(serverConfigError, (newVal, oldVal) => {
if (oldVal && oldVal.ref) {
errorsStore.removeErrorByRef(oldVal.ref);
}
if (newVal) {
errorsStore.setError(newVal);
}
});
const tooManyDevices = computed((): boolean => {
return (
(deviceCount.value !== 0 &&
computedRegDevs.value > 0 &&
deviceCount.value > computedRegDevs.value) ||
(!config.value?.valid && config.value?.error === "INVALID")
);
});
const pluginInstallFailed = computed((): Error | undefined => {
if (
connectPluginInstalled.value &&
connectPluginInstalled.value.includes("_installFailed")
) {
return {
actions: [
{
external: true,
href: "https://forums.unraid.net/topic/112073-my-servers-releases/#comment-1154449",
icon: InformationCircleIcon,
text: "Learn More",
},
],
heading: "Unraid Connect Install Failed",
level: "error",
message: "Rebooting will likely solve this.",
ref: "pluginInstallFailed",
type: "server",
};
}
return undefined;
});
watch(pluginInstallFailed, (newVal, oldVal) => {
if (oldVal && oldVal.ref) {
errorsStore.removeErrorByRef(oldVal.ref);
}
if (newVal) {
errorsStore.setError(newVal);
}
});
/**
* Deprecation warning for [hash].unraid.net SSL certs. Deprecation started 2023-01-01
*/
const deprecatedUnraidSSL = ref<Error | undefined>(
window.location.hostname.includes("localhost") &&
window.location.port !== "4321"
? {
actions: [
{
href: WEBGUI_SETTINGS_MANAGMENT_ACCESS.toString(),
icon: CogIcon,
text: "Go to Management Access Now",
},
{
external: true,
href: "https://unraid.net/blog/ssl-certificate-update",
icon: InformationCircleIcon,
text: "Learn More",
},
],
forumLink: true,
heading: "SSL certificates for unraid.net deprecated",
level: "error",
message:
"On January 1st, 2023 SSL certificates for unraid.net were deprecated. You MUST provision a new SSL certificate to use our new myunraid.net domain. You can do this on the Settings > Management Access page.",
ref: "deprecatedUnraidSSL",
type: "server",
}
: undefined
);
watch(deprecatedUnraidSSL, (newVal, oldVal) => {
if (oldVal && oldVal.ref) {
errorsStore.removeErrorByRef(oldVal.ref);
}
if (newVal) {
errorsStore.setError(newVal);
}
});
const cloudError = computed((): Error | undefined => {
// if we're not registered or we're in the process of signing out then the cloud error should be ignored
if (
!registered.value ||
!cloud.value?.error ||
accountStore.accountActionType === "signOut" ||
accountStore.accountActionType === "oemSignOut"
) {
return;
}
// otherwise if we are we should display any cloud errors
return {
actions: [
{
click: () => {
errorsStore.openTroubleshoot({
email: email.value,
includeUnraidApiLogs: !!connectPluginInstalled.value,
});
},
icon: QuestionMarkCircleIcon,
text: "Contact Support",
},
],
debugServer: serverDebugPayload.value,
heading: "Unraid Connect Error",
level: "error",
message: cloud.value?.error ?? "",
ref: "cloudError",
type: "unraidApiState",
};
});
watch(cloudError, (newVal, oldVal) => {
if (oldVal && oldVal.ref) {
errorsStore.removeErrorByRef(oldVal.ref);
}
if (newVal) {
errorsStore.setError(newVal);
}
});
const serverErrors = computed(() => {
return [
stateDataError.value,
serverConfigError.value,
pluginInstallFailed.value,
deprecatedUnraidSSL.value,
cloudError.value,
].filter(Boolean);
});
/**
* Actions
*/
const setServer = (data: Server) => {
console.debug("[setServer]", data);
if (typeof data?.apiKey !== "undefined") {
apiKey.value = data.apiKey;
}
if (typeof data?.array !== "undefined") {
array.value = data.array;
}
if (typeof data?.apiVersion !== "undefined") {
apiVersion.value = data.apiVersion;
}
if (typeof data?.avatar !== "undefined") {
avatar.value = data.avatar;
}
if (typeof data?.caseModel !== "undefined") {
caseModel.value = data.caseModel;
}
if (typeof data?.cloud !== "undefined") {
cloud.value = data.cloud;
}
if (typeof data?.combinedKnownOrigins !== "undefined") {
combinedKnownOrigins.value = data.combinedKnownOrigins;
}
if (typeof data?.config !== "undefined") {
config.value = data.config;
}
if (typeof data?.connectPluginInstalled !== "undefined") {
connectPluginInstalled.value = data.connectPluginInstalled;
}
if (typeof data?.connectPluginVersion !== "undefined") {
connectPluginVersion.value = data.connectPluginVersion;
}
if (typeof data?.csrf !== "undefined") {
csrf.value = data.csrf;
}
if (typeof data?.dateTimeFormat !== "undefined") {
dateTimeFormat.value = data.dateTimeFormat;
}
if (typeof data?.description !== "undefined") {
description.value = data.description;
}
if (typeof data?.deviceCount !== "undefined") {
deviceCount.value = data.deviceCount;
}
if (typeof data?.email !== "undefined") {
email.value = data.email;
}
if (typeof data?.expireTime !== "undefined") {
expireTime.value = data.expireTime;
}
if (typeof data?.flashBackupActivated !== "undefined") {
flashBackupActivated.value = data.flashBackupActivated;
}
if (typeof data?.flashProduct !== "undefined") {
flashProduct.value = data.flashProduct;
}
if (typeof data?.flashVendor !== "undefined") {
flashVendor.value = data.flashVendor;
}
if (typeof data?.guid !== "undefined") {
guid.value = data.guid;
}
if (typeof data?.keyfile !== "undefined") {
keyfile.value = data.keyfile;
}
if (typeof data?.lanIp !== "undefined") {
lanIp.value = data.lanIp;
}
if (typeof data?.license !== "undefined") {
license.value = data.license;
}
if (typeof data?.locale !== "undefined") {
locale.value = data.locale;
}
if (typeof data?.name !== "undefined") {
name.value = data.name;
}
if (typeof data?.osVersion !== "undefined") {
osVersion.value = data.osVersion;
}
if (typeof data?.osVersionBranch !== "undefined") {
osVersionBranch.value = data.osVersionBranch;
}
if (typeof data?.rebootType !== "undefined") {
rebootType.value = data.rebootType;
}
if (typeof data?.rebootVersion !== "undefined") {
rebootVersion.value = data.rebootVersion;
}
if (typeof data?.registered !== "undefined") {
registered.value = data.registered;
}
if (typeof data?.regGen !== "undefined") {
regGen.value = data.regGen;
}
if (typeof data?.regGuid !== "undefined") {
regGuid.value = data.regGuid;
}
if (typeof data?.regTy !== "undefined") {
regTy.value = data.regTy;
}
if (typeof data?.regExp !== "undefined") {
regExp.value = data.regExp;
}
if (typeof data?.site !== "undefined") {
site.value = data.site;
}
if (typeof data?.state !== "undefined") {
state.value = data.state;
}
if (typeof data?.theme !== "undefined") {
theme.value = data.theme;
}
if (typeof data?.updateOsIgnoredReleases !== "undefined") {
updateOsIgnoredReleases.value = data.updateOsIgnoredReleases;
}
if (typeof data?.updateOsNotificationsEnabled !== "undefined") {
updateOsNotificationsEnabled.value = data.updateOsNotificationsEnabled;
}
if (typeof data?.updateOsResponse !== "undefined") {
updateOsResponse.value = data.updateOsResponse;
}
if (typeof data?.uptime !== "undefined") {
uptime.value = data.uptime;
}
if (typeof data?.username !== "undefined") {
username.value = data.username;
}
if (typeof data?.wanFQDN !== "undefined") {
wanFQDN.value = data.wanFQDN;
}
if (typeof data?.regTm !== "undefined") {
regTm.value = data.regTm;
}
if (typeof data?.regTo !== "undefined") {
regTo.value = data.regTo;
}
if (typeof data.activationCodeData !== "undefined") {
const activationCodeStore = useActivationCodeStore();
activationCodeStore.setData(data.activationCodeData);
}
};
const setUpdateOsResponse = (response: ServerUpdateOsResponse) => {
updateOsResponse.value = response;
};
const mutateServerStateFromApi = (data: serverStateQuery): Server => {
console.debug("mutateServerStateFromApi", data);
const mutatedData: Server = {
// if we get an owners obj back and the username is root we don't want to overwrite the values
...(data.owner && data.owner.username !== "root"
? {
// avatar: data.owner.avatar,
username: data.owner.username ?? "",
registered: true,
}
: {
// handles sign outs
// avatar: data.owner.avatar,
username: "",
registered: false,
}),
name:
data.info && data.info.os && data.info.os.hostname
? data.info.os.hostname
: undefined,
keyfile:
data.registration &&
data.registration.keyFile &&
data.registration.keyFile.contents
? data.registration.keyFile.contents
: undefined,
regGen:
data.vars && data.vars.regGen ? parseInt(data.vars.regGen) : undefined,
state: data.vars && data.vars.regState ? data.vars.regState : undefined,
config: data.config
? { id: "config", ...data.config }
: {
id: "config",
error:
data.vars && data.vars.configError
? data.vars.configError
: undefined,
valid:
data.vars && data.vars.configValid ? data.vars.configValid : true,
},
expireTime:
data.registration && data.registration.expiration
? parseInt(data.registration.expiration)
: 0,
cloud: data.cloud
? useFragment(SERVER_CLOUD_FRAGMENT, data.cloud)
: undefined,
regExp:
data.registration && data.registration.updateExpiration
? Number(data.registration.updateExpiration)
: undefined,
};
console.debug("mutatedData", mutatedData);
return mutatedData;
};
const {
load,
refetch: refetchServerState,
onResult,
onError,
} = useLazyQuery(SERVER_STATE_QUERY);
setTimeout(() => {
load();
}, 500);
onResult((result) => {
if (result.data) {
const { unraidApiStatus } = toRefs(useUnraidApiStore());
unraidApiStatus.value = "online";
apiServerStateRefresh.value = refetchServerState;
const mutatedServerStateResult = mutateServerStateFromApi(result.data);
setServer(mutatedServerStateResult);
}
});
onError((error) => {
console.error("[serverStateQuery] error", error);
const { unraidApiStatus } = toRefs(useUnraidApiStore());
unraidApiStatus.value = "offline";
});
const phpServerStateRefresh = async () => {
try {
const stateResponse: Server = await WebguiState.get().json();
setServer(stateResponse);
return stateResponse;
} catch (error) {
console.error("[phpServerStateRefresh] error", error);
}
};
let refreshCount = 0;
const refreshLimit = 20;
const refreshTimeout = 250;
const refreshServerStateStatus = ref<
"done" | "ready" | "refreshing" | "timeout"
>("ready");
const refreshServerState = async () => {
// If we've reached the refresh limit, stop refreshing
if (refreshCount >= refreshLimit) {
refreshServerStateStatus.value = "timeout";
return false;
}
refreshCount++;
refreshServerStateStatus.value = "refreshing";
// Values to compare to response values should be set before the response is set
const oldRegistered = registered.value;
const oldState = state.value;
const oldRegExp = regExp.value;
const fromApi = Boolean(apiServerStateRefresh.value);
// Fetch the server state from the API or PHP
const response = fromApi
? await refetchServerState()
: await phpServerStateRefresh();
if (!response) {
return setTimeout(() => {
refreshServerState();
}, refreshTimeout);
}
// Extract the new values from the response
const output: {
newRegistered: boolean;
newState: ServerState | ServerState | null;
newRegExp: number | null;
} = {
newRegistered: false,
newState: null,
newRegExp: null,
};
if ("data" in response) {
output.newRegistered = Boolean(
response.data.owner && response.data.owner.username !== "root"
);
output.newState = response.data.vars?.regState ?? null;
output.newRegExp = Number(
response.data.registration?.updateExpiration ?? 0
);
} else {
output.newRegistered = Boolean(response.registered);
output.newState = response.state;
output.newRegExp = Number(response.regExp ?? 0);
}
// Compare the new values to the old values
const registrationStatusChanged = output.newRegistered !== oldRegistered;
const stateChanged = output.newState !== oldState;
const regExpChanged = output.newRegExp ?? 0 > oldRegExp;
// If the registration status or state changed, stop refreshing
if (registrationStatusChanged || stateChanged || regExpChanged) {
refreshServerStateStatus.value = "done";
return true;
}
// If we haven't reached the refresh limit, try again
setTimeout(() => {
return refreshServerState();
}, refreshTimeout);
};
const filteredKeyActions = (
filterType: "by" | "out",
filters: string | ServerStateDataKeyActions[]
): ServerStateDataAction[] | undefined => {
if (!stateData.value.actions) {
return;
}
return stateData.value.actions.filter((action) => {
return filterType === "out"
? !filters.includes(action.name as ServerStateDataKeyActions)
: filters.includes(action.name as ServerStateDataKeyActions);
});
};
const setRebootVersion = (version: string) => {
rebootVersion.value = version;
};
watchEffect(() => {
if (rebootVersion.value) {
console.debug("[server.rebootVersion]", rebootVersion.value);
}
});
const updateOsIgnoreRelease = (release: string) => {
updateOsIgnoredReleases.value.push(release);
const response = WebguiUpdateIgnore({
action: "ignoreVersion",
version: release,
});
console.debug("[updateOsIgnoreRelease] response", response);
/** @todo when update check modal is displayed and there's no available updates, allow users to remove ignored releases from the list */
};
const updateOsRemoveIgnoredRelease = (release: string) => {
updateOsIgnoredReleases.value = updateOsIgnoredReleases.value.filter(
(r) => r !== release
);
const response = WebguiUpdateIgnore({
action: "removeIgnoredVersion",
version: release,
});
console.debug("[updateOsRemoveIgnoredRelease] response", response);
};
const updateOsRemoveAllIgnoredReleases = () => {
updateOsIgnoredReleases.value = [];
const response = WebguiUpdateIgnore({
action: "removeAllIgnored",
});
console.debug("[updateOsRemoveAllIgnoredReleases] response", response);
};
return {
// state
apiKey,
array,
avatar,
cloud,
config,
connectPluginInstalled,
csrf,
dateTimeFormat,
description,
deviceCount,
expireTime,
flashBackupActivated,
flashProduct,
flashVendor,
guid,
keyfile,
inIframe,
locale,
lanIp,
name,
osVersion,
osVersionBranch,
rebootType,
rebootVersion,
registered,
computedRegDevs,
regGen,
regGuid,
regTm,
regTo,
regTy,
regExp,
parsedRegExp,
regUpdatesExpired,
site,
state,
theme,
updateOsIgnoredReleases,
updateOsNotificationsEnabled,
updateOsResponse,
uptime,
username,
refreshServerStateStatus,
isOsVersionStable,
renewAction,
// getters
authAction,
deprecatedUnraidSSL,
isRemoteAccess,
keyActions,
pluginInstallFailed,
pluginOutdated,
server,
serverAccountPayload,
serverPurchasePayload,
stateData,
stateDataError,
serverErrors,
tooManyDevices,
serverConfigError,
arrayWarning,
computedArray,
// actions
setServer,
setUpdateOsResponse,
refreshServerState,
filteredKeyActions,
setRebootVersion,
updateOsIgnoreRelease,
updateOsRemoveIgnoredRelease,
updateOsRemoveAllIgnoredReleases,
};
});