mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
fix(ui): select and star the default hub on the Hubs page
With no own hub and nothing picked, the Hubs page opened on an empty "pick a hub" state, and when the config saved no default the built-in Skillshare Hub had no star even though `search --hub` falls back to it. The page now treats the built-in hub as the default when none is saved and opens on the default hub (or the first one). An own hub still wins, as before, and nothing is written to the config.
This commit is contained in:
@@ -174,3 +174,24 @@ it('gives an added hub a label no other hub has', async () => {
|
||||
await waitFor(() => expect(api.putHubConfig).toHaveBeenCalled());
|
||||
expect(vi.mocked(api.putHubConfig).mock.calls[0][0].hubs[1].label).toBe('skillshare-hub.json (2)');
|
||||
});
|
||||
|
||||
it('opens on the built-in hub and stars it when no default is saved', async () => {
|
||||
vi.mocked(hubDrafts.list).mockResolvedValue([]);
|
||||
vi.mocked(api.getHubConfig).mockResolvedValue({ hubs: [{ label: 'acme', url: 'https://acme.dev/hub.json' }], default: '' });
|
||||
renderPage();
|
||||
const builtIn = await screen.findByRole('button', { name: /Skillshare Hub/ });
|
||||
await waitFor(() => expect(builtIn).toHaveAttribute('aria-current', 'true'));
|
||||
expect(within(builtIn).getByLabelText('Default')).toBeInTheDocument();
|
||||
expect(within(screen.getByRole('button', { name: /acme/ })).queryByLabelText('Default')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('opens on the saved default hub without offering to make it the default', async () => {
|
||||
vi.mocked(hubDrafts.list).mockResolvedValue([]);
|
||||
vi.mocked(api.getHubConfig).mockResolvedValue({ hubs: [{ label: 'acme', url: 'https://acme.dev/hub.json' }], default: 'Acme' });
|
||||
renderPage();
|
||||
const acme = await screen.findByRole('button', { name: /acme/ });
|
||||
await waitFor(() => expect(acme).toHaveAttribute('aria-current', 'true'));
|
||||
expect(within(acme).getByLabelText('Default')).toBeInTheDocument();
|
||||
expect(within(screen.getByRole('button', { name: /Skillshare Hub/ })).queryByLabelText('Default')).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: 'Make default' })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
@@ -38,7 +38,7 @@ export default function HubPage() {
|
||||
queryFn: () => api.getHubConfig(),
|
||||
staleTime: staleTimes.config,
|
||||
});
|
||||
const { data: drafts = [] } = useQuery({ queryKey: queryKeys.hub.drafts, queryFn: () => hubDrafts.list() });
|
||||
const { data: drafts = [], isPending: draftsPending } = useQuery({ queryKey: queryKeys.hub.drafts, queryFn: () => hubDrafts.list() });
|
||||
|
||||
// A subscription to one of the user's own published hubs is the same hub; show it once.
|
||||
const saved = useMemo(() => {
|
||||
@@ -46,8 +46,9 @@ export default function HubPage() {
|
||||
const rest = (config?.hubs ?? []).filter((h) => !sameURL(h.url, COMMUNITY.url) && !own.some((u) => sameURL(u, h.url)));
|
||||
return [COMMUNITY, ...rest];
|
||||
}, [config, drafts]);
|
||||
// With no default saved, `search --hub` falls back to the built-in hub, so that one is the default.
|
||||
const isDefault = (hub: HubSavedEntry) =>
|
||||
Boolean(config?.default) && hub.label.toLowerCase() === config!.default.toLowerCase();
|
||||
config?.default ? hub.label.toLowerCase() === config.default.toLowerCase() : hub.builtIn === true;
|
||||
|
||||
const items: HubListItem[] = [
|
||||
...saved.map((h) => ({ key: h.url, label: h.label, sub: h.url, mine: false, isDefault: isDefault(h) })),
|
||||
@@ -62,7 +63,9 @@ export default function HubPage() {
|
||||
];
|
||||
|
||||
// Open on the user's own hub when there is one; that is the one they came to work on.
|
||||
const selected = picked && items.some((i) => i.key === picked) ? picked : drafts[0] ? DRAFT + drafts[0].id : picked;
|
||||
// Otherwise open on the hub installs come from, once it is known there is no own hub.
|
||||
const fallback = draftsPending ? null : (items.find((i) => i.isDefault) ?? items[0])?.key ?? null;
|
||||
const selected = picked && items.some((i) => i.key === picked) ? picked : drafts[0] ? DRAFT + drafts[0].id : fallback;
|
||||
const draftId = selected?.startsWith(DRAFT) ? selected.slice(DRAFT.length) : undefined;
|
||||
const hub = draftId ? undefined : saved.find((h) => h.url === selected);
|
||||
const current = items.find((i) => i.key === selected);
|
||||
|
||||
Reference in New Issue
Block a user