Skip to content

Commit

Permalink
refine offline tracking (#4825)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianjelfs authored Nov 24, 2023
1 parent 7b506b1 commit 3f0c4cd
Show file tree
Hide file tree
Showing 23 changed files with 250 additions and 172 deletions.
6 changes: 3 additions & 3 deletions frontend/app/src/components/home/Home.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
? selectedMultiUserChat.subtype?.governanceCanisterId
: undefined;
$: nervousSystem = client.tryGetNervousSystem(governanceCanisterId);
$: networkStatus = client.networkStatus;
$: offlineStore = client.offlineStore;
$: {
if ($identityState.kind === "registering") {
Expand Down Expand Up @@ -1058,7 +1058,7 @@
on:close={() => (showProfileCard = undefined)} />
{/if}

<main class:anon={$anonUser} class:offline={$networkStatus === "offline"}>
<main class:anon={$anonUser} class:offline={$offlineStore}>
{#if $layoutStore.showNav}
<LeftNav
on:profile={showProfile}
Expand Down Expand Up @@ -1132,7 +1132,7 @@
<AnonFooter />
{/if}

{#if $networkStatus === "offline"}
{#if $offlineStore}
<OfflineFooter />
{/if}

Expand Down
4 changes: 2 additions & 2 deletions frontend/app/src/components/home/LinkPreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$: youtubeMatch = text.match(client.youtubeRegex());
$: twitterLinkMatch = text.match(client.twitterLinkRegex());
$: networkStatus = client.networkStatus;
$: offlineStore = client.offlineStore;
function closestAncestor(
el: HTMLElement | null | undefined,
Expand Down Expand Up @@ -55,7 +55,7 @@
intersecting &&
!$eventListScrolling &&
!rendered &&
$networkStatus === "online"
!$offlineStore
) {
// make sure we only actually *load* the preview(s) once
previewsPromise = previewsPromise ?? loadPreviews(links);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$: more = total > searchResults.length;
$: isDiamond = client.isDiamond;
$: loading = searching && searchResults.length === 0;
$: networkStatus = client.networkStatus;
$: offlineStore = client.offlineStore;
let filters = derived(
[communityFiltersStore, client.moderationFlags],
Expand Down Expand Up @@ -167,7 +167,7 @@
<FancyLoader />
</div>
{:else if searchResults.length === 0}
{#if $networkStatus === "offline"}
{#if $offlineStore}
<div class="no-match">
<CloudOffOutline size={"1.8em"} color={"var(--txt-light)"} />
<p class="sub-header">{$_("offlineError")}</p>
Expand Down
6 changes: 0 additions & 6 deletions frontend/openchat-agent/src/constants.ts

This file was deleted.

4 changes: 2 additions & 2 deletions frontend/openchat-agent/src/services/candidService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Actor, HttpAgent, type Identity } from "@dfinity/agent";
import type { IDL } from "@dfinity/candid";
import type { Principal } from "@dfinity/principal";
import { AuthError, DestinationInvalidError, SessionExpiryError } from "openchat-shared";
import { AuthError, DestinationInvalidError, SessionExpiryError, offline } from "openchat-shared";
import type { AgentConfig } from "../config";
import { ReplicaNotUpToDateError, toCanisterResponseError } from "./error";

Expand All @@ -21,7 +21,7 @@ export abstract class CandidService {
const host = config.icUrl;
const agent = new HttpAgent({ identity: this.identity, host, retryTimes: 5 });
const isMainnet = config.icUrl.includes("icp-api.io");
if (!isMainnet && navigator.onLine) {
if (!isMainnet && !offline()) {
agent.fetchRootKey();
}
return Actor.createActor<T>(factory, {
Expand Down
22 changes: 14 additions & 8 deletions frontend/openchat-agent/src/services/community/community.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,20 @@ import type {
ClaimPrizeResponse,
OptionalChatPermissions,
} from "openchat-shared";
import { textToCode, DestinationInvalidError } from "openchat-shared";
import {
textToCode,
DestinationInvalidError,
offline,
MAX_EVENTS,
MAX_MESSAGES,
MAX_MISSING,
} from "openchat-shared";
import {
apiOptionalGroupPermissions,
apiUpdatedRules,
getMessagesByMessageIndexResponse,
} from "../group/mappers";
import { DataClient } from "../data/data.client";
import { MAX_EVENTS, MAX_MESSAGES, MAX_MISSING } from "../../constants";
import { getEventsResponse } from "../group/mappers";
import {
type Database,
Expand Down Expand Up @@ -740,7 +746,7 @@ export class CommunityClient extends CandidService {
): Promise<CommunityDetailsResponse> {
const fromCache = await getCachedCommunityDetails(this.db, id.communityId);
if (fromCache !== undefined) {
if (fromCache.lastUpdated >= communityLastUpdated || !navigator.onLine) {
if (fromCache.lastUpdated >= communityLastUpdated || offline()) {
return fromCache;
} else {
return this.getCommunityDetailsUpdates(id, fromCache);
Expand Down Expand Up @@ -807,7 +813,7 @@ export class CommunityClient extends CandidService {
): Promise<GroupChatDetailsResponse> {
const fromCache = await getCachedGroupDetails(this.db, chatId.channelId);
if (fromCache !== undefined) {
if (fromCache.timestamp >= chatLastUpdated || !navigator.onLine) {
if (fromCache.timestamp >= chatLastUpdated || offline()) {
return fromCache;
} else {
return this.getChannelDetailsUpdates(chatId, fromCache);
Expand Down Expand Up @@ -1115,8 +1121,8 @@ export class CommunityClient extends CandidService {
gate === undefined
? { NoChange: null }
: gate.kind === "no_gate"
? { SetToNone: null }
: { SetToSome: apiAccessGate(gate) },
? { SetToNone: null }
: { SetToSome: apiAccessGate(gate) },
avatar:
avatar === undefined
? { NoChange: null }
Expand Down Expand Up @@ -1155,8 +1161,8 @@ export class CommunityClient extends CandidService {
gate === undefined
? { NoChange: null }
: gate.kind === "no_gate"
? { SetToNone: null }
: { SetToSome: apiAccessGate(gate) },
? { SetToNone: null }
: { SetToSome: apiAccessGate(gate) },
avatar:
avatar === undefined
? { NoChange: null }
Expand Down
12 changes: 9 additions & 3 deletions frontend/openchat-agent/src/services/group/group.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ import type {
OptionalChatPermissions,
ToggleMuteNotificationResponse,
} from "openchat-shared";
import { DestinationInvalidError, textToCode } from "openchat-shared";
import {
DestinationInvalidError,
offline,
textToCode,
MAX_EVENTS,
MAX_MESSAGES,
MAX_MISSING,
} from "openchat-shared";
import { CandidService } from "../candidService";
import {
apiRole,
Expand Down Expand Up @@ -109,7 +116,6 @@ import {
} from "../common/chatMappers";
import { DataClient } from "../data/data.client";
import { mergeGroupChatDetails } from "../../utils/chat";
import { MAX_EVENTS, MAX_MESSAGES, MAX_MISSING } from "../../constants";
import { publicSummaryResponse } from "../common/publicSummaryMapper";
import { apiOptionUpdate, identity } from "../../utils/mapping";
import { generateUint64 } from "../../utils/rng";
Expand Down Expand Up @@ -574,7 +580,7 @@ export class GroupClient extends CandidService {
async getGroupDetails(chatLastUpdated: bigint): Promise<GroupChatDetailsResponse> {
const fromCache = await getCachedGroupDetails(this.db, this.chatId.groupId);
if (fromCache !== undefined) {
if (fromCache.timestamp >= chatLastUpdated || !navigator.onLine) {
if (fromCache.timestamp >= chatLastUpdated || offline()) {
return fromCache;
} else {
return this.getGroupDetailsUpdates(fromCache);
Expand Down
Loading

0 comments on commit 3f0c4cd

Please sign in to comment.