LCOV - code coverage report
Current view: top level - pkg/lib - utils.tsx Coverage Total Hit
Test: cockpit Lines: 83.7 % 43 36
Test Date: 2026-06-17 06:28:00

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2018 Red Hat, Inc.
       3              :  * SPDX-License-Identifier: LGPL-2.1-or-later
       4              :  */
       5              : 
       6          183 : import React from "react";
       7              : 
       8          183 : import cockpit from "cockpit";
       9              : 
      10            6 : export function fmt_to_fragments(format: string, ...args: React.ReactNode[]) {
      11            6 :     const fragments = format.split(/(\$[0-9]+)/g).map(part => {
      12            6 :         if (part[0] == "$") {
      13            6 :             return args[parseInt(part.slice(1))]; // placeholder, from `args`
      14            6 :         } else
      15            6 :             return part; // literal string content
      16            6 :     });
      17              : 
      18            6 :     return React.createElement(React.Fragment, { }, ...fragments);
      19            6 : }
      20              : 
      21              : /**
      22              :  * Checks if a JsonValue is a JsonObject, and acts as a type guard.
      23              :  *
      24              :  * This function produces correct results for any possible JsonValue, and also
      25              :  * for undefined.  If you pass other types of values to this function it may
      26              :  * return an incorrect result (ie: it doesn't check deeply, so anything that
      27              :  * looks like a "simple object" will pass the check).
      28              :  */
      29            6 : export function is_json_dict(value: cockpit.JsonValue | undefined): value is cockpit.JsonObject {
      30            6 :     return value?.constructor === Object;
      31            6 : }
      32              : 
      33            6 : function try_fields(
      34            6 :     dict: cockpit.JsonObject, fields: (string | undefined)[], def: cockpit.JsonValue
      35            6 : ): cockpit.JsonValue {
      36            6 :     for (const field of fields)
      37            6 :         if (field && field in dict)
      38            4 :             return dict[field];
      39            5 :     return def;
      40            6 : }
      41              : 
      42              : /**
      43              :  * Get an entry from a manifest's ".config[config_name]" field.
      44              :  *
      45              :  * This can either be a direct value, e.g.
      46              :  *
      47              :  *   "config": { "color": "yellow" }
      48              :  *
      49              :  * Or an object indexed by any value in "matches". Commonly these are fields
      50              :  * from os-release(5) like PLATFORM_ID or ID; e.g.
      51              :  *
      52              :  *   "config": {
      53              :  *      "fedora": { "color": "blue" },
      54              :  *      "platform:el9": { "color": "red" }
      55              :  *  }
      56              :  */
      57              : 
      58              : /** Parse `sessionStorage.cockpit_anaconda` as JSON (storaged uses the object when present). */
      59              : export function read_anaconda_session_storage(): cockpit.JsonValue | null {
      60              :     try {
      61              :         const value = JSON.parse(
      62              :             window.sessionStorage.getItem("cockpit_anaconda") as string
      63              :         ) as cockpit.JsonValue;
      64              :         if (value)
      65              :             console.log("ANACONDA", value);
      66              :         return value;
      67              :     } catch {
      68              :         console.warn("Can't parse cockpit_anaconda configuration as JSON");
      69              :         return null;
      70              :     }
      71              : }
      72              : 
      73              : /** True when embedded in Anaconda (parent sets JSON in sessionStorage `cockpit_anaconda`). */
      74            1 : export function in_anaconda_mode(): boolean {
      75            1 :     try {
      76            1 :         const raw = window.sessionStorage.getItem("cockpit_anaconda");
      77            1 :         return !!JSON.parse(raw ?? "null");
      78            0 :     } catch {
      79            0 :         return false;
      80            0 :     }
      81            1 : }
      82              : 
      83            6 : export function get_manifest_config_matchlist(
      84            6 :     manifest_name: string, config_name: string, default_value: cockpit.JsonValue, matches: (string | undefined)[]
      85            6 : ): cockpit.JsonValue {
      86            6 :     const config = cockpit.manifests[manifest_name]?.config;
      87              : 
      88            6 :     if (is_json_dict(config)) {
      89            6 :         const val = config[config_name];
      90            6 :         if (is_json_dict(val))
      91            3 :             return try_fields(val, matches, default_value);
      92              :         else
      93            0 :             return val !== undefined ? val : default_value;
      94            0 :     } else {
      95            0 :         return default_value;
      96            0 :     }
      97            6 : }
        

Generated by: LCOV version 2.0-1