Skip to content

Commit

Permalink
feat: derive has any rules (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Mar 6, 2025
1 parent 1fea0a6 commit 07794bb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/frontend/src/lib/components/assets/Assets.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { emit } from '$lib/utils/events.utils';
import { i18nFormat } from '$lib/utils/i18n.utils';
const { store }: RulesContext = getContext<RulesContext>(RULES_CONTEXT_KEY);
const { store, hasAnyRules }: RulesContext = getContext<RulesContext>(RULES_CONTEXT_KEY);
let collection: string | undefined = $state();
run(() => {
Expand All @@ -45,8 +45,6 @@
const { store: assetsStore, resetData }: DataContext<AssetNoContent> =
getContext<DataContext<AssetNoContent>>(DATA_CONTEXT_KEY);
let emptyCollection = $derived($store.rules?.length === 0);
const load = async () => {
resetPage();
resetData();
Expand Down Expand Up @@ -123,7 +121,7 @@
</DataCollectionHeader>
</div>

{#if !emptyCollection}
{#if $hasAnyRules}
<div
class="data"
class:data-selected={nonNullish($assetsStore?.data)}
Expand Down
6 changes: 2 additions & 4 deletions src/frontend/src/lib/components/docs/Docs.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { emit } from '$lib/utils/events.utils';
import { i18nFormat } from '$lib/utils/i18n.utils';
const { store }: RulesContext = getContext<RulesContext>(RULES_CONTEXT_KEY);
const { store, hasAnyRules }: RulesContext = getContext<RulesContext>(RULES_CONTEXT_KEY);
let collection: string | undefined = $state();
run(() => {
Expand All @@ -43,8 +43,6 @@
const { store: docsStore, resetData }: DataContext<DocType> =
getContext<DataContext<DocType>>(DATA_CONTEXT_KEY);
let emptyCollection = $derived($store.rules?.length === 0);
const load = async () => {
resetPage();
resetData();
Expand Down Expand Up @@ -121,7 +119,7 @@
</DataCollectionHeader>
</div>

{#if !emptyCollection}
{#if $hasAnyRules}
<div
class="data"
class:data-selected={nonNullish($docsStore?.data)}
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/src/lib/stores/rules.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { reloadContextRules } from '$lib/services/rules.loader.services';
import type { OptionIdentity } from '$lib/types/itentity';
import type { RulesContext, RulesData } from '$lib/types/rules.context';
import type { Principal } from '@dfinity/principal';
import { get, writable } from 'svelte/store';
import { derived, get, writable } from 'svelte/store';

export const initRulesContext = ({
satelliteId: initialSatelliteId,
Expand Down Expand Up @@ -42,9 +42,12 @@ export const initRulesContext = ({
await reloadRules({ identity });
};

const hasAnyRules = derived(store, ({ rules }) => (rules?.length ?? 0) > 0);

return {
store,
reload: reloadRules,
init: initRules
init: initRules,
hasAnyRules
};
};
5 changes: 4 additions & 1 deletion src/frontend/src/lib/types/rules.context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Rule } from '$declarations/satellite/satellite.did';
import type { OptionIdentity } from '$lib/types/itentity';
import type { Principal } from '@dfinity/principal';
import type { Writable } from 'svelte/store';
import type { Readable, Writable } from 'svelte/store';

export interface RulesData {
satelliteId: Principal;
Expand All @@ -11,8 +11,11 @@ export interface RulesData {

export interface RulesContext {
store: Writable<RulesData>;

reload: (params: { identity: OptionIdentity }) => Promise<void>;
init: (params: { satelliteId: Principal; identity: OptionIdentity }) => Promise<void>;

hasAnyRules: Readable<boolean>;
}

export const RULES_CONTEXT_KEY = Symbol('rules');

0 comments on commit 07794bb

Please sign in to comment.