Skip to content

Commit dad9303

Browse files
committed
refactor: replace author image URL logic with derived stores
Refactor the author image URL logic in multiple components to use derived stores for better reactivity and cleaner code. This change eliminates redundant code and ensures that the author image URL updates automatically when the user or commit data changes.
1 parent 48405fc commit dad9303

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

apps/desktop/src/lib/commit/CommitCard.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@
137137
}
138138
139139
const commitShortSha = commit.id.substring(0, 7);
140-
const authorImgUrl =
141-
commit.author.email?.toLowerCase() === $user?.email?.toLowerCase()
140+
const authorImgUrl = $derived.by(() => {
141+
return commit.author.email?.toLowerCase() === $user?.email?.toLowerCase()
142142
? $user?.picture
143143
: commit.author.gravatarUrl;
144+
});
144145
145146
function handleUncommit(e: MouseEvent) {
146147
e.stopPropagation();

apps/desktop/src/lib/components/EditMode.svelte

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@
4242
4343
let initialFiles = $state<[RemoteFile, ConflictEntryPresence | undefined][]>([]);
4444
let commit = $state<Commit | undefined>(undefined);
45-
let authorImgUrl = $state<string | undefined>(undefined);
45+
const authorImgUrl = $derived.by(() => {
46+
if (commit) {
47+
return commit.author.email?.toLowerCase() === $user?.email?.toLowerCase()
48+
? $user?.picture
49+
: commit.author.gravatarUrl;
50+
}
51+
return undefined;
52+
});
4653
4754
let filesList = $state<HTMLDivElement | undefined>(undefined);
4855
let contextMenu = $state<ReturnType<typeof FileContextMenu> | undefined>(undefined);
@@ -59,15 +66,6 @@
5966
});
6067
});
6168
62-
$effect(() => {
63-
if (commit) {
64-
authorImgUrl =
65-
commit.author.email?.toLowerCase() === $user?.email?.toLowerCase()
66-
? $user?.picture
67-
: commit.author.gravatarUrl;
68-
}
69-
});
70-
7169
interface FileEntry {
7270
name: string;
7371
path: string;

apps/desktop/src/lib/navigation/BranchListingSidebarEntry.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@
9696
if (ownedByUser) {
9797
const name = (await gitConfigService.get('user.name')) || unknownName;
9898
const email = (await gitConfigService.get('user.email')) || unknownEmail;
99-
const srcUrl = $user?.picture ? $user?.picture : await gravatarUrlFromEmail(email);
99+
const srcUrl =
100+
email.toLowerCase() === $user?.email?.toLowerCase()
101+
? $user?.picture
102+
: await gravatarUrlFromEmail(email);
100103
101104
avatars = [{ name, srcUrl }];
102105
} else if (branchListingDetails) {

apps/desktop/src/lib/navigation/PullRequestSidebarEntry.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
const userService = getContext(UserService);
2020
const user = userService.user;
2121
22-
const authorImgUrl =
23-
pullRequest.author?.email?.toLowerCase() === $user?.email?.toLowerCase()
22+
const authorImgUrl = $derived.by(() => {
23+
return pullRequest.author?.email?.toLowerCase() === $user?.email?.toLowerCase()
2424
? $user?.picture
2525
: pullRequest.author?.gravatarUrl;
26+
});
2627
2728
function onMouseDown() {
2829
goto(formatPullRequestURL(project, pullRequest.number));

0 commit comments

Comments
 (0)