Skip to content

Commit f7b9fcd

Browse files
committed
client(admin): pickPrimarySlug returns the slug string, not the slug object
Returning the object forced every call site to do `primary.slug` (or `primary ? primary.slug : ''`) and made the helper's contract ambiguous about what shape the result has. Returning the string directly collapses each call site to a single line, makes the empty-slugs case fold cleanly into the falsy check, and removes the misleading "defensive fallback for malformed data" comment.
1 parent acedb4b commit f7b9fcd

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

src/client.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ function t(key, params) {
3939
return val;
4040
}
4141
42-
// The slug to display or copy when "the link" needs one representative chip:
43-
// the link's primary, with slugs[0] as a defensive fallback for malformed data.
42+
// The slug string to display or copy when "the link" needs one representative
43+
// chip: the link's primary, falling back to the first slug.
4444
function pickPrimarySlug(link) {
45-
if (!link.slugs || link.slugs.length === 0) return null;
45+
if (!link.slugs || link.slugs.length === 0) return '';
4646
for (var i = 0; i < link.slugs.length; i++) {
47-
if (link.slugs[i].is_primary) return link.slugs[i];
47+
if (link.slugs[i].is_primary) return link.slugs[i].slug;
4848
}
49-
return link.slugs[0];
49+
return link.slugs[0].slug;
5050
}
5151
5252
// ---- Toast ----
@@ -161,7 +161,7 @@ function quickShorten() {
161161
}
162162
} else {
163163
var primary = pickPrimarySlug(link);
164-
if (primary) copyUrl(primary.slug);
164+
if (primary) copyUrl(primary);
165165
toast(t('client.linkCreatedCopied'));
166166
window.location.href = '/_/admin/links/' + link.id;
167167
}
@@ -258,7 +258,7 @@ function createDuplicate(url) {
258258
if (res.ok) {
259259
return res.json().then(function(link) {
260260
var primary = pickPrimarySlug(link);
261-
if (primary) copyUrl(primary.slug);
261+
if (primary) copyUrl(primary);
262262
toast(t('client.linkCreatedCopied'));
263263
window.location.href = '/_/admin/links/' + link.id;
264264
});
@@ -1135,8 +1135,7 @@ function pollDashboard() {
11351135
if (recentNoData) recentNoData.remove();
11361136
for (var ri = 0; ri < d.recent_links.length; ri++) {
11371137
var link = d.recent_links[ri];
1138-
var primarySlug = pickPrimarySlug(link);
1139-
var slug = primarySlug ? primarySlug.slug : '';
1138+
var slug = pickPrimarySlug(link);
11401139
var a = document.createElement('a');
11411140
a.href = '/_/admin/links/' + link.id;
11421141
a.style.cssText = 'display:flex;align-items:center;gap:0.75rem;padding:0.5rem 0;cursor:pointer;overflow:hidden;min-width:0;text-decoration:none;color:inherit';
@@ -1168,8 +1167,7 @@ function pollDashboard() {
11681167
if (tlNoData) tlNoData.remove();
11691168
for (var ti = 0; ti < d.top_links.length; ti++) {
11701169
var tLink = d.top_links[ti];
1171-
var tPrimary = pickPrimarySlug(tLink);
1172-
var tSlug = tPrimary ? tPrimary.slug : '';
1170+
var tSlug = pickPrimarySlug(tLink);
11731171
var tPct = Math.round((tLink.total_clicks / tlMax) * 100);
11741172
var a = document.createElement('a');
11751173
a.href = '/_/admin/links/' + tLink.id;
@@ -1507,8 +1505,7 @@ function showAddLinkToBundlePicker(bundleId, excludeIds) {
15071505
html += '<div class="muted-hint">' + esc(t('bundles.allLinksAdded')) + '</div>';
15081506
} else {
15091507
available.forEach(function(link) {
1510-
var primary = pickPrimarySlug(link);
1511-
var slug = primary ? primary.slug : '';
1508+
var slug = pickPrimarySlug(link);
15121509
var label = link.label || link.url;
15131510
html += '<div class="add-to-bundle-row" data-search="' + esc((link.label || '') + ' ' + link.url + ' ' + slug).toLowerCase() + '" onclick="doAddLinkToBundle(' + bundleId + ',' + link.id + ')">';
15141511
html += '<span class="slug-chip">' + esc(slug) + '</span>';

0 commit comments

Comments
 (0)