Skip to content

Commit

Permalink
Merge pull request #1041 from cardstack/cs-6381-code-mode-inspector-i…
Browse files Browse the repository at this point in the history
…nherit-create-and-duplicate-instance

Realm dropdown shows only readable realms
  • Loading branch information
habdelra authored Feb 20, 2024
2 parents 97acb37 + bb9bdac commit e3de0c2
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 60 deletions.
35 changes: 28 additions & 7 deletions packages/host/app/components/realm-dropdown.gts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { DropdownArrowDown } from '@cardstack/boxel-ui/icons';

import { type RealmInfo, RealmPaths } from '@cardstack/runtime-common';

import ENV from '@cardstack/host/config/environment';

import RealmIcon from './operator-mode/realm-icon';

import type RealmInfoService from '../services/realm-info-service';

export interface RealmDropdownItem extends RealmInfo {
path: string;
}
const { ownRealmURL } = ENV;

interface Signature {
Args: {
Expand Down Expand Up @@ -55,7 +58,7 @@ export default class RealmDropdown extends Component<Signature> {
@realmIconURL={{this.selectedRealm.iconURL}}
@realmName={{this.selectedRealm.name}}
/>
<div class='selected-item'>
<div class='selected-item' data-test-selected-realm>
{{this.selectedRealm.name}}
</div>
{{else}}
Expand Down Expand Up @@ -124,13 +127,17 @@ export default class RealmDropdown extends Component<Signature> {
path,
realmInfo,
] of this.realmInfoService.cachedRealmInfos.entries()) {
if (!realmInfo.canWrite) {
continue;
}
let item: RealmDropdownItem = {
path,
...realmInfo,
iconURL: realmInfo.iconURL ?? this.defaultRealmIcon,
};
items = [item, ...items];
}
items.sort((a, b) => a.name.localeCompare(b.name));
return items;
}

Expand All @@ -146,12 +153,26 @@ export default class RealmDropdown extends Component<Signature> {
}

get selectedRealm(): RealmDropdownItem | undefined {
if (!this.args.selectedRealmURL) {
return undefined;
let selectedRealm: RealmDropdownItem | undefined;
if (this.args.selectedRealmURL) {
selectedRealm = this.realms.find(
(realm) =>
realm.path === new RealmPaths(this.args.selectedRealmURL as URL).url,
);
}
if (selectedRealm) {
return selectedRealm;
}

// if there is no selected realm then default to the user's personal
// realm. Currently the personal realm has not yet been implemented,
// until then default to the realm serving the host app if it is writable,
// otherwise default to the first writable realm lexically
let ownRealm = this.realms.find((r) => r.path === ownRealmURL);
if (ownRealm) {
return ownRealm;
} else {
return this.realms[0];
}
return this.realms.find(
(realm) =>
realm.path === new RealmPaths(this.args.selectedRealmURL as URL).url,
);
}
}
1 change: 0 additions & 1 deletion packages/host/app/resources/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export class Search extends Resource<Args> {
private search = restartableTask(async (query: Query) => {
this._instances = flatMap(
await Promise.all(
// use a Set since the default URL may actually be the base realm
this.realmsToSearch.map(
async (realm) => await this.cardService.search(query, new URL(realm)),
),
Expand Down
6 changes: 5 additions & 1 deletion packages/host/app/services/matrix-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,11 @@ export default class MatrixService extends Service {
return inflightAuth;
}

let realmAuthClient = new RealmAuthClient(realmURL, this.client);
let realmAuthClient = new RealmAuthClient(
realmURL,
this.client,
this.loaderService.loader,
);

let jwtPromise = realmAuthClient.getJWT();

Expand Down
23 changes: 20 additions & 3 deletions packages/host/app/services/realm-info-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ import {
RealmPaths,
} from '@cardstack/runtime-common';

import { getRealmSession } from '@cardstack/host/resources/realm-session';

import type CardService from '@cardstack/host/services/card-service';
import type LoaderService from '@cardstack/host/services/loader-service';

const waiter = buildWaiter('realm-info-service:waiter');

type RealmInfoWithPermissions = RealmInfo & {
canRead: boolean;
canWrite: boolean;
};

export default class RealmInfoService extends Service {
@service declare loaderService: LoaderService;
@service declare cardService: CardService;
cachedRealmURLsForURL: Map<string, string> = new Map(); // Has the file url already been resolved to a realm url?
cachedRealmInfos: TrackedMap<string, RealmInfo> = new TrackedMap(); // Has the realm url already been resolved to a realm info?
cachedRealmInfos: TrackedMap<string, RealmInfoWithPermissions> =
new TrackedMap(); // Has the realm url already been resolved to a realm info?
cachedPublicReadableRealms: Map<string, boolean> = new Map();

async fetchRealmURL(url: string): Promise<URL | undefined> {
Expand Down Expand Up @@ -97,8 +105,17 @@ export default class RealmInfoService extends Service {
{ headers: { Accept: SupportedMimeType.RealmInfo } },
);

let realmInfo = (await realmInfoResponse.json())?.data?.attributes;
this.cachedRealmInfos.set(realmURLString, realmInfo);
let realmInfo = (await realmInfoResponse.json())?.data
?.attributes as RealmInfo;
let realmSession = getRealmSession(this, {
realmURL: () => new URL(realmURLString!),
});
await realmSession.loaded;
this.cachedRealmInfos.set(realmURLString, {
...realmInfo,
canRead: !!realmSession.canRead,
canWrite: !!realmSession.canWrite,
});
return realmInfo;
}
} finally {
Expand Down
7 changes: 4 additions & 3 deletions packages/host/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ module.exports = function (environment) {
: process.env.OWN_REALM_URL || 'http://localhost:4200/', // this should be provided as an *unresolved* URL
// This is temporary until we have a better way to discover realms besides
// our own
otherRealmURLs: process.env.OTHER_REALM_URLS
? process.env.OTHER_REALM_URLS.split(',').map((u) => u.trim())
: [],
otherRealmURLs:
environment !== 'test' && process.env.OTHER_REALM_URLS
? process.env.OTHER_REALM_URLS.split(',').map((u) => u.trim())
: [],
hostsOwnAssets: true,
resolvedBaseRealmURL:
process.env.RESOLVED_BASE_REALM_URL || 'http://localhost:4201/base/',
Expand Down
Loading

0 comments on commit e3de0c2

Please sign in to comment.