Skip to content
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
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ AUTH_API_IV_KEY = 'AUTH_API_IV_KEY'
# VIASOCKET INTERFACE
INTERFACE_SCRIPT_URL = 'INTERFACE_SCRIPT_URL'
#CLIENT
CLIENT_BASE_URL = 'CLIENT_BASE_URL'
CLIENT_BASE_URL = 'CLIENT_BASE_URL'

# STEWARD FEEDBACK
STEWARD_FEEDBACK_KEY = 'STEWARD_FEEDBACK_KEY'
1 change: 1 addition & 0 deletions apps/36-blocks/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!-- App Router Outlet :: START -->
<router-outlet></router-outlet>
<!-- App Router Outlet :: END -->
<proxy-feedback-floating-button></proxy-feedback-floating-button>
<proxy-prime-ng-toast></proxy-prime-ng-toast>
3 changes: 2 additions & 1 deletion apps/36-blocks/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import { IAppState, selectClientSettings } from './core/ngrx';
import { rootActions } from './core/ngrx/actions';
import { IClientSettings, IFirebaseUserModel } from '@proxy/models/root-models';
import { readReturnUrlFromUrl, resolvePostLoginTarget } from '@proxy/utils/return-url';
import { FeedbackFloatingButtonComponent } from './panel/feedback/feedback-floating-button/feedback-floating-button.component';

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'proxy-root',
imports: [RouterModule, PrimeNgToastComponent],
imports: [RouterModule, PrimeNgToastComponent, FeedbackFloatingButtonComponent],
templateUrl: './app.component.html',
})
export class AppComponent extends BaseComponent implements OnInit, OnDestroy {
Expand Down
202 changes: 202 additions & 0 deletions apps/36-blocks/src/app/panel/feedback/feedback-api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import { Injectable, inject } from '@angular/core';
import { AuthService } from '@proxy/services/proxy/auth';
import { CookieService } from 'ngx-cookie-service';
import { Store, select } from '@ngrx/store';
import { selectClientSettings } from '../../core/ngrx';
import { selectLogInData } from '../../website/home/ngrx/selector/login.selector';
import { ILogInFeatureStateWithRootState } from '../../website/home/ngrx/store/login.state';
import { IClientSettings, IFirebaseUserModel } from '@proxy/models/root-models';
import { BillingService } from '../billing/billing.service';
import { environment } from '../../../environments/environment';
import { take } from 'rxjs/operators';

export const STEWARD_FEEDBACK_URL = 'https://steward.embarko.app/api/v1/feedback';

export interface IFeedbackUser {
id: string | number | null;
email: string | null;
name: string | null;
company: string | null;
plan: string | null;
}

export interface IFeedbackSendResult {
success: boolean;
status: number;
error?: string;
}

@Injectable({
providedIn: 'root',
})
export class FeedbackApiService {
private readonly authService = inject(AuthService);
private readonly cookieService = inject(CookieService);
private readonly store = inject<Store<ILogInFeatureStateWithRootState>>(Store);
private readonly billingService = inject(BillingService);

private currentLoginData: IFirebaseUserModel | null = null;
private currentClientSettings: IClientSettings | null = null;
private cachedPlan: string | null = null;

constructor() {
this.store.pipe(select(selectLogInData)).subscribe((loginData) => {
this.currentLoginData = loginData ?? null;
});

this.store.pipe(select(selectClientSettings)).subscribe((settings) => {
this.currentClientSettings = settings ?? null;
});
}

public refreshPlan(): void {
const rawToken = this.authService.getTokenSync() || this.cookieService.get('authToken');
if (!rawToken) return;

this.billingService
.getSubscriptionStatus()
.pipe(take(1))
.subscribe({
next: (res) => {
if (res?.data?.active_plan_code) {
this.cachedPlan = res.data.active_plan_code;
} else if (res?.data && !res.data.has_subscription) {
this.cachedPlan = 'free';
}
},
error: () => {
// Ignore errors when refreshing plan in background
},
});
}

private parseJwt(token: string | null | undefined): Record<string, any> | null {
if (!token || typeof token !== 'string') return null;
try {
const parts = token.split('.');
if (parts.length < 2) return null;
const base64Url = parts[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(
atob(base64)
.split('')
.map((c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
.join('')
);
return JSON.parse(jsonPayload);
} catch {
return null;
}
}

public getSessionUser(): IFeedbackUser {
const rawToken = this.authService.getTokenSync() || this.cookieService.get('authToken');
const jwt = this.parseJwt(rawToken);

const id =
this.currentLoginData?.uid ??
jwt?.['id'] ??
jwt?.['user_id'] ??
jwt?.['sub'] ??
(this.currentClientSettings?.client?.id != null ? String(this.currentClientSettings.client.id) : null);

const email =
this.currentLoginData?.email ?? jwt?.['email'] ?? this.currentClientSettings?.client?.email ?? null;

const name =
this.currentLoginData?.displayName ?? jwt?.['name'] ?? this.currentClientSettings?.client?.name ?? null;

const company = this.currentClientSettings?.client?.name ?? jwt?.['company_name'] ?? jwt?.['company'] ?? null;

const plan =
this.cachedPlan ??
jwt?.['plan'] ??
jwt?.['plan_code'] ??
this.currentClientSettings?.client?.settings?.['plan'] ??
null;

return {
id,
email,
name,
company,
plan,
};
}

public async sendFeedback(text: string): Promise<IFeedbackSendResult> {
const key = (environment as any).stewardFeedbackKey;
const appVersion = (environment as any).appVersion || '0.0.3';

if (!key) {
return {
success: false,
status: 0,
error: 'Feedback service is not configured.',
};
}

const user = this.getSessionUser();
const payload = {
text,
user,
page: {
url: typeof window !== 'undefined' ? window.location.href : '',
title: typeof document !== 'undefined' ? document.title : '',
},
meta: {
app_version: appVersion,
},
};

try {
const response = await fetch(STEWARD_FEEDBACK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Steward-Key': key,
},
body: JSON.stringify(payload),
});

if (response.status === 202 || (response.status >= 200 && response.status < 300)) {
return { success: true, status: response.status };
}

if (response.status === 429) {
return {
success: false,
status: 429,
error: 'Too many at once. Please try again shortly.',
};
}

let errorMessage = `Request failed (${response.status})`;
try {
const data = await response.json();
if (data?.message) {
errorMessage = data.message;
} else if (data?.error) {
errorMessage = typeof data.error === 'string' ? data.error : JSON.stringify(data.error);
}
} catch {
const textOutput = await response.text().catch(() => '');
if (textOutput) {
errorMessage = textOutput;
}
}

return {
success: false,
status: response.status,
error: errorMessage,
};
} catch (err: any) {
return {
success: false,
status: 0,
error: err?.message || 'Network error. Please check your connection and try again.',
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<div class="feedback-dialog-shell">
<div class="feedback-dialog-header">
<h2 mat-dialog-title class="feedback-dialog-title">
{{ state() === 'success' ? 'Feedback sent' : 'Send feedback' }}
</h2>
<button
mat-icon-button
type="button"
class="feedback-dialog-close"
aria-label="Close feedback dialog"
[disabled]="state() === 'loading'"
(click)="close()"
>
<mat-icon class="mat-icon-20">close</mat-icon>
</button>
</div>

@if (state() === 'success') {
<mat-dialog-content class="feedback-dialog-content feedback-success-view">
<div class="success-icon-wrap" aria-hidden="true">
<mat-icon class="success-icon">check_circle</mat-icon>
</div>
<h3 class="success-title">Thanks, we got it</h3>
<p class="success-subtitle">Your feedback helps us make Proxy better.</p>
</mat-dialog-content>
<mat-dialog-actions align="end" class="feedback-dialog-actions">
<button mat-flat-button color="primary" type="button" (click)="close()">Done</button>
</mat-dialog-actions>
} @else {
<mat-dialog-content class="feedback-dialog-content">
<form id="feedback-form" (ngSubmit)="send()">
<mat-form-field appearance="outline" class="w-full feedback-form-field">
<mat-label>Feedback</mat-label>
<textarea
matInput
[(ngModel)]="feedbackText"
name="feedbackText"
rows="5"
placeholder="What's on your mind? Tell us what you love or what could be better..."
[disabled]="state() === 'loading'"
required
autocomplete="off"
autofocus
></textarea>
</mat-form-field>
</form>

@if (errorMessage(); as error) {
<div class="feedback-error-banner" role="alert">
<mat-icon class="mat-icon-18 text-danger shrink-0">error_outline</mat-icon>
<span class="feedback-error-text text-danger">{{ error }}</span>
</div>
}
</mat-dialog-content>

<mat-dialog-actions align="end" class="feedback-dialog-actions">
<button type="button" mat-button [disabled]="state() === 'loading'" (click)="close()">Cancel</button>
<button
type="submit"
form="feedback-form"
mat-flat-button
color="primary"
[disabled]="state() === 'loading' || !canSend()"
>
@if (state() === 'loading') {
<mat-spinner diameter="18" class="feedback-spinner mr-2"></mat-spinner>
<span>Sending…</span>
} @else {
<span>Send</span>
}
</button>
</mat-dialog-actions>
}
</div>
Loading
Loading