feat: restore last active channel on login
- Channel store saves {serverId, channelId} to localStorage on select
- ServerBar restores last server+channel after fetching servers
- Fetches channels for the saved server, then sets the channel if valid
- Gracefully handles missing/deleted servers or channels
This commit is contained in:
@@ -34,8 +34,28 @@ export function ServerBar() {
|
|||||||
const { showMenu, MenuPortal } = useContextMenu();
|
const { showMenu, MenuPortal } = useContextMenu();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchServers();
|
fetchServers().then(() => {
|
||||||
}, [fetchServers]);
|
// ponytail: restore last active server + channel from localStorage
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem('dumpster:lastChannel');
|
||||||
|
if (raw) {
|
||||||
|
const { serverId, channelId } = JSON.parse(raw);
|
||||||
|
const srv = useServerStore.getState().servers.find((s) => s.id === serverId);
|
||||||
|
if (srv) {
|
||||||
|
setActiveServer(serverId);
|
||||||
|
setDM(false);
|
||||||
|
// Fetch channels then restore the channel
|
||||||
|
useChannelStore.getState().fetchChannels(serverId).then(() => {
|
||||||
|
const ch = useChannelStore.getState().channelsByServer[serverId]?.find((c) => c.id === channelId);
|
||||||
|
if (ch) {
|
||||||
|
setActiveChannel(channelId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch { /* ignore bad localStorage */ }
|
||||||
|
});
|
||||||
|
}, [fetchServers, setActiveServer, setActiveChannel, setDM]);
|
||||||
|
|
||||||
const handleSelect = (id: string) => {
|
const handleSelect = (id: string) => {
|
||||||
setActiveServer(id);
|
setActiveServer(id);
|
||||||
|
|||||||
@@ -48,7 +48,19 @@ export const useChannelStore = create<ChannelState>((set) => ({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
setActiveChannel: (id) => set({ activeChannelId: id }),
|
setActiveChannel: (id) => {
|
||||||
|
set({ activeChannelId: id });
|
||||||
|
if (id) {
|
||||||
|
// ponytail: persist last active channel for session restore
|
||||||
|
const state = useChannelStore.getState();
|
||||||
|
for (const [serverId, channels] of Object.entries(state.channelsByServer)) {
|
||||||
|
if (channels.some((c) => c.id === id)) {
|
||||||
|
localStorage.setItem('dumpster:lastChannel', JSON.stringify({ serverId, channelId: id }));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
addChannel: (channel) =>
|
addChannel: (channel) =>
|
||||||
set((state) => {
|
set((state) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user