Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(manager): server allowance usage component #2311

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions server_manager/www/views/server_view/server_allowance_usage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Copyright 2024 The Outline Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {css, html, LitElement} from 'lit';
import {customElement, property} from 'lit/decorators.js';

@customElement('server-allowance-usage')
export class ServerAllowanceUsage extends LitElement {
@property({type: String}) message: string;
@property({type: Number}) allowanceUsed: number;
@property({type: Number}) allowanceLimit: number;
@property({type: String}) allowanceUnit: 'gigabyte' | 'terabyte' = 'terabyte';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit Shall we be "smart" to determine the unit automatically? For example, just let the caller pass a raw bytes: 1,234,567,000, then we automatically display it as 1.23 GB?

@property({type: String}) languageCode: string = 'en-US';

static styles = css`
:host {
background: var(--server-allowance-usage-background);
border-radius: 0.5rem;
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 0.25rem;
justify-content: space-between;
overflow: hidden;
padding: 1rem;
width: 100%;
border: 2px solid var(--server-allowance-usage-foreground);
}

.message,
.allowance,
.allowance-percentage,
.allowance-limit {
all: initial;
font-family: 'Roboto', sans-serif;
}

.message {
color: var(--server-allowance-usage-foreground);
}

.allowance {
margin-top: 1rem;
}

.allowance-percentage {
color: var(--server-allowance-usage-highlight);
font-size: 2rem;
}

.allowance-limit {
color: var(--server-allowance-usage-highlight);
}

progress {
width: 100%;
height: 1rem;
appearance: none;
}

progress[value]::-webkit-progress-bar {
border-radius: 1rem;
background: var(--server-allowance-usage-highlight);
}

progress[value]::-webkit-progress-value {
border-radius: 1rem;
background: var(--server-allowance-usage-progress);
}
`;

get formattedPercentage() {
return Intl.NumberFormat(this.languageCode, {
style: 'percent',
maximumSignificantDigits: 2,
}).format(this.allowanceUsed / this.allowanceLimit);
}

get formattedLimit() {
return Intl.NumberFormat(this.languageCode, {
style: 'unit',
unit: this.allowanceUnit,
}).format(this.allowanceLimit);
}

render() {
return html`
<p class="message">${this.message}</p>
<p class="allowance">
<span class="allowance-percentage">${this.formattedPercentage}</span>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some unit tests to validate the formatted values.

Copy link
Contributor Author

@daniellacosse daniellacosse Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we don't really have UI unit tests set up yet - well, we have this approach, but yikes

Some options here -

  1. use the above approach, drilling down via querySelector and shadowRoot - not ideal imo. Klugy, cumbersome, fragile.
  2. set up something better - karma snapshots or storybook test runner. Ideal, but should probably be done in a separate PR.
  3. Break these out into separate functions and test them that way. Prefer to not let testability determine architecture but would be good enough and kicks the can down the road.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with something like querySelector for now. That's how I tested the contact form logic in the absence of a better testing framework. I really just want something here to prevent regression of this formatting.

<span class="allowance-limit">/${this.formattedLimit}</span>
</p>
<progress
value=${this.allowanceUsed}
max=${this.allowanceLimit}
></progress>
`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2024 The Outline Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {html} from 'lit';

import './index';
import {ServerAllowanceUsage} from './index';

export default {
title: 'Manager/Server View/Server Allowance Usage',
component: 'server-allowance-usage',
args: {
message: 'Allowance used in the last 30 days',
allowanceUsed: 0.38,
allowanceLimit: 1,
},
};

export const Example = ({
message,
allowanceUsed,
allowanceLimit,
}: ServerAllowanceUsage) => html`
<div style="height: 300px;">
<style>
:root {
--server-allowance-usage-background: var(--outline-dark-primary);
--server-allowance-usage-foreground: var(--outline-medium-gray);
--server-allowance-usage-highlight: var(--outline-white);
--server-allowance-usage-progress: var(--outline-primary);
}
</style>
<server-allowance-usage
message=${message}
allowanceUsed=${allowanceUsed}
allowanceLimit=${allowanceLimit}
/>
</div>
`;
Loading