Skip to content

Commit

Permalink
Merge pull request #363 from CirclesUBI/feature/app-387-add-torus-log…
Browse files Browse the repository at this point in the history
…out-to-homepage-2

fixing logout function on homepage for all browsers
  • Loading branch information
codeho authored Jul 18, 2023
2 parents 418f79c + ef139fa commit 6e8833b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 81 deletions.
4 changes: 2 additions & 2 deletions shell/src/dapps/o-homepage.manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Privacy from "./o-homepage/pages/Privacy.svelte";
import NotFound from "./o-homepage/pages/NotFound.svelte";
import ListComponent from "../shared/molecules/NextNav/Components/List.svelte";
import { logout } from "./o-passport/processes/logout";
import { push } from "svelte-spa-router";

const externalChat: Link<any, DappState> = {
type: "link",
Expand Down Expand Up @@ -46,8 +47,7 @@ const index: Page<any, DappState> = {
icon: "exit",
backgroundColorClass: "transparent",
action: () => {
window.o.runProcess(logout, {});
location.reload();
push("#/passport/actions/logout");
},
},
},
Expand Down
163 changes: 84 additions & 79 deletions shell/src/shared/stores/pagedEventQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,60 @@ import {
EventType,
PaginationArgs,
Profile,
ProfileEvent, ProfileEventFilter, QueryEventsArgs,
ProfileEvent,
ProfileEventFilter,
QueryEventsArgs,
SortOrder,
StreamDocument
StreamDocument,
} from "../api/data/types";
import {writable} from "svelte/store";
import {me} from "./me";
import {Environment} from "../environment";
import { writable } from "svelte/store";
import { me } from "./me";
import { Environment } from "../environment";

export type PagedEventQueryIndexEntry = {
indexName: string,
indexKey: string
}
indexName: string;
indexKey: string;
};

export interface ObjectCache<T> {
findByPrimaryKey(type:string, primaryKey:string) : Promise<T|undefined>;
findByIndexKey(type:string, indexName:string, indexKey:string) : T[];
findByPrimaryKey(type: string, primaryKey: string): Promise<T | undefined>;
findByIndexKey(type: string, indexName: string, indexKey: string): T[];
}

export abstract class PagedEventQuery implements ObjectCache<ProfileEvent>{
readonly eventTypes:EventType[];
export abstract class PagedEventQuery implements ObjectCache<ProfileEvent> {
readonly eventTypes: EventType[];
readonly sortOrder: SortOrder;
readonly pageSize: number;
readonly query = StreamDocument;
readonly filter?:ProfileEventFilter;
readonly filter?: ProfileEventFilter;

private readonly _subscribe:any;
private readonly _set:any;
private readonly _subscribe: any;
private readonly _set: any;

private _pagination?: PaginationArgs;
private _primaryCache: {[primaryKey:string]:ProfileEvent} = {};
private _indexCache: {[indexKey:string]: string[]} = {};
private _primaryCache: { [primaryKey: string]: ProfileEvent } = {};
private _indexCache: { [indexKey: string]: string[] } = {};

private _profileChangedSubscription:any;
public get isInitialized() : boolean {
private _profileChangedSubscription: any;
public get isInitialized(): boolean {
return this._isInitialized;
}
private _isInitialized = false;
private _onDestroy?: () => void;

abstract getPrimaryKey(eventPayload:EventPayload) : string;
protected abstract getIndexedValues(event:ProfileEvent) : PagedEventQueryIndexEntry[];
protected abstract maintainExternalCaches(event:ProfileEvent) : void;
abstract getPrimaryKey(eventPayload: EventPayload): string;
protected abstract getIndexedValues(event: ProfileEvent): PagedEventQueryIndexEntry[];
protected abstract maintainExternalCaches(event: ProfileEvent): void;

abstract findSingleItemFallback(types:string[], primaryKey:string) : Promise<ProfileEvent|undefined>;
abstract findSingleItemFallback(types: string[], primaryKey: string): Promise<ProfileEvent | undefined>;

get scrollPosition() : number {
get scrollPosition(): number {
return this._scrollPosition;
}
set scrollPosition(val:number) {
set scrollPosition(val: number) {
this._scrollPosition = val;
}
private _scrollPosition:number;
private _scrollPosition: number;

constructor(eventTypes: EventType[], order: SortOrder, pageSize: number, filter?: ProfileEventFilter, onDestroy?: () => void) {
this.eventTypes = eventTypes;
Expand All @@ -63,40 +65,38 @@ export abstract class PagedEventQuery implements ObjectCache<ProfileEvent>{
this.filter = filter;
this._onDestroy = onDestroy;

const { subscribe, set, update } = writable<ProfileEvent[]>(
[], (set) => {
if (!this._isInitialized) {
this._profileChangedSubscription = me.subscribe(async $me => {
if (this._isInitialized) {
this.reset();
await this.next();
}
});

this.next().then(result => {
if (result) {
console.log(`Initial loading complete. More data available.`)
} else {
console.log(`Initial loading complete. All loaded.`)
}
this._isInitialized = true;
this.refresh();
});
}

return () => {
this._profileChangedSubscription();
this._onDestroy?.call(this);
}
const { subscribe, set, update } = writable<ProfileEvent[]>([], (set) => {
if (!this._isInitialized) {
this._profileChangedSubscription = me.subscribe(async ($me) => {
if (this._isInitialized) {
this.reset();
await this.next();
}
});

this.next().then((result) => {
if (result) {
console.log(`Initial loading complete. More data available.`);
} else {
console.log(`Initial loading complete. All loaded.`);
}
this._isInitialized = true;
this.refresh();
});
}
);

return () => {
this._profileChangedSubscription();
this._onDestroy?.call(this);
};
});
this._subscribe = subscribe;
this._set = set;

this.reset();
}

protected getPageDelimiterValue(event:ProfileEvent) : string {
protected getPageDelimiterValue(event: ProfileEvent): string {
return event.timestamp;
}

Expand All @@ -106,16 +106,22 @@ export abstract class PagedEventQuery implements ObjectCache<ProfileEvent>{
this._pagination = {
order: this.sortOrder,
limit: this.pageSize,
continueAt: this.sortOrder == SortOrder.Desc
? Environment.endOfTime.toJSON()
: Environment.beginningOfTime.toJSON()
continueAt: this.sortOrder == SortOrder.Desc ? Environment.endOfTime.toJSON() : Environment.beginningOfTime.toJSON(),
};
this.refresh();
}

async next() : Promise<boolean> {
let $me:Profile;
me.subscribe(m => $me = m)();
async next(): Promise<boolean> {
let $me: Profile;
me.subscribe((m) => ($me = m))();

// Possible Solution for avoiding thrown an Error here when the user is not logged in
// const sessionInfo = await me.getSessionInfo(true);
// if (!sessionInfo.isLoggedOn) {
// console.log("NO SESSION, BAILING");
// return false;
// }

const args = <QueryEventsArgs>{
safeAddress: $me.circlesAddress,
types: this.eventTypes,
Expand All @@ -130,10 +136,14 @@ export abstract class PagedEventQuery implements ObjectCache<ProfileEvent>{
});

if (nextPageQueryResult.errors) {
throw new Error(window.o.i18n("shared.stores.transactions.errors.couldNotLoadData", { values: { error: JSON.stringify(nextPageQueryResult.errors)}}));
throw new Error(
window.o.i18n("shared.stores.transactions.errors.couldNotLoadData", {
values: { error: JSON.stringify(nextPageQueryResult.errors) },
})
);
}

let nextPage:ProfileEvent[] = nextPageQueryResult.data.events;
let nextPage: ProfileEvent[] = nextPageQueryResult.data.events;
if (nextPage.length > 0) {
nextPage.forEach((e) => {
this.addToCache(e);
Expand All @@ -155,26 +165,23 @@ export abstract class PagedEventQuery implements ObjectCache<ProfileEvent>{
return newEventCount >= this.pageSize;
}

refresh(newItem:boolean = false) {
refresh(newItem: boolean = false) {
let eventList = Object.values(this._primaryCache).sort((a, b) => {
const _a = new Date(a.timestamp).getTime();
const _b = new Date(b.timestamp).getTime();
return _a > _b
? -1
: _a < _b
? 1
: 0;
return _a > _b ? -1 : _a < _b ? 1 : 0;
});

if (this.filter?.unreadOnly) {
eventList = eventList.filter(e => e.unread);
eventList = eventList.filter((e) => e.unread);
}

this._set({
events: eventList,
metadata: {
itemAdded: newItem
}});
itemAdded: newItem,
},
});

return eventList.length;
}
Expand All @@ -183,38 +190,36 @@ export abstract class PagedEventQuery implements ObjectCache<ProfileEvent>{
const primaryKey = `${e.type}_${this.getPrimaryKey(e.payload)}`;
this._primaryCache[primaryKey] = e;

this.getIndexedValues(e).forEach(o => {
this.getIndexedValues(e).forEach((o) => {
const indexKey = `${e.type}_${o.indexName}_${o.indexKey}`;
this._indexCache[indexKey] = this._indexCache[indexKey]
? this._indexCache[indexKey].concat([primaryKey])
: [primaryKey];
this._indexCache[indexKey] = this._indexCache[indexKey] ? this._indexCache[indexKey].concat([primaryKey]) : [primaryKey];
});
}

subscribe(run: (next:ProfileEvent[]) => void) {
subscribe(run: (next: ProfileEvent[]) => void) {
return this._subscribe(run);
}

async fetchMore() {
return await this.next();
}

async findByPrimaryKey(type:EventType, primaryKey:string) : Promise<ProfileEvent|undefined> {
async findByPrimaryKey(type: EventType, primaryKey: string): Promise<ProfileEvent | undefined> {
const result = this._primaryCache[`${type}_${primaryKey}`];
if (this.filter?.unreadOnly && !result?.unread) {
return undefined;
}
return result;
}

findByIndexKey(type:EventType, indexName:string, indexKey:string) : ProfileEvent[] {
findByIndexKey(type: EventType, indexName: string, indexKey: string): ProfileEvent[] {
const entries = this._indexCache[`${type}_${indexName}_${indexKey}`];
if (!entries) {
return [];
}
const result = entries.map(o => this._primaryCache[o]);
const result = entries.map((o) => this._primaryCache[o]);
if (this.filter?.unreadOnly) {
return result.filter(o => o?.unread);
return result.filter((o) => o?.unread);
}
return result;
}
Expand Down

0 comments on commit 6e8833b

Please sign in to comment.