|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import mapValues from "lodash/mapValues"; |
| 5 | + |
| 6 | +import { Density, Mode } from "@cloudscape-design/global-styles"; |
| 7 | + |
| 8 | +export interface AppUrlParams { |
| 9 | + mode: Mode; |
| 10 | + density: Density; |
| 11 | + direction: "ltr" | "rtl"; |
| 12 | + motionDisabled: boolean; |
| 13 | + visualRefresh: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +export const defaults: AppUrlParams = { |
| 17 | + mode: Mode.Light, |
| 18 | + density: Density.Comfortable, |
| 19 | + direction: "ltr", |
| 20 | + motionDisabled: false, |
| 21 | + visualRefresh: true, |
| 22 | +}; |
| 23 | + |
| 24 | +export function parseQuery(urlParams: URLSearchParams) { |
| 25 | + const queryParams: Record<string, any> = { ...defaults }; |
| 26 | + urlParams.forEach((value, key) => (queryParams[key] = value)); |
| 27 | + |
| 28 | + return mapValues(queryParams, (value) => { |
| 29 | + if (value === "true" || value === "false") { |
| 30 | + return value === "true"; |
| 31 | + } |
| 32 | + return value; |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +export function formatQuery(params: AppUrlParams) { |
| 37 | + const query: Record<string, string> = {}; |
| 38 | + for (const [key, value] of Object.entries(params)) { |
| 39 | + if (value === defaults[key as keyof AppUrlParams]) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + query[key as keyof AppUrlParams] = String(value); |
| 43 | + } |
| 44 | + return query; |
| 45 | +} |
| 46 | + |
| 47 | +const awsuiVisualRefreshFlag = Symbol.for("awsui-visual-refresh-flag"); |
| 48 | +interface ExtendedWindow extends Window { |
| 49 | + [awsuiVisualRefreshFlag]?: () => boolean; |
| 50 | +} |
| 51 | +declare const window: ExtendedWindow; |
| 52 | + |
| 53 | +export function setVisualRefresh() { |
| 54 | + const { visualRefresh } = parseQuery(new URLSearchParams(window.location.hash.split("?")[1])); |
| 55 | + |
| 56 | + window[awsuiVisualRefreshFlag] = () => visualRefresh ?? true; |
| 57 | +} |
0 commit comments